1
0
mirror of https://github.com/godotengine/godot.git synced 2025-12-05 17:15:09 +00:00

C#: Add global class support

Co-authored-by: willnationsdev <willnationsdev@gmail.com>
This commit is contained in:
Raul Santos
2023-02-02 00:54:16 +01:00
parent 2eec9a67d5
commit a1f454fee3
15 changed files with 201 additions and 25 deletions

View File

@@ -0,0 +1,12 @@
using System;
#nullable enable
namespace Godot
{
/// <summary>
/// Exposes the target class as a global script class to Godot Engine.
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public sealed class GlobalClassAttribute : Attribute { }
}

View File

@@ -0,0 +1,25 @@
using System;
namespace Godot
{
/// <summary>
/// Specifies a custom icon for representing this class in the Godot Editor.
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
public sealed class IconAttribute : Attribute
{
/// <summary>
/// File path to a custom icon for representing this class in the Godot Editor.
/// </summary>
public string Path { get; }
/// <summary>
/// Specify the custom icon that represents the class.
/// </summary>
/// <param name="path">File path to the custom icon.</param>
public IconAttribute(string path)
{
Path = path;
}
}
}

View File

@@ -25,7 +25,7 @@ namespace Godot.Bridge
public delegate* unmanaged<godot_string*, godot_ref*, void> ScriptManagerBridge_GetOrCreateScriptBridgeForPath;
public delegate* unmanaged<IntPtr, void> ScriptManagerBridge_RemoveScriptBridge;
public delegate* unmanaged<IntPtr, godot_bool> ScriptManagerBridge_TryReloadRegisteredScriptWithClass;
public delegate* unmanaged<IntPtr, godot_bool*, godot_array*, godot_dictionary*, godot_dictionary*, godot_ref*, void> ScriptManagerBridge_UpdateScriptClassInfo;
public delegate* unmanaged<IntPtr, godot_string*, godot_bool*, godot_bool*, godot_string*, godot_array*, godot_dictionary*, godot_dictionary*, godot_ref*, void> ScriptManagerBridge_UpdateScriptClassInfo;
public delegate* unmanaged<IntPtr, IntPtr*, godot_bool, godot_bool> ScriptManagerBridge_SwapGCHandleForType;
public delegate* unmanaged<IntPtr, delegate* unmanaged<IntPtr, godot_string*, void*, int, void>, void> ScriptManagerBridge_GetPropertyInfoList;
public delegate* unmanaged<IntPtr, delegate* unmanaged<IntPtr, void*, int, void>, void> ScriptManagerBridge_GetPropertyDefaultValues;

View File

@@ -298,6 +298,13 @@ namespace Godot.Bridge
_pathTypeBiMap.Add(scriptPathAttr.Path, type);
// This method may be called before initialization.
if (NativeFuncs.godotsharp_dotnet_module_is_initialized().ToBool() && Engine.IsEditorHint())
{
using godot_string scriptPath = Marshaling.ConvertStringToNative(scriptPathAttr.Path);
NativeFuncs.godotsharp_internal_editor_file_system_update_file(scriptPath);
}
if (AlcReloadCfg.IsAlcReloadingEnabled)
{
AddTypeForAlcReloading(type);
@@ -584,7 +591,8 @@ namespace Godot.Bridge
}
[UnmanagedCallersOnly]
internal static unsafe void UpdateScriptClassInfo(IntPtr scriptPtr, godot_bool* outTool,
internal static unsafe void UpdateScriptClassInfo(IntPtr scriptPtr, godot_string* outClassName,
godot_bool* outTool, godot_bool* outGlobal, godot_string* outIconPath,
godot_array* outMethodsDest, godot_dictionary* outRpcFunctionsDest,
godot_dictionary* outEventSignalsDest, godot_ref* outBaseScript)
{
@@ -593,6 +601,8 @@ namespace Godot.Bridge
// Performance is not critical here as this will be replaced with source generators.
var scriptType = _scriptTypeBiMap.GetScriptType(scriptPtr);
*outClassName = Marshaling.ConvertStringToNative(scriptType.Name);
*outTool = scriptType.GetCustomAttributes(inherit: false)
.OfType<ToolAttribute>()
.Any().ToGodotBool();
@@ -607,6 +617,18 @@ namespace Godot.Bridge
if (!(*outTool).ToBool() && scriptType.Assembly.GetName().Name == "GodotTools")
*outTool = godot_bool.True;
var globalAttr = scriptType.GetCustomAttributes(inherit: false)
.OfType<GlobalClassAttribute>()
.FirstOrDefault();
*outGlobal = (globalAttr != null).ToGodotBool();
var iconAttr = scriptType.GetCustomAttributes(inherit: false)
.OfType<IconAttribute>()
.FirstOrDefault();
*outIconPath = Marshaling.ConvertStringToNative(iconAttr?.Path);
// Methods
// Performance is not critical here as this will be replaced with source generators.
@@ -693,7 +715,7 @@ namespace Godot.Bridge
}
*outRpcFunctionsDest = NativeFuncs.godotsharp_dictionary_new_copy(
(godot_dictionary)(rpcFunctions).NativeValue);
(godot_dictionary)rpcFunctions.NativeValue);
// Event signals
@@ -755,7 +777,10 @@ namespace Godot.Bridge
catch (Exception e)
{
ExceptionUtils.LogException(e);
*outClassName = default;
*outTool = godot_bool.False;
*outGlobal = godot_bool.False;
*outIconPath = default;
*outMethodsDest = NativeFuncs.godotsharp_array_new();
*outRpcFunctionsDest = NativeFuncs.godotsharp_dictionary_new();
*outEventSignalsDest = NativeFuncs.godotsharp_dictionary_new();

View File

@@ -37,6 +37,8 @@ namespace Godot.NativeInterop
// Custom functions
internal static partial godot_bool godotsharp_dotnet_module_is_initialized();
public static partial IntPtr godotsharp_method_bind_get_method(in godot_string_name p_classname,
in godot_string_name p_methodname);
@@ -52,6 +54,8 @@ namespace Godot.NativeInterop
internal static partial void godotsharp_stack_info_vector_destroy(
ref DebuggingUtils.godot_stack_info_vector p_stack_info_vector);
internal static partial void godotsharp_internal_editor_file_system_update_file(in godot_string p_script_path);
internal static partial void godotsharp_internal_script_debugger_send_error(in godot_string p_func,
in godot_string p_file, int p_line, in godot_string p_err, in godot_string p_descr,
godot_bool p_warning, in DebuggingUtils.godot_stack_info_vector p_stack_info_vector);

View File

@@ -60,7 +60,9 @@
<Compile Include="Core\Attributes\ExportCategoryAttribute.cs" />
<Compile Include="Core\Attributes\ExportGroupAttribute.cs" />
<Compile Include="Core\Attributes\ExportSubgroupAttribute.cs" />
<Compile Include="Core\Attributes\GlobalClassAttribute.cs" />
<Compile Include="Core\Attributes\GodotClassNameAttribute.cs" />
<Compile Include="Core\Attributes\IconAttribute.cs" />
<Compile Include="Core\Attributes\MustBeVariantAttribute.cs" />
<Compile Include="Core\Attributes\RpcAttribute.cs" />
<Compile Include="Core\Attributes\ScriptPathAttribute.cs" />