From 72e87ee9ad3432f2bda5b2908529888e4b0a74c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Sun, 27 Jul 2025 17:43:00 +0200 Subject: [PATCH] Extending ScriptGlue now possible --- .../src/EditWindows/ComponentEditWindow.bf | 9 --- .../Extensions/Engine/ScriptGlueExtension.bf | 24 ++++++ GlitchyEngine/src/Scripting/ScriptGlue.bf | 79 +++++++++---------- ScriptCore/Extensions/ImGuiExtension.cs | 4 +- 4 files changed, 64 insertions(+), 52 deletions(-) create mode 100644 GlitchyEditor/src/Extensions/Engine/ScriptGlueExtension.bf diff --git a/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf b/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf index 82e79d9..359e726 100644 --- a/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf +++ b/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf @@ -19,15 +19,6 @@ namespace GlitchyEditor.EditWindows { private static uint32 TableId; - public static this() - { - /*ScriptGlue.OnRegisterNativeCalls.Add(new () => { - // TODO: Implement - Runtime.NotImplemented(); - //ScriptGlue.RegisterCall("ScriptGlue::ImGuiExtension_ShowAssetDropTarget", => ShowAssetDropTarget); - });*/ - } - public static void ShowComponents(Entity entity, Type componentType = null) { float cellPaddingY = ImGui.GetTextLineHeight() / 3.0f; diff --git a/GlitchyEditor/src/Extensions/Engine/ScriptGlueExtension.bf b/GlitchyEditor/src/Extensions/Engine/ScriptGlueExtension.bf new file mode 100644 index 0000000..1e37546 --- /dev/null +++ b/GlitchyEditor/src/Extensions/Engine/ScriptGlueExtension.bf @@ -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; + } +} diff --git a/GlitchyEngine/src/Scripting/ScriptGlue.bf b/GlitchyEngine/src/Scripting/ScriptGlue.bf index 3f43624..31ee296 100644 --- a/GlitchyEngine/src/Scripting/ScriptGlue.bf +++ b/GlitchyEngine/src/Scripting/ScriptGlue.bf @@ -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 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() 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 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() 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 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() 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)); } diff --git a/ScriptCore/Extensions/ImGuiExtension.cs b/ScriptCore/Extensions/ImGuiExtension.cs index d673c09..3b95143 100644 --- a/ScriptCore/Extensions/ImGuiExtension.cs +++ b/ScriptCore/Extensions/ImGuiExtension.cs @@ -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);