From 38fc1f63b34d6b98f101f367434fa59084a7f4c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Sat, 28 Oct 2023 21:47:46 +0200 Subject: [PATCH] Small script engine improvements - Fixed script class name not visible in Component Editor - Retain user assembly file system watcher (fixed deletion while handling events) - Fixed crash when a scritp constains a List or Array (by ignoring them for now) - Fixed Typo in Genericinst - Scene: Invoke OnCreate for copied entity scripts --- .../src/EditWindows/ComponentEditWindow.bf | 3 +- GlitchyEngine/src/Scripting/ScriptEngine.bf | 62 +++++++++++-------- .../src/Scripting/ScriptFieldType.bf | 3 + GlitchyEngine/src/World/Scene.bf | 11 ++++ GlitchyEngineHelper/src/Mono/Mono.bf | 2 +- 5 files changed, 53 insertions(+), 28 deletions(-) diff --git a/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf b/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf index e700a4b..e3daf9b 100644 --- a/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf +++ b/GlitchyEditor/src/EditWindows/ComponentEditWindow.bf @@ -541,7 +541,7 @@ namespace GlitchyEditor.EditWindows StringView search = StringView(); - char8* scriptLabel = scriptComponent.Instance?.ScriptClass.FullName.ToScopeCStr!() ?? "Select Script..."; + char8* scriptLabel = scriptComponent.ScriptClassName.ToScopeCStr!() ?? "Select Script..."; if (ImGui.Button(scriptLabel)) ImGui.OpenPopup("SelectScript"); @@ -791,7 +791,6 @@ namespace GlitchyEditor.EditWindows var value = field.GetData(); if (ImGui.Checkbox(fieldName.CStr(), &value)) field.SetData(value); - case .SByte: var value = field.GetData(); if (ImGui.DragScalar(fieldName.CStr(), .S8, &value)) diff --git a/GlitchyEngine/src/Scripting/ScriptEngine.bf b/GlitchyEngine/src/Scripting/ScriptEngine.bf index 753ef60..43753c2 100644 --- a/GlitchyEngine/src/Scripting/ScriptEngine.bf +++ b/GlitchyEngine/src/Scripting/ScriptEngine.bf @@ -27,7 +27,6 @@ static sealed class ScriptEngineHelper ("System.UInt64", .ULong), ("System.Single", .Float), - // TODO: We probably want to switch the C#-Library to use the superior floatN-Names ("GlitchyEngine.Math.float2", .float2), ("GlitchyEngine.Math.float3", .float3), ("GlitchyEngine.Math.float4", .float4), @@ -153,38 +152,40 @@ static class ScriptEngine String directory = scope .(); Path.GetDirectoryPath(_appAssemblyPath, directory); - if (_userAssemblyWatcher != null) // _userAssemblyWatcher.Directory != directory + /*if (_userAssemblyWatcher != null) // _userAssemblyWatcher.Directory != directory { delete _userAssemblyWatcher; - } + }*/ String fileName = scope .("*/"); Path.GetFileName(_appAssemblyPath, fileName); - // TODO: Obviously don't hardcode path - _userAssemblyWatcher = new FileSystemWatcher(directory, fileName); - _userAssemblyWatcher.OnChanged.Add(new (fileName) => + if (_userAssemblyWatcher == null) { - // TODO: Temporary, we want to be able to reload while in play-mode. (+ Editor Scripts will be a thing some day) - if (_entityScriptInstances.Count > 0) + _userAssemblyWatcher = new FileSystemWatcher(directory, fileName); + _userAssemblyWatcher.OnChanged.Add(new (fileName) => { - Log.EngineLogger.Warning("There are script instances. Skipping assembly reload."); - return; - } - - Log.EngineLogger.Info("Script reload requested."); - _userAssemblyWatcher.StopRaisingEvents(); - - Application.Instance.InvokeOnMainThread(new () => - { - Log.EngineLogger.Info("Reloading scripts..."); - ReloadAssemblies(); - Log.EngineLogger.Info("Scripts reloaded!"); - - _userAssemblyWatcher.StartRaisingEvents(); + // TODO: Temporary, we want to be able to reload while in play-mode. (+ Editor Scripts will be a thing some day) + if (_entityScriptInstances.Count > 0) + { + Log.EngineLogger.Warning("There are script instances. Skipping assembly reload."); + return; + } + + Log.EngineLogger.Info("Script reload requested."); + _userAssemblyWatcher.StopRaisingEvents(); + + Application.Instance.InvokeOnMainThread(new () => + { + Log.EngineLogger.Info("Reloading scripts..."); + ReloadAssemblies(); + Log.EngineLogger.Info("Scripts reloaded!"); + + _userAssemblyWatcher.StartRaisingEvents(); + }); + }); - - }); + } _userAssemblyWatcher.StartRaisingEvents(); } @@ -628,7 +629,7 @@ static class ScriptEngine Mono.MonoTypeEnum fieldType = Mono.Mono.mono_type_get_type(monoType); - MonoClass* monoClass = Mono.mono_type_get_class(monoType); + MonoClass* monoClass = Mono.mono_class_from_mono_type(monoType); if (monoClass == null) return null; @@ -667,6 +668,17 @@ static class ScriptEngine { scriptType = .Struct; } + // TODO! ?! + else if (fieldType == .Genericinst) + { + scriptType = .GenericClass; + return null; + } + else if (fieldType == .SzArray) + { + scriptType = .Array; + return null; + } Log.EngineLogger.AssertDebug(scriptType != .None); diff --git a/GlitchyEngine/src/Scripting/ScriptFieldType.bf b/GlitchyEngine/src/Scripting/ScriptFieldType.bf index acb9012..5ec55f4 100644 --- a/GlitchyEngine/src/Scripting/ScriptFieldType.bf +++ b/GlitchyEngine/src/Scripting/ScriptFieldType.bf @@ -11,6 +11,9 @@ enum ScriptFieldType case Class; case Enum; case Struct; + + case GenericClass; + case Array; case Bool; diff --git a/GlitchyEngine/src/World/Scene.bf b/GlitchyEngine/src/World/Scene.bf index d722a1b..6ca639c 100644 --- a/GlitchyEngine/src/World/Scene.bf +++ b/GlitchyEngine/src/World/Scene.bf @@ -841,6 +841,9 @@ namespace GlitchyEngine.World // TODO: Copy Data from one instance to another //ScriptEngine.CopyFieldsToInstance(targetScript, sourceScript); + + // TODO: Only if we are in Runtime + targetScript.Instance.InvokeOnCreate(); } // This is kinda slow because it's in O(n*m) where n is the tree depth and m is the total number of entities in the scene... @@ -920,6 +923,14 @@ namespace GlitchyEngine.World } } + private void OnComponentRemoved(Entity entity, Type componentType, void* component) + { + if (_onComponentAddedHandlers.TryGetValue(componentType, let handler)) + { + handler(entity, componentType, component); + } + } + public WorldEnumerator GetEntities() where TComponent : struct { return _ecsWorld.Enumerate(); diff --git a/GlitchyEngineHelper/src/Mono/Mono.bf b/GlitchyEngineHelper/src/Mono/Mono.bf index 7452995..701c1da 100644 --- a/GlitchyEngineHelper/src/Mono/Mono.bf +++ b/GlitchyEngineHelper/src/Mono/Mono.bf @@ -523,7 +523,7 @@ enum MonoTypeEnum : int32 Class = 0x12, /* arg: token */ Var = 0x13, /* number */ Array = 0x14, /* type, rank, boundsCount, bound1, loCount, lo1 */ - Genericins = 0x15, /* \x{2026} */ + Genericinst = 0x15, /* \x{2026} */ Typedbyref = 0x16, I = 0x18, U = 0x19,