Wer weiß, was ich alles so gemacht hatte...

- Fixed crash caused by early ScriptEngine shutdown
This commit is contained in:
Simon Lübeß
2023-05-30 16:50:22 +02:00
parent ef5e4ba64f
commit 5855b13873
12 changed files with 310 additions and 80 deletions
@@ -1,9 +1,24 @@
using Mono;
using System;
using GlitchyEngine.Core;
using System.Collections;
namespace GlitchyEngine.Scripting;
using internal GlitchyEngine.Scripting;
public struct ScriptField
{
public StringView Name;
internal MonoClassField* _monoField;
internal this(StringView name, MonoClassField* monoField)
{
Name = name;
_monoField = monoField;
}
}
class ScriptClass : RefCounter
{
private String _namespace ~ delete _;
@@ -12,6 +27,9 @@ class ScriptClass : RefCounter
private MonoClass* _monoClass;
//private List<MonoClassField*> _monoFields ~ delete _;
private Dictionary<StringView, ScriptField> _monoFields ~ delete _;
//typealias ConstructorMethod = function void(MonoObject* instance, UUID uuid, MonoException** exception);
typealias OnCreateMethod = function MonoObject*(MonoObject* instance, MonoException** exception);
typealias OnUpdateMethod = function MonoObject*(MonoObject* instance, float deltaTime, MonoException** exception);
@@ -27,6 +45,8 @@ class ScriptClass : RefCounter
public StringView ClassName => _className;
public StringView FullName => _fullName;
public Dictionary<StringView, ScriptField> Fields => _monoFields;
[AllowAppend]
public this(StringView classNamespace, StringView className)
{
@@ -41,6 +61,24 @@ class ScriptClass : RefCounter
_onCreate = (OnCreateMethod)GetMethodThunk("OnCreate");
_onUpdate = (OnUpdateMethod)GetMethodThunk("OnUpdate", 1);
_onDestroy = (OnDestroyMethod)GetMethodThunk("OnDestroy");
ExtractFields();
}
private void ExtractFields()
{
//_monoFields = new List<MonoClassField*>();´
_monoFields = new Dictionary<StringView, ScriptField>();
void* iterator = null;
MonoClassField* currentField = null;
while ((currentField = Mono.mono_class_get_fields(_monoClass, &iterator)) != null)
{
// TODO: does the pointer from mono really never move?
StringView name = StringView(Mono.mono_field_get_name(currentField));
_monoFields[name] = .(name, currentField);
}
}
public void OnCreate(MonoObject* instance)
+40 -1
View File
@@ -129,19 +129,58 @@ static class ScriptEngine
MonoTableInfo* typeDefinitionsTable = Mono.mono_image_get_table_info(image, .MONO_TABLE_TYPEDEF);
int32 numTypes = Mono.mono_table_info_get_rows(typeDefinitionsTable);
/*MonoTableInfo* fieldsTable = Mono.mono_image_get_table_info(image, .MONO_TABLE_FIELD);
int32 numFields = Mono.mono_table_info_get_rows(fieldsTable);*/
s_EngineObject = new ScriptClass("GlitchyEngine.Core", "EngineObject");
s_EntityRoot = new ScriptClass("GlitchyEngine", "Entity");
Log.EngineLogger.Assert(s_EntityRoot != null);
/*for (int32 fieldIndex < numFields)
{
uint32[(.)FIELD_TABLE_FLAGS.MONO_FIELD_SIZE] fieldCols = .();
Mono.mono_metadata_decode_row(fieldsTable, fieldIndex, (.)&fieldCols, (.)FIELD_TABLE_FLAGS.MONO_FIELD_SIZE);
char8* fieldName = Mono.mono_metadata_string_heap(image, (.)fieldCols[(.)FIELD_TABLE_FLAGS.MONO_FIELD_NAME]);
Log.EngineLogger.Info($"{StringView(fieldName)}");
}*/
for (int32 i = 0; i < numTypes; i++)
{
int32[(.)SOME_RANDOM_ENUM.MONO_TYPEDEF_SIZE] cols = .();
Mono.mono_metadata_decode_row(typeDefinitionsTable, i, (.)&cols, (.)SOME_RANDOM_ENUM.MONO_TYPEDEF_SIZE);
char8* nameSpace = Mono.mono_metadata_string_heap(image, (.)cols[(.)SOME_RANDOM_ENUM.MONO_TYPEDEF_NAMESPACE]);
char8* name = Mono.mono_metadata_string_heap(image, (.)cols[(.)SOME_RANDOM_ENUM.MONO_TYPEDEF_NAME]);
char8* name = Mono.mono_metadata_string_heap(image, (.)cols[(.)SOME_RANDOM_ENUM.MONO_TYPEDEF_NAME]);
/*int32 firstFieldIndex = cols[(.)SOME_RANDOM_ENUM.MONO_TYPEDEF_FIELD_LIST] - 1;
if (firstFieldIndex >= 0)
{
int32 lastFieldIndex;
if ((i + 1) < numTypes)
{
int32[(.)SOME_RANDOM_ENUM.MONO_TYPEDEF_SIZE] nextCols = .();
Mono.mono_metadata_decode_row(typeDefinitionsTable, i + 1, (.)&nextCols, (.)SOME_RANDOM_ENUM.MONO_TYPEDEF_SIZE);
lastFieldIndex = nextCols[(.)SOME_RANDOM_ENUM.MONO_TYPEDEF_FIELD_LIST] - 1;
}
else
{
lastFieldIndex = numFields;
}
for (int32 fieldIndex = firstFieldIndex; fieldIndex < lastFieldIndex; fieldIndex++)
{
uint32[(.)FIELD_TABLE_FLAGS.MONO_FIELD_SIZE] fieldCols = .();
Mono.mono_metadata_decode_row(fieldsTable, fieldIndex, (.)&fieldCols, (.)FIELD_TABLE_FLAGS.MONO_FIELD_SIZE);
char8* fieldName = Mono.mono_metadata_string_heap(image, (.)fieldCols[(.)FIELD_TABLE_FLAGS.MONO_FIELD_NAME]);
Log.EngineLogger.Info($"{StringView(nameSpace)}.{StringView(name)}::{StringView(fieldName)}");
//char8* fieldSignature = Mono.mono_metadata_blob_heap(image, (.)fieldCols[(.)FIELD_TABLE_FLAGS.MONO_FIELD_SIGNATURE]);
}
}*/
MonoClass* monoClass = Mono.mono_class_from_name(image, nameSpace, name);
if (monoClass != null && Mono.mono_class_is_subclass_of(monoClass, s_EntityRoot.[Friend]_monoClass, false))
+29 -5
View File
@@ -7,6 +7,7 @@ using GlitchyEngine.Math;
using GlitchyEngine.World;
using GlitchyEngine.Core;
using System.Collections;
using Box2D;
namespace GlitchyEngine.Scripting;
@@ -94,7 +95,6 @@ static class ScriptGlue
}
}
[RegisterCall("Log::LogMessage_Impl")]
static void Log(int32 logLevel, MonoString* message)
{
@@ -147,8 +147,8 @@ static class ScriptGlue
Entity entity = ScriptEngine.Context.GetEntityByID(entityId);
if (s_AddComponentMethods.TryGetValue(type, let addMethod))
addMethod(entity);
Log.EngineLogger.AssertDebug(false, "No managed component with the given type registered.");
else
Log.EngineLogger.AssertDebug(false, "No managed component with the given type registered.");
}
[RegisterCall("ScriptGlue::Entity_HasComponent")]
@@ -173,8 +173,8 @@ static class ScriptGlue
Entity entity = ScriptEngine.Context.GetEntityByID(entityId);
if (s_RemoveComponentMethods.TryGetValue(type, let removeMethod))
removeMethod(entity);
Log.EngineLogger.AssertDebug(false, "No managed component with the given type registered.");
else
Log.EngineLogger.AssertDebug(false, "No managed component with the given type registered.");
}
/*[RegisterCall("Input::IsMouseButtonReleasing")]
static void Destroy()
@@ -230,6 +230,30 @@ static class ScriptGlue
#endregion RigidBody2D
#region Physics2D
// TODO: We need a wrapper class!
[RegisterCall("ScriptGlue::Physics2D_GetGravity")]
static void Physics2D_GetGravity(ref Vector2 gravity)
{
Scene scene = ScriptEngine.Context;
var box2DGravity = Box2D.World.GetGravity(scene.[Friend]_physicsWorld2D);
gravity = *(Vector2*)&box2DGravity;
}
[RegisterCall("ScriptGlue::Physics2D_SetGravity")]
static void Physics2D_SetGravity(ref Vector2 gravity)
{
Scene scene = ScriptEngine.Context;
#unwarn
Box2D.World.SetGravity(scene.[Friend]_physicsWorld2D, ref *(b2Vec2*)&gravity);
}
#endregion Physics2D
private static void RegisterCall<T>(String name, T method) where T : var
{
Mono.mono_add_internal_call(scope $"GlitchyEngine.{name}", (void*)method);