updated c# source generator to also take other SDL methods into account

This commit is contained in:
WizzardMaker 2024-12-03 12:20:09 +01:00
parent 5b0abdca7c
commit 1e78619716
4 changed files with 31 additions and 6 deletions

View File

@ -15,14 +15,43 @@ namespace SDL.SourceGeneration
{
public readonly Dictionary<string, List<GeneratedMethod>> Methods = new Dictionary<string, List<GeneratedMethod>>();
/// <summary>
/// Checks whether the method is from any SDL library.
/// It identifies those by checking if the method has a DllImport attribute for a library starting with "SDL".
/// </summary>
private static bool IsMethodFromSDL(MethodDeclarationSyntax methodNode)
{
if (methodNode.AttributeLists.Count == 0)
return false;
foreach (var attributeList in methodNode.AttributeLists)
{
foreach (var attribute in attributeList.Attributes)
{
if (attribute.Name.ToString() != "DllImport") continue;
if (attribute.ArgumentList == null || attribute.ArgumentList.Arguments.Count <= 0) continue; // this should never happen, but rather continue than throw
var libraryNameArgument = attribute.ArgumentList.Arguments[0];
if (libraryNameArgument.Expression is LiteralExpressionSyntax literal &&
literal.Token.ValueText.StartsWith("SDL", StringComparison.Ordinal))
{
return true;
}
}
}
return false;
}
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
if (syntaxNode is MethodDeclarationSyntax method)
{
string name = method.Identifier.ValueText;
bool isUnsafe = name.StartsWith($"{Helper.UnsafePrefix}SDL_", StringComparison.Ordinal);
bool isUnsafe = name.StartsWith($"{Helper.UnsafePrefix}", StringComparison.Ordinal);
if (!name.StartsWith("SDL_", StringComparison.Ordinal) && !isUnsafe)
if (!IsMethodFromSDL(method) && !isUnsafe)
return;
if (method.ParameterList.Parameters.Any(p => p.Identifier.IsKind(SyntaxKind.ArgListKeyword)))

View File

@ -28,7 +28,6 @@ using System.Runtime.InteropServices;
namespace SDL
{
[NativeTypeName("int")]
public enum SDL_AudioFormat : uint
{
SDL_AUDIO_UNKNOWN = 0x0000U,

View File

@ -89,7 +89,6 @@ namespace SDL
SDL_PACKEDLAYOUT_1010102,
}
[NativeTypeName("int")]
public enum SDL_PixelFormat : uint
{
SDL_PIXELFORMAT_UNKNOWN = 0,
@ -240,7 +239,6 @@ namespace SDL
SDL_CHROMA_LOCATION_TOPLEFT = 3,
}
[NativeTypeName("int")]
public enum SDL_Colorspace : uint
{
SDL_COLORSPACE_UNKNOWN = 0,

View File

@ -265,7 +265,6 @@ base_command = [
"--include-directory", repository_root / SDL_lib_include_root["SDL3"],
"--include-directory", repository_root / SDL_lib_include_root["SDL3_image"],
"--include-directory", repository_root / SDL_lib_include_root["SDL3_ttf"],
# "--libraryPath", "SDL3",
"--methodClassName", "SDL3",
"--namespace", "SDL",