From 913afa5b4fa7e0cfaffc3f37f79647f1de75c9ac Mon Sep 17 00:00:00 2001 From: Dan Balasescu Date: Sun, 7 Apr 2024 12:31:45 +0900 Subject: [PATCH] Refactorings + auto styling --- Directory.Build.props | 4 +- .../FriendlyOverloadGenerator.cs | 75 ++++++++++--------- .../UnfriendlyMethodFinder.cs | 7 +- SDL3-CS.Tests/MyWindow.cs | 27 +++---- SDL3-CS.Tests/ObjectHandle.cs | 2 +- SDL3-CS.Tests/Program.cs | 2 +- SDL3-CS.sln.DotSettings | 1 + SDL3-CS/ConstantAttribute.cs | 2 +- SDL3-CS/SDL3/SDL_camera.cs | 3 + SDL3-CS/TypedefAttribute.cs | 2 +- 10 files changed, 66 insertions(+), 59 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index a22ba87..0af7e08 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -21,7 +21,7 @@ SDL3-CS Automated release. MIT - https://github.com/ppy/osu-framework - https://github.com/ppy/osu-framework + https://github.com/ppy/SDL3-CS + https://github.com/ppy/SDL3-CS diff --git a/SDL3-CS.SourceGeneration/FriendlyOverloadGenerator.cs b/SDL3-CS.SourceGeneration/FriendlyOverloadGenerator.cs index 387c1df..0830bd1 100644 --- a/SDL3-CS.SourceGeneration/FriendlyOverloadGenerator.cs +++ b/SDL3-CS.SourceGeneration/FriendlyOverloadGenerator.cs @@ -29,6 +29,7 @@ using System; public void Execute(GeneratorExecutionContext context) { var finder = (UnfriendlyMethodFinder)context.SyntaxReceiver!; + foreach (var kvp in finder.Methods) { string filename = kvp.Key; @@ -38,16 +39,16 @@ using System; result.Append(file_header); result.Append( SyntaxFactory.NamespaceDeclaration( - SyntaxFactory.IdentifierName("SDL")) - .WithMembers( - SyntaxFactory.SingletonList( - SyntaxFactory.ClassDeclaration("SDL3") - .WithModifiers( - SyntaxFactory.TokenList( - SyntaxFactory.Token(SyntaxKind.UnsafeKeyword), - SyntaxFactory.Token(SyntaxKind.PartialKeyword))) - .WithMembers(SyntaxFactory.List(foundMethods.Select(makeFriendlyMethod))))) - .NormalizeWhitespace()); + SyntaxFactory.IdentifierName("SDL")) + .WithMembers( + SyntaxFactory.SingletonList( + SyntaxFactory.ClassDeclaration("SDL3") + .WithModifiers( + SyntaxFactory.TokenList( + SyntaxFactory.Token(SyntaxKind.UnsafeKeyword), + SyntaxFactory.Token(SyntaxKind.PartialKeyword))) + .WithMembers(SyntaxFactory.List(foundMethods.Select(makeFriendlyMethod))))) + .NormalizeWhitespace()); context.AddSource(filename, result.ToString()); } @@ -64,16 +65,16 @@ using System; : gm.NativeMethod.Identifier; return SyntaxFactory.MethodDeclaration(returnType, identifier) - .WithModifiers( - SyntaxFactory.TokenList( - SyntaxFactory.Token(SyntaxKind.PublicKeyword), - SyntaxFactory.Token(SyntaxKind.StaticKeyword))) - .WithParameterList( - SyntaxFactory.ParameterList( - SyntaxFactory.SeparatedList(transformParams(gm)))) - .WithBody( - SyntaxFactory.Block( - makeMethodBody(gm))); + .WithModifiers( + SyntaxFactory.TokenList( + SyntaxFactory.Token(SyntaxKind.PublicKeyword), + SyntaxFactory.Token(SyntaxKind.StaticKeyword))) + .WithParameterList( + SyntaxFactory.ParameterList( + SyntaxFactory.SeparatedList(transformParams(gm)))) + .WithBody( + SyntaxFactory.Block( + makeMethodBody(gm))); } private static IEnumerable transformParams(GeneratedMethod gm) @@ -84,7 +85,7 @@ using System; { Debug.Assert(gm.RequiredChanges.HasFlag(Changes.ChangeParamsToReadOnlySpan)); yield return param.WithType(SyntaxFactory.ParseTypeName("ReadOnlySpan")) - .WithAttributeLists(SyntaxFactory.List()); + .WithAttributeLists(SyntaxFactory.List()); } else { @@ -108,10 +109,10 @@ using System; SyntaxFactory.ParseTypeName("byte*"), SyntaxFactory.SingletonSeparatedList( SyntaxFactory.VariableDeclarator( - param.Identifier.ValueText + pointer_suffix) - .WithInitializer( - SyntaxFactory.EqualsValueClause( - SyntaxFactory.IdentifierName(param.Identifier))))), + param.Identifier.ValueText + pointer_suffix) + .WithInitializer( + SyntaxFactory.EqualsValueClause( + SyntaxFactory.IdentifierName(param.Identifier))))), expr); } @@ -125,16 +126,16 @@ using System; if (gm.RequiredChanges.HasFlag(Changes.ChangeReturnTypeToString)) { expr = SyntaxFactory.InvocationExpression( - SyntaxFactory.IdentifierName("PtrToStringUTF8")) - .WithArguments(new[] - { - SyntaxFactory.Argument(makeFunctionCall(gm)), - SyntaxFactory.Argument(SyntaxFactory.LiteralExpression( - gm.RequiredChanges.HasFlag(Changes.FreeReturnedPointer) - ? SyntaxKind.TrueLiteralExpression - : SyntaxKind.FalseLiteralExpression)) - } - ); + SyntaxFactory.IdentifierName("PtrToStringUTF8")) + .WithArguments(new[] + { + SyntaxFactory.Argument(makeFunctionCall(gm)), + SyntaxFactory.Argument(SyntaxFactory.LiteralExpression( + gm.RequiredChanges.HasFlag(Changes.FreeReturnedPointer) + ? SyntaxKind.TrueLiteralExpression + : SyntaxKind.FalseLiteralExpression)) + } + ); } else { @@ -150,8 +151,8 @@ using System; private static InvocationExpressionSyntax makeFunctionCall(GeneratedMethod gm) { return SyntaxFactory.InvocationExpression( - SyntaxFactory.IdentifierName(gm.NativeMethod.Identifier)) - .WithArguments(makeArguments(gm)); + SyntaxFactory.IdentifierName(gm.NativeMethod.Identifier)) + .WithArguments(makeArguments(gm)); } private static IEnumerable makeArguments(GeneratedMethod gm) diff --git a/SDL3-CS.SourceGeneration/UnfriendlyMethodFinder.cs b/SDL3-CS.SourceGeneration/UnfriendlyMethodFinder.cs index 56bfa67..944fa3c 100644 --- a/SDL3-CS.SourceGeneration/UnfriendlyMethodFinder.cs +++ b/SDL3-CS.SourceGeneration/UnfriendlyMethodFinder.cs @@ -1,6 +1,7 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -12,16 +13,16 @@ namespace SDL3.SourceGeneration { public class UnfriendlyMethodFinder : ISyntaxReceiver { - public readonly Dictionary> Methods = new(); + public readonly Dictionary> Methods = new Dictionary>(); public void OnVisitSyntaxNode(SyntaxNode syntaxNode) { if (syntaxNode is MethodDeclarationSyntax method) { string name = method.Identifier.ValueText; - bool isUnsafe = name.StartsWith($"{Helper.UnsafePrefix}SDL_"); + bool isUnsafe = name.StartsWith($"{Helper.UnsafePrefix}SDL_", StringComparison.Ordinal); - if (!name.StartsWith("SDL_") && !isUnsafe) + if (!name.StartsWith("SDL_", StringComparison.Ordinal) && !isUnsafe) return; if (method.ParameterList.Parameters.Any(p => p.Identifier.IsKind(SyntaxKind.ArgListKeyword))) diff --git a/SDL3-CS.Tests/MyWindow.cs b/SDL3-CS.Tests/MyWindow.cs index e23e98b..b647deb 100644 --- a/SDL3-CS.Tests/MyWindow.cs +++ b/SDL3-CS.Tests/MyWindow.cs @@ -3,7 +3,6 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using osu.Framework.Allocation; using SDL; using static SDL.SDL3; @@ -13,15 +12,15 @@ namespace SDL3.Tests { private bool flash; private ObjectHandle objectHandle { get; } - private SDL_Window* SDLWindowHandle; - private SDL_Renderer* Renderer; + private SDL_Window* sdlWindowHandle; + private SDL_Renderer* renderer; private readonly bool initSuccess; - private const SDL_InitFlags initFlags = SDL_InitFlags.SDL_INIT_VIDEO | SDL_InitFlags.SDL_INIT_GAMEPAD; + private const SDL_InitFlags init_flags = SDL_InitFlags.SDL_INIT_VIDEO | SDL_InitFlags.SDL_INIT_GAMEPAD; public MyWindow() { - if (SDL_InitSubSystem(initFlags) < 0) + if (SDL_InitSubSystem(init_flags) < 0) throw new InvalidOperationException($"failed to initialise SDL. Error: {SDL_GetError()}"); initSuccess = true; @@ -42,6 +41,7 @@ namespace SDL3.Tests private static SDL_bool wndProc(IntPtr userdata, MSG* message) { var handle = new ObjectHandle(userdata); + if (handle.GetTarget(out var window)) { Console.WriteLine($"from {window}, message: {message->message}"); @@ -90,8 +90,8 @@ namespace SDL3.Tests public void Create() { - SDLWindowHandle = SDL_CreateWindow("hello"u8, 800, 600, SDL_WindowFlags.SDL_WINDOW_RESIZABLE | SDL_WindowFlags.SDL_WINDOW_HIGH_PIXEL_DENSITY); - Renderer = SDL_CreateRenderer(SDLWindowHandle, (byte*)null, SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC); + sdlWindowHandle = SDL_CreateWindow("hello"u8, 800, 600, SDL_WindowFlags.SDL_WINDOW_RESIZABLE | SDL_WindowFlags.SDL_WINDOW_HIGH_PIXEL_DENSITY); + renderer = SDL_CreateRenderer(sdlWindowHandle, (byte*)null, SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC); } private void handleEvent(SDL_Event e) @@ -116,11 +116,11 @@ namespace SDL3.Tests break; case SDL_Keycode.SDLK_F10: - SDL_SetWindowFullscreen(SDLWindowHandle, SDL_bool.SDL_FALSE); + SDL_SetWindowFullscreen(sdlWindowHandle, SDL_bool.SDL_FALSE); break; case SDL_Keycode.SDLK_F11: - SDL_SetWindowFullscreen(SDLWindowHandle, SDL_bool.SDL_TRUE); + SDL_SetWindowFullscreen(sdlWindowHandle, SDL_bool.SDL_TRUE); break; case SDL_Keycode.SDLK_j: @@ -134,6 +134,7 @@ namespace SDL3.Tests int count; var bindings = SDL_GetGamepadBindings(gamepad, &count); + for (int i = 0; i < count; i++) { var binding = *bindings[i]; @@ -217,9 +218,9 @@ namespace SDL3.Tests pollEvents(); - SDL_SetRenderDrawColorFloat(Renderer, SDL_sinf(frame) / 2 + 0.5f, SDL_cosf(frame) / 2 + 0.5f, 0.3f, 1.0f); - SDL_RenderClear(Renderer); - SDL_RenderPresent(Renderer); + SDL_SetRenderDrawColorFloat(renderer, SDL_sinf(frame) / 2 + 0.5f, SDL_cosf(frame) / 2 + 0.5f, 0.3f, 1.0f); + SDL_RenderClear(renderer); + SDL_RenderPresent(renderer); frame += 0.015f; @@ -230,7 +231,7 @@ namespace SDL3.Tests public void Dispose() { if (initSuccess) - SDL_QuitSubSystem(initFlags); + SDL_QuitSubSystem(init_flags); objectHandle.Dispose(); } diff --git a/SDL3-CS.Tests/ObjectHandle.cs b/SDL3-CS.Tests/ObjectHandle.cs index 4252799..1cd332f 100644 --- a/SDL3-CS.Tests/ObjectHandle.cs +++ b/SDL3-CS.Tests/ObjectHandle.cs @@ -5,7 +5,7 @@ using System.Runtime.InteropServices; -namespace osu.Framework.Allocation +namespace SDL3.Tests { /// /// Wrapper on that supports the pattern. diff --git a/SDL3-CS.Tests/Program.cs b/SDL3-CS.Tests/Program.cs index 045e97a..6e87f94 100644 --- a/SDL3-CS.Tests/Program.cs +++ b/SDL3-CS.Tests/Program.cs @@ -10,7 +10,7 @@ namespace SDL3.Tests { public static class Program { - private static void Main() + public static void Main() { Console.OutputEncoding = Encoding.UTF8; diff --git a/SDL3-CS.sln.DotSettings b/SDL3-CS.sln.DotSettings index 21fd1f1..45e9c9e 100644 --- a/SDL3-CS.sln.DotSettings +++ b/SDL3-CS.sln.DotSettings @@ -54,6 +54,7 @@ WARNING WARNING WARNING + DO_NOT_SHOW HINT WARNING HINT diff --git a/SDL3-CS/ConstantAttribute.cs b/SDL3-CS/ConstantAttribute.cs index 9dce031..12c1059 100644 --- a/SDL3-CS/ConstantAttribute.cs +++ b/SDL3-CS/ConstantAttribute.cs @@ -8,7 +8,7 @@ namespace SDL { /// /// Denotes a manually defined constant. - /// Such consants should be excluded from ClangSharp generation to prevent warnings or duplicate definitions. + /// Such constants should be excluded from ClangSharp generation to prevent warnings or duplicate definitions. /// Handled by get_manually_written_symbols() in generate_bindings.py. /// [AttributeUsage(AttributeTargets.Field)] diff --git a/SDL3-CS/SDL3/SDL_camera.cs b/SDL3-CS/SDL3/SDL_camera.cs index b6a664e..714929b 100644 --- a/SDL3-CS/SDL3/SDL_camera.cs +++ b/SDL3-CS/SDL3/SDL_camera.cs @@ -2,6 +2,7 @@ // See the LICENCE file in the repository root for full licence text. using System; +using JetBrains.Annotations; namespace SDL { @@ -10,6 +11,7 @@ namespace SDL public static partial class SDL3 { + [MustDisposeResource] public static unsafe SDLArray? SDL_GetCameraDevices() { int count; @@ -17,6 +19,7 @@ namespace SDL return SDLArray.Create(array, count); } + [MustDisposeResource] public static unsafe SDLArray? SDL_GetCameraDeviceSupportedFormats(SDL_CameraDeviceID devid) { int count; diff --git a/SDL3-CS/TypedefAttribute.cs b/SDL3-CS/TypedefAttribute.cs index 54d4f7e..901271c 100644 --- a/SDL3-CS/TypedefAttribute.cs +++ b/SDL3-CS/TypedefAttribute.cs @@ -8,7 +8,7 @@ namespace SDL { /// /// Denotes a C typedef that is expressed as an enum in C#. - /// These need to be manually signaled to ClagnSharp so it uses the custom enum type instaed of the underlying type. + /// These need to be manually signaled to ClangSharp so it uses the custom enum type instead of the underlying type. /// Handled by get_typedefs() in generate_bindings.py. /// [AttributeUsage(AttributeTargets.Enum)]