Refactorings + auto styling

This commit is contained in:
Dan Balasescu 2024-04-07 12:31:45 +09:00
parent 8003b85aa2
commit 913afa5b4f
No known key found for this signature in database
10 changed files with 66 additions and 59 deletions

View File

@ -21,7 +21,7 @@
<Product>SDL3-CS</Product> <Product>SDL3-CS</Product>
<PackageReleaseNotes>Automated release.</PackageReleaseNotes> <PackageReleaseNotes>Automated release.</PackageReleaseNotes>
<PackageLicenseExpression>MIT</PackageLicenseExpression> <PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/ppy/osu-framework</PackageProjectUrl> <PackageProjectUrl>https://github.com/ppy/SDL3-CS</PackageProjectUrl>
<RepositoryUrl>https://github.com/ppy/osu-framework</RepositoryUrl> <RepositoryUrl>https://github.com/ppy/SDL3-CS</RepositoryUrl>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@ -29,6 +29,7 @@ using System;
public void Execute(GeneratorExecutionContext context) public void Execute(GeneratorExecutionContext context)
{ {
var finder = (UnfriendlyMethodFinder)context.SyntaxReceiver!; var finder = (UnfriendlyMethodFinder)context.SyntaxReceiver!;
foreach (var kvp in finder.Methods) foreach (var kvp in finder.Methods)
{ {
string filename = kvp.Key; string filename = kvp.Key;

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
@ -12,16 +13,16 @@ namespace SDL3.SourceGeneration
{ {
public class UnfriendlyMethodFinder : ISyntaxReceiver public class UnfriendlyMethodFinder : ISyntaxReceiver
{ {
public readonly Dictionary<string, List<GeneratedMethod>> Methods = new(); public readonly Dictionary<string, List<GeneratedMethod>> Methods = new Dictionary<string, List<GeneratedMethod>>();
public void OnVisitSyntaxNode(SyntaxNode syntaxNode) public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{ {
if (syntaxNode is MethodDeclarationSyntax method) if (syntaxNode is MethodDeclarationSyntax method)
{ {
string name = method.Identifier.ValueText; 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; return;
if (method.ParameterList.Parameters.Any(p => p.Identifier.IsKind(SyntaxKind.ArgListKeyword))) if (method.ParameterList.Parameters.Any(p => p.Identifier.IsKind(SyntaxKind.ArgListKeyword)))

View File

@ -3,7 +3,6 @@
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using osu.Framework.Allocation;
using SDL; using SDL;
using static SDL.SDL3; using static SDL.SDL3;
@ -13,15 +12,15 @@ namespace SDL3.Tests
{ {
private bool flash; private bool flash;
private ObjectHandle<MyWindow> objectHandle { get; } private ObjectHandle<MyWindow> objectHandle { get; }
private SDL_Window* SDLWindowHandle; private SDL_Window* sdlWindowHandle;
private SDL_Renderer* Renderer; private SDL_Renderer* renderer;
private readonly bool initSuccess; 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() public MyWindow()
{ {
if (SDL_InitSubSystem(initFlags) < 0) if (SDL_InitSubSystem(init_flags) < 0)
throw new InvalidOperationException($"failed to initialise SDL. Error: {SDL_GetError()}"); throw new InvalidOperationException($"failed to initialise SDL. Error: {SDL_GetError()}");
initSuccess = true; initSuccess = true;
@ -42,6 +41,7 @@ namespace SDL3.Tests
private static SDL_bool wndProc(IntPtr userdata, MSG* message) private static SDL_bool wndProc(IntPtr userdata, MSG* message)
{ {
var handle = new ObjectHandle<MyWindow>(userdata); var handle = new ObjectHandle<MyWindow>(userdata);
if (handle.GetTarget(out var window)) if (handle.GetTarget(out var window))
{ {
Console.WriteLine($"from {window}, message: {message->message}"); Console.WriteLine($"from {window}, message: {message->message}");
@ -90,8 +90,8 @@ namespace SDL3.Tests
public void Create() public void Create()
{ {
SDLWindowHandle = SDL_CreateWindow("hello"u8, 800, 600, SDL_WindowFlags.SDL_WINDOW_RESIZABLE | SDL_WindowFlags.SDL_WINDOW_HIGH_PIXEL_DENSITY); 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); renderer = SDL_CreateRenderer(sdlWindowHandle, (byte*)null, SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC);
} }
private void handleEvent(SDL_Event e) private void handleEvent(SDL_Event e)
@ -116,11 +116,11 @@ namespace SDL3.Tests
break; break;
case SDL_Keycode.SDLK_F10: case SDL_Keycode.SDLK_F10:
SDL_SetWindowFullscreen(SDLWindowHandle, SDL_bool.SDL_FALSE); SDL_SetWindowFullscreen(sdlWindowHandle, SDL_bool.SDL_FALSE);
break; break;
case SDL_Keycode.SDLK_F11: case SDL_Keycode.SDLK_F11:
SDL_SetWindowFullscreen(SDLWindowHandle, SDL_bool.SDL_TRUE); SDL_SetWindowFullscreen(sdlWindowHandle, SDL_bool.SDL_TRUE);
break; break;
case SDL_Keycode.SDLK_j: case SDL_Keycode.SDLK_j:
@ -134,6 +134,7 @@ namespace SDL3.Tests
int count; int count;
var bindings = SDL_GetGamepadBindings(gamepad, &count); var bindings = SDL_GetGamepadBindings(gamepad, &count);
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
var binding = *bindings[i]; var binding = *bindings[i];
@ -217,9 +218,9 @@ namespace SDL3.Tests
pollEvents(); pollEvents();
SDL_SetRenderDrawColorFloat(Renderer, SDL_sinf(frame) / 2 + 0.5f, SDL_cosf(frame) / 2 + 0.5f, 0.3f, 1.0f); SDL_SetRenderDrawColorFloat(renderer, SDL_sinf(frame) / 2 + 0.5f, SDL_cosf(frame) / 2 + 0.5f, 0.3f, 1.0f);
SDL_RenderClear(Renderer); SDL_RenderClear(renderer);
SDL_RenderPresent(Renderer); SDL_RenderPresent(renderer);
frame += 0.015f; frame += 0.015f;
@ -230,7 +231,7 @@ namespace SDL3.Tests
public void Dispose() public void Dispose()
{ {
if (initSuccess) if (initSuccess)
SDL_QuitSubSystem(initFlags); SDL_QuitSubSystem(init_flags);
objectHandle.Dispose(); objectHandle.Dispose();
} }

View File

@ -5,7 +5,7 @@
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace osu.Framework.Allocation namespace SDL3.Tests
{ {
/// <summary> /// <summary>
/// Wrapper on <see cref="GCHandle" /> that supports the <see cref="IDisposable" /> pattern. /// Wrapper on <see cref="GCHandle" /> that supports the <see cref="IDisposable" /> pattern.

View File

@ -10,7 +10,7 @@ namespace SDL3.Tests
{ {
public static class Program public static class Program
{ {
private static void Main() public static void Main()
{ {
Console.OutputEncoding = Encoding.UTF8; Console.OutputEncoding = Encoding.UTF8;

View File

@ -54,6 +54,7 @@
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=CanBeReplacedWithTryCastAndCheckForNull/@EntryIndexedValue">WARNING</s:String> <s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=CanBeReplacedWithTryCastAndCheckForNull/@EntryIndexedValue">WARNING</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=CheckForReferenceEqualityInstead_002E1/@EntryIndexedValue">WARNING</s:String> <s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=CheckForReferenceEqualityInstead_002E1/@EntryIndexedValue">WARNING</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=CheckForReferenceEqualityInstead_002E2/@EntryIndexedValue">WARNING</s:String> <s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=CheckForReferenceEqualityInstead_002E2/@EntryIndexedValue">WARNING</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=CheckNamespace/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ClassNeverInstantiated_002EGlobal/@EntryIndexedValue">HINT</s:String> <s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ClassNeverInstantiated_002EGlobal/@EntryIndexedValue">HINT</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ClassNeverInstantiated_002ELocal/@EntryIndexedValue">WARNING</s:String> <s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ClassNeverInstantiated_002ELocal/@EntryIndexedValue">WARNING</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ClassWithVirtualMembersNeverInherited_002EGlobal/@EntryIndexedValue">HINT</s:String> <s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=ClassWithVirtualMembersNeverInherited_002EGlobal/@EntryIndexedValue">HINT</s:String>

View File

@ -8,7 +8,7 @@ namespace SDL
{ {
/// <summary> /// <summary>
/// Denotes a manually defined constant. /// 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 <c>get_manually_written_symbols()</c> in generate_bindings.py. /// Handled by <c>get_manually_written_symbols()</c> in generate_bindings.py.
/// </summary> /// </summary>
[AttributeUsage(AttributeTargets.Field)] [AttributeUsage(AttributeTargets.Field)]

View File

@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using JetBrains.Annotations;
namespace SDL namespace SDL
{ {
@ -10,6 +11,7 @@ namespace SDL
public static partial class SDL3 public static partial class SDL3
{ {
[MustDisposeResource]
public static unsafe SDLArray<SDL_CameraDeviceID>? SDL_GetCameraDevices() public static unsafe SDLArray<SDL_CameraDeviceID>? SDL_GetCameraDevices()
{ {
int count; int count;
@ -17,6 +19,7 @@ namespace SDL
return SDLArray.Create(array, count); return SDLArray.Create(array, count);
} }
[MustDisposeResource]
public static unsafe SDLArray<SDL_CameraSpec>? SDL_GetCameraDeviceSupportedFormats(SDL_CameraDeviceID devid) public static unsafe SDLArray<SDL_CameraSpec>? SDL_GetCameraDeviceSupportedFormats(SDL_CameraDeviceID devid)
{ {
int count; int count;

View File

@ -8,7 +8,7 @@ namespace SDL
{ {
/// <summary> /// <summary>
/// Denotes a C <c>typedef</c> that is expressed as an enum in C#. /// Denotes a C <c>typedef</c> 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 <c>get_typedefs()</c> in generate_bindings.py. /// Handled by <c>get_typedefs()</c> in generate_bindings.py.
/// </summary> /// </summary>
[AttributeUsage(AttributeTargets.Enum)] [AttributeUsage(AttributeTargets.Enum)]