1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-09 12:50:35 +00:00

C#: Replace most conversions between Variant and System.Object

This commit replaces most usages of `ConvertManagedObjectToVariant` and
`ConvertVariantToManagedObjectOfType`, by using the `Godot.Variant`
struct instead of `System.Object`.

The most notable change is to the `GetGodotPropertyDefaultValues` method
that's generated for scripts. The dictionary it returns now stores
`Godot.Variant` values.

Remaining usages are:

- The `DelegateUtils` class, for the serialization of closure display
  classes during assembly reloading by the editor. These display classes
  are compiler generated classes to store values captured by a closure.
  Since it's generated by the compiler, the only way we have to access
  the fields is through reflection. This leads to using `System.Object`.
- Converting parameters when invoking constructors from the engine.
  This will be replaced with source generators in the future.
- Legacy support for old `GetGodotPropertyDefaultValues` return values.
  We need to keep supporting the old version of this generated method
  for some time. Otherwise, if loading a project built with the previous
  version, it could lead to the loss of exported property values.
  Ideally, we should remove this legacy support before a stable release.
This commit is contained in:
Ignacio Roldán Etcheverry
2022-11-29 01:01:36 +01:00
parent 3afeb28560
commit f86c6b6ac4
8 changed files with 350 additions and 443 deletions

View File

@@ -455,7 +455,7 @@ namespace GodotTools
_menuPopup.IdPressed += _MenuOptionPressed;
// External editor settings
EditorDef("mono/editor/external_editor", ExternalEditorId.None);
EditorDef("mono/editor/external_editor", Variant.From(ExternalEditorId.None));
string settingsHintStr = "Disabled";

View File

@@ -1,3 +1,4 @@
using Godot;
using Godot.NativeInterop;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
@@ -8,30 +9,31 @@ namespace GodotTools.Internals
{
public static float EditorScale => Internal.godot_icall_Globals_EditorScale();
public static unsafe object GlobalDef(string setting, object defaultValue, bool restartIfChanged = false)
// ReSharper disable once UnusedMethodReturnValue.Global
public static Variant GlobalDef(string setting, Variant defaultValue, bool restartIfChanged = false)
{
using godot_string settingIn = Marshaling.ConvertStringToNative(setting);
using godot_variant defaultValueIn = Marshaling.ConvertManagedObjectToVariant(defaultValue);
Internal.godot_icall_Globals_GlobalDef(settingIn, defaultValueIn, restartIfChanged, out godot_variant result);
using (result)
return Marshaling.ConvertVariantToManagedObject(result);
using godot_variant defaultValueIn = defaultValue.CopyNativeVariant();
Internal.godot_icall_Globals_GlobalDef(settingIn, defaultValueIn, restartIfChanged,
out godot_variant result);
return Variant.CreateTakingOwnershipOfDisposableValue(result);
}
public static unsafe object EditorDef(string setting, object defaultValue, bool restartIfChanged = false)
// ReSharper disable once UnusedMethodReturnValue.Global
public static Variant EditorDef(string setting, Variant defaultValue, bool restartIfChanged = false)
{
using godot_string settingIn = Marshaling.ConvertStringToNative(setting);
using godot_variant defaultValueIn = Marshaling.ConvertManagedObjectToVariant(defaultValue);
Internal.godot_icall_Globals_EditorDef(settingIn, defaultValueIn, restartIfChanged, out godot_variant result);
using (result)
return Marshaling.ConvertVariantToManagedObject(result);
using godot_variant defaultValueIn = defaultValue.CopyNativeVariant();
Internal.godot_icall_Globals_EditorDef(settingIn, defaultValueIn, restartIfChanged,
out godot_variant result);
return Variant.CreateTakingOwnershipOfDisposableValue(result);
}
public static object EditorShortcut(string setting)
public static Variant EditorShortcut(string setting)
{
using godot_string settingIn = Marshaling.ConvertStringToNative(setting);
Internal.godot_icall_Globals_EditorShortcut(settingIn, out godot_variant result);
using (result)
return Marshaling.ConvertVariantToManagedObject(result);
return Variant.CreateTakingOwnershipOfDisposableValue(result);
}
[SuppressMessage("ReSharper", "InconsistentNaming")]