diff --git a/SDL3-CS.SourceGeneration/UnfriendlyMethodFinder.cs b/SDL3-CS.SourceGeneration/UnfriendlyMethodFinder.cs index 7f5461f..4554ede 100644 --- a/SDL3-CS.SourceGeneration/UnfriendlyMethodFinder.cs +++ b/SDL3-CS.SourceGeneration/UnfriendlyMethodFinder.cs @@ -15,14 +15,43 @@ namespace SDL.SourceGeneration { public readonly Dictionary> Methods = new Dictionary>(); + /// + /// 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". + /// + 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))) diff --git a/SDL3-CS/SDL3/ClangSharp/SDL_audio.g.cs b/SDL3-CS/SDL3/ClangSharp/SDL_audio.g.cs index addb1b8..de63476 100644 --- a/SDL3-CS/SDL3/ClangSharp/SDL_audio.g.cs +++ b/SDL3-CS/SDL3/ClangSharp/SDL_audio.g.cs @@ -28,7 +28,6 @@ using System.Runtime.InteropServices; namespace SDL { - [NativeTypeName("int")] public enum SDL_AudioFormat : uint { SDL_AUDIO_UNKNOWN = 0x0000U, diff --git a/SDL3-CS/SDL3/ClangSharp/SDL_pixels.g.cs b/SDL3-CS/SDL3/ClangSharp/SDL_pixels.g.cs index b963cec..aae40df 100644 --- a/SDL3-CS/SDL3/ClangSharp/SDL_pixels.g.cs +++ b/SDL3-CS/SDL3/ClangSharp/SDL_pixels.g.cs @@ -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, diff --git a/SDL3-CS/generate_bindings.py b/SDL3-CS/generate_bindings.py index 7eaad20..023e294 100644 --- a/SDL3-CS/generate_bindings.py +++ b/SDL3-CS/generate_bindings.py @@ -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",