Fixed ScriptGlue function discovery and slighty automated extension assignment

This commit is contained in:
Simon Lübeß
2026-04-11 09:38:43 +02:00
parent 28a9242023
commit bca4281d84
2 changed files with 32 additions and 20 deletions
@@ -11,14 +11,20 @@ extension ScriptGlue
OnRegisterNativeCalls.Add(new => RegisterExtensionCalls); OnRegisterNativeCalls.Add(new => RegisterExtensionCalls);
} }
[RegisterMethod(IsExtension = true)]
private static void RegisterExtensionCalls() private static void RegisterExtensionCalls()
{ {
EngineFunctions.[Friend]_functions.ImGuiExtension_ShowAssetDropTarget = => ImGuiExtension_ShowAssetDropTarget;
} }
[RegisterCall(EngineResultAsBool = true, IsExtension = true)] [RegisterCall(EngineResultAsBool = true, IsExtension = true)]
public static EngineResult ImGuiExtension_ShowAssetDropTarget(ref AssetHandle assetHandle) static EngineResult ImGuiExtension_ShowAssetDropTarget(ref AssetHandle assetHandle)
{ {
return ComponentEditWindow.ShowAssetDropTarget(ref assetHandle) ? .Ok : .False; return ComponentEditWindow.ShowAssetDropTarget(ref assetHandle) ? .Ok : .False;
} }
[RegisterCall(IsExtension = true), CallingConvention(.Cdecl)]
static void ImGuiExtension_ListElementGrabber()
{
ImGui.ImGui.ListElementGrabber();
}
} }
+23 -17
View File
@@ -203,9 +203,11 @@ struct EngineFunctions
} }
/* Adding this attribute to a method will log method entry and returned Result<T> errors */ /* Adding this attribute to a method will log method entry and returned Result<T> errors */
[AttributeUsage(.Method)] [AttributeUsage(.Method | .Constructor)]
public struct RegisterMethodAttribute : Attribute, IOnMethodInit public struct RegisterMethodAttribute : Attribute, IOnMethodInit
{ {
public bool IsExtension = false;
[Comptime] [Comptime]
public void OnMethodInit(MethodInfo method, Self* prev) public void OnMethodInit(MethodInfo method, Self* prev)
{ {
@@ -213,12 +215,26 @@ public struct RegisterMethodAttribute : Attribute, IOnMethodInit
int i = 0; int i = 0;
for (var methodInfo in typeof(ScriptGlue).GetMethods(.Static | .NonPublic | .FlattenHierarchy)) BindingFlags flags = .Static;
if (IsExtension)
{ {
if (methodInfo.GetCustomAttribute<RegisterCallAttribute>() case .Ok(let attribute) && !attribute.IsExtension) flags |= .DeclaredOnly;
}
for (var methodInfo in typeof(ScriptGlue).GetMethods(flags))
{
if (methodInfo.GetCustomAttribute<RegisterCallAttribute>() not case .Ok(let attribute))
continue;
if (IsExtension && attribute.IsExtension)
{
functionContent.AppendF($"EngineFunctions.[Friend]_functions.{methodInfo.Name} = => ScriptGlue.{methodInfo.Name};\n");
i++;
}
else if (!IsExtension && !attribute.IsExtension)
{ {
functionContent.AppendF($"_functions.{methodInfo.Name} = => ScriptGlue.[Friend]{methodInfo.Name};\n"); functionContent.AppendF($"_functions.{methodInfo.Name} = => ScriptGlue.[Friend]{methodInfo.Name};\n");
//functionContent.AppendF($"functions[{i}] = (void*)( => {methodInfo.Name});\n");
i++; i++;
} }
} }
@@ -1467,7 +1483,7 @@ static class ScriptGlue
} }
[RegisterCall, CallingConvention(.Cdecl)] [RegisterCall, CallingConvention(.Cdecl)]
public static void Serialization_DeserializeField(void* internalContext, SerializationType expectedType, StringView fieldName, uint8* target, out SerializationType actualType) static void Serialization_DeserializeField(void* internalContext, SerializationType expectedType, StringView fieldName, uint8* target, out SerializationType actualType)
{ {
SerializedObject context = Internal.UnsafeCastToObject(internalContext) as SerializedObject; SerializedObject context = Internal.UnsafeCastToObject(internalContext) as SerializedObject;
@@ -1477,7 +1493,7 @@ static class ScriptGlue
} }
[RegisterCall, CallingConvention(.Cdecl)] [RegisterCall, CallingConvention(.Cdecl)]
public static void Serialization_GetObject(void* internalContext, UUID id, out void* objectContext) static void Serialization_GetObject(void* internalContext, UUID id, out void* objectContext)
{ {
SerializedObject context = Internal.UnsafeCastToObject(internalContext) as SerializedObject; SerializedObject context = Internal.UnsafeCastToObject(internalContext) as SerializedObject;
@@ -1494,7 +1510,7 @@ static class ScriptGlue
} }
[RegisterCall, CallingConvention(.Cdecl)] [RegisterCall, CallingConvention(.Cdecl)]
public static void Serialization_GetObjectTypeName(void* internalContext, out StringView fullTypeName) static void Serialization_GetObjectTypeName(void* internalContext, out StringView fullTypeName)
{ {
SerializedObject context = Internal.UnsafeCastToObject(internalContext) as SerializedObject; SerializedObject context = Internal.UnsafeCastToObject(internalContext) as SerializedObject;
@@ -1657,15 +1673,5 @@ static class ScriptGlue
return .Ok; return .Ok;
} }
#endregion
#region ImGui Extension
[RegisterCall, CallingConvention(.Cdecl)]
static void ImGuiExtension_ListElementGrabber()
{
ImGui.ImGui.ListElementGrabber();
}
#endregion #endregion
} }