From 1aceacaa6b0820629586d49fc6d3d8ba7f1694eb Mon Sep 17 00:00:00 2001 From: Raul Santos Date: Wed, 7 Dec 2022 16:16:51 +0100 Subject: C#: Rename `Object` to `GodotObject` --- modules/mono/csharp_script.cpp | 2 +- .../Godot.SourceGenerators.Sample/Bar.cs | 4 +- .../Godot.SourceGenerators.Sample/EventSignals.cs | 2 +- .../ExportedFields.cs | 6 +- .../ExportedProperties.cs | 6 +- .../Godot.SourceGenerators.Sample/Foo.cs | 2 +- .../Godot.SourceGenerators.Sample/Generic.cs | 6 +- .../Godot.SourceGenerators.Sample/Methods.cs | 2 +- .../MoreExportedFields.cs | 2 +- .../Godot.NET.Sdk/Godot.SourceGenerators/Common.cs | 8 +- .../Godot.SourceGenerators/ExtensionMethods.cs | 2 +- .../Godot.SourceGenerators/GodotClasses.cs | 2 +- .../GodotPluginsInitializerGenerator.cs | 4 +- .../Godot.SourceGenerators/MarshalUtils.cs | 2 +- modules/mono/editor/bindings_generator.cpp | 28 +-- .../EditorScenePostImport/basic_import_script.cs | 2 +- .../EditorScenePostImport/no_comments.cs | 2 +- modules/mono/glue/GodotSharp/GodotPlugins/Main.cs | 2 +- .../mono/glue/GodotSharp/GodotSharp/Core/Array.cs | 2 +- .../GodotSharp/Core/Bridge/CSharpInstanceBridge.cs | 16 +- .../GodotSharp/Core/Bridge/ScriptManagerBridge.cs | 26 +-- .../glue/GodotSharp/GodotSharp/Core/Callable.cs | 8 +- .../GodotSharp/GodotSharp/Core/DelegateUtils.cs | 18 +- .../glue/GodotSharp/GodotSharp/Core/Dictionary.cs | 4 +- .../GodotSharp/Core/DisposablesTracker.cs | 12 +- .../Core/Extensions/GodotObjectExtensions.cs | 86 ++++++++ .../GodotSharp/Core/Extensions/ObjectExtensions.cs | 86 -------- .../GodotSharp/GodotSharp/Core/GodotObject.base.cs | 242 +++++++++++++++++++++ .../GodotSharp/Core/GodotObject.exceptions.cs | 142 ++++++++++++ .../GodotSharp/Core/NativeInterop/InteropUtils.cs | 14 +- .../GodotSharp/Core/NativeInterop/Marshaling.cs | 12 +- .../GodotSharp/Core/NativeInterop/VariantUtils.cs | 10 +- .../Core/NativeInterop/VariantUtils.generic.cs | 6 +- .../glue/GodotSharp/GodotSharp/Core/Object.base.cs | 242 --------------------- .../GodotSharp/Core/Object.exceptions.cs | 142 ------------ .../mono/glue/GodotSharp/GodotSharp/Core/Rid.cs | 2 +- .../mono/glue/GodotSharp/GodotSharp/Core/Signal.cs | 7 +- .../GodotSharp/GodotSharp/Core/SignalAwaiter.cs | 6 +- .../glue/GodotSharp/GodotSharp/Core/Variant.cs | 14 +- .../glue/GodotSharp/GodotSharp/GodotSharp.csproj | 6 +- modules/mono/utils/naming_utils.cpp | 1 + 41 files changed, 595 insertions(+), 593 deletions(-) create mode 100644 modules/mono/glue/GodotSharp/GodotSharp/Core/Extensions/GodotObjectExtensions.cs delete mode 100644 modules/mono/glue/GodotSharp/GodotSharp/Core/Extensions/ObjectExtensions.cs create mode 100644 modules/mono/glue/GodotSharp/GodotSharp/Core/GodotObject.base.cs create mode 100644 modules/mono/glue/GodotSharp/GodotSharp/Core/GodotObject.exceptions.cs delete mode 100644 modules/mono/glue/GodotSharp/GodotSharp/Core/Object.base.cs delete mode 100644 modules/mono/glue/GodotSharp/GodotSharp/Core/Object.exceptions.cs diff --git a/modules/mono/csharp_script.cpp b/modules/mono/csharp_script.cpp index 5172c63e10..fe0f4aae81 100644 --- a/modules/mono/csharp_script.cpp +++ b/modules/mono/csharp_script.cpp @@ -410,7 +410,7 @@ static String variant_type_to_managed_name(const String &p_var_type_name) { } if (p_var_type_name == Variant::get_type_name(Variant::OBJECT)) { - return "Godot.Object"; + return "GodotObject"; } if (p_var_type_name == Variant::get_type_name(Variant::INT)) { diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/Bar.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/Bar.cs index 5eaebc4474..2d797e2f46 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/Bar.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/Bar.cs @@ -1,6 +1,6 @@ namespace Godot.SourceGenerators.Sample { - partial class Bar : Godot.Object + partial class Bar : GodotObject { } @@ -9,7 +9,7 @@ namespace Godot.SourceGenerators.Sample { } - partial class NotSameNameAsFile : Godot.Object + partial class NotSameNameAsFile : GodotObject { } } diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/EventSignals.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/EventSignals.cs index 764ba8f121..ee6aa857fc 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/EventSignals.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/EventSignals.cs @@ -1,6 +1,6 @@ namespace Godot.SourceGenerators.Sample; -public partial class EventSignals : Godot.Object +public partial class EventSignals : GodotObject { [Signal] public delegate void MySignalEventHandler(string str, int num); diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/ExportedFields.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/ExportedFields.cs index 813b810eea..31e66ac306 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/ExportedFields.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/ExportedFields.cs @@ -11,7 +11,7 @@ namespace Godot.SourceGenerators.Sample [SuppressMessage("ReSharper", "RedundantNameQualifier")] [SuppressMessage("ReSharper", "ArrangeObjectCreationWhenTypeEvident")] [SuppressMessage("ReSharper", "InconsistentNaming")] - public partial class ExportedFields : Godot.Object + public partial class ExportedFields : GodotObject { [Export] private Boolean field_Boolean = true; [Export] private Char field_Char = 'f'; @@ -80,7 +80,7 @@ namespace Godot.SourceGenerators.Sample [Export] private Vector2[] field_Vector2Array = { Vector2.Up, Vector2.Down, Vector2.Left, Vector2.Right }; [Export] private Vector3[] field_Vector3Array = { Vector3.Up, Vector3.Down, Vector3.Left, Vector3.Right }; [Export] private Color[] field_ColorArray = { Colors.Aqua, Colors.Aquamarine, Colors.Azure, Colors.Beige }; - [Export] private Godot.Object[] field_GodotObjectOrDerivedArray = { null }; + [Export] private GodotObject[] field_GodotObjectOrDerivedArray = { null }; [Export] private StringName[] field_StringNameArray = { "foo", "bar" }; [Export] private NodePath[] field_NodePathArray = { "foo", "bar" }; [Export] private Rid[] field_RidArray = { default, default, default }; @@ -93,7 +93,7 @@ namespace Godot.SourceGenerators.Sample [Export] private Variant field_Variant = "foo"; // Classes - [Export] private Godot.Object field_GodotObjectOrDerived; + [Export] private GodotObject field_GodotObjectOrDerived; [Export] private Godot.Texture field_GodotResourceTexture; [Export] private StringName field_StringName = new StringName("foo"); [Export] private NodePath field_NodePath = new NodePath("foo"); diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/ExportedProperties.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/ExportedProperties.cs index cd8559d16c..aef2a8824e 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/ExportedProperties.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/ExportedProperties.cs @@ -10,7 +10,7 @@ namespace Godot.SourceGenerators.Sample [SuppressMessage("ReSharper", "RedundantNameQualifier")] [SuppressMessage("ReSharper", "ArrangeObjectCreationWhenTypeEvident")] [SuppressMessage("ReSharper", "InconsistentNaming")] - public partial class ExportedProperties : Godot.Object + public partial class ExportedProperties : GodotObject { // Do not generate default value private String _notGenerate_Property_String = new string("not generate"); @@ -168,7 +168,7 @@ namespace Godot.SourceGenerators.Sample [Export] private Vector2[] property_Vector2Array { get; set; } = { Vector2.Up, Vector2.Down, Vector2.Left, Vector2.Right }; [Export] private Vector3[] property_Vector3Array { get; set; } = { Vector3.Up, Vector3.Down, Vector3.Left, Vector3.Right }; [Export] private Color[] property_ColorArray { get; set; } = { Colors.Aqua, Colors.Aquamarine, Colors.Azure, Colors.Beige }; - [Export] private Godot.Object[] property_GodotObjectOrDerivedArray { get; set; } = { null }; + [Export] private GodotObject[] property_GodotObjectOrDerivedArray { get; set; } = { null }; [Export] private StringName[] field_StringNameArray { get; set; } = { "foo", "bar" }; [Export] private NodePath[] field_NodePathArray { get; set; } = { "foo", "bar" }; [Export] private Rid[] field_RidArray { get; set; } = { default, default, default }; @@ -177,7 +177,7 @@ namespace Godot.SourceGenerators.Sample [Export] private Variant property_Variant { get; set; } = "foo"; // Classes - [Export] private Godot.Object property_GodotObjectOrDerived { get; set; } + [Export] private GodotObject property_GodotObjectOrDerived { get; set; } [Export] private Godot.Texture property_GodotResourceTexture { get; set; } [Export] private StringName property_StringName { get; set; } = new StringName("foo"); [Export] private NodePath property_NodePath { get; set; } = new NodePath("foo"); diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/Foo.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/Foo.cs index 21a5bfe560..9ef72d9e02 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/Foo.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/Foo.cs @@ -1,6 +1,6 @@ namespace Godot.SourceGenerators.Sample { - partial class Foo : Godot.Object + partial class Foo : GodotObject { } diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/Generic.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/Generic.cs index b21b035b4d..2cd1a08c0e 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/Generic.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/Generic.cs @@ -2,19 +2,19 @@ namespace Godot.SourceGenerators.Sample { - partial class Generic : Godot.Object + partial class Generic : GodotObject { private int _field; } // Generic again but different generic parameters - partial class Generic : Godot.Object + partial class Generic : GodotObject { private int _field; } // Generic again but without generic parameters - partial class Generic : Godot.Object + partial class Generic : GodotObject { private int _field; } diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/Methods.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/Methods.cs index 618ba24abc..9aa6be3972 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/Methods.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/Methods.cs @@ -3,7 +3,7 @@ using System.Diagnostics.CodeAnalysis; namespace Godot.SourceGenerators.Sample; [SuppressMessage("ReSharper", "RedundantNameQualifier")] -public partial class Methods : Godot.Object +public partial class Methods : GodotObject { private void MethodWithOverload() { diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/MoreExportedFields.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/MoreExportedFields.cs index a6c8e52667..64088215e9 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/MoreExportedFields.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators.Sample/MoreExportedFields.cs @@ -11,7 +11,7 @@ namespace Godot.SourceGenerators.Sample [SuppressMessage("ReSharper", "ArrangeObjectCreationWhenTypeEvident")] [SuppressMessage("ReSharper", "InconsistentNaming")] // We split the definition of ExportedFields to verify properties work across multiple files. - public partial class ExportedFields : Godot.Object + public partial class ExportedFields : GodotObject { // Note we use Array and not System.Array. This tests the generated namespace qualification. [Export] private Int64[] field_empty_Int64Array = Array.Empty(); diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Common.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Common.cs index 4eed2d7b7b..41bf89e6d8 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Common.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/Common.cs @@ -14,9 +14,9 @@ namespace Godot.SourceGenerators { string message = "Missing partial modifier on declaration of type '" + - $"{symbol.FullQualifiedNameOmitGlobal()}' which is a subclass of '{GodotClasses.Object}'"; + $"{symbol.FullQualifiedNameOmitGlobal()}' which is a subclass of '{GodotClasses.GodotObject}'"; - string description = $"{message}. Subclasses of '{GodotClasses.Object}' " + + string description = $"{message}. Subclasses of '{GodotClasses.GodotObject}' " + "must be declared with the partial modifier."; context.ReportDiagnostic(Diagnostic.Create( @@ -46,9 +46,9 @@ namespace Godot.SourceGenerators string message = $"Missing partial modifier on declaration of type '{fullQualifiedName}', " + - $"which contains one or more subclasses of '{GodotClasses.Object}'"; + $"which contains one or more subclasses of '{GodotClasses.GodotObject}'"; - string description = $"{message}. Subclasses of '{GodotClasses.Object}' and their " + + string description = $"{message}. Subclasses of '{GodotClasses.GodotObject}' and their " + "containing types must be declared with the partial modifier."; context.ReportDiagnostic(Diagnostic.Create( diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs index 8852b7ebad..f0c9043fd5 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/ExtensionMethods.cs @@ -85,7 +85,7 @@ namespace Godot.SourceGenerators var classTypeSymbol = sm.GetDeclaredSymbol(cds); if (classTypeSymbol?.BaseType == null - || !classTypeSymbol.BaseType.InheritsFrom("GodotSharp", GodotClasses.Object)) + || !classTypeSymbol.BaseType.InheritsFrom("GodotSharp", GodotClasses.GodotObject)) { symbol = null; return false; diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotClasses.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotClasses.cs index a0ed1bf1d9..b60148b34f 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotClasses.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotClasses.cs @@ -2,7 +2,7 @@ namespace Godot.SourceGenerators { public static class GodotClasses { - public const string Object = "Godot.Object"; + public const string GodotObject = "Godot.GodotObject"; public const string AssemblyHasScriptsAttr = "Godot.AssemblyHasScriptsAttribute"; public const string ExportAttr = "Godot.ExportAttribute"; public const string ExportCategoryAttr = "Godot.ExportCategoryAttribute"; diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotPluginsInitializerGenerator.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotPluginsInitializerGenerator.cs index 19fdd51dab..813bdf1e9f 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotPluginsInitializerGenerator.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/GodotPluginsInitializerGenerator.cs @@ -34,7 +34,7 @@ namespace GodotPlugins.Game { DllImportResolver dllImportResolver = new GodotDllImportResolver(godotDllHandle).OnResolveDllImport; - var coreApiAssembly = typeof(Godot.Object).Assembly; + var coreApiAssembly = typeof(global::Godot.GodotObject).Assembly; NativeLibrary.SetDllImportResolver(coreApiAssembly, dllImportResolver); @@ -42,7 +42,7 @@ namespace GodotPlugins.Game ManagedCallbacks.Create(outManagedCallbacks); - ScriptManagerBridge.LookupScriptsInAssembly(typeof(GodotPlugins.Game.Main).Assembly); + ScriptManagerBridge.LookupScriptsInAssembly(typeof(global::GodotPlugins.Game.Main).Assembly); return godot_bool.True; } diff --git a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/MarshalUtils.cs b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/MarshalUtils.cs index 70fb7d7b2d..0258f53062 100644 --- a/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/MarshalUtils.cs +++ b/modules/mono/editor/Godot.NET.Sdk/Godot.SourceGenerators/MarshalUtils.cs @@ -19,7 +19,7 @@ namespace Godot.SourceGenerators throw new InvalidOperationException($"Type not found: '{fullyQualifiedMetadataName}'."); } - GodotObjectType = GetTypeByMetadataNameOrThrow("Godot.Object"); + GodotObjectType = GetTypeByMetadataNameOrThrow(GodotClasses.GodotObject); } } diff --git a/modules/mono/editor/bindings_generator.cpp b/modules/mono/editor/bindings_generator.cpp index 137e222c70..390d1e5d3d 100644 --- a/modules/mono/editor/bindings_generator.cpp +++ b/modules/mono/editor/bindings_generator.cpp @@ -497,7 +497,7 @@ void BindingsGenerator::_append_xml_method(StringBuilder &p_xml_output, const Ty } else if (!p_target_itype || !p_target_itype->is_object_type) { if (OS::get_singleton()->is_stdout_verbose()) { if (p_target_itype) { - OS::get_singleton()->print("Cannot resolve method reference for non-Godot.Object type in documentation: %s\n", p_link_target.utf8().get_data()); + OS::get_singleton()->print("Cannot resolve method reference for non-GodotObject type in documentation: %s\n", p_link_target.utf8().get_data()); } else { OS::get_singleton()->print("Cannot resolve type from method reference in documentation: %s\n", p_link_target.utf8().get_data()); } @@ -537,7 +537,7 @@ void BindingsGenerator::_append_xml_member(StringBuilder &p_xml_output, const Ty } else if (!p_target_itype || !p_target_itype->is_object_type) { if (OS::get_singleton()->is_stdout_verbose()) { if (p_target_itype) { - OS::get_singleton()->print("Cannot resolve member reference for non-Godot.Object type in documentation: %s\n", p_link_target.utf8().get_data()); + OS::get_singleton()->print("Cannot resolve member reference for non-GodotObject type in documentation: %s\n", p_link_target.utf8().get_data()); } else { OS::get_singleton()->print("Cannot resolve type from member reference in documentation: %s\n", p_link_target.utf8().get_data()); } @@ -573,7 +573,7 @@ void BindingsGenerator::_append_xml_signal(StringBuilder &p_xml_output, const Ty if (!p_target_itype || !p_target_itype->is_object_type) { if (OS::get_singleton()->is_stdout_verbose()) { if (p_target_itype) { - OS::get_singleton()->print("Cannot resolve signal reference for non-Godot.Object type in documentation: %s\n", p_link_target.utf8().get_data()); + OS::get_singleton()->print("Cannot resolve signal reference for non-GodotObject type in documentation: %s\n", p_link_target.utf8().get_data()); } else { OS::get_singleton()->print("Cannot resolve type from signal reference in documentation: %s\n", p_link_target.utf8().get_data()); } @@ -630,7 +630,7 @@ void BindingsGenerator::_append_xml_constant(StringBuilder &p_xml_output, const if (OS::get_singleton()->is_stdout_verbose()) { if (p_target_itype) { - OS::get_singleton()->print("Cannot resolve constant reference for non-Godot.Object type in documentation: %s\n", p_link_target.utf8().get_data()); + OS::get_singleton()->print("Cannot resolve constant reference for non-GodotObject type in documentation: %s\n", p_link_target.utf8().get_data()); } else { OS::get_singleton()->print("Cannot resolve type from constant reference in documentation: %s\n", p_link_target.utf8().get_data()); } @@ -1317,7 +1317,7 @@ Error BindingsGenerator::_generate_cs_type(const TypeInterface &itype, const Str bool is_derived_type = itype.base_name != StringName(); if (!is_derived_type) { - // Some Godot.Object assertions + // Some GodotObject assertions CRASH_COND(itype.cname != name_cache.type_Object); CRASH_COND(!itype.is_instantiable); CRASH_COND(itype.api_type != ClassDB::API_CORE); @@ -1479,9 +1479,9 @@ Error BindingsGenerator::_generate_cs_type(const TypeInterface &itype, const Str if (itype.is_singleton) { // Add the type name and the singleton pointer as static fields - output.append(MEMBER_BEGIN "private static Godot.Object singleton;\n"); + output.append(MEMBER_BEGIN "private static GodotObject singleton;\n"); - output << MEMBER_BEGIN "public static Godot.Object " CS_PROPERTY_SINGLETON "\n" INDENT1 "{\n" + output << MEMBER_BEGIN "public static GodotObject " CS_PROPERTY_SINGLETON "\n" INDENT1 "{\n" << INDENT2 "get\n" INDENT2 "{\n" INDENT3 "if (singleton == null)\n" << INDENT4 "singleton = " C_METHOD_ENGINE_GET_SINGLETON "(typeof(" << itype.proxy_name @@ -1491,8 +1491,8 @@ Error BindingsGenerator::_generate_cs_type(const TypeInterface &itype, const Str output.append(itype.name); output.append("\";\n"); } else { - // IMPORTANT: We also generate the static fields for Godot.Object instead of declaring - // them manually in the `Object.base.cs` partial class declaration, because they're + // IMPORTANT: We also generate the static fields for GodotObject instead of declaring + // them manually in the `GodotObject.base.cs` partial class declaration, because they're // required by other static fields in this generated partial class declaration. // Static fields are initialized in order of declaration, but when they're in different // partial class declarations then it becomes harder to tell (Rider warns about this). @@ -2062,9 +2062,9 @@ Error BindingsGenerator::_generate_cs_method(const BindingsGenerator::TypeInterf << INDENT1 "private static readonly IntPtr " << method_bind_field << " = "; if (p_itype.is_singleton) { - // Singletons are static classes. They don't derive Godot.Object, + // Singletons are static classes. They don't derive GodotObject, // so we need to specify the type to call the static method. - p_output << "Object."; + p_output << "GodotObject."; } p_output << ICALL_CLASSDB_GET_METHOD "(" BINDINGS_NATIVE_NAME_FIELD ", MethodName." @@ -2843,9 +2843,9 @@ bool BindingsGenerator::_populate_object_type_interfaces() { itype.cs_type = itype.proxy_name; if (itype.is_singleton) { - itype.cs_in_expr = "Object." CS_STATIC_METHOD_GETINSTANCE "(" CS_PROPERTY_SINGLETON ")"; + itype.cs_in_expr = "GodotObject." CS_STATIC_METHOD_GETINSTANCE "(" CS_PROPERTY_SINGLETON ")"; } else { - itype.cs_in_expr = "Object." CS_STATIC_METHOD_GETINSTANCE "(%0)"; + itype.cs_in_expr = "GodotObject." CS_STATIC_METHOD_GETINSTANCE "(%0)"; } itype.cs_out = "%5return (%2)%0(%1);"; @@ -2853,7 +2853,7 @@ bool BindingsGenerator::_populate_object_type_interfaces() { itype.c_arg_in = "(void*)%s"; itype.c_type = "IntPtr"; itype.c_type_in = itype.c_type; - itype.c_type_out = "Object"; + itype.c_type_out = "GodotObject"; // Populate properties diff --git a/modules/mono/editor/script_templates/EditorScenePostImport/basic_import_script.cs b/modules/mono/editor/script_templates/EditorScenePostImport/basic_import_script.cs index 9e1b7ef580..fa2ff30a09 100644 --- a/modules/mono/editor/script_templates/EditorScenePostImport/basic_import_script.cs +++ b/modules/mono/editor/script_templates/EditorScenePostImport/basic_import_script.cs @@ -7,7 +7,7 @@ using System; [Tool] public partial class _CLASS_ : _BASE_ { - public override Godot.Object _PostImport(Node scene) + public override GodotObject _PostImport(Node scene) { // Modify the contents of the scene upon import. return scene; // Return the modified root node when you're done. diff --git a/modules/mono/editor/script_templates/EditorScenePostImport/no_comments.cs b/modules/mono/editor/script_templates/EditorScenePostImport/no_comments.cs index bf2c9434e4..4365ba5a04 100644 --- a/modules/mono/editor/script_templates/EditorScenePostImport/no_comments.cs +++ b/modules/mono/editor/script_templates/EditorScenePostImport/no_comments.cs @@ -7,7 +7,7 @@ using System; [Tool] public partial class _CLASS_ : _BASE_ { - public override Godot.Object _PostImport(Node scene) + public override GodotObject _PostImport(Node scene) { return scene; } diff --git a/modules/mono/glue/GodotSharp/GodotPlugins/Main.cs b/modules/mono/glue/GodotSharp/GodotPlugins/Main.cs index 4ce02d221e..2a72b7c53e 100644 --- a/modules/mono/glue/GodotSharp/GodotPlugins/Main.cs +++ b/modules/mono/glue/GodotSharp/GodotPlugins/Main.cs @@ -65,7 +65,7 @@ namespace GodotPlugins } private static readonly List SharedAssemblies = new(); - private static readonly Assembly CoreApiAssembly = typeof(Godot.Object).Assembly; + private static readonly Assembly CoreApiAssembly = typeof(global::Godot.GodotObject).Assembly; private static Assembly? _editorApiAssembly; private static PluginLoadContextWrapper? _projectLoadContext; private static bool _editorHint = false; diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs index 469238db4d..f4646d1d3c 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Array.cs @@ -120,7 +120,7 @@ namespace Godot.Collections // fine as long as the array is not mutated. However, Span does this type checking at // instantiation, so it's not possible to use it even when not mutating anything. // ReSharper disable once RedundantNameQualifier - public Array(ReadOnlySpan array) : this() + public Array(ReadOnlySpan array) : this() { if (array == null) throw new ArgumentNullException(nameof(array)); diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/CSharpInstanceBridge.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/CSharpInstanceBridge.cs index ccb21502c0..16be494d99 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/CSharpInstanceBridge.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/CSharpInstanceBridge.cs @@ -12,7 +12,7 @@ namespace Godot.Bridge { try { - var godotObject = (Object)GCHandle.FromIntPtr(godotObjectGCHandle).Target; + var godotObject = (GodotObject)GCHandle.FromIntPtr(godotObjectGCHandle).Target; if (godotObject == null) { @@ -49,7 +49,7 @@ namespace Godot.Bridge { try { - var godotObject = (Object)GCHandle.FromIntPtr(godotObjectGCHandle).Target; + var godotObject = (GodotObject)GCHandle.FromIntPtr(godotObjectGCHandle).Target; if (godotObject == null) throw new InvalidOperationException(); @@ -79,7 +79,7 @@ namespace Godot.Bridge { try { - var godotObject = (Object)GCHandle.FromIntPtr(godotObjectGCHandle).Target; + var godotObject = (GodotObject)GCHandle.FromIntPtr(godotObjectGCHandle).Target; if (godotObject == null) throw new InvalidOperationException(); @@ -134,7 +134,7 @@ namespace Godot.Bridge { try { - var godotObject = (Object)GCHandle.FromIntPtr(godotObjectGCHandle).Target; + var godotObject = (GodotObject)GCHandle.FromIntPtr(godotObjectGCHandle).Target; if (okIfNull.ToBool()) godotObject?.Dispose(); @@ -152,7 +152,7 @@ namespace Godot.Bridge { try { - var self = (Object)GCHandle.FromIntPtr(godotObjectGCHandle).Target; + var self = (GodotObject)GCHandle.FromIntPtr(godotObjectGCHandle).Target; if (self == null) { @@ -186,7 +186,7 @@ namespace Godot.Bridge { try { - var godotObject = (Object)GCHandle.FromIntPtr(godotObjectGCHandle).Target; + var godotObject = (GodotObject)GCHandle.FromIntPtr(godotObjectGCHandle).Target; if (godotObject == null) return godot_bool.False; @@ -209,7 +209,7 @@ namespace Godot.Bridge { try { - var godotObject = (Object)GCHandle.FromIntPtr(godotObjectGCHandle).Target; + var godotObject = (GodotObject)GCHandle.FromIntPtr(godotObjectGCHandle).Target; if (godotObject == null) return; @@ -242,7 +242,7 @@ namespace Godot.Bridge { try { - var godotObject = (Object)GCHandle.FromIntPtr(godotObjectGCHandle).Target; + var godotObject = (GodotObject)GCHandle.FromIntPtr(godotObjectGCHandle).Target; if (godotObject == null) return; diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs index feb08bee0d..859b47c276 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.cs @@ -100,7 +100,7 @@ namespace Godot.Bridge Type nativeType = TypeGetProxyClass(nativeTypeNameStr) ?? throw new InvalidOperationException( "Wrapper class not found for type: " + nativeTypeNameStr); - var obj = (Object)FormatterServices.GetUninitializedObject(nativeType); + var obj = (GodotObject)FormatterServices.GetUninitializedObject(nativeType); var ctor = nativeType.GetConstructor( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, @@ -150,7 +150,7 @@ namespace Godot.Bridge } } - var obj = (Object)FormatterServices.GetUninitializedObject(scriptType); + var obj = (GodotObject)FormatterServices.GetUninitializedObject(scriptType); var parameters = ctor.GetParameters(); int paramCount = parameters.Length; @@ -189,7 +189,7 @@ namespace Godot.Bridge return; } - var native = Object.InternalGetClassNativeBase(scriptType); + var native = GodotObject.InternalGetClassNativeBase(scriptType); var field = native?.GetField("NativeName", BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); @@ -222,7 +222,7 @@ namespace Godot.Bridge { try { - var target = (Object?)GCHandle.FromIntPtr(gcHandlePtr).Target; + var target = (GodotObject?)GCHandle.FromIntPtr(gcHandlePtr).Target; if (target != null) target.NativePtr = newPtr; } @@ -239,7 +239,7 @@ namespace Godot.Bridge if (nativeTypeNameStr[0] == '_') nativeTypeNameStr = nativeTypeNameStr.Substring(1); - Type? wrapperType = typeof(Object).Assembly.GetType("Godot." + nativeTypeNameStr); + Type? wrapperType = typeof(GodotObject).Assembly.GetType("Godot." + nativeTypeNameStr); if (wrapperType == null) { @@ -252,8 +252,8 @@ namespace Godot.Bridge if (wrapperType != null && IsStatic(wrapperType)) { - // A static class means this is a Godot singleton class. If an instance is needed we use Godot.Object. - return typeof(Object); + // A static class means this is a Godot singleton class. If an instance is needed we use GodotObject. + return typeof(GodotObject); } return wrapperType; @@ -293,7 +293,7 @@ namespace Godot.Bridge // such as when disabling C# source generators (for whatever reason) or when using a // language other than C# that has nothing similar to source generators to automate it. - var typeOfGodotObject = typeof(Object); + var typeOfGodotObject = typeof(GodotObject); foreach (var type in assembly.GetTypes()) { @@ -331,7 +331,7 @@ namespace Godot.Bridge { try { - var owner = (Object?)GCHandle.FromIntPtr(ownerGCHandlePtr).Target; + var owner = (GodotObject?)GCHandle.FromIntPtr(ownerGCHandlePtr).Target; if (owner == null) { @@ -539,9 +539,9 @@ namespace Godot.Bridge } // ReSharper disable once RedundantNameQualifier - if (!typeof(Godot.Object).IsAssignableFrom(scriptType)) + if (!typeof(GodotObject).IsAssignableFrom(scriptType)) { - // The class no longer inherits Godot.Object, can't reload + // The class no longer inherits GodotObject, can't reload return godot_bool.False; } @@ -589,7 +589,7 @@ namespace Godot.Bridge using var methods = new Collections.Array(); Type? top = scriptType; - Type native = Object.InternalGetClassNativeBase(top); + Type native = GodotObject.InternalGetClassNativeBase(top); while (top != null && top != native) { @@ -899,7 +899,7 @@ namespace Godot.Bridge try { Type? top = _scriptTypeBiMap.GetScriptType(scriptPtr); - Type native = Object.InternalGetClassNativeBase(top); + Type native = GodotObject.InternalGetClassNativeBase(top); while (top != null && top != native) { diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Callable.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Callable.cs index 23b0aa9204..219a9a8c15 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Callable.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Callable.cs @@ -28,7 +28,7 @@ namespace Godot /// public readonly partial struct Callable { - private readonly Object _target; + private readonly GodotObject _target; private readonly StringName _method; private readonly Delegate _delegate; private readonly unsafe delegate* managed _trampoline; @@ -36,7 +36,7 @@ namespace Godot /// /// Object that contains the method. /// - public Object Target => _target; + public GodotObject Target => _target; /// /// Name of the method that will be called. @@ -60,7 +60,7 @@ namespace Godot /// /// Object that contains the method. /// Name of the method that will be called. - public unsafe Callable(Object target, StringName method) + public unsafe Callable(GodotObject target, StringName method) { _target = target; _method = method; @@ -71,7 +71,7 @@ namespace Godot private unsafe Callable(Delegate @delegate, delegate* managed trampoline) { - _target = @delegate?.Target as Object; + _target = @delegate?.Target as GodotObject; _method = null; _delegate = @delegate; _trampoline = trampoline; diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/DelegateUtils.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/DelegateUtils.cs index 43bb5c5598..4a54f67cc9 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/DelegateUtils.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/DelegateUtils.cs @@ -126,7 +126,7 @@ namespace Godot } } // ReSharper disable once RedundantNameQualifier - case Godot.Object godotObject: + case GodotObject godotObject: { using (var stream = new MemoryStream()) using (var writer = new BinaryWriter(stream)) @@ -399,7 +399,7 @@ namespace Godot { ulong objectId = reader.ReadUInt64(); // ReSharper disable once RedundantNameQualifier - Godot.Object godotObject = Godot.Object.InstanceFromId(objectId); + GodotObject godotObject = GodotObject.InstanceFromId(objectId); if (godotObject == null) return false; @@ -641,7 +641,7 @@ namespace Godot return VariantUtils.CreateFrom(nodePathArray); case Rid[] ridArray: return VariantUtils.CreateFrom(ridArray); - case Godot.Object[] godotObjectArray: + case GodotObject[] godotObjectArray: return VariantUtils.CreateFrom(godotObjectArray); case StringName stringName: return VariantUtils.CreateFrom(stringName); @@ -655,7 +655,7 @@ namespace Godot return VariantUtils.CreateFrom(godotArray); case Variant variant: return VariantUtils.CreateFrom(variant); - case Godot.Object godotObject: + case GodotObject godotObject: return VariantUtils.CreateFrom(godotObject); case Enum @enum: return VariantUtils.CreateFrom(Convert.ToInt64(@enum)); @@ -736,18 +736,18 @@ namespace Godot if (ToSystemObjectFuncByType.TryGetValue(type, out var func)) return func(variant); - if (typeof(Godot.Object).IsAssignableFrom(type)) - return Convert.ChangeType(VariantUtils.ConvertTo(variant), type); + if (typeof(GodotObject).IsAssignableFrom(type)) + return Convert.ChangeType(VariantUtils.ConvertTo(variant), type); - if (typeof(Godot.Object[]).IsAssignableFrom(type)) + if (typeof(GodotObject[]).IsAssignableFrom(type)) { - static Godot.Object[] ConvertToSystemArrayOfGodotObject(in godot_array nativeArray, Type type) + static GodotObject[] ConvertToSystemArrayOfGodotObject(in godot_array nativeArray, Type type) { var array = Collections.Array.CreateTakingOwnershipOfDisposableValue( NativeFuncs.godotsharp_array_new_copy(nativeArray)); int length = array.Count; - var ret = (Godot.Object[])Activator.CreateInstance(type, length)!; + var ret = (GodotObject[])Activator.CreateInstance(type, length)!; for (int i = 0; i < length; i++) ret[i] = array[i].AsGodotObject(); diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs index 505763e6a2..fa304c1b94 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Dictionary.cs @@ -74,7 +74,7 @@ namespace Godot.Collections /// the original dictionary. If , a shallow copy is made and /// references to the original nested arrays and dictionaries are kept, so that /// modifying a sub-array or dictionary in the copy will also impact those - /// referenced in the source dictionary. Note that any derived + /// referenced in the source dictionary. Note that any derived /// elements will be shallow copied regardless of the /// setting. /// @@ -494,7 +494,7 @@ namespace Godot.Collections /// the original dictionary. If , a shallow copy is made and /// references to the original nested arrays and dictionaries are kept, so that /// modifying a sub-array or dictionary in the copy will also impact those - /// referenced in the source dictionary. Note that any derived + /// referenced in the source dictionary. Note that any derived /// elements will be shallow copied regardless of the /// setting. /// diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/DisposablesTracker.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/DisposablesTracker.cs index bdefb3c2c5..53292e10cf 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/DisposablesTracker.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/DisposablesTracker.cs @@ -43,9 +43,9 @@ namespace Godot // like StringName, NodePath, Godot.Collections.Array/Dictionary, etc. // The Godot Object Dispose() method may need any of the later instances. - foreach (WeakReference item in GodotObjectInstances.Keys) + foreach (WeakReference item in GodotObjectInstances.Keys) { - if (item.TryGetTarget(out Object? self)) + if (item.TryGetTarget(out GodotObject? self)) self.Dispose(); } @@ -60,15 +60,15 @@ namespace Godot } // ReSharper disable once RedundantNameQualifier - private static ConcurrentDictionary, byte> GodotObjectInstances { get; } = + private static ConcurrentDictionary, byte> GodotObjectInstances { get; } = new(); private static ConcurrentDictionary, byte> OtherInstances { get; } = new(); - public static WeakReference RegisterGodotObject(Object godotObject) + public static WeakReference RegisterGodotObject(GodotObject godotObject) { - var weakReferenceToSelf = new WeakReference(godotObject); + var weakReferenceToSelf = new WeakReference(godotObject); GodotObjectInstances.TryAdd(weakReferenceToSelf, 0); return weakReferenceToSelf; } @@ -80,7 +80,7 @@ namespace Godot return weakReferenceToSelf; } - public static void UnregisterGodotObject(Object godotObject, WeakReference weakReferenceToSelf) + public static void UnregisterGodotObject(GodotObject godotObject, WeakReference weakReferenceToSelf) { if (!GodotObjectInstances.TryRemove(weakReferenceToSelf, out _)) throw new ArgumentException("Godot Object not registered.", nameof(weakReferenceToSelf)); diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Extensions/GodotObjectExtensions.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Extensions/GodotObjectExtensions.cs new file mode 100644 index 0000000000..6c90c17078 --- /dev/null +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Extensions/GodotObjectExtensions.cs @@ -0,0 +1,86 @@ +using System; +using Godot.NativeInterop; + +namespace Godot +{ + public partial class GodotObject + { + /// + /// Returns the that corresponds to . + /// All Objects have a unique instance ID. See also . + /// + /// + /// + /// public partial class MyNode : Node + /// { + /// public string Foo { get; set; } = "bar"; + /// + /// public override void _Ready() + /// { + /// ulong id = GetInstanceId(); + /// var inst = (MyNode)InstanceFromId(Id); + /// GD.Print(inst.Foo); // Prints bar + /// } + /// } + /// + /// + /// Instance ID of the Object to retrieve. + /// The instance. + public static GodotObject InstanceFromId(ulong instanceId) + { + return InteropUtils.UnmanagedGetManaged(NativeFuncs.godotsharp_instance_from_id(instanceId)); + } + + /// + /// Returns if the that corresponds + /// to is a valid object (e.g. has not been deleted from + /// memory). All Objects have a unique instance ID. + /// + /// The Object ID to check. + /// If the instance with the given ID is a valid object. + public static bool IsInstanceIdValid(ulong id) + { + return IsInstanceValid(InstanceFromId(id)); + } + + /// + /// Returns if is a + /// valid (e.g. has not been deleted from memory). + /// + /// The instance to check. + /// If the instance is a valid object. + public static bool IsInstanceValid(GodotObject instance) + { + return instance != null && instance.NativeInstance != IntPtr.Zero; + } + + /// + /// Returns a weak reference to an object, or + /// if the argument is invalid. + /// A weak reference to an object is not enough to keep the object alive: + /// when the only remaining references to a referent are weak references, + /// garbage collection is free to destroy the referent and reuse its memory + /// for something else. However, until the object is actually destroyed the + /// weak reference may return the object even if there are no strong references + /// to it. + /// + /// The object. + /// + /// The reference to the object or . + /// + public static WeakRef WeakRef(GodotObject obj) + { + if (!IsInstanceValid(obj)) + return null; + + NativeFuncs.godotsharp_weakref(GetPtr(obj), out godot_ref weakRef); + using (weakRef) + { + if (weakRef.IsNull) + return null; + + return (WeakRef)InteropUtils.UnmanagedGetManaged(weakRef.Reference); + } + } + } +} diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Extensions/ObjectExtensions.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Extensions/ObjectExtensions.cs deleted file mode 100644 index a9f3f02856..0000000000 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Extensions/ObjectExtensions.cs +++ /dev/null @@ -1,86 +0,0 @@ -using System; -using Godot.NativeInterop; - -namespace Godot -{ - public partial class Object - { - /// - /// Returns the that corresponds to . - /// All Objects have a unique instance ID. See also . - /// - /// - /// - /// public partial class MyNode : Node - /// { - /// public string Foo { get; set; } = "bar"; - /// - /// public override void _Ready() - /// { - /// ulong id = GetInstanceId(); - /// var inst = (MyNode)InstanceFromId(Id); - /// GD.Print(inst.Foo); // Prints bar - /// } - /// } - /// - /// - /// Instance ID of the Object to retrieve. - /// The instance. - public static Object InstanceFromId(ulong instanceId) - { - return InteropUtils.UnmanagedGetManaged(NativeFuncs.godotsharp_instance_from_id(instanceId)); - } - - /// - /// Returns if the that corresponds - /// to is a valid object (e.g. has not been deleted from - /// memory). All Objects have a unique instance ID. - /// - /// The Object ID to check. - /// If the instance with the given ID is a valid object. - public static bool IsInstanceIdValid(ulong id) - { - return IsInstanceValid(InstanceFromId(id)); - } - - /// - /// Returns if is a - /// valid (e.g. has not been deleted from memory). - /// - /// The instance to check. - /// If the instance is a valid object. - public static bool IsInstanceValid(Object instance) - { - return instance != null && instance.NativeInstance != IntPtr.Zero; - } - - /// - /// Returns a weak reference to an object, or - /// if the argument is invalid. - /// A weak reference to an object is not enough to keep the object alive: - /// when the only remaining references to a referent are weak references, - /// garbage collection is free to destroy the referent and reuse its memory - /// for something else. However, until the object is actually destroyed the - /// weak reference may return the object even if there are no strong references - /// to it. - /// - /// The object. - /// - /// The reference to the object or . - /// - public static WeakRef WeakRef(Object obj) - { - if (!IsInstanceValid(obj)) - return null; - - NativeFuncs.godotsharp_weakref(GetPtr(obj), out godot_ref weakRef); - using (weakRef) - { - if (weakRef.IsNull) - return null; - - return (WeakRef)InteropUtils.UnmanagedGetManaged(weakRef.Reference); - } - } - } -} diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotObject.base.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotObject.base.cs new file mode 100644 index 0000000000..b9a5ac82d1 --- /dev/null +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotObject.base.cs @@ -0,0 +1,242 @@ +using System; +using System.Runtime.InteropServices; +using Godot.Bridge; +using Godot.NativeInterop; + +namespace Godot +{ + public partial class GodotObject : IDisposable + { + private bool _disposed = false; + private static readonly Type CachedType = typeof(GodotObject); + + internal IntPtr NativePtr; + private bool _memoryOwn; + + private WeakReference _weakReferenceToSelf; + + /// + /// Constructs a new . + /// + public GodotObject() : this(false) + { + unsafe + { + _ConstructAndInitialize(NativeCtor, NativeName, CachedType, refCounted: false); + } + } + + internal unsafe void _ConstructAndInitialize( + delegate* unmanaged nativeCtor, + StringName nativeName, + Type cachedType, + bool refCounted + ) + { + if (NativePtr == IntPtr.Zero) + { + NativePtr = nativeCtor(); + + InteropUtils.TieManagedToUnmanaged(this, NativePtr, + nativeName, refCounted, GetType(), cachedType); + } + else + { + InteropUtils.TieManagedToUnmanagedWithPreSetup(this, NativePtr, + GetType(), cachedType); + } + + _weakReferenceToSelf = DisposablesTracker.RegisterGodotObject(this); + } + + internal GodotObject(bool memoryOwn) + { + _memoryOwn = memoryOwn; + } + + /// + /// The pointer to the native instance of this . + /// + public IntPtr NativeInstance => NativePtr; + + internal static IntPtr GetPtr(GodotObject instance) + { + if (instance == null) + return IntPtr.Zero; + + // We check if NativePtr is null because this may be called by the debugger. + // If the debugger puts a breakpoint in one of the base constructors, before + // 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); + + return instance.NativePtr; + } + + ~GodotObject() + { + Dispose(false); + } + + /// + /// Disposes of this . + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Disposes implementation of this . + /// + protected virtual void Dispose(bool disposing) + { + if (_disposed) + return; + + _disposed = true; + + if (NativePtr != IntPtr.Zero) + { + IntPtr gcHandleToFree = NativeFuncs.godotsharp_internal_object_get_associated_gchandle(NativePtr); + + if (gcHandleToFree != IntPtr.Zero) + { + object target = GCHandle.FromIntPtr(gcHandleToFree).Target; + // The GC handle may have been replaced in another thread. Release it only if + // it's associated to this managed instance, or if the target is no longer alive. + if (target != this && target != null) + gcHandleToFree = IntPtr.Zero; + } + + if (_memoryOwn) + { + NativeFuncs.godotsharp_internal_refcounted_disposed(NativePtr, gcHandleToFree, + (!disposing).ToGodotBool()); + } + else + { + NativeFuncs.godotsharp_internal_object_disposed(NativePtr, gcHandleToFree); + } + + NativePtr = IntPtr.Zero; + } + + DisposablesTracker.UnregisterGodotObject(this, _weakReferenceToSelf); + } + + /// + /// Converts this to a string. + /// + /// A string representation of this object. + public override string ToString() + { + NativeFuncs.godotsharp_object_to_string(GetPtr(this), out godot_string str); + using (str) + return Marshaling.ConvertStringToManaged(str); + } + + /// + /// Returns a new awaiter configured to complete when the instance + /// emits the signal specified by the parameter. + /// + /// + /// The instance the awaiter will be listening to. + /// + /// + /// The signal the awaiter will be waiting for. + /// + /// + /// This sample prints a message once every frame up to 100 times. + /// + /// public override void _Ready() + /// { + /// for (int i = 0; i < 100; i++) + /// { + /// await ToSignal(GetTree(), "process_frame"); + /// GD.Print($"Frame {i}"); + /// } + /// } + /// + /// + /// + /// A that completes when + /// emits the . + /// + public SignalAwaiter ToSignal(GodotObject source, StringName signal) + { + return new SignalAwaiter(source, signal, this); + } + + internal static Type InternalGetClassNativeBase(Type t) + { + do + { + var assemblyName = t.Assembly.GetName(); + + if (assemblyName.Name == "GodotSharp") + return t; + + if (assemblyName.Name == "GodotSharpEditor") + return t; + } while ((t = t.BaseType) != null); + + return null; + } + + // ReSharper disable once VirtualMemberNeverOverridden.Global + protected internal virtual bool SetGodotClassPropertyValue(in godot_string_name name, in godot_variant value) + { + return false; + } + + // ReSharper disable once VirtualMemberNeverOverridden.Global + protected internal virtual bool GetGodotClassPropertyValue(in godot_string_name name, out godot_variant value) + { + value = default; + return false; + } + + // ReSharper disable once VirtualMemberNeverOverridden.Global + protected internal virtual void RaiseGodotClassSignalCallbacks(in godot_string_name signal, + NativeVariantPtrArgs args) + { + } + + internal static IntPtr ClassDB_get_method(StringName type, StringName method) + { + var typeSelf = (godot_string_name)type.NativeValue; + var methodSelf = (godot_string_name)method.NativeValue; + IntPtr methodBind = NativeFuncs.godotsharp_method_bind_get_method(typeSelf, methodSelf); + + if (methodBind == IntPtr.Zero) + throw new NativeMethodBindNotFoundException(type + "." + method); + + return methodBind; + } + + internal static unsafe delegate* unmanaged ClassDB_get_constructor(StringName type) + { + // for some reason the '??' operator doesn't support 'delegate*' + var typeSelf = (godot_string_name)type.NativeValue; + var nativeConstructor = NativeFuncs.godotsharp_get_class_constructor(typeSelf); + + if (nativeConstructor == null) + throw new NativeConstructorNotFoundException(type); + + return nativeConstructor; + } + + protected internal virtual void SaveGodotObjectData(GodotSerializationInfo info) + { + } + + // TODO: Should this be a constructor overload? + protected internal virtual void RestoreGodotObjectData(GodotSerializationInfo info) + { + } + } +} diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotObject.exceptions.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotObject.exceptions.cs new file mode 100644 index 0000000000..a7640043ce --- /dev/null +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/GodotObject.exceptions.cs @@ -0,0 +1,142 @@ +using System; +using System.Text; + +#nullable enable + +namespace Godot +{ + public partial class GodotObject + { + public class NativeMemberNotFoundException : Exception + { + public NativeMemberNotFoundException() + { + } + + public NativeMemberNotFoundException(string? message) : base(message) + { + } + + public NativeMemberNotFoundException(string? message, Exception? innerException) + : base(message, innerException) + { + } + } + + public class NativeConstructorNotFoundException : NativeMemberNotFoundException + { + private readonly string? _nativeClassName; + + // ReSharper disable once InconsistentNaming + private const string Arg_NativeConstructorNotFoundException = "Unable to find the native constructor."; + + public NativeConstructorNotFoundException() + : base(Arg_NativeConstructorNotFoundException) + { + } + + public NativeConstructorNotFoundException(string? nativeClassName) + : this(Arg_NativeConstructorNotFoundException, nativeClassName) + { + } + + public NativeConstructorNotFoundException(string? message, Exception? innerException) + : base(message, innerException) + { + } + + public NativeConstructorNotFoundException(string? message, string? nativeClassName) + : base(message) + { + _nativeClassName = nativeClassName; + } + + public NativeConstructorNotFoundException(string? message, string? nativeClassName, Exception? innerException) + : base(message, innerException) + { + _nativeClassName = nativeClassName; + } + + public override string Message + { + get + { + StringBuilder sb; + if (string.IsNullOrEmpty(base.Message)) + { + sb = new(Arg_NativeConstructorNotFoundException); + } + else + { + sb = new(base.Message); + } + + if (!string.IsNullOrEmpty(_nativeClassName)) + { + sb.Append($" (Method '{_nativeClassName}')"); + } + + return sb.ToString(); + } + } + } + + public class NativeMethodBindNotFoundException : NativeMemberNotFoundException + { + private readonly string? _nativeMethodName; + + // ReSharper disable once InconsistentNaming + private const string Arg_NativeMethodBindNotFoundException = "Unable to find the native method bind."; + + public NativeMethodBindNotFoundException() + : base(Arg_NativeMethodBindNotFoundException) + { + } + + public NativeMethodBindNotFoundException(string? nativeMethodName) + : this(Arg_NativeMethodBindNotFoundException, nativeMethodName) + { + } + + public NativeMethodBindNotFoundException(string? message, Exception? innerException) + : base(message, innerException) + { + } + + public NativeMethodBindNotFoundException(string? message, string? nativeMethodName) + : base(message) + { + _nativeMethodName = nativeMethodName; + } + + public NativeMethodBindNotFoundException(string? message, string? nativeMethodName, Exception? innerException) + : base(message, innerException) + { + _nativeMethodName = nativeMethodName; + } + + public override string Message + { + get + { + StringBuilder sb; + if (string.IsNullOrEmpty(base.Message)) + { + sb = new(Arg_NativeMethodBindNotFoundException); + } + else + { + sb = new(base.Message); + } + + if (!string.IsNullOrEmpty(_nativeMethodName)) + { + sb.Append($" (Method '{_nativeMethodName}')"); + } + + return sb.ToString(); + } + } + } + } +} diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/InteropUtils.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/InteropUtils.cs index 82f1c04d40..cc308bfdb3 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/InteropUtils.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/InteropUtils.cs @@ -8,7 +8,7 @@ namespace Godot.NativeInterop { internal static class InteropUtils { - public static Object UnmanagedGetManaged(IntPtr unmanaged) + public static GodotObject UnmanagedGetManaged(IntPtr unmanaged) { // The native pointer may be null if (unmanaged == IntPtr.Zero) @@ -23,7 +23,7 @@ namespace Godot.NativeInterop unmanaged, out hasCsScriptInstance); if (gcHandlePtr != IntPtr.Zero) - return (Object)GCHandle.FromIntPtr(gcHandlePtr).Target; + return (GodotObject)GCHandle.FromIntPtr(gcHandlePtr).Target; // Otherwise, if the object has a CSharpInstance script instance, return null @@ -37,17 +37,17 @@ namespace Godot.NativeInterop object target = gcHandlePtr != IntPtr.Zero ? GCHandle.FromIntPtr(gcHandlePtr).Target : null; if (target != null) - return (Object)target; + return (GodotObject)target; // If the native instance binding GC handle target was collected, create a new one gcHandlePtr = NativeFuncs.godotsharp_internal_unmanaged_instance_binding_create_managed( unmanaged, gcHandlePtr); - return gcHandlePtr != IntPtr.Zero ? (Object)GCHandle.FromIntPtr(gcHandlePtr).Target : null; + return gcHandlePtr != IntPtr.Zero ? (GodotObject)GCHandle.FromIntPtr(gcHandlePtr).Target : null; } - public static void TieManagedToUnmanaged(Object managed, IntPtr unmanaged, + public static void TieManagedToUnmanaged(GodotObject managed, IntPtr unmanaged, StringName nativeName, bool refCounted, Type type, Type nativeType) { var gcHandle = refCounted ? @@ -76,7 +76,7 @@ namespace Godot.NativeInterop } } - public static void TieManagedToUnmanagedWithPreSetup(Object managed, IntPtr unmanaged, + public static void TieManagedToUnmanagedWithPreSetup(GodotObject managed, IntPtr unmanaged, Type type, Type nativeType) { if (type == nativeType) @@ -87,7 +87,7 @@ namespace Godot.NativeInterop GCHandle.ToIntPtr(strongGCHandle), unmanaged); } - public static Object EngineGetSingleton(string name) + public static GodotObject EngineGetSingleton(string name) { using godot_string src = Marshaling.ConvertStringToNative(name); return UnmanagedGetManaged(NativeFuncs.godotsharp_engine_get_singleton(src)); diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/Marshaling.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/Marshaling.cs index f79ad2c0ed..93a83b701b 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/Marshaling.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/Marshaling.cs @@ -143,12 +143,12 @@ namespace Godot.NativeInterop if (type == typeof(Rid[])) return Variant.Type.Array; - if (typeof(Godot.Object[]).IsAssignableFrom(type)) + if (typeof(GodotObject[]).IsAssignableFrom(type)) return Variant.Type.Array; } else if (type.IsGenericType) { - if (typeof(Godot.Object).IsAssignableFrom(type)) + if (typeof(GodotObject).IsAssignableFrom(type)) return Variant.Type.Object; // We use `IsAssignableFrom` with our helper interfaces to detect generic Godot collections @@ -167,7 +167,7 @@ namespace Godot.NativeInterop } else { - if (typeof(Godot.Object).IsAssignableFrom(type)) + if (typeof(GodotObject).IsAssignableFrom(type)) return Variant.Type.Object; if (typeof(StringName) == type) @@ -232,7 +232,7 @@ namespace Godot.NativeInterop var gcHandle = CustomGCHandle.AllocStrong(p_managed_callable.Delegate); IntPtr objectPtr = p_managed_callable.Target != null ? - Object.GetPtr(p_managed_callable.Target) : + GodotObject.GetPtr(p_managed_callable.Target) : IntPtr.Zero; unsafe @@ -310,7 +310,7 @@ namespace Godot.NativeInterop public static Signal ConvertSignalToManaged(in godot_signal p_signal) { - var owner = Godot.Object.InstanceFromId(p_signal.ObjectId); + var owner = GodotObject.InstanceFromId(p_signal.ObjectId); var name = StringName.CreateTakingOwnershipOfDisposableValue( NativeFuncs.godotsharp_string_name_new_copy(p_signal.Name)); return new Signal(owner, name); @@ -319,7 +319,7 @@ namespace Godot.NativeInterop // Array internal static T[] ConvertNativeGodotArrayToSystemArrayOfGodotObjectType(in godot_array p_array) - where T : Godot.Object + where T : GodotObject { var array = Collections.Array.CreateTakingOwnershipOfDisposableValue( NativeFuncs.godotsharp_array_new_copy(p_array)); diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.cs index ff083929d4..e6bcd9393d 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.cs @@ -241,7 +241,7 @@ namespace Godot.NativeInterop => CreateFromArray(new Collections.Array(from)); // ReSharper disable once RedundantNameQualifier - public static godot_variant CreateFromSystemArrayOfGodotObject(Godot.Object[]? from) + public static godot_variant CreateFromSystemArrayOfGodotObject(GodotObject[]? from) { if (from == null) return default; // Nil @@ -307,8 +307,8 @@ namespace Godot.NativeInterop [MethodImpl(MethodImplOptions.AggressiveInlining)] // ReSharper disable once RedundantNameQualifier - public static godot_variant CreateFromGodotObject(Godot.Object? from) - => from != null ? CreateFromGodotObjectPtr(Object.GetPtr(from)) : default; + public static godot_variant CreateFromGodotObject(GodotObject? from) + => from != null ? CreateFromGodotObjectPtr(GodotObject.GetPtr(from)) : default; // We avoid the internal call if the stored type is the same we want. @@ -460,7 +460,7 @@ namespace Godot.NativeInterop [MethodImpl(MethodImplOptions.AggressiveInlining)] // ReSharper disable once RedundantNameQualifier - public static Godot.Object ConvertToGodotObject(in godot_variant p_var) + public static GodotObject ConvertToGodotObject(in godot_variant p_var) => InteropUtils.UnmanagedGetManaged(ConvertToGodotObjectPtr(p_var)); public static string ConvertToString(in godot_variant p_var) @@ -616,7 +616,7 @@ namespace Godot.NativeInterop public static T[] ConvertToSystemArrayOfGodotObject(in godot_variant p_var) // ReSharper disable once RedundantNameQualifier - where T : Godot.Object + where T : GodotObject { using var godotArray = NativeFuncs.godotsharp_variant_as_array(p_var); return Marshaling.ConvertNativeGodotArrayToSystemArrayOfGodotObjectType(godotArray); 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 bfa5bc7f41..12b0a47079 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.generic.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/VariantUtils.generic.cs @@ -192,8 +192,8 @@ public partial class VariantUtils // `typeof(X).IsAssignableFrom(typeof(T))` is optimized away - if (typeof(Godot.Object).IsAssignableFrom(typeof(T))) - return CreateFromGodotObject(UnsafeAs(from)); + if (typeof(GodotObject).IsAssignableFrom(typeof(T))) + return CreateFromGodotObject(UnsafeAs(from)); // `typeof(T).IsValueType` is optimized away // `typeof(T).IsEnum` is NOT optimized away: https://github.com/dotnet/runtime/issues/67113 @@ -381,7 +381,7 @@ public partial class VariantUtils // `typeof(X).IsAssignableFrom(typeof(T))` is optimized away - if (typeof(Godot.Object).IsAssignableFrom(typeof(T))) + if (typeof(GodotObject).IsAssignableFrom(typeof(T))) return (T)(object)ConvertToGodotObject(variant); // `typeof(T).IsValueType` is optimized away diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Object.base.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Object.base.cs deleted file mode 100644 index 60ee6eb6f4..0000000000 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Object.base.cs +++ /dev/null @@ -1,242 +0,0 @@ -using System; -using System.Runtime.InteropServices; -using Godot.Bridge; -using Godot.NativeInterop; - -namespace Godot -{ - public partial class Object : IDisposable - { - private bool _disposed = false; - private static readonly Type CachedType = typeof(Object); - - internal IntPtr NativePtr; - private bool _memoryOwn; - - private WeakReference _weakReferenceToSelf; - - /// - /// Constructs a new . - /// - public Object() : this(false) - { - unsafe - { - _ConstructAndInitialize(NativeCtor, NativeName, CachedType, refCounted: false); - } - } - - internal unsafe void _ConstructAndInitialize( - delegate* unmanaged nativeCtor, - StringName nativeName, - Type cachedType, - bool refCounted - ) - { - if (NativePtr == IntPtr.Zero) - { - NativePtr = nativeCtor(); - - InteropUtils.TieManagedToUnmanaged(this, NativePtr, - nativeName, refCounted, GetType(), cachedType); - } - else - { - InteropUtils.TieManagedToUnmanagedWithPreSetup(this, NativePtr, - GetType(), cachedType); - } - - _weakReferenceToSelf = DisposablesTracker.RegisterGodotObject(this); - } - - internal Object(bool memoryOwn) - { - _memoryOwn = memoryOwn; - } - - /// - /// The pointer to the native instance of this . - /// - public IntPtr NativeInstance => NativePtr; - - internal static IntPtr GetPtr(Object instance) - { - if (instance == null) - return IntPtr.Zero; - - // We check if NativePtr is null because this may be called by the debugger. - // If the debugger puts a breakpoint in one of the base constructors, before - // 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); - - return instance.NativePtr; - } - - ~Object() - { - Dispose(false); - } - - /// - /// Disposes of this . - /// - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } - - /// - /// Disposes implementation of this . - /// - protected virtual void Dispose(bool disposing) - { - if (_disposed) - return; - - _disposed = true; - - if (NativePtr != IntPtr.Zero) - { - IntPtr gcHandleToFree = NativeFuncs.godotsharp_internal_object_get_associated_gchandle(NativePtr); - - if (gcHandleToFree != IntPtr.Zero) - { - object target = GCHandle.FromIntPtr(gcHandleToFree).Target; - // The GC handle may have been replaced in another thread. Release it only if - // it's associated to this managed instance, or if the target is no longer alive. - if (target != this && target != null) - gcHandleToFree = IntPtr.Zero; - } - - if (_memoryOwn) - { - NativeFuncs.godotsharp_internal_refcounted_disposed(NativePtr, gcHandleToFree, - (!disposing).ToGodotBool()); - } - else - { - NativeFuncs.godotsharp_internal_object_disposed(NativePtr, gcHandleToFree); - } - - NativePtr = IntPtr.Zero; - } - - DisposablesTracker.UnregisterGodotObject(this, _weakReferenceToSelf); - } - - /// - /// Converts this to a string. - /// - /// A string representation of this object. - public override string ToString() - { - NativeFuncs.godotsharp_object_to_string(GetPtr(this), out godot_string str); - using (str) - return Marshaling.ConvertStringToManaged(str); - } - - /// - /// Returns a new awaiter configured to complete when the instance - /// emits the signal specified by the parameter. - /// - /// - /// The instance the awaiter will be listening to. - /// - /// - /// The signal the awaiter will be waiting for. - /// - /// - /// This sample prints a message once every frame up to 100 times. - /// - /// public override void _Ready() - /// { - /// for (int i = 0; i < 100; i++) - /// { - /// await ToSignal(GetTree(), "process_frame"); - /// GD.Print($"Frame {i}"); - /// } - /// } - /// - /// - /// - /// A that completes when - /// emits the . - /// - public SignalAwaiter ToSignal(Object source, StringName signal) - { - return new SignalAwaiter(source, signal, this); - } - - internal static Type InternalGetClassNativeBase(Type t) - { - do - { - var assemblyName = t.Assembly.GetName(); - - if (assemblyName.Name == "GodotSharp") - return t; - - if (assemblyName.Name == "GodotSharpEditor") - return t; - } while ((t = t.BaseType) != null); - - return null; - } - - // ReSharper disable once VirtualMemberNeverOverridden.Global - protected internal virtual bool SetGodotClassPropertyValue(in godot_string_name name, in godot_variant value) - { - return false; - } - - // ReSharper disable once VirtualMemberNeverOverridden.Global - protected internal virtual bool GetGodotClassPropertyValue(in godot_string_name name, out godot_variant value) - { - value = default; - return false; - } - - // ReSharper disable once VirtualMemberNeverOverridden.Global - protected internal virtual void RaiseGodotClassSignalCallbacks(in godot_string_name signal, - NativeVariantPtrArgs args) - { - } - - internal static IntPtr ClassDB_get_method(StringName type, StringName method) - { - var typeSelf = (godot_string_name)type.NativeValue; - var methodSelf = (godot_string_name)method.NativeValue; - IntPtr methodBind = NativeFuncs.godotsharp_method_bind_get_method(typeSelf, methodSelf); - - if (methodBind == IntPtr.Zero) - throw new NativeMethodBindNotFoundException(type + "." + method); - - return methodBind; - } - - internal static unsafe delegate* unmanaged ClassDB_get_constructor(StringName type) - { - // for some reason the '??' operator doesn't support 'delegate*' - var typeSelf = (godot_string_name)type.NativeValue; - var nativeConstructor = NativeFuncs.godotsharp_get_class_constructor(typeSelf); - - if (nativeConstructor == null) - throw new NativeConstructorNotFoundException(type); - - return nativeConstructor; - } - - protected internal virtual void SaveGodotObjectData(GodotSerializationInfo info) - { - } - - // TODO: Should this be a constructor overload? - protected internal virtual void RestoreGodotObjectData(GodotSerializationInfo info) - { - } - } -} diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Object.exceptions.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Object.exceptions.cs deleted file mode 100644 index 0fcc4ee01b..0000000000 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Object.exceptions.cs +++ /dev/null @@ -1,142 +0,0 @@ -using System; -using System.Text; - -#nullable enable - -namespace Godot -{ - public partial class Object - { - public class NativeMemberNotFoundException : Exception - { - public NativeMemberNotFoundException() - { - } - - public NativeMemberNotFoundException(string? message) : base(message) - { - } - - public NativeMemberNotFoundException(string? message, Exception? innerException) - : base(message, innerException) - { - } - } - - public class NativeConstructorNotFoundException : NativeMemberNotFoundException - { - private readonly string? _nativeClassName; - - // ReSharper disable once InconsistentNaming - private const string Arg_NativeConstructorNotFoundException = "Unable to find the native constructor."; - - public NativeConstructorNotFoundException() - : base(Arg_NativeConstructorNotFoundException) - { - } - - public NativeConstructorNotFoundException(string? nativeClassName) - : this(Arg_NativeConstructorNotFoundException, nativeClassName) - { - } - - public NativeConstructorNotFoundException(string? message, Exception? innerException) - : base(message, innerException) - { - } - - public NativeConstructorNotFoundException(string? message, string? nativeClassName) - : base(message) - { - _nativeClassName = nativeClassName; - } - - public NativeConstructorNotFoundException(string? message, string? nativeClassName, Exception? innerException) - : base(message, innerException) - { - _nativeClassName = nativeClassName; - } - - public override string Message - { - get - { - StringBuilder sb; - if (string.IsNullOrEmpty(base.Message)) - { - sb = new(Arg_NativeConstructorNotFoundException); - } - else - { - sb = new(base.Message); - } - - if (!string.IsNullOrEmpty(_nativeClassName)) - { - sb.Append($" (Method '{_nativeClassName}')"); - } - - return sb.ToString(); - } - } - } - - public class NativeMethodBindNotFoundException : NativeMemberNotFoundException - { - private readonly string? _nativeMethodName; - - // ReSharper disable once InconsistentNaming - private const string Arg_NativeMethodBindNotFoundException = "Unable to find the native method bind."; - - public NativeMethodBindNotFoundException() - : base(Arg_NativeMethodBindNotFoundException) - { - } - - public NativeMethodBindNotFoundException(string? nativeMethodName) - : this(Arg_NativeMethodBindNotFoundException, nativeMethodName) - { - } - - public NativeMethodBindNotFoundException(string? message, Exception? innerException) - : base(message, innerException) - { - } - - public NativeMethodBindNotFoundException(string? message, string? nativeMethodName) - : base(message) - { - _nativeMethodName = nativeMethodName; - } - - public NativeMethodBindNotFoundException(string? message, string? nativeMethodName, Exception? innerException) - : base(message, innerException) - { - _nativeMethodName = nativeMethodName; - } - - public override string Message - { - get - { - StringBuilder sb; - if (string.IsNullOrEmpty(base.Message)) - { - sb = new(Arg_NativeMethodBindNotFoundException); - } - else - { - sb = new(base.Message); - } - - if (!string.IsNullOrEmpty(_nativeMethodName)) - { - sb.Append($" (Method '{_nativeMethodName}')"); - } - - return sb.ToString(); - } - } - } - } -} diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Rid.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Rid.cs index e727526ec3..150eb98fc7 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Rid.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Rid.cs @@ -24,7 +24,7 @@ namespace Godot /// /// Constructs a new for the given . /// - public Rid(Godot.Object from) + public Rid(GodotObject from) => _id = from is Resource res ? res.GetRid()._id : default; /// diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Signal.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Signal.cs index f9b8f06603..9ac8abd37b 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Signal.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Signal.cs @@ -5,13 +5,14 @@ namespace Godot /// public readonly struct Signal : IAwaitable { - private readonly Object _owner; + private readonly GodotObject _owner; private readonly StringName _signalName; /// /// Object that contains the signal. /// - public Object Owner => _owner; + public GodotObject Owner => _owner; + /// /// Name of the signal. /// @@ -23,7 +24,7 @@ namespace Godot /// /// Object that contains the signal. /// Name of the signal. - public Signal(Object owner, StringName name) + public Signal(GodotObject owner, StringName name) { _owner = owner; _signalName = name; diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/SignalAwaiter.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/SignalAwaiter.cs index 96fb891086..a67f626d35 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/SignalAwaiter.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/SignalAwaiter.cs @@ -10,13 +10,13 @@ namespace Godot private Variant[] _result; private Action _continuation; - public SignalAwaiter(Object source, StringName signal, Object target) + public SignalAwaiter(GodotObject source, StringName signal, GodotObject target) { var awaiterGcHandle = CustomGCHandle.AllocStrong(this); using godot_string_name signalSrc = NativeFuncs.godotsharp_string_name_new_copy( (godot_string_name)(signal?.NativeValue ?? default)); - NativeFuncs.godotsharp_internal_signal_awaiter_connect(Object.GetPtr(source), in signalSrc, - Object.GetPtr(target), GCHandle.ToIntPtr(awaiterGcHandle)); + NativeFuncs.godotsharp_internal_signal_awaiter_connect(GodotObject.GetPtr(source), in signalSrc, + GodotObject.GetPtr(target), GCHandle.ToIntPtr(awaiterGcHandle)); } public bool IsCompleted => _completed; diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Variant.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Variant.cs index 63a9ba2747..9aad965ad0 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Variant.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Variant.cs @@ -324,7 +324,7 @@ public partial struct Variant : IDisposable [MethodImpl(MethodImplOptions.AggressiveInlining)] public T[] AsGodotObjectArray() - where T : Godot.Object => + where T : GodotObject => VariantUtils.ConvertToSystemArrayOfGodotObject((godot_variant)NativeVar); [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -348,7 +348,7 @@ public partial struct Variant : IDisposable VariantUtils.ConvertToSystemArrayOfRid((godot_variant)NativeVar); [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Godot.Object AsGodotObject() => + public GodotObject AsGodotObject() => VariantUtils.ConvertToGodotObject((godot_variant)NativeVar); [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -503,7 +503,7 @@ public partial struct Variant : IDisposable public static explicit operator Rid[](Variant from) => from.AsSystemArrayOfRid(); [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static explicit operator Godot.Object(Variant from) => from.AsGodotObject(); + public static explicit operator GodotObject(Variant from) => from.AsGodotObject(); [MethodImpl(MethodImplOptions.AggressiveInlining)] public static explicit operator StringName(Variant from) => from.AsStringName(); @@ -644,7 +644,7 @@ public partial struct Variant : IDisposable public static Variant CreateFrom(Span from) => from; [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Variant CreateFrom(Godot.Object[] from) => from; + public static Variant CreateFrom(GodotObject[] from) => from; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Variant CreateFrom<[MustBeVariant] TKey, [MustBeVariant] TValue>(Collections.Dictionary from) => @@ -664,7 +664,7 @@ public partial struct Variant : IDisposable public static Variant CreateFrom(Span from) => from; [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static Variant CreateFrom(Godot.Object from) => from; + public static Variant CreateFrom(GodotObject from) => from; [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Variant CreateFrom(StringName from) => from; @@ -844,7 +844,7 @@ public partial struct Variant : IDisposable (Variant)from.AsSpan(); [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator Variant(Godot.Object[] from) => + public static implicit operator Variant(GodotObject[] from) => CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromSystemArrayOfGodotObject(from)); [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -908,7 +908,7 @@ public partial struct Variant : IDisposable CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromSystemArrayOfRid(from)); [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator Variant(Godot.Object from) => + public static implicit operator Variant(GodotObject from) => CreateTakingOwnershipOfDisposableValue(VariantUtils.CreateFromGodotObject(from)); [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/modules/mono/glue/GodotSharp/GodotSharp/GodotSharp.csproj b/modules/mono/glue/GodotSharp/GodotSharp/GodotSharp.csproj index 465d6f1a5e..7aa2f7e959 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/GodotSharp.csproj +++ b/modules/mono/glue/GodotSharp/GodotSharp/GodotSharp.csproj @@ -81,11 +81,13 @@ + - + + @@ -105,8 +107,6 @@ - - diff --git a/modules/mono/utils/naming_utils.cpp b/modules/mono/utils/naming_utils.cpp index b96abb7eb4..dc9bc3485a 100644 --- a/modules/mono/utils/naming_utils.cpp +++ b/modules/mono/utils/naming_utils.cpp @@ -45,6 +45,7 @@ HashMap _create_hashmap_from_vector(Vector> const HashMap pascal_case_name_overrides = _create_hashmap_from_vector({ { "BitMap", "Bitmap" }, { "JSONRPC", "JsonRpc" }, + { "Object", "GodotObject" }, { "OpenXRIPBinding", "OpenXRIPBinding" }, { "SkeletonModification2DCCDIK", "SkeletonModification2DCcdik" }, { "SkeletonModification2DFABRIK", "SkeletonModification2DFabrik" }, -- cgit v1.2.3