From fb8553e4d763543ac2aad380189c5d98011269a4 Mon Sep 17 00:00:00 2001 From: Paul Joannon Date: Fri, 19 Apr 2024 21:38:32 +0200 Subject: [PATCH] Move to .NET8 - Change TFM and LangVersion - Better exception throwing (CA1510, CA1512, CA1513) - Better exception utility method definition (CA1859) - Prefer comparing `.Count` over calling `.Any()` (CA1860) - Prefer `.AsSpan()` over `.Substring()` (CA1846) - Add a few more `scoped` - Use `RuntimeHelpers.GetUninitializedObject()` instead of `FormatterServices.GetUninitializedObject()` - Use delegate instead of delegate pointer in variant generic conversions - Enable EnforceExtendedAnalyzerRules in source generator projects - Disable CS8981 on structs named movable in Godot.NativeInterop --- .../mono/build_scripts/build_assemblies.py | 2 +- .../Godot.SourceGenerators.Sample.csproj | 4 +- .../CSharpAnalyzerVerifier.cs | 2 +- .../CSharpCodeFixVerifier.cs | 3 +- .../CSharpSourceGeneratorVerifier.cs | 2 +- .../Godot.SourceGenerators.Tests/Constants.cs | 8 +++ .../Godot.SourceGenerators.Tests.csproj | 5 +- .../Godot.SourceGenerators.csproj | 1 + .../GodotTools.Core/GodotTools.Core.csproj | 6 +- .../GodotTools.ProjectEditor.csproj | 8 ++- .../GodotTools.Shared.csproj | 12 ++-- .../GodotTools/GodotTools/GodotTools.csproj | 12 +++- .../Godot.SourceGenerators.Internal.csproj | 8 ++- .../UnmanagedCallbacksGenerator.cs | 24 +++++--- .../GodotPlugins/GodotPlugins.csproj | 4 +- .../glue/GodotSharp/GodotSharp/Core/Array.cs | 21 +++---- .../Core/Bridge/ScriptManagerBridge.cs | 5 +- .../GodotSharp/Core/DebuggingUtils.cs | 4 +- .../GodotSharp/GodotSharp/Core/Dictionary.cs | 12 ++-- .../GodotSharp/Core/GodotObject.base.cs | 3 +- .../GodotSharp/Core/GodotTaskScheduler.cs | 2 +- .../Core/NativeInterop/InteropStructs.cs | 13 ++++- .../Core/NativeInterop/NativeFuncs.cs | 57 +++++++++---------- .../Core/NativeInterop/VariantUtils.cs | 21 ++++++- .../NativeInterop/VariantUtils.generic.cs | 19 +++---- .../GodotSharp/Core/StringExtensions.cs | 2 +- .../GodotSharp/GodotSharp/GodotSharp.csproj | 13 ++++- .../GodotSharpEditor/GodotSharpEditor.csproj | 4 +- 28 files changed, 167 insertions(+), 110 deletions(-) diff --git a/modules/mono/build_scripts/build_assemblies.py b/modules/mono/build_scripts/build_assemblies.py index 2a1ecba3323..bdb0ce89213 100755 --- a/modules/mono/build_scripts/build_assemblies.py +++ b/modules/mono/build_scripts/build_assemblies.py @@ -229,7 +229,7 @@ def build_godot_api(msbuild_tool, module_dir, output_dir, push_nupkgs_local, pre core_src_dir = os.path.abspath(os.path.join(sln, os.pardir, "GodotSharp", "bin", build_config)) editor_src_dir = os.path.abspath(os.path.join(sln, os.pardir, "GodotSharpEditor", "bin", build_config)) - plugins_src_dir = os.path.abspath(os.path.join(sln, os.pardir, "GodotPlugins", "bin", build_config, "net7.0")) + plugins_src_dir = os.path.abspath(os.path.join(sln, os.pardir, "GodotPlugins", "bin", build_config, "net8.0")) if not os.path.isdir(editor_api_dir): assert not os.path.isfile(editor_api_dir) diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/Godot.SourceGenerators.Sample.csproj b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/Godot.SourceGenerators.Sample.csproj index 5152b383ead..bbb180914cc 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/Godot.SourceGenerators.Sample.csproj +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/Godot.SourceGenerators.Sample.csproj @@ -1,8 +1,8 @@  - net7.0 - 11 + net8.0 + 12 diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/CSharpAnalyzerVerifier.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/CSharpAnalyzerVerifier.cs index 253889296b1..09d8faa35f1 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/CSharpAnalyzerVerifier.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/CSharpAnalyzerVerifier.cs @@ -21,7 +21,7 @@ public static class CSharpAnalyzerVerifier { public Test() { - ReferenceAssemblies = ReferenceAssemblies.Net.Net60; + ReferenceAssemblies = Constants.Net80; SolutionTransforms.Add((Solution solution, ProjectId projectId) => { diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/CSharpCodeFixVerifier.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/CSharpCodeFixVerifier.cs index 51e215a17b2..c00ac45b89c 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/CSharpCodeFixVerifier.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/CSharpCodeFixVerifier.cs @@ -4,7 +4,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.CSharp.Testing; using Microsoft.CodeAnalysis.Diagnostics; -using Microsoft.CodeAnalysis.Testing; using Microsoft.CodeAnalysis.Testing.Verifiers; namespace Godot.SourceGenerators.Tests; @@ -17,7 +16,7 @@ public static class CSharpCodeFixVerifier { public Test() { - ReferenceAssemblies = ReferenceAssemblies.Net.Net60; + ReferenceAssemblies = Constants.Net80; SolutionTransforms.Add((Solution solution, ProjectId projectId) => { Project project = solution.GetProject(projectId)! diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/CSharpSourceGeneratorVerifier.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/CSharpSourceGeneratorVerifier.cs index 84e319352d9..975f9f4e3f5 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/CSharpSourceGeneratorVerifier.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/CSharpSourceGeneratorVerifier.cs @@ -18,7 +18,7 @@ where TSourceGenerator : ISourceGenerator, new() { public Test() { - ReferenceAssemblies = ReferenceAssemblies.Net.Net60; + ReferenceAssemblies = Constants.Net80; SolutionTransforms.Add((Solution solution, ProjectId projectId) => { diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/Constants.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/Constants.cs index 783b1e42987..bfd8fc73692 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/Constants.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/Constants.cs @@ -1,5 +1,6 @@ using System.IO; using System.Reflection; +using Microsoft.CodeAnalysis.Testing; namespace Godot.SourceGenerators.Tests; @@ -7,6 +8,13 @@ public static class Constants { public static Assembly GodotSharpAssembly => typeof(GodotObject).Assembly; + // Can't find what needs updating to be able to access ReferenceAssemblies.Net.Net80, so we're making our own one. + public static ReferenceAssemblies Net80 => new ReferenceAssemblies( + "net8.0", + new PackageIdentity("Microsoft.NETCore.App.Ref", "8.0.0"), + Path.Combine("ref", "net8.0") + ); + public static string ExecutingAssemblyPath { get; } public static string SourceFolderPath { get; } public static string GeneratedSourceFolderPath { get; } diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/Godot.SourceGenerators.Tests.csproj b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/Godot.SourceGenerators.Tests.csproj index 31a255dcdfc..676facf90d1 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/Godot.SourceGenerators.Tests.csproj +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Tests/Godot.SourceGenerators.Tests.csproj @@ -1,9 +1,8 @@  - net6.0 - - 11 + net8.0 + 12 enable false diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Godot.SourceGenerators.csproj b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Godot.SourceGenerators.csproj index 8e407da7a6a..e8302f41375 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Godot.SourceGenerators.csproj +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Godot.SourceGenerators.csproj @@ -3,6 +3,7 @@ netstandard2.0 10 enable + true Core C# source generator for Godot projects. diff --git a/modules/mono/editor/GodotTools/GodotTools.Core/GodotTools.Core.csproj b/modules/mono/editor/GodotTools/GodotTools.Core/GodotTools.Core.csproj index 37563e0cc8e..833f6b269a6 100644 --- a/modules/mono/editor/GodotTools/GodotTools.Core/GodotTools.Core.csproj +++ b/modules/mono/editor/GodotTools/GodotTools.Core/GodotTools.Core.csproj @@ -1,8 +1,10 @@  + {639E48BD-44E5-4091-8EDD-22D36DC0768D} - net7.0 - 10 + net8.0 + 12 enable + diff --git a/modules/mono/editor/GodotTools/GodotTools.ProjectEditor/GodotTools.ProjectEditor.csproj b/modules/mono/editor/GodotTools/GodotTools.ProjectEditor/GodotTools.ProjectEditor.csproj index 9a73a7f81d4..b08efd0964c 100644 --- a/modules/mono/editor/GodotTools/GodotTools.ProjectEditor/GodotTools.ProjectEditor.csproj +++ b/modules/mono/editor/GodotTools/GodotTools.ProjectEditor/GodotTools.ProjectEditor.csproj @@ -1,17 +1,21 @@  + {A8CDAD94-C6D4-4B19-A7E7-76C53CC92984} - net7.0 - 10 + net8.0 + 12 enable + + + diff --git a/modules/mono/editor/GodotTools/GodotTools.Shared/GodotTools.Shared.csproj b/modules/mono/editor/GodotTools/GodotTools.Shared/GodotTools.Shared.csproj index e916980f15f..a281853f682 100644 --- a/modules/mono/editor/GodotTools/GodotTools.Shared/GodotTools.Shared.csproj +++ b/modules/mono/editor/GodotTools/GodotTools.Shared/GodotTools.Shared.csproj @@ -1,8 +1,12 @@ - + + - net8.0 - - false + net8.0 + 12 + + false + + diff --git a/modules/mono/editor/GodotTools/GodotTools/GodotTools.csproj b/modules/mono/editor/GodotTools/GodotTools/GodotTools.csproj index a8deeb2fea2..c82da741bd1 100644 --- a/modules/mono/editor/GodotTools/GodotTools/GodotTools.csproj +++ b/modules/mono/editor/GodotTools/GodotTools/GodotTools.csproj @@ -1,9 +1,10 @@  + {27B00618-A6F2-4828-B922-05CAEB08C286} - net7.0 + net8.0 + 12 true - 10 enable Debug @@ -13,13 +14,16 @@ false true + true + + @@ -27,6 +31,7 @@ False + @@ -41,14 +46,17 @@ False + + + diff --git a/modules/mono/glue/GodotSharp/Godot.SourceGenerators.Internal/Godot.SourceGenerators.Internal.csproj b/modules/mono/glue/GodotSharp/Godot.SourceGenerators.Internal/Godot.SourceGenerators.Internal.csproj index 81add0e44f1..ee607ff279e 100644 --- a/modules/mono/glue/GodotSharp/Godot.SourceGenerators.Internal/Godot.SourceGenerators.Internal.csproj +++ b/modules/mono/glue/GodotSharp/Godot.SourceGenerators.Internal/Godot.SourceGenerators.Internal.csproj @@ -1,11 +1,15 @@  + netstandard2.0 10 enable + true + - - + + + diff --git a/modules/mono/glue/GodotSharp/Godot.SourceGenerators.Internal/UnmanagedCallbacksGenerator.cs b/modules/mono/glue/GodotSharp/Godot.SourceGenerators.Internal/UnmanagedCallbacksGenerator.cs index 3d6a5b4662a..80d6160f3d6 100644 --- a/modules/mono/glue/GodotSharp/Godot.SourceGenerators.Internal/UnmanagedCallbacksGenerator.cs +++ b/modules/mono/glue/GodotSharp/Godot.SourceGenerators.Internal/UnmanagedCallbacksGenerator.cs @@ -172,7 +172,11 @@ using Godot.NativeInterop; { var parameter = callback.Parameters[i]; - source.Append(parameter.DeclaringSyntaxReferences[0].GetSyntax().ToString()); + AppendRefKind(source, parameter.RefKind, parameter.ScopedKind); + source.Append(' '); + source.Append(parameter.Type.FullQualifiedNameIncludeGlobal()); + source.Append(' '); + source.Append(parameter.Name); if (parameter.RefKind == RefKind.Out) { @@ -204,7 +208,7 @@ using Godot.NativeInterop; { // If it's a by-ref param and we can't get the pointer // just pass it by-ref and let it be pinned. - AppendRefKind(methodCallArguments, parameter.RefKind) + AppendRefKind(methodCallArguments, parameter.RefKind, parameter.ScopedKind) .Append(' ') .Append(parameter.Name); } @@ -342,7 +346,7 @@ using Godot.NativeInterop; { // If it's a by-ref param and we can't get the pointer // just pass it by-ref and let it be pinned. - AppendRefKind(source, parameter.RefKind) + AppendRefKind(source, parameter.RefKind, parameter.ScopedKind) .Append(' ') .Append(parameter.Type.FullQualifiedNameIncludeGlobal()); } @@ -388,14 +392,18 @@ using Godot.NativeInterop; private static bool IsByRefParameter(IParameterSymbol parameter) => parameter.RefKind is RefKind.In or RefKind.Out or RefKind.Ref; - private static StringBuilder AppendRefKind(StringBuilder source, RefKind refKind) => - refKind switch + private static StringBuilder AppendRefKind(StringBuilder source, RefKind refKind, ScopedKind scopedKind) + { + return (refKind, scopedKind) switch { - RefKind.In => source.Append("in"), - RefKind.Out => source.Append("out"), - RefKind.Ref => source.Append("ref"), + (RefKind.Out, _) => source.Append("out"), + (RefKind.In, ScopedKind.ScopedRef) => source.Append("scoped in"), + (RefKind.In, _) => source.Append("in"), + (RefKind.Ref, ScopedKind.ScopedRef) => source.Append("scoped ref"), + (RefKind.Ref, _) => source.Append("ref"), _ => source, }; + } private static void AppendPointerType(StringBuilder source, ITypeSymbol type) { diff --git a/modules/mono/glue/GodotSharp/GodotPlugins/GodotPlugins.csproj b/modules/mono/glue/GodotSharp/GodotPlugins/GodotPlugins.csproj index 1deccbc09d9..6d557c1eacf 100644 --- a/modules/mono/glue/GodotSharp/GodotPlugins/GodotPlugins.csproj +++ b/modules/mono/glue/GodotSharp/GodotPlugins/GodotPlugins.csproj @@ -1,8 +1,8 @@  - net7.0 - 10 + net8.0 + 12 enable true diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs index c4bfed85429..9879a781043 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs @@ -50,8 +50,7 @@ namespace Godot.Collections /// A new Godot Array. public Array(IEnumerable collection) : this() { - if (collection == null) - throw new ArgumentNullException(nameof(collection)); + ArgumentNullException.ThrowIfNull(collection); foreach (Variant element in collection) Add(element); @@ -67,8 +66,7 @@ namespace Godot.Collections /// A new Godot Array. public Array(Variant[] array) { - if (array == null) - throw new ArgumentNullException(nameof(array)); + ArgumentNullException.ThrowIfNull(array); NativeValue = (godot_array.movable)NativeFuncs.godotsharp_array_new(); _weakReferenceToSelf = DisposablesTracker.RegisterDisposable(this); @@ -1056,7 +1054,7 @@ namespace Godot.Collections IEnumerable, IGenericGodotArray { - private static godot_variant ToVariantFunc(in Array godotArray) => + private static godot_variant ToVariantFunc(scoped in Array godotArray) => VariantUtils.CreateFromArray(godotArray); private static Array FromVariantFunc(in godot_variant variant) => @@ -1080,8 +1078,8 @@ namespace Godot.Collections static unsafe Array() { - VariantUtils.GenericConversion>.ToVariantCb = &ToVariantFunc; - VariantUtils.GenericConversion>.FromVariantCb = &FromVariantFunc; + VariantUtils.GenericConversion>.ToVariantCb = ToVariantFunc; + VariantUtils.GenericConversion>.FromVariantCb = FromVariantFunc; } private readonly Array _underlyingArray; @@ -1114,8 +1112,7 @@ namespace Godot.Collections /// A new Godot Array. public Array(IEnumerable collection) { - if (collection == null) - throw new ArgumentNullException(nameof(collection)); + ArgumentNullException.ThrowIfNull(collection); _underlyingArray = new Array(); SetTypedForUnderlyingArray(); @@ -1134,8 +1131,7 @@ namespace Godot.Collections /// A new Godot Array. public Array(T[] array) { - if (array == null) - throw new ArgumentNullException(nameof(array)); + ArgumentNullException.ThrowIfNull(array); _underlyingArray = new Array(); SetTypedForUnderlyingArray(); @@ -1154,8 +1150,7 @@ namespace Godot.Collections /// A new Godot Array. public Array(Array array) { - if (array == null) - throw new ArgumentNullException(nameof(array)); + ArgumentNullException.ThrowIfNull(array); _underlyingArray = array; } diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs index 93a3f52f7db..7019ea2932e 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs @@ -90,8 +90,7 @@ namespace Godot.Bridge } [UnmanagedCallersOnly] - internal static unsafe IntPtr CreateManagedForGodotObjectBinding(godot_string_name* nativeTypeName, - IntPtr godotObject) + internal static unsafe IntPtr CreateManagedForGodotObjectBinding(godot_string_name* nativeTypeName, IntPtr godotObject) { try { @@ -143,7 +142,7 @@ namespace Godot.Bridge } } - var obj = (GodotObject)FormatterServices.GetUninitializedObject(scriptType); + var obj = (GodotObject)RuntimeHelpers.GetUninitializedObject(scriptType); var parameters = ctor.GetParameters(); int paramCount = parameters.Length; diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/DebuggingUtils.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/DebuggingUtils.cs index 0d96a9a5c18..0c83fd58aa1 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/DebuggingUtils.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/DebuggingUtils.cs @@ -87,8 +87,8 @@ namespace Godot public void Resize(int size) { - if (size < 0) - throw new ArgumentOutOfRangeException(nameof(size)); + ArgumentOutOfRangeException.ThrowIfNegative(size); + var err = NativeFuncs.godotsharp_stack_info_vector_resize(ref this, size); if (err != Error.Ok) throw new InvalidOperationException("Failed to resize vector. Error code is: " + err.ToString()); diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs index ad313dbbf03..dcb94f05fdf 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs @@ -492,7 +492,7 @@ namespace Godot.Collections IReadOnlyDictionary, IGenericGodotDictionary { - private static godot_variant ToVariantFunc(in Dictionary godotDictionary) => + private static godot_variant ToVariantFunc(scoped in Dictionary godotDictionary) => VariantUtils.CreateFromDictionary(godotDictionary); private static Dictionary FromVariantFunc(in godot_variant variant) => @@ -521,8 +521,8 @@ namespace Godot.Collections static unsafe Dictionary() { - VariantUtils.GenericConversion>.ToVariantCb = &ToVariantFunc; - VariantUtils.GenericConversion>.FromVariantCb = &FromVariantFunc; + VariantUtils.GenericConversion>.ToVariantCb = ToVariantFunc; + VariantUtils.GenericConversion>.FromVariantCb = FromVariantFunc; } private readonly Dictionary _underlyingDict; @@ -555,8 +555,7 @@ namespace Godot.Collections /// A new Godot Dictionary. public Dictionary(IDictionary dictionary) { - if (dictionary == null) - throw new ArgumentNullException(nameof(dictionary)); + ArgumentNullException.ThrowIfNull(dictionary); _underlyingDict = new Dictionary(); SetTypedForUnderlyingDictionary(); @@ -575,8 +574,7 @@ namespace Godot.Collections /// A new Godot Dictionary. public Dictionary(Dictionary dictionary) { - if (dictionary == null) - throw new ArgumentNullException(nameof(dictionary)); + ArgumentNullException.ThrowIfNull(dictionary); _underlyingDict = dictionary; } diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotObject.base.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotObject.base.cs index 00eb64dd170..08aea77e84c 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotObject.base.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotObject.base.cs @@ -90,8 +90,7 @@ namespace Godot // NativePtr is assigned, that would result in UB or crashes when calling // native functions that receive the pointer, which can happen because the // debugger calls ToString() and tries to get the value of properties. - if (instance._disposed || instance.NativePtr == IntPtr.Zero) - throw new ObjectDisposedException(instance.GetType().FullName); + ObjectDisposedException.ThrowIf(instance._disposed || instance.NativePtr == IntPtr.Zero, instance); return instance.NativePtr; } diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotTaskScheduler.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotTaskScheduler.cs index f6c36455b27..f84bf9de7c0 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotTaskScheduler.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotTaskScheduler.cs @@ -88,7 +88,7 @@ namespace Godot lock (_tasks) { - if (_tasks.Any()) + if (_tasks.Count > 0) { task = _tasks.First.Value; _tasks.RemoveFirst(); diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/InteropStructs.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/InteropStructs.cs index 6e34c2bfbfc..dda776fee5f 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/InteropStructs.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/InteropStructs.cs @@ -173,7 +173,8 @@ namespace Godot.NativeInterop internal readonly unsafe godot_variant* GetUnsafeAddress() => (godot_variant*)Unsafe.AsPointer(ref Unsafe.AsRef(in _typeField)); - // Variant.Type is generated as an enum of type long, so we can't use for the field as it must only take 32-bits. (the native enum actually has no fixed underlying type so it is only at least 6 bits long) + // Variant.Type is generated as an enum of type long, so we can't use for the field as it must only take 32-bits. + // The native enum actually has no fixed underlying type, so it is only at least 6 bits long. private int _typeField; // There's padding here @@ -481,8 +482,10 @@ namespace Godot.NativeInterop Type = Variant.Type.Nil; } +#pragma warning disable CS8981 // The type name only contains lower-cased ascii characters [StructLayout(LayoutKind.Explicit)] internal struct movable +#pragma warning restore CS8981 { // Variant.Type is generated as an enum of type long, so we can't use for the field as it must only take 32-bits. [FieldOffset(0)] private int _typeField; @@ -588,8 +591,10 @@ namespace Godot.NativeInterop return _data.GetHashCode(); } +#pragma warning disable CS8981 // The type name only contains lower-cased ascii characters [StructLayout(LayoutKind.Sequential)] internal struct movable +#pragma warning restore CS8981 { private IntPtr _data; @@ -634,8 +639,10 @@ namespace Godot.NativeInterop get => _data == IntPtr.Zero; } +#pragma warning disable CS8981 // The type name only contains lower-cased ascii characters [StructLayout(LayoutKind.Sequential)] internal struct movable +#pragma warning restore CS8981 { private IntPtr _data; @@ -809,8 +816,10 @@ namespace Godot.NativeInterop _p = null; } +#pragma warning disable CS8981 // The type name only contains lower-cased ascii characters [StructLayout(LayoutKind.Sequential)] internal struct movable +#pragma warning restore CS8981 { private unsafe ArrayPrivate* _p; @@ -876,8 +885,10 @@ namespace Godot.NativeInterop _p = null; } +#pragma warning disable CS8981 // The type name only contains lower-cased ascii characters [StructLayout(LayoutKind.Sequential)] internal struct movable +#pragma warning restore CS8981 { private unsafe DictionaryPrivate* _p; diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.cs index 8c2bc7941b3..021a701521e 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.cs @@ -233,51 +233,51 @@ namespace Godot.NativeInterop public static partial void godotsharp_variant_new_packed_color_array(out godot_variant r_dest, scoped in godot_packed_color_array p_pca); - public static partial godot_bool godotsharp_variant_as_bool(in godot_variant p_self); + public static partial godot_bool godotsharp_variant_as_bool(scoped in godot_variant p_self); - public static partial Int64 godotsharp_variant_as_int(in godot_variant p_self); + public static partial Int64 godotsharp_variant_as_int(scoped in godot_variant p_self); - public static partial double godotsharp_variant_as_float(in godot_variant p_self); + public static partial double godotsharp_variant_as_float(scoped in godot_variant p_self); public static partial godot_string godotsharp_variant_as_string(scoped in godot_variant p_self); - public static partial Vector2 godotsharp_variant_as_vector2(in godot_variant p_self); + public static partial Vector2 godotsharp_variant_as_vector2(scoped in godot_variant p_self); - public static partial Vector2I godotsharp_variant_as_vector2i(in godot_variant p_self); + public static partial Vector2I godotsharp_variant_as_vector2i(scoped in godot_variant p_self); - public static partial Rect2 godotsharp_variant_as_rect2(in godot_variant p_self); + public static partial Rect2 godotsharp_variant_as_rect2(scoped in godot_variant p_self); - public static partial Rect2I godotsharp_variant_as_rect2i(in godot_variant p_self); + public static partial Rect2I godotsharp_variant_as_rect2i(scoped in godot_variant p_self); - public static partial Vector3 godotsharp_variant_as_vector3(in godot_variant p_self); + public static partial Vector3 godotsharp_variant_as_vector3(scoped in godot_variant p_self); - public static partial Vector3I godotsharp_variant_as_vector3i(in godot_variant p_self); + public static partial Vector3I godotsharp_variant_as_vector3i(scoped in godot_variant p_self); - public static partial Transform2D godotsharp_variant_as_transform2d(in godot_variant p_self); + public static partial Transform2D godotsharp_variant_as_transform2d(scoped in godot_variant p_self); - public static partial Vector4 godotsharp_variant_as_vector4(in godot_variant p_self); + public static partial Vector4 godotsharp_variant_as_vector4(scoped in godot_variant p_self); - public static partial Vector4I godotsharp_variant_as_vector4i(in godot_variant p_self); + public static partial Vector4I godotsharp_variant_as_vector4i(scoped in godot_variant p_self); - public static partial Plane godotsharp_variant_as_plane(in godot_variant p_self); + public static partial Plane godotsharp_variant_as_plane(scoped in godot_variant p_self); - public static partial Quaternion godotsharp_variant_as_quaternion(in godot_variant p_self); + public static partial Quaternion godotsharp_variant_as_quaternion(scoped in godot_variant p_self); - public static partial Aabb godotsharp_variant_as_aabb(in godot_variant p_self); + public static partial Aabb godotsharp_variant_as_aabb(scoped in godot_variant p_self); - public static partial Basis godotsharp_variant_as_basis(in godot_variant p_self); + public static partial Basis godotsharp_variant_as_basis(scoped in godot_variant p_self); - public static partial Transform3D godotsharp_variant_as_transform3d(in godot_variant p_self); + public static partial Transform3D godotsharp_variant_as_transform3d(scoped in godot_variant p_self); - public static partial Projection godotsharp_variant_as_projection(in godot_variant p_self); + public static partial Projection godotsharp_variant_as_projection(scoped in godot_variant p_self); - public static partial Color godotsharp_variant_as_color(in godot_variant p_self); + public static partial Color godotsharp_variant_as_color(scoped in godot_variant p_self); public static partial godot_string_name godotsharp_variant_as_string_name(scoped in godot_variant p_self); public static partial godot_node_path godotsharp_variant_as_node_path(scoped in godot_variant p_self); - public static partial Rid godotsharp_variant_as_rid(in godot_variant p_self); + public static partial Rid godotsharp_variant_as_rid(scoped in godot_variant p_self); public static partial godot_callable godotsharp_variant_as_callable(scoped in godot_variant p_self); @@ -293,27 +293,22 @@ namespace Godot.NativeInterop public static partial godot_packed_int64_array godotsharp_variant_as_packed_int64_array(scoped in godot_variant p_self); - public static partial godot_packed_float32_array godotsharp_variant_as_packed_float32_array( - scoped in godot_variant p_self); + public static partial godot_packed_float32_array godotsharp_variant_as_packed_float32_array(scoped in godot_variant p_self); - public static partial godot_packed_float64_array godotsharp_variant_as_packed_float64_array( - scoped in godot_variant p_self); + public static partial godot_packed_float64_array godotsharp_variant_as_packed_float64_array(scoped in godot_variant p_self); - public static partial godot_packed_string_array godotsharp_variant_as_packed_string_array( - scoped in godot_variant p_self); + public static partial godot_packed_string_array godotsharp_variant_as_packed_string_array(scoped in godot_variant p_self); - public static partial godot_packed_vector2_array godotsharp_variant_as_packed_vector2_array( - scoped in godot_variant p_self); + public static partial godot_packed_vector2_array godotsharp_variant_as_packed_vector2_array(scoped in godot_variant p_self); - public static partial godot_packed_vector3_array godotsharp_variant_as_packed_vector3_array( - scoped in godot_variant p_self); + public static partial godot_packed_vector3_array godotsharp_variant_as_packed_vector3_array(scoped in godot_variant p_self); public static partial godot_packed_vector4_array godotsharp_variant_as_packed_vector4_array( in godot_variant p_self); public static partial godot_packed_color_array godotsharp_variant_as_packed_color_array(scoped in godot_variant p_self); - public static partial godot_bool godotsharp_variant_equals(in godot_variant p_a, in godot_variant p_b); + public static partial godot_bool godotsharp_variant_equals(scoped in godot_variant p_a, scoped in godot_variant p_b); // string.h diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.cs index 5ff9969cfd4..a48e60a30f6 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.cs @@ -248,13 +248,28 @@ namespace Godot.NativeInterop } public static godot_variant CreateFromSystemArrayOfStringName(scoped Span from) - => CreateFromArray(new Collections.Array(from)); + { + if (from == null) + return default; + using var fromGodot = new Collections.Array(from); + return CreateFromArray((godot_array)fromGodot.NativeValue); + } public static godot_variant CreateFromSystemArrayOfNodePath(scoped Span from) - => CreateFromArray(new Collections.Array(from)); + { + if (from == null) + return default; + using var fromGodot = new Collections.Array(from); + return CreateFromArray((godot_array)fromGodot.NativeValue); + } public static godot_variant CreateFromSystemArrayOfRid(scoped Span from) - => CreateFromArray(new Collections.Array(from)); + { + if (from == null) + return default; + using var fromGodot = new Collections.Array(from); + return CreateFromArray((godot_array)fromGodot.NativeValue); + } public static godot_variant CreateFromSystemArrayOfGodotObject(GodotObject[]? from) { diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.generic.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.generic.cs index a5ef11404f4..d8c8ea568fd 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.generic.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.generic.cs @@ -7,24 +7,23 @@ namespace Godot.NativeInterop; public partial class VariantUtils { - private static Exception UnsupportedType() => new InvalidOperationException( + private static InvalidOperationException UnsupportedType() => new InvalidOperationException( $"The type is not supported for conversion to/from Variant: '{typeof(T).FullName}'"); internal static class GenericConversion { + internal delegate godot_variant ToVariantConverter(scoped in T from); + internal delegate T FromVariantConverter(in godot_variant from); + public static unsafe godot_variant ToVariant(scoped in T from) => -#pragma warning disable CS9088 // the delegate pointer cannot be marked scoped, but it should be - ToVariantCb != null ? ToVariantCb(from) : throw UnsupportedType(); -#pragma warning restore CS9088 + ToVariantCb != null ? ToVariantCb(from) : throw UnsupportedType(); public static unsafe T FromVariant(in godot_variant variant) => FromVariantCb != null ? FromVariantCb(variant) : throw UnsupportedType(); - // ReSharper disable once StaticMemberInGenericType - internal static unsafe delegate* ToVariantCb; + internal static ToVariantConverter? ToVariantCb; - // ReSharper disable once StaticMemberInGenericType - internal static unsafe delegate* FromVariantCb; + internal static FromVariantConverter? FromVariantCb; static GenericConversion() { @@ -36,7 +35,7 @@ public partial class VariantUtils public static godot_variant CreateFrom<[MustBeVariant] T>(scoped in T from) { [MethodImpl(MethodImplOptions.AggressiveInlining)] - static TTo UnsafeAs(in T f) => Unsafe.As(ref Unsafe.AsRef(f)); + static TTo UnsafeAs(in T f) => Unsafe.As(ref Unsafe.AsRef(in f)); // `typeof(T) == typeof(X)` is optimized away. We cannot cache `typeof(T)` in a local variable, as it's not optimized when done like that. @@ -229,7 +228,7 @@ public partial class VariantUtils public static T ConvertTo<[MustBeVariant] T>(in godot_variant variant) { [MethodImpl(MethodImplOptions.AggressiveInlining)] - static T UnsafeAsT(TFrom f) => Unsafe.As(ref Unsafe.AsRef(f)); + static T UnsafeAsT(TFrom f) => Unsafe.As(ref Unsafe.AsRef(in f)); if (typeof(T) == typeof(bool)) return UnsafeAsT(ConvertToBool(variant)); diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs index ad75195f0fa..c79c45fa08c 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/StringExtensions.cs @@ -1581,7 +1581,7 @@ namespace Godot if (end < 0) end = len; if (allowEmpty || end > from) - ret.Add(float.Parse(instance.Substring(from), CultureInfo.InvariantCulture)); + ret.Add(float.Parse(instance.AsSpan(from), CultureInfo.InvariantCulture)); if (end == len) break; diff --git a/modules/mono/glue/GodotSharp/GodotSharp/GodotSharp.csproj b/modules/mono/glue/GodotSharp/GodotSharp/GodotSharp.csproj index 09516cc893e..ab9c5fe18a2 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/GodotSharp.csproj +++ b/modules/mono/glue/GodotSharp/GodotSharp/GodotSharp.csproj @@ -1,17 +1,19 @@  + {AEBF0036-DA76-4341-B651-A3F2856AB2FA} + net8.0 + 12 bin/$(Configuration) false Godot - net7.0 $(OutputPath)/$(AssemblyName).xml false true - 11 Recommended + Godot C# Core API. Godot Engine contributors @@ -28,23 +30,28 @@ true snupkg + SdkPackageVersions.props + $(DefineConstants);GODOT REAL_T_IS_DOUBLE;$(DefineConstants) + + + @@ -135,10 +142,12 @@ + +