ScriptEngine refactoring

This commit is contained in:
Simon Lübeß
2024-03-31 12:36:22 +02:00
parent abcacc84a9
commit 777f52ab49
4 changed files with 13 additions and 48 deletions
+7 -25
View File
@@ -13,19 +13,16 @@ abstract class SharpType : RefCounter
protected String _namespace ~ delete _; protected String _namespace ~ delete _;
protected String _className ~ delete _; protected String _className ~ delete _;
protected String _fullName ~ delete _; protected String _fullName ~ delete _;
protected ScriptFieldType _scriptType;
public StringView Namespace => _namespace; public StringView Namespace => _namespace;
public StringView ClassName => _className; public StringView ClassName => _className;
public StringView FullName => _fullName; public StringView FullName => _fullName;
public ScriptFieldType ScriptType => _scriptType;
public this(StringView classNamespace, StringView className, ScriptFieldType scriptType) public this(StringView classNamespace, StringView className)
{ {
_namespace = new String(classNamespace); _namespace = new String(classNamespace);
_className = new String(className); _className = new String(className);
_fullName = new $"{_namespace}.{_className}"; _fullName = new $"{_namespace}.{_className}";
_scriptType = scriptType;
} }
} }
@@ -33,36 +30,21 @@ class SharpClass : SharpType
{ {
protected internal MonoClass* _monoClass; protected internal MonoClass* _monoClass;
public this(StringView classNamespace, StringView className, MonoImage* image, ScriptFieldType fieldType = .Class) : public this(StringView classNamespace, StringView className, MonoImage* image) :
base(classNamespace, className, fieldType) base(classNamespace, className)
{ {
_monoClass = Mono.mono_class_from_name(image, _namespace, _className); _monoClass = Mono.mono_class_from_name(image, _namespace, _className);
Log.EngineLogger.AssertDebug(_monoClass != null); Log.EngineLogger.AssertDebug(_monoClass != null);
} }
internal this(MonoClass* monoClass, ScriptFieldType fieldType = .Class) : internal this(MonoClass* monoClass) :
base(StringView(Mono.mono_class_get_namespace(monoClass)), StringView(Mono.mono_class_get_name(monoClass)), fieldType) base(StringView(Mono.mono_class_get_namespace(monoClass)), StringView(Mono.mono_class_get_name(monoClass)))
{ {
_monoClass = monoClass; _monoClass = monoClass;
Log.EngineLogger.AssertDebug(_monoClass != null); Log.EngineLogger.AssertDebug(_monoClass != null);
} }
internal MonoType* GetMonoType()
{
return Mono.mono_class_get_type(_monoClass);
}
internal bool IsType(MonoType* type)
{
return GetMonoType() == type;
}
internal bool IsSubclass(MonoClass* @class)
{
return Mono.mono_class_is_subclass_of(_monoClass, @class, false);
}
} }
class ScriptClass : SharpClass class ScriptClass : SharpClass
@@ -88,8 +70,8 @@ class ScriptClass : SharpClass
public bool RunInEditMode => _runInEditMode; public bool RunInEditMode => _runInEditMode;
[AllowAppend] [AllowAppend]
public this(StringView classNamespace, StringView className, MonoImage* image, ScriptFieldType scriptFieldType = .Class) : public this(StringView classNamespace, StringView className, MonoImage* image) :
base(classNamespace, className, image, scriptFieldType) base(classNamespace, className, image)
{ {
_constructor = FindConstructor(); _constructor = FindConstructor();
_onCreate = (OnCreateMethod)GetMethodThunk("OnCreate"); _onCreate = (OnCreateMethod)GetMethodThunk("OnCreate");
+2 -5
View File
@@ -585,15 +585,12 @@ static class ScriptEngine
if (scriptClass == null) if (scriptClass == null)
return; return;
let monoType = scriptClass.GetMonoType();
let monoReflectionType = Mono.mono_type_get_object(s_AppDomain, monoType);
let entityId = entity.UUID; let entityId = entity.UUID;
#unwarn #unwarn
void*[2] args = .(&entityId, monoReflectionType); void*[1] args = .(&entityId);
let method = Classes.EntityEditor.GetMethod("ShowDefaultEntityEditor", 2); let method = Classes.EntityEditor.GetMethod("ShowDefaultEntityEditor", 1);
Classes.EntityEditor.Invoke(method, null, &args); Classes.EntityEditor.Invoke(method, null, &args);
} }
@@ -1,16 +0,0 @@
using System;
using GlitchyEngine.Core;
using GlitchyEngine.Math;
namespace GlitchyEngine.Scripting;
enum ScriptFieldType
{
case None;
case Class;
case Struct;
case Entity;
case Component;
}
+4 -2
View File
@@ -146,14 +146,16 @@ internal class EntityEditor
return false; return false;
} }
public static void ShowDefaultEntityEditor(UUID entityId, Type entityType) public static void ShowDefaultEntityEditor(UUID entityId)
{ {
// TODO: Call custom editors here! // TODO: Call custom editors here!
ScriptGlue.Entity_GetScriptInstance(entityId, out object? instance); ScriptGlue.Entity_GetScriptInstance(entityId, out object? instance);
if (instance != null) if (instance != null)
ShowEditor(entityType, instance); {
ShowEditor(instance.GetType(), instance);
}
} }
private static T? GetAttribute<T>(IEnumerable<Attribute>? attributes) where T : Attribute private static T? GetAttribute<T>(IEnumerable<Attribute>? attributes) where T : Attribute