mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 21:01:52 +00:00
Almost done refactoring ScriptEngine
This commit is contained in:
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,40 +8,29 @@ namespace GlitchyEngine.Scripting;
|
|||||||
|
|
||||||
using internal GlitchyEngine.Scripting;
|
using internal GlitchyEngine.Scripting;
|
||||||
|
|
||||||
abstract class SharpType : RefCounter
|
class SharpClass : RefCounter
|
||||||
{
|
{
|
||||||
protected String _namespace ~ delete _;
|
protected String _fullName ~ delete:append _;
|
||||||
protected String _className ~ delete _;
|
|
||||||
protected String _fullName ~ delete _;
|
protected StringView _namespace;
|
||||||
|
protected StringView _className;
|
||||||
|
|
||||||
|
protected internal MonoClass* _monoClass;
|
||||||
|
|
||||||
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 this(StringView classNamespace, StringView className)
|
[AllowAppend]
|
||||||
|
public this(StringView classNamespace, StringView className, MonoImage* image)
|
||||||
{
|
{
|
||||||
_namespace = new String(classNamespace);
|
String fullName = append $"{classNamespace}.{className}";
|
||||||
_className = new String(className);
|
|
||||||
_fullName = new $"{_namespace}.{_className}";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class SharpClass : SharpType
|
_fullName = fullName;
|
||||||
{
|
_namespace = _fullName.Substring(0, classNamespace.Length);
|
||||||
protected internal MonoClass* _monoClass;
|
_className = _fullName.Substring(classNamespace.Length + 1);
|
||||||
|
|
||||||
public this(StringView classNamespace, StringView className, MonoImage* image) :
|
_monoClass = Mono.mono_class_from_name(image, _namespace.ToScopeCStr!(), _className.ToScopeCStr!());
|
||||||
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;
|
|
||||||
|
|
||||||
Log.EngineLogger.AssertDebug(_monoClass != null);
|
Log.EngineLogger.AssertDebug(_monoClass != null);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ class EngineClasses
|
|||||||
private ScriptClass s_EntityRoot ~ _?.ReleaseRef();
|
private ScriptClass s_EntityRoot ~ _?.ReleaseRef();
|
||||||
private ScriptClass s_EngineObject ~ _?.ReleaseRef();
|
private ScriptClass s_EngineObject ~ _?.ReleaseRef();
|
||||||
|
|
||||||
private ScriptClass s_EntityEditor ~ _?.ReleaseRef();
|
private EntityEditorWrapper s_EntityEditor ~ _?.ReleaseRef();
|
||||||
|
|
||||||
private ScriptClass s_EntitySerializer ~ _?.ReleaseRef();
|
private ScriptClass s_EntitySerializer ~ _?.ReleaseRef();
|
||||||
private ScriptClass s_SerializationContext ~ _?.ReleaseRef();
|
private ScriptClass s_SerializationContext ~ _?.ReleaseRef();
|
||||||
@@ -33,7 +33,7 @@ class EngineClasses
|
|||||||
public ScriptClass EntityRoot => s_EntityRoot;
|
public ScriptClass EntityRoot => s_EntityRoot;
|
||||||
public ScriptClass EngineObject => s_EngineObject;
|
public ScriptClass EngineObject => s_EngineObject;
|
||||||
|
|
||||||
public ScriptClass EntityEditor => s_EntityEditor;
|
public EntityEditorWrapper EntityEditor => s_EntityEditor;
|
||||||
|
|
||||||
public ScriptClass EntitySerializer => s_EntitySerializer;
|
public ScriptClass EntitySerializer => s_EntitySerializer;
|
||||||
|
|
||||||
@@ -61,19 +61,19 @@ class EngineClasses
|
|||||||
{
|
{
|
||||||
ReleaseAndNullify();
|
ReleaseAndNullify();
|
||||||
|
|
||||||
s_EngineObject = new ScriptClass("GlitchyEngine.Core", "EngineObject", image, .Class);
|
s_EngineObject = new ScriptClass("GlitchyEngine.Core", "EngineObject", image);
|
||||||
s_EntityRoot = new ScriptClass("GlitchyEngine", "Entity", image, .Entity);
|
s_EntityRoot = new ScriptClass("GlitchyEngine", "Entity", image);
|
||||||
s_ComponentRoot = new ScriptClass("GlitchyEngine.Core", "Component", image, .Component);
|
s_ComponentRoot = new ScriptClass("GlitchyEngine.Core", "Component", image);
|
||||||
|
|
||||||
// Editor classes
|
// 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
|
// 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 _;
|
private static FileSystemWatcher _userAssemblyWatcher ~ delete _;
|
||||||
|
|
||||||
// TODO: This should be a global setting somewhere
|
// TODO: This should be a global setting somewhere
|
||||||
private static bool _debuggingEnabled = true;
|
private static bool _debuggingEnabled = false;
|
||||||
|
|
||||||
private static String _appAssemblyPath = new .() ~ delete _;
|
private static String _appAssemblyPath = new .() ~ delete _;
|
||||||
|
|
||||||
@@ -471,7 +471,7 @@ static class ScriptEngine
|
|||||||
// Check if it is an entity
|
// Check if it is an entity
|
||||||
if (monoClass != null && Mono.mono_class_is_subclass_of(monoClass, Classes.EntityRoot.[Friend]_monoClass, false))
|
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);
|
_entityScripts.Add(entityScript.FullName, entityScript);
|
||||||
|
|
||||||
Log.EngineLogger.Info($"Added entity \"{entityScript.FullName}\"");
|
Log.EngineLogger.Info($"Added entity \"{entityScript.FullName}\"");
|
||||||
@@ -580,19 +580,7 @@ static class ScriptEngine
|
|||||||
if (scriptComponent.Instance == null)
|
if (scriptComponent.Instance == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
ScriptClass scriptClass = ScriptEngine.GetScriptClass(scriptComponent.ScriptClassName);
|
Classes.EntityEditor.ShowEntityEditor(scriptComponent.Instance, entity.UUID);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Serializes all script instances
|
/// Serializes all script instances
|
||||||
|
|||||||
@@ -146,16 +146,17 @@ internal class EntityEditor
|
|||||||
return false;
|
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!
|
// TODO: Call custom editors here!
|
||||||
|
ShowEditor(scriptInstance.GetType(), scriptInstance);
|
||||||
ScriptGlue.Entity_GetScriptInstance(entityId, out object? instance);
|
|
||||||
|
|
||||||
if (instance != null)
|
|
||||||
{
|
|
||||||
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
|
||||||
|
|||||||
Reference in New Issue
Block a user