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; 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) public static void ShowComponents(Entity entity, Type componentType = null)
{ {
float cellPaddingY = ImGui.GetTextLineHeight() / 3.0f; 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;
}
}
+21 -22
View File
@@ -47,6 +47,7 @@ class MessageOrigin
struct RegisterCallAttribute : Attribute struct RegisterCallAttribute : Attribute
{ {
public bool EngineResultAsBool { get; set mut; } public bool EngineResultAsBool { get; set mut; }
public bool IsExtension { get; set mut; }
} }
struct TypeTranslationTemplate struct TypeTranslationTemplate
@@ -177,14 +178,22 @@ struct EngineFunctionsGeneratorAttribute : Attribute, IComptimeTypeApply
[EngineFunctionsGenerator] [EngineFunctionsGenerator]
struct EngineFunctions struct EngineFunctions
{ {
static Self _functions;
[RegisterMethod]
public static void FillEngineFunctions()
{
OnRegisterNativeCalls.Invoke();
ScriptGlue.[Friend]_setEngineFunctions(&_functions);
}
} }
static class ScriptGlue /* Adding this attribute to a method will log method entry and returned Result<T> errors */
[AttributeUsage(.Method)]
public struct RegisterMethodAttribute : Attribute, IOnMethodInit
{ {
/* Adding this attribute to a method will log method entry and returned Result<T> errors */
[AttributeUsage(.Method)]
struct RegisterMethodAttribute : Attribute, IOnMethodInit
{
[Comptime] [Comptime]
public void OnMethodInit(MethodInfo method, Self* prev) public void OnMethodInit(MethodInfo method, Self* prev)
{ {
@@ -194,21 +203,22 @@ static class ScriptGlue
for (var methodInfo in typeof(ScriptGlue).GetMethods(.Static | .NonPublic | .FlattenHierarchy)) for (var methodInfo in typeof(ScriptGlue).GetMethods(.Static | .NonPublic | .FlattenHierarchy))
{ {
if (methodInfo.GetCustomAttribute<RegisterCallAttribute>() case .Ok(let attribute)) if (methodInfo.GetCustomAttribute<RegisterCallAttribute>() case .Ok(let attribute) && !attribute.IsExtension)
{ {
functionContent.AppendF($"functions.{methodInfo.Name} = => {methodInfo.Name};\n"); functionContent.AppendF($"_functions.{methodInfo.Name} = => ScriptGlue.[Friend]{methodInfo.Name};\n");
//functionContent.AppendF($"functions[{i}] = (void*)( => {methodInfo.Name});\n"); //functionContent.AppendF($"functions[{i}] = (void*)( => {methodInfo.Name});\n");
i++; i++;
} }
} }
functionContent.Insert(0, scope $"EngineFunctions functions = .();\n"); //functionContent.Insert(0, scope $"EngineFunctions functions = .();\n");
Compiler.EmitMethodEntry(method, functionContent); Compiler.EmitMethodEntry(method, functionContent);
} }
} }
static class ScriptGlue
{
public static Event<delegate void()> OnRegisterNativeCalls ~ _.Dispose(); public static Event<delegate void()> OnRegisterNativeCalls ~ _.Dispose();
private function void SetEngineFunctions(EngineFunctions* engineFunctions); private function void SetEngineFunctions(EngineFunctions* engineFunctions);
@@ -242,16 +252,7 @@ static class ScriptGlue
private static void RegisterCalls() 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 private static void RegisterComponent<T>() where T : struct, new
@@ -1421,8 +1422,6 @@ static class ScriptGlue
Log.EngineLogger.AssertDebug(context != null); Log.EngineLogger.AssertDebug(context != null);
Log.EngineLogger.Trace(StringView(fieldName));
context.AddField(StringView(fieldName), type, valueObject, StringView(fullTypeName)); 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) public static bool ShowAssetDropTarget(ref UUID uuid)
{ {
// TODO: !!! return ScriptGlue.ImGuiExtension_ShowAssetDropTarget(ref uuid);
// return ScriptGlue.ImGuiExtension_ShowAssetDropTarget(ref uuid);
return false;
} }
public static bool Checkbox2(string label, ref bool2 value) => CheckboxN(2, label, ref value.X); public static bool Checkbox2(string label, ref bool2 value) => CheckboxN(2, label, ref value.X);