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
@@ -459,6 +459,14 @@ namespace GlitchyEditor.EditWindows
ImGui.EndCombo();
}
if (scriptComponent.Instance?.ScriptClass != null)
{
for (let (fieldName, scriptField) in scriptComponent.Instance?.ScriptClass.Fields)
{
ImGui.TextUnformatted(fieldName);
}
}
}
private static void LabelColumn(StringView label)
+2 -1
View File
@@ -93,7 +93,6 @@ namespace GlitchyEngine
SamplerStateManager.Uninit();
ScriptEngine.Shutdown();
Renderer.Deinit();
delete _contentManager;
@@ -103,6 +102,8 @@ namespace GlitchyEngine
delete _gameTime;
delete _layerStack;
ScriptEngine.Shutdown();
}
public void OnEvent(Event e)
@@ -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)
@@ -129,11 +129,23 @@ 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 = .();
@@ -142,6 +154,33 @@ static class ScriptEngine
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]);
/*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))
+27 -3
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,7 +147,7 @@ static class ScriptGlue
Entity entity = ScriptEngine.Context.GetEntityByID(entityId);
if (s_AddComponentMethods.TryGetValue(type, let addMethod))
addMethod(entity);
else
Log.EngineLogger.AssertDebug(false, "No managed component with the given type registered.");
}
@@ -173,7 +173,7 @@ static class ScriptGlue
Entity entity = ScriptEngine.Context.GetEntityByID(entityId);
if (s_RemoveComponentMethods.TryGetValue(type, let removeMethod))
removeMethod(entity);
else
Log.EngineLogger.AssertDebug(false, "No managed component with the given type registered.");
}
/*[RegisterCall("Input::IsMouseButtonReleasing")]
@@ -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);
+17 -4
View File
@@ -46,6 +46,15 @@ namespace GlitchyEngine.World
cameraComponent.Camera.SetViewportSize(e.Scene._viewportWidth, e.Scene._viewportHeight);
});
/*_onComponentAddedHandlers.Add(typeof(Rigidbody2DComponent), (e, t, c) => {
if ()
Rigidbody2DComponent* rigidBodyComponent = (.)c;
//cameraComponent.Camera.SetViewportSize(e.Scene._viewportWidth, e.Scene._viewportHeight);
});*/
}
public ~this()
@@ -155,12 +164,17 @@ namespace GlitchyEngine.World
{
_physicsWorld2D = Box2D.World.Create(ref _gravity2D);
for (var entry in _ecsWorld.Enumerate<Rigidbody2DComponent>())
for (let (ecsHandle, rigidBody) in _ecsWorld.Enumerate<Rigidbody2DComponent>())
{
Entity entity = .(entry.Entity, this);
Entity entity = .(ecsHandle, this);
InitializeRigidbody2D(entity, rigidBody);
}
}
private void InitializeRigidbody2D(Entity entity, Rigidbody2DComponent* rigidBody)
{
var transform = entity.Transform;
var rigidBody = entry.Component;
b2BodyDef def = .();
def.type = GetBox2DBodyType(rigidBody.BodyType);
@@ -208,7 +222,6 @@ namespace GlitchyEngine.World
circleCollider.RuntimeFixture = fixture;
}
}
}
public void OnSimulationStop()
{
+19 -1
View File
@@ -58,7 +58,11 @@ static class Mono
int32 res_size);
[LinkName(.C)]
public static extern char8* mono_metadata_string_heap(MonoImage *meta, uint32 table_index);
public static extern char8* mono_metadata_string_heap(MonoImage* meta, uint32 table_index);
[LinkName(.C)]
public static extern uint8* mono_metadata_blob_heap(MonoImage* meta, uint32 index);
[LinkName(.C)]
public static extern uint32 mono_metadata_decode_blob_size(uint8* xptr, out uint8* newPosition);
typealias gconstpointer = void*;
typealias gpointer = void*;
@@ -129,6 +133,12 @@ static class Mono
[LinkName(.C)]
public static extern MonoType* mono_reflection_type_get_type(MonoReflectionType* reflectionType);
[LinkName(.C)]
public static extern MonoClassField* mono_class_get_fields(MonoClass* klass, gpointer* iter);
[LinkName(.C)]
public static extern char8* mono_field_get_name(MonoClassField* field);
}
struct MonoDomain;
@@ -173,6 +183,14 @@ enum SOME_RANDOM_ENUM{
MONO_TYPEDEF_SIZE
}
enum FIELD_TABLE_FLAGS : uint32
{
MONO_FIELD_FLAGS,
MONO_FIELD_NAME,
MONO_FIELD_SIGNATURE,
MONO_FIELD_SIZE
}
enum MonoMetaTableEnum : int32
{
MONO_TABLE_MODULE,
+64
View File
@@ -1,4 +1,5 @@
using System;
using System.Runtime.CompilerServices;
namespace GlitchyEngine.Math;
@@ -50,4 +51,67 @@ public struct Vector2
}
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 operator +(in Vector2 a, in Vector2 b) => new(a.X + b.X, a.Y + b.Y);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 operator +(float a, in Vector2 b) => new(a + b.X, a + b.Y);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 operator +(in Vector2 a, float b) => new(a.X + b, a.Y + b);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 operator +(in Vector2 a) => a;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 operator -(in Vector2 a, in Vector2 b) => new(a.X - b.X, a.Y - b.Y);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 operator -(float a, in Vector2 b) => new(a - b.X, a - b.Y);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 operator -(in Vector2 a, float b) => new(a.X - b, a.Y - b);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 operator -(in Vector2 a) => new Vector2(-a.X, -a.Y);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 operator *(in Vector2 a, in Vector2 b) => new(a.X * b.X, a.Y * b.Y);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 operator *(float a, in Vector2 b) => new(a * b.X, a * b.Y);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 operator *(in Vector2 a, float b) => new(a.X * b, a.Y * b);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 operator /(in Vector2 a, in Vector2 b) => new(a.X / b.X, a.Y / b.Y);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 operator /(float a, in Vector2 b) => new(a / b.X, a / b.Y);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 operator /(in Vector2 a, float b) => new(a.X / b, a.Y / b);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Vector2 a, Vector2 b)
{
float diffX = a.X - b.X;
float diffY = a.Y - b.Y;
return diffX * diffX + diffY * diffY < 0.00001f;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Vector2 a, Vector2 b)
{
return !(a == b);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = X.GetHashCode();
hashCode = (hashCode * 397) ^ Y.GetHashCode();
return hashCode;
}
}
public override string ToString()
{
return $"X:{X}, Y:{Y}";
}
}
+22 -30
View File
@@ -14,47 +14,39 @@ class MyTestEntity : Entity
{
Log.Info($"Create! {UUID}");
//_rigidBody = GetComponent<RigidBody2D>();
RemoveComponent<RigidBody2D>();
_rigidBody ??= GetComponent<RigidBody2D>() ?? AddComponent<RigidBody2D>();
}
///// <summary>
///// Called after the entity was created completely.
///// </summary>
//void OnInstantiate()
//{
// Log.Warning($"Instantiated!");
//}
/// <summary>
/// Called every frame.
/// </summary>
/// <param name="deltaTime"></param>
void OnUpdate(float deltaTime)
{
//Vector2 force = Vector2.Zero;
Vector2 force = Vector2.Zero;
//if (Input.IsKeyPressed(Key.A))
//{
// force.X -= 1000 * deltaTime;
//}
//if (Input.IsKeyPressed(Key.D))
//{
// force.X += 1000 * deltaTime;
//}
//if (Input.IsKeyPressing(Key.Space))
//{
// force.Y += 2000;
//}
//_rigidBody.ApplyForceToCenter(force);
if (Input.IsMouseButtonReleasing(MouseButton.LeftButton))
if (Input.IsKeyPressed(Key.A))
{
Log.Info("Ouha!");
force.X -= 1000 * deltaTime;
}
if (Input.IsKeyPressed(Key.D))
{
force.X += 1000 * deltaTime;
}
if (Input.IsKeyPressing(Key.Space))
{
force.Y += 2000;
}
_rigidBody.ApplyForceToCenter(force);
//if (Input.IsMouseButtonReleasing(MouseButton.MiddleButton))
//{
// Log.Info("Ouha!");
// Physics2D.Gravity *= new Vector2(1, -1);
//}
}
/// <summary>
+22
View File
@@ -0,0 +1,22 @@
using GlitchyEngine.Math;
namespace GlitchyEngine;
/// <summary>
/// Allows changing the properties of the 2D physics simulation in the current scene.
/// </summary>
public static class Physics2D
{
/// <summary>
/// Gets or sets the gravity of the current scene.
/// </summary>
public static Vector2 Gravity
{
get
{
ScriptGlue.Physics2D_GetGravity(out Vector2 gravity);
return gravity;
}
set => ScriptGlue.Physics2D_SetGravity(in value);
}
}
+1
View File
@@ -57,6 +57,7 @@
<Compile Include="Math\Vector3.cs" />
<Compile Include="MouseButton.cs" />
<Compile Include="MyTestEntity.cs" />
<Compile Include="Physics2D.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ScriptGlue.cs" />
</ItemGroup>
+10
View File
@@ -43,4 +43,14 @@ internal static class ScriptGlue
#endregion RigidBody2D
#region Physics2D
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Physics2D_GetGravity(out Vector2 gravity);
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void Physics2D_SetGravity(in Vector2 gravity);
#endregion Physics2D
}