Almost done refactoring ScriptEngine

This commit is contained in:
Simon Lübeß
2024-04-10 13:50:12 +02:00
parent 777f52ab49
commit f05cf721e8
4 changed files with 74 additions and 59 deletions
@@ -0,0 +1,37 @@
using System;
using Mono;
using GlitchyEngine.Core;
namespace GlitchyEngine.Scripting;
using internal GlitchyEngine.Scripting;
class EntityEditorWrapper : ScriptClass
{
function void ShowEntityEditorFunc(MonoObject* scriptInstance, MonoException** exception);
private ShowEntityEditorFunc _showEntityEditorFunc;
[AllowAppend]
public this(StringView classNamespace, StringView className, MonoImage* image) : base(classNamespace, className, image)
{
_showEntityEditorFunc = (ShowEntityEditorFunc)GetMethodThunk("ShowEntityEditor", 1);
if (_showEntityEditorFunc == null)
{
Log.EngineLogger.Error("Entity editor has no show entity editor func.");
}
}
public void ShowEntityEditor(ScriptInstance instance, UUID entityId)
{
MonoException* exception = null;
_showEntityEditorFunc(instance.MonoInstance, &exception);
if (exception != null)
{
ScriptEngine.HandleMonoException(exception, entityId);
}
}
}
+14 -25
View File
@@ -8,40 +8,29 @@ namespace GlitchyEngine.Scripting;
using internal GlitchyEngine.Scripting;
abstract class SharpType : RefCounter
class SharpClass : RefCounter
{
protected String _namespace ~ delete _;
protected String _className ~ delete _;
protected String _fullName ~ delete _;
protected String _fullName ~ delete:append _;
protected StringView _namespace;
protected StringView _className;
protected internal MonoClass* _monoClass;
public StringView Namespace => _namespace;
public StringView ClassName => _className;
public StringView FullName => _fullName;
public this(StringView classNamespace, StringView className)
[AllowAppend]
public this(StringView classNamespace, StringView className, MonoImage* image)
{
_namespace = new String(classNamespace);
_className = new String(className);
_fullName = new $"{_namespace}.{_className}";
}
}
String fullName = append $"{classNamespace}.{className}";
class SharpClass : SharpType
{
protected internal MonoClass* _monoClass;
_fullName = fullName;
_namespace = _fullName.Substring(0, classNamespace.Length);
_className = _fullName.Substring(classNamespace.Length + 1);
public this(StringView classNamespace, StringView className, MonoImage* image) :
base(classNamespace, className)
{
_monoClass = Mono.mono_class_from_name(image, _namespace, _className);
Log.EngineLogger.AssertDebug(_monoClass != null);
}
internal this(MonoClass* monoClass) :
base(StringView(Mono.mono_class_get_namespace(monoClass)), StringView(Mono.mono_class_get_name(monoClass)))
{
_monoClass = monoClass;
_monoClass = Mono.mono_class_from_name(image, _namespace.ToScopeCStr!(), _className.ToScopeCStr!());
Log.EngineLogger.AssertDebug(_monoClass != null);
}
+12 -24
View File
@@ -20,7 +20,7 @@ class EngineClasses
private ScriptClass s_EntityRoot ~ _?.ReleaseRef();
private ScriptClass s_EngineObject ~ _?.ReleaseRef();
private ScriptClass s_EntityEditor ~ _?.ReleaseRef();
private EntityEditorWrapper s_EntityEditor ~ _?.ReleaseRef();
private ScriptClass s_EntitySerializer ~ _?.ReleaseRef();
private ScriptClass s_SerializationContext ~ _?.ReleaseRef();
@@ -33,7 +33,7 @@ class EngineClasses
public ScriptClass EntityRoot => s_EntityRoot;
public ScriptClass EngineObject => s_EngineObject;
public ScriptClass EntityEditor => s_EntityEditor;
public EntityEditorWrapper EntityEditor => s_EntityEditor;
public ScriptClass EntitySerializer => s_EntitySerializer;
@@ -61,19 +61,19 @@ class EngineClasses
{
ReleaseAndNullify();
s_EngineObject = new ScriptClass("GlitchyEngine.Core", "EngineObject", image, .Class);
s_EntityRoot = new ScriptClass("GlitchyEngine", "Entity", image, .Entity);
s_ComponentRoot = new ScriptClass("GlitchyEngine.Core", "Component", image, .Component);
s_EngineObject = new ScriptClass("GlitchyEngine.Core", "EngineObject", image);
s_EntityRoot = new ScriptClass("GlitchyEngine", "Entity", image);
s_ComponentRoot = new ScriptClass("GlitchyEngine.Core", "Component", image);
// Editor classes
s_EntityEditor = new ScriptClass("GlitchyEngine.Editor", "EntityEditor", image, .Class);
s_EntityEditor = new EntityEditorWrapper("GlitchyEngine.Editor", "EntityEditor", image);
s_EntitySerializer = new ScriptClass("GlitchyEngine.Serialization", "EntitySerializer", image, .Class);
s_EntitySerializer = new ScriptClass("GlitchyEngine.Serialization", "EntitySerializer", image);
s_Collision2D = new ScriptClass("GlitchyEngine.Physics", "Collision2D", image, .Struct);
s_Collision2D = new ScriptClass("GlitchyEngine.Physics", "Collision2D", image);
// Attributes
s_RunInEditModeAttribute = new ScriptClass("GlitchyEngine.Editor", "RunInEditModeAttribute", image, .Struct);
s_RunInEditModeAttribute = new ScriptClass("GlitchyEngine.Editor", "RunInEditModeAttribute", image);
}
}
@@ -116,7 +116,7 @@ static class ScriptEngine
private static FileSystemWatcher _userAssemblyWatcher ~ delete _;
// TODO: This should be a global setting somewhere
private static bool _debuggingEnabled = true;
private static bool _debuggingEnabled = false;
private static String _appAssemblyPath = new .() ~ delete _;
@@ -471,7 +471,7 @@ static class ScriptEngine
// Check if it is an entity
if (monoClass != null && Mono.mono_class_is_subclass_of(monoClass, Classes.EntityRoot.[Friend]_monoClass, false))
{
ScriptClass entityScript = new ScriptClass(StringView(nameSpace), StringView(name), s_AppAssemblyImage, .Entity);
ScriptClass entityScript = new ScriptClass(StringView(nameSpace), StringView(name), s_AppAssemblyImage);
_entityScripts.Add(entityScript.FullName, entityScript);
Log.EngineLogger.Info($"Added entity \"{entityScript.FullName}\"");
@@ -580,19 +580,7 @@ static class ScriptEngine
if (scriptComponent.Instance == null)
return;
ScriptClass scriptClass = ScriptEngine.GetScriptClass(scriptComponent.ScriptClassName);
if (scriptClass == null)
return;
let entityId = entity.UUID;
#unwarn
void*[1] args = .(&entityId);
let method = Classes.EntityEditor.GetMethod("ShowDefaultEntityEditor", 1);
Classes.EntityEditor.Invoke(method, null, &args);
Classes.EntityEditor.ShowEntityEditor(scriptComponent.Instance, entity.UUID);
}
/// Serializes all script instances
+9 -8
View File
@@ -146,16 +146,17 @@ internal class EntityEditor
return false;
}
public static void ShowDefaultEntityEditor(UUID entityId)
/// <summary>
/// Entry point for the engine to show the editor for the given script instance.
/// </summary>
/// <param name="scriptInstance">The script instance to show the editor for.</param>
internal static void ShowEntityEditor(Entity? scriptInstance)
{
if (scriptInstance == null)
return;
// TODO: Call custom editors here!
ScriptGlue.Entity_GetScriptInstance(entityId, out object? instance);
if (instance != null)
{
ShowEditor(instance.GetType(), instance);
}
ShowEditor(scriptInstance.GetType(), scriptInstance);
}
private static T? GetAttribute<T>(IEnumerable<Attribute>? attributes) where T : Attribute