Skip to content

Commit

Permalink
C#: Add dedicated Variant struct, replacing System.Object
Browse files Browse the repository at this point in the history
  • Loading branch information
neikeq committed Jul 28, 2022
1 parent a2ef306 commit 61c2255
Show file tree
Hide file tree
Showing 23 changed files with 625 additions and 124 deletions.
2 changes: 1 addition & 1 deletion modules/mono/csharp_script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Error CSharpLanguage::execute_file(const String &p_path) {
return OK;
}

extern void *godotsharp_pinvoke_funcs[177];
extern void *godotsharp_pinvoke_funcs[178];
[[maybe_unused]] volatile void **do_not_strip_godotsharp_pinvoke_funcs;
#ifdef TOOLS_ENABLED
extern void *godotsharp_editor_pinvoke_funcs[30];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ enum MyEnum
[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 object[] field_SystemObjectArray = { 0, 1f, 2d, "foo", Vector3i.Up };

// Generics
[Export] private Godot.Collections.Dictionary<string, string> field_GodotGenericDictionary =
Expand All @@ -91,7 +90,7 @@ enum MyEnum
new System.Collections.Generic.List<string> { "elem1", "elem2", "elem3" };

// Variant
[Export] private object field_SystemObject = "foo";
[Export] private Variant field_Variant = "foo";

// Classes
[Export] private Godot.Object field_GodotObjectOrDerived;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ enum MyEnum
[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 object[] property_SystemObjectArray { get; set; } = { 0, 1f, 2d, "foo", Vector3i.Up };

// Generics
[Export] private Godot.Collections.Dictionary<string, string> property_GodotGenericDictionary { get; set; } =
Expand All @@ -91,7 +90,7 @@ enum MyEnum
new System.Collections.Generic.List<string> { "elem1", "elem2", "elem3" };

// Variant
[Export] private object property_SystemObject { get; set; } = "foo";
[Export] private Variant property_Variant { get; set; } = "foo";

// Classes
[Export] private Godot.Object property_GodotObjectOrDerived { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ partial struct OuterClass
{
public partial class NesterClass : RefCounted
{
public override object _Get(StringName property) => null;
public override Variant _Get(StringName property) => default;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public enum MarshalType
Vector3Array,
ColorArray,
GodotObjectOrDerivedArray,
SystemObjectArray,
SystemArrayOfSupportedType,

// Generics
Expand All @@ -63,7 +62,7 @@ public enum MarshalType
GenericIEnumerable,

// Variant
SystemObject,
Variant,

// Classes
GodotObjectOrDerived,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ INamedTypeSymbol GetTypeByMetadataNameOrThrow(string fullyQualifiedMetadataName)
MarshalType.Vector3Array => VariantType.PackedVector3Array,
MarshalType.ColorArray => VariantType.PackedColorArray,
MarshalType.GodotObjectOrDerivedArray => VariantType.Array,
MarshalType.SystemObjectArray => VariantType.Array,
MarshalType.SystemArrayOfSupportedType => VariantType.Array,
MarshalType.GodotGenericDictionary => VariantType.Dictionary,
MarshalType.GodotGenericArray => VariantType.Array,
Expand All @@ -92,7 +91,7 @@ INamedTypeSymbol GetTypeByMetadataNameOrThrow(string fullyQualifiedMetadataName)
MarshalType.GenericIDictionary => VariantType.Dictionary,
MarshalType.GenericICollection => VariantType.Array,
MarshalType.GenericIEnumerable => VariantType.Array,
MarshalType.SystemObject => VariantType.Nil,
MarshalType.Variant => VariantType.Nil,
MarshalType.GodotObjectOrDerived => VariantType.Object,
MarshalType.StringName => VariantType.StringName,
MarshalType.NodePath => VariantType.NodePath,
Expand Down Expand Up @@ -137,8 +136,6 @@ INamedTypeSymbol GetTypeByMetadataNameOrThrow(string fullyQualifiedMetadataName)
return MarshalType.Double;
case SpecialType.System_String:
return MarshalType.String;
case SpecialType.System_Object:
return MarshalType.SystemObject;
default:
{
var typeKind = type.TypeKind;
Expand Down Expand Up @@ -169,6 +166,7 @@ INamedTypeSymbol GetTypeByMetadataNameOrThrow(string fullyQualifiedMetadataName)
{ Name: "RID" } => MarshalType.RID,
{ Name: "Callable" } => MarshalType.Callable,
{ Name: "SignalInfo" } => MarshalType.SignalInfo,
{ Name: "Variant" } => MarshalType.Variant,
_ => null
};
}
Expand All @@ -192,8 +190,6 @@ INamedTypeSymbol GetTypeByMetadataNameOrThrow(string fullyQualifiedMetadataName)
return MarshalType.Float64Array;
case SpecialType.System_String:
return MarshalType.StringArray;
case SpecialType.System_Object:
return MarshalType.SystemObjectArray;
}

if (elementType.SimpleDerivesFrom(typeCache.GodotObjectType))
Expand Down Expand Up @@ -317,6 +313,9 @@ private static bool SimpleDerivesFrom(this ITypeSymbol? type, ITypeSymbol candid
return null;
}

private static StringBuilder Append(this StringBuilder source, string a, string b)
=> source.Append(a).Append(b);

private static StringBuilder Append(this StringBuilder source, string a, string b, string c)
=> source.Append(a).Append(b).Append(c);

Expand All @@ -340,7 +339,6 @@ private static StringBuilder Append(this StringBuilder source, string a, string
string c, string d, string e, string f, string g, string h)
=> source.Append(a).Append(b).Append(c).Append(d).Append(e).Append(f).Append(g).Append(h);

private const string Marshaling = "global::Godot.NativeInterop.Marshaling";
private const string VariantUtils = "global::Godot.NativeInterop.VariantUtils";

public static StringBuilder AppendVariantToManagedExpr(this StringBuilder source,
Expand Down Expand Up @@ -428,8 +426,6 @@ public static StringBuilder AppendVariantToManagedExpr(this StringBuilder source
MarshalType.GodotObjectOrDerivedArray =>
source.Append(VariantUtils, ".ConvertToSystemArrayOfGodotObject<",
((IArrayTypeSymbol)typeSymbol).ElementType.FullQualifiedName(), ">(", inputExpr, ")"),
MarshalType.SystemObjectArray =>
source.Append(VariantUtils, ".ConvertToSystemArrayOfVariant(", inputExpr, ")"),
MarshalType.SystemArrayOfSupportedType =>
source.Append(VariantUtils, ".ConvertToSystemArrayOfSupportedType<",
((IArrayTypeSymbol)typeSymbol).ElementType.FullQualifiedName(), ">(", inputExpr, ")"),
Expand All @@ -454,8 +450,8 @@ public static StringBuilder AppendVariantToManagedExpr(this StringBuilder source
MarshalType.GenericICollection or MarshalType.GenericIEnumerable =>
source.Append(VariantUtils, ".ConvertToGenericArrayObject<",
((INamedTypeSymbol)typeSymbol).TypeArguments[0].FullQualifiedName(), ">(", inputExpr, ")"),
MarshalType.SystemObject =>
source.Append(Marshaling, ".ConvertVariantToManagedObject(", inputExpr, ")"),
MarshalType.Variant =>
source.Append("global::Godot.Variant.CreateCopyingBorrowed(", inputExpr, ")"),
MarshalType.GodotObjectOrDerived =>
source.Append("(", typeSymbol.FullQualifiedName(),
")", VariantUtils, ".ConvertToGodotObject(", inputExpr, ")"),
Expand Down Expand Up @@ -561,8 +557,6 @@ public static StringBuilder AppendManagedToVariantExpr(
source.Append(VariantUtils, ".CreateFromPackedColorArray(", inputExpr, ")"),
MarshalType.GodotObjectOrDerivedArray =>
source.Append(VariantUtils, ".CreateFromSystemArrayOfGodotObject(", inputExpr, ")"),
MarshalType.SystemObjectArray =>
source.Append(VariantUtils, ".CreateFromSystemArrayOfVariant(", inputExpr, ")"),
MarshalType.SystemArrayOfSupportedType =>
source.Append(VariantUtils, ".CreateFromSystemArrayOfSupportedType(", inputExpr, ")"),
MarshalType.GodotGenericDictionary =>
Expand All @@ -579,8 +573,8 @@ public static StringBuilder AppendManagedToVariantExpr(
source.Append(VariantUtils, ".CreateFromSystemGenericICollection(", inputExpr, ")"),
MarshalType.GenericIEnumerable =>
source.Append(VariantUtils, ".CreateFromSystemGenericIEnumerable(", inputExpr, ")"),
MarshalType.SystemObject =>
source.Append(Marshaling, ".ConvertManagedObjectToVariant(", inputExpr, ")"),
MarshalType.Variant =>
source.Append(inputExpr, ".CopyNativeVariant()"),
MarshalType.GodotObjectOrDerived =>
source.Append(VariantUtils, ".CreateFromGodotObject(", inputExpr, ")"),
MarshalType.StringName =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private void IssueActivated(int idx)
throw new IndexOutOfRangeException("Item list index out of range");

// Get correct issue idx from issue list
int issueIndex = (int)(long)_issuesList.GetItemMetadata(idx);
int issueIndex = _issuesList.GetItemMetadata(idx).AsInt32();

if (issueIndex < 0 || issueIndex >= _issues.Count)
throw new IndexOutOfRangeException("Issue index out of range");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public override void _ExportFile(string path, string type, string[] features)

// TODO What if the source file is not part of the game's C# project

bool includeScriptsContent = (bool)ProjectSettings.GetSetting("mono/export/include_scripts_content");
bool includeScriptsContent = ProjectSettings.GetSetting("mono/export/include_scripts_content").AsBool();

if (!includeScriptsContent)
{
Expand Down Expand Up @@ -224,7 +224,7 @@ private static bool DeterminePlatformFromFeatures(IEnumerable<string> features,

private static string DetermineDataDirNameForProject()
{
string appName = (string)ProjectSettings.GetSetting("application/config/name");
string appName = ProjectSettings.GetSetting("application/config/name").AsString();
string appNameSafe = appName.ToSafeDirName();
return $"data_{appNameSafe}";
}
Expand Down
8 changes: 4 additions & 4 deletions modules/mono/editor/GodotTools/GodotTools/GodotSharpEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static string ProjectAssemblyName
{
get
{
string projectAssemblyName = (string)ProjectSettings.GetSetting("application/config/name");
string projectAssemblyName = ProjectSettings.GetSetting("application/config/name").AsString();
projectAssemblyName = projectAssemblyName.ToSafeDirName();
if (string.IsNullOrEmpty(projectAssemblyName))
projectAssemblyName = "UnnamedProject";
Expand Down Expand Up @@ -175,7 +175,7 @@ public void ShowErrorDialog(string message, string title = "Error")
[UsedImplicitly]
public Error OpenInExternalEditor(Script script, int line, int col)
{
var editorId = (ExternalEditorId)_editorSettings.GetSetting("mono/editor/external_editor");
var editorId = (ExternalEditorId)_editorSettings.GetSetting("mono/editor/external_editor").AsInt32();

switch (editorId)
{
Expand Down Expand Up @@ -327,7 +327,7 @@ public Error OpenInExternalEditor(Script script, int line, int col)
[UsedImplicitly]
public bool OverridesExternalEditor()
{
return (ExternalEditorId)_editorSettings.GetSetting("mono/editor/external_editor") != ExternalEditorId.None;
return (ExternalEditorId)_editorSettings.GetSetting("mono/editor/external_editor").AsInt32() != ExternalEditorId.None;
}

public override bool _Build()
Expand Down Expand Up @@ -521,7 +521,7 @@ protected override void Dispose(bool disposing)
// Otherwise, if the GC disposes it at a later time, EditorExportPlatformAndroid
// will be freed after EditorSettings already was, and its device polling thread
// will try to access the EditorSettings singleton, resulting in null dereferencing.
(_exportPluginWeak.GetRef() as ExportPlugin)?.Dispose();
(_exportPluginWeak.GetRef().AsGodotObject() as ExportPlugin)?.Dispose();

_exportPluginWeak.Dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private string GetExternalEditorIdentity(ExternalEditorId editorId)
public async Task<EditorPick?> LaunchIdeAsync(int millisecondsTimeout = 10000)
{
var editorId = (ExternalEditorId)GodotSharpEditor.Instance.GetEditorInterface()
.GetEditorSettings().GetSetting("mono/editor/external_editor");
.GetEditorSettings().GetSetting("mono/editor/external_editor").AsInt32();
string editorIdentity = GetExternalEditorIdentity(editorId);

var runningServer = GetRunningOrNewServer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ private static string GetRiderPathFromSettings()
{
var editorSettings = GodotSharpEditor.Instance.GetEditorInterface().GetEditorSettings();
if (editorSettings.HasSetting(EditorPathSettingName))
return (string)editorSettings.GetSetting(EditorPathSettingName);
return editorSettings.GetSetting(EditorPathSettingName).AsString();
return null;
}

public static void Initialize()
{
var editorSettings = GodotSharpEditor.Instance.GetEditorInterface().GetEditorSettings();
var editor = (ExternalEditorId)editorSettings.GetSetting("mono/editor/external_editor");
var editor = (ExternalEditorId)editorSettings.GetSetting("mono/editor/external_editor").AsInt32();
if (editor == ExternalEditorId.Rider)
{
if (!editorSettings.HasSetting(EditorPathSettingName))
Expand All @@ -37,7 +37,7 @@ public static void Initialize()
});
}

var riderPath = (string)editorSettings.GetSetting(EditorPathSettingName);
var riderPath = editorSettings.GetSetting(EditorPathSettingName).AsString();
if (IsRiderAndExists(riderPath))
{
Globals.EditorDef(EditorPathSettingName, riderPath);
Expand All @@ -58,7 +58,7 @@ public static void Initialize()
public static bool IsExternalEditorSetToRider(EditorSettings editorSettings)
{
return editorSettings.HasSetting(EditorPathSettingName) &&
IsRider((string)editorSettings.GetSetting(EditorPathSettingName));
IsRider(editorSettings.GetSetting(EditorPathSettingName).AsString());
}

public static bool IsRider(string path)
Expand Down
Loading

0 comments on commit 61c2255

Please sign in to comment.