Extending ScriptGlue now possible

This commit is contained in:
Simon Lübeß
2025-07-27 17:43:00 +02:00
parent 566d39d928
commit 72e87ee9ad
4 changed files with 64 additions and 52 deletions
@@ -19,15 +19,6 @@ namespace GlitchyEditor.EditWindows
{
private static uint32 TableId;
public static this()
{
/*ScriptGlue.OnRegisterNativeCalls.Add(new () => {
// TODO: Implement
Runtime.NotImplemented();
//ScriptGlue.RegisterCall<function bool(ref AssetHandle)>("ScriptGlue::ImGuiExtension_ShowAssetDropTarget", => ShowAssetDropTarget);
});*/
}
public static void ShowComponents(Entity entity, Type componentType = null)
{
float cellPaddingY = ImGui.GetTextLineHeight() / 3.0f;
@@ -0,0 +1,24 @@
using System;
using GlitchyEditor.EditWindows;
using GlitchyEngine.Content;
namespace GlitchyEngine.Scripting;
extension ScriptGlue
{
static this()
{
OnRegisterNativeCalls.Add(new => RegisterExtensionCalls);
}
private static void RegisterExtensionCalls()
{
EngineFunctions.[Friend]_functions.ImGuiExtension_ShowAssetDropTarget = => ImGuiExtension_ShowAssetDropTarget;
}
[RegisterCall(EngineResultAsBool = true, IsExtension = true)]
public static EngineResult ImGuiExtension_ShowAssetDropTarget(ref AssetHandle assetHandle)
{
return ComponentEditWindow.ShowAssetDropTarget(ref assetHandle) ? .Ok : .False;
}
}
+39 -40
View File
@@ -47,6 +47,7 @@ class MessageOrigin
struct RegisterCallAttribute : Attribute
{
public bool EngineResultAsBool { get; set mut; }
public bool IsExtension { get; set mut; }
}
struct TypeTranslationTemplate
@@ -177,38 +178,47 @@ struct EngineFunctionsGeneratorAttribute : Attribute, IComptimeTypeApply
[EngineFunctionsGenerator]
struct EngineFunctions
{
static Self _functions;
[RegisterMethod]
public static void FillEngineFunctions()
{
OnRegisterNativeCalls.Invoke();
ScriptGlue.[Friend]_setEngineFunctions(&_functions);
}
}
/* Adding this attribute to a method will log method entry and returned Result<T> errors */
[AttributeUsage(.Method)]
public struct RegisterMethodAttribute : Attribute, IOnMethodInit
{
[Comptime]
public void OnMethodInit(MethodInfo method, Self* prev)
{
String functionContent = new .();
int i = 0;
for (var methodInfo in typeof(ScriptGlue).GetMethods(.Static | .NonPublic | .FlattenHierarchy))
{
if (methodInfo.GetCustomAttribute<RegisterCallAttribute>() case .Ok(let attribute) && !attribute.IsExtension)
{
functionContent.AppendF($"_functions.{methodInfo.Name} = => ScriptGlue.[Friend]{methodInfo.Name};\n");
//functionContent.AppendF($"functions[{i}] = (void*)( => {methodInfo.Name});\n");
i++;
}
}
//functionContent.Insert(0, scope $"EngineFunctions functions = .();\n");
Compiler.EmitMethodEntry(method, functionContent);
}
}
static class ScriptGlue
{
/* Adding this attribute to a method will log method entry and returned Result<T> errors */
[AttributeUsage(.Method)]
struct RegisterMethodAttribute : Attribute, IOnMethodInit
{
[Comptime]
public void OnMethodInit(MethodInfo method, Self* prev)
{
String functionContent = new .();
int i = 0;
for (var methodInfo in typeof(ScriptGlue).GetMethods(.Static | .NonPublic | .FlattenHierarchy))
{
if (methodInfo.GetCustomAttribute<RegisterCallAttribute>() case .Ok(let attribute))
{
functionContent.AppendF($"functions.{methodInfo.Name} = => {methodInfo.Name};\n");
//functionContent.AppendF($"functions[{i}] = (void*)( => {methodInfo.Name});\n");
i++;
}
}
functionContent.Insert(0, scope $"EngineFunctions functions = .();\n");
Compiler.EmitMethodEntry(method, functionContent);
}
}
public static Event<delegate void()> OnRegisterNativeCalls ~ _.Dispose();
private function void SetEngineFunctions(EngineFunctions* engineFunctions);
@@ -242,18 +252,9 @@ static class ScriptGlue
private static void RegisterCalls()
{
FillEngineFunctions();
EngineFunctions.FillEngineFunctions();
}
[RegisterMethod]
private static void FillEngineFunctions()
{
OnRegisterNativeCalls.Invoke();
_setEngineFunctions(&functions);
}
private static void RegisterComponent<T>() where T : struct, new
{
String fullComponentTypeName = scope String();
@@ -1421,8 +1422,6 @@ static class ScriptGlue
Log.EngineLogger.AssertDebug(context != null);
Log.EngineLogger.Trace(StringView(fieldName));
context.AddField(StringView(fieldName), type, valueObject, StringView(fullTypeName));
}
+1 -3
View File
@@ -77,9 +77,7 @@ public static class ImGuiExtension
public static bool ShowAssetDropTarget(ref UUID uuid)
{
// TODO: !!!
// return ScriptGlue.ImGuiExtension_ShowAssetDropTarget(ref uuid);
return false;
return ScriptGlue.ImGuiExtension_ShowAssetDropTarget(ref uuid);
}
public static bool Checkbox2(string label, ref bool2 value) => CheckboxN(2, label, ref value.X);