Weiß ich nicht, ist Monate her...

This commit is contained in:
Simon Lübeß
2023-05-30 16:54:57 +02:00
parent 5855b13873
commit 6a59deb9e2
20 changed files with 1021 additions and 234 deletions
+14 -1
View File
@@ -1,5 +1,6 @@
using DirectX.Common;
using GlitchyEngine.Core;
using System.Collections;
namespace GlitchyEngine
{
@@ -20,7 +21,7 @@ namespace GlitchyEngine
value?.ReleaseRef();
value = null;
}
public static mixin DeleteContainerAndReleaseItems(var container)
{
if (container != null)
@@ -37,5 +38,17 @@ namespace GlitchyEngine
value?.ReleaseRef();
container.Clear();
}
public static mixin DeleteDictionaryAndReleaseValues(var container)
{
if (container != null)
{
for (var value in container)
{
value.value?.ReleaseRef();
}
delete container;
}
}
}
}
+121 -33
View File
@@ -11,56 +11,55 @@ public struct ScriptField
{
public StringView Name;
internal MonoClassField* _monoField;
public bool IsStatic;
public ScriptFieldType FieldType;
internal this(StringView name, MonoClassField* monoField)
internal this(StringView name, MonoClassField* monoField, bool isStatic, ScriptFieldType fieldType)
{
Name = name;
_monoField = monoField;
IsStatic = isStatic;
FieldType = fieldType;
}
}
class ScriptClass : RefCounter
abstract class SharpType : RefCounter
{
private String _namespace ~ delete _;
private String _className ~ delete _;
private String _fullName ~ delete _;
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);
typealias OnDestroyMethod = function MonoObject*(MonoObject* instance, MonoException** exception);
private MonoMethod* _constructor;
//private ConstructorMethod _constructor;
private OnCreateMethod _onCreate;
private OnUpdateMethod _onUpdate;
private OnDestroyMethod _onDestroy;
protected String _namespace ~ delete _;
protected String _className ~ delete _;
protected String _fullName ~ delete _;
protected ScriptFieldType _scriptType;
protected Dictionary<StringView, ScriptField> _monoFields ~ delete _;
public StringView Namespace => _namespace;
public StringView ClassName => _className;
public StringView FullName => _fullName;
public ScriptFieldType ScriptType => _scriptType;
public Dictionary<StringView, ScriptField> Fields => _monoFields;
[AllowAppend]
public this(StringView classNamespace, StringView className)
public this(StringView classNamespace, StringView className, ScriptFieldType scriptType)
{
_namespace = new String(classNamespace);
_className = new String(className);
_fullName = new $"{_namespace}.{_className}";
_scriptType = scriptType;
ScriptEngine.RegisterSharpType(this);
}
}
_monoClass = Mono.mono_class_from_name(ScriptEngine.[Friend]s_CoreAssemblyImage, _namespace, _className);
class SharpClass : SharpType
{
protected internal MonoClass* _monoClass;
public this(StringView classNamespace, StringView className, MonoImage* image, ScriptFieldType fieldType = .Class) :
base(classNamespace, className, fieldType)
{
_monoClass = Mono.mono_class_from_name(image, _namespace, _className);
//_constructor = (ConstructorMethod)GetMethodThunk(".ctor", 1); // GetMethod(".ctor", 1);//
_constructor = GetMethod(".ctor", 1);
_onCreate = (OnCreateMethod)GetMethodThunk("OnCreate");
_onUpdate = (OnUpdateMethod)GetMethodThunk("OnUpdate", 1);
_onDestroy = (OnDestroyMethod)GetMethodThunk("OnDestroy");
Log.EngineLogger.AssertDebug(_monoClass != null);
ExtractFields();
}
@@ -74,12 +73,89 @@ class ScriptClass : RefCounter
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);
MonoType* type = Mono.mono_field_get_type(currentField);
ScriptFieldType fieldType = ScriptEngineHelper.GetScriptFieldType(type);
// If field type is none the field might be a struct, class or enum
if (fieldType == .None)
{
SharpType sharpType = ScriptEngine.GetSharpType(type);
fieldType = sharpType?.ScriptType ?? .None;
if (sharpType == null)
continue;
}
//Mono.MonoTypeEnum fieldType = Mono.Mono.mono_type_get_type(type);
FieldAttribute flags = (.)Mono.mono_field_get_flags(currentField);
MonoCustomAttrInfo* attributes = Mono.mono_custom_attrs_from_field(_monoClass, currentField);
if (flags.HasFlag(.Public) ||
(attributes != null &&
Mono.mono_custom_attrs_has_attr(attributes, ScriptEngine.Attributes.s_ShowInEditorAttribute)))
{
_monoFields[name] = .(name, currentField, flags.HasFlag(.Static), fieldType);
}
}
}
}
class SharpStruct : SharpClass
{
protected int _size;
public int Size => _size;
public this(StringView classNamespace, StringView className, MonoImage* image)
: base(classNamespace, className, image)
{
}
}
class ScriptClass : SharpClass
{
/*private String _namespace ~ delete _;
private String _className ~ delete _;
private String _fullName ~ delete _;
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);
typealias OnDestroyMethod = function MonoObject*(MonoObject* instance, MonoException** exception);
private MonoMethod* _constructor;
//private ConstructorMethod _constructor;
private OnCreateMethod _onCreate;
private OnUpdateMethod _onUpdate;
private OnDestroyMethod _onDestroy;
/*public StringView Namespace => _namespace;
public StringView ClassName => _className;
public StringView FullName => _fullName;
public Dictionary<StringView, ScriptField> Fields => _monoFields;*/
[AllowAppend]
public this(StringView classNamespace, StringView className, MonoImage* image) :
base(classNamespace, className, image)
{
//_constructor = (ConstructorMethod)GetMethodThunk(".ctor", 1); // GetMethod(".ctor", 1);//
_constructor = GetMethod(".ctor", 1);
_onCreate = (OnCreateMethod)GetMethodThunk("OnCreate");
_onUpdate = (OnUpdateMethod)GetMethodThunk("OnUpdate", 1);
_onDestroy = (OnDestroyMethod)GetMethodThunk("OnDestroy");
}
public void OnCreate(MonoObject* instance)
{
@@ -155,4 +231,16 @@ class ScriptClass : RefCounter
MonoObject* object = Invoke(method, instance, args.Ptr);
return *(T*)Mono.mono_object_unbox(object);
}
public T GetFieldValue<T>(MonoObject* instance, MonoClassField* field)
{
T value = default;
Mono.Mono.mono_field_get_value(instance, field, &value);
return value;
}
public void SetFieldValue<T>(MonoObject* instance, MonoClassField* field, in T value)
{
Mono.Mono.mono_field_set_value(instance, field, &value);
}
}
+157 -73
View File
@@ -8,6 +8,68 @@ using GlitchyEngine.Core;
namespace GlitchyEngine.Scripting;
using internal GlitchyEngine.Scripting;
enum ScriptFieldType
{
None,
Class,
Enum,
Struct,
// TODO!
Bool,
SByte,
Short,
Int,// Int2, Int3, Int4,
Long,
Byte,
UShort,
UInt, // UInt2, UInt3, UInt4,
ULong,
// Half, Half2, Half3, Half4,
Float, Vector2, Vector3, Vector4,
Double, // Double2, Double3, Double4,
Entity
}
static sealed class ScriptEngineHelper
{
private static Dictionary<StringView, ScriptFieldType> _scriptFieldTypes = new Dictionary<StringView, ScriptFieldType>()
{
("System.Boolean", .Bool),
("System.SByte", .SByte),
("System.Int16", .Short),
("System.Int32", .Int),
("System.Int64", .Long),
("System.Byte", .Byte),
("System.UInt16", .UShort),
("System.UInt32", .UInt),
("System.UInt64", .ULong),
("System.Single", .Float),
("GlitchyEngine.Math.Vector2", .Vector2),
("GlitchyEngine.Math.Vector3", .Vector3),
("GlitchyEngine.Math.Vector4", .Vector4),
("System.Double", .Double),
("GlitchyEngine.Entity", .Entity),
} ~ delete _;
public static ScriptFieldType GetScriptFieldType(MonoType* monoType)
{
StringView typeName = StringView(Mono.mono_type_get_name(monoType));
return _scriptFieldTypes.TryGetValue(typeName, .. let scriptFieldType);
}
}
static class ScriptEngine
{
private static MonoDomain* s_RootDomain;
@@ -16,31 +78,35 @@ static class ScriptEngine
private static MonoAssembly* s_CoreAssembly;
private static MonoImage* s_CoreAssemblyImage;
private static MonoAssembly* s_AppAssembly;
private static MonoImage* s_AppAssemblyImage;
private static Scene s_Context ~ _?.ReleaseRef();
private static ScriptClass s_EntityRoot ~ _?.ReleaseRef();
private static ScriptClass s_EngineObject ~ _?.ReleaseRef();
private static Dictionary<StringView, ScriptClass> _entityScripts = new .() ~ {
for (var entry in _)
{
entry.value.ReleaseRef();
}
delete _entityScripts;
};
private static Dictionary<UUID, ScriptInstance> _entityScriptInstances = new .() ~ {
private static Dictionary<StringView, SharpType> _sharpClasses = new .() ~ DeleteDictionaryAndReleaseValues!(_);
private static Dictionary<StringView, ScriptClass> _entityScripts = new .() ~ DeleteDictionaryAndReleaseValues!(_);
/*private static Dictionary<UUID, ScriptInstance> _entityScriptInstances = new .() ~ {
for (var entry in _)
{
entry.value?.ReleaseRef();
}
delete _;
};
};*/
public static Dictionary<StringView, ScriptClass> EntityClasses => _entityScripts;
public static Scene Context => s_Context;
internal static class Attributes
{
internal static MonoClass* s_ShowInEditorAttribute;
}
public static void Init()
{
Mono.mono_set_assemblies_path("mono/lib");
@@ -50,7 +116,11 @@ static class ScriptEngine
ScriptGlue.Init();
LoadAssembly("resources/scripts/ScriptCore.dll");
CreateAppDomain("GlitchyEngineScriptRuntime");
(s_CoreAssembly, s_CoreAssemblyImage) = LoadAssembly("resources/scripts/ScriptCore.dll");
(s_AppAssembly, s_AppAssemblyImage) = LoadAssembly("SandboxProject/Assets/Scripts/bin/Sandbox.dll");
GetEntitiesFromAssemblies();
ScriptGlue.RegisterManagedComponents();
@@ -64,18 +134,18 @@ static class ScriptEngine
public static void OnRuntimeStop()
{
for (var entry in _entityScriptInstances)
/*for (var entry in _entityScriptInstances)
{
entry.value.ReleaseRef();
}
_entityScriptInstances.Clear();
_entityScriptInstances.Clear();*/
SetContext(null);
}
public static void InitializeInstance(Entity entity, ScriptComponent* script)
{
_entityScriptInstances[entity.UUID] = script.Instance..AddRef();
//_entityScriptInstances[entity.UUID] = script.Instance..AddRef();
script.Instance.Instantiate(entity.UUID);
}
@@ -107,85 +177,54 @@ static class ScriptEngine
return assembly;
}
static void LoadAssembly(StringView filepath)
static void CreateAppDomain(StringView name)
{
s_AppDomain = Mono.mono_domain_create_appdomain("GlitchyEngineScriptRuntime", null);
s_AppDomain = Mono.mono_domain_create_appdomain(name.ToScopeCStr!(), null);
Mono.mono_domain_set(s_AppDomain, true);
s_CoreAssembly = LoadCSharpAssembly(filepath);
s_CoreAssemblyImage = Mono.mono_assembly_get_image(s_CoreAssembly);
GetEntitiesFromAssembly(s_CoreAssembly);
}
private static void GetEntitiesFromAssembly(MonoAssembly* assembly)
static (MonoAssembly* assembly, MonoImage* image) LoadAssembly(StringView filepath)
{
MonoAssembly* assembly = LoadCSharpAssembly(filepath);
MonoImage* image = Mono.mono_assembly_get_image(assembly);
return (assembly, image);
}
private static void GetEntitiesFromAssemblies()
{
for (var entry in _entityScripts)
{
entry.value.ReleaseRef();
}
_entityScripts.Clear();
MonoImage* image = Mono.mono_assembly_get_image(assembly);
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)
if (s_EngineObject == null)
{
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)}");
}*/
s_EngineObject = new ScriptClass("GlitchyEngine.Core", "EngineObject", s_CoreAssemblyImage);
s_EntityRoot = new ScriptClass("GlitchyEngine", "Entity", s_CoreAssemblyImage);
Log.EngineLogger.Assert(s_EntityRoot != null);
}
Attributes.s_ShowInEditorAttribute = Mono.mono_class_from_name(s_CoreAssemblyImage, "GlitchyEngine.Editor", "ShowInEditorAttribute");
MonoTableInfo* typeDefinitionsTable = Mono.mono_image_get_table_info(s_AppAssemblyImage, .MONO_TABLE_TYPEDEF);
int32 numTypes = Mono.mono_table_info_get_rows(typeDefinitionsTable);
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* nameSpace = Mono.mono_metadata_string_heap(s_AppAssemblyImage, (.)cols[(.)SOME_RANDOM_ENUM.MONO_TYPEDEF_NAMESPACE]);
char8* name = Mono.mono_metadata_string_heap(s_AppAssemblyImage, (.)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);
MonoClass* monoClass = Mono.mono_class_from_name(s_AppAssemblyImage, nameSpace, name);
if (monoClass != null && Mono.mono_class_is_subclass_of(monoClass, s_EntityRoot.[Friend]_monoClass, false))
{
ScriptClass entityScript = new ScriptClass(StringView(nameSpace), StringView(name));
ScriptClass entityScript = new ScriptClass(StringView(nameSpace), StringView(name), s_AppAssemblyImage);
_entityScripts.Add(entityScript.FullName, entityScript);
Log.EngineLogger.Info($"Added entity \"{entityScript.FullName}\"");
@@ -205,6 +244,51 @@ static class ScriptEngine
s_RootDomain = null;
}
internal static void RegisterSharpType(SharpType sharpType)
{
_sharpClasses.Add(sharpType.FullName, sharpType..AddRef());
}
internal static SharpType GetSharpType(MonoType* monoType)
{
StringView typeName = StringView(Mono.mono_type_get_name(monoType));
if (_sharpClasses.TryGetValue(typeName, let sharpType))
return sharpType;
Mono.MonoTypeEnum fieldType = Mono.Mono.mono_type_get_type(monoType);
MonoClass* monoClass = Mono.mono_type_get_class(monoType);
if (monoClass == null)
return null;
StringView className = StringView(Mono.mono_class_get_name(monoClass));
StringView classNamespace = StringView(Mono.mono_class_get_namespace(monoClass));
// TODO: at the moment only allow user-structs
if (classNamespace.StartsWith("GlitchyEngine"))
return null;
switch (fieldType)
{
case .Class, .Valuetype, .Enum:
ScriptFieldType scriptType = .None;
if (fieldType == .Class)
scriptType = .Class;
else if (fieldType == .Enum)
scriptType = .Enum;
else if (fieldType == .Valuetype)
scriptType = .Struct;
Log.EngineLogger.AssertDebug(scriptType != .None);
return new SharpClass(classNamespace, className, Mono.mono_class_get_image(monoClass), scriptType);
default:
return null;
}
}
@@ -1,5 +1,6 @@
using Mono;
using GlitchyEngine.Core;
using System;
namespace GlitchyEngine.Scripting;
@@ -50,4 +51,22 @@ class ScriptInstance : RefCounter
{
_scriptClass.OnDestroy(_instance);
}
public T GetFieldValue<T>(MonoClassField* field)
{
return _scriptClass.GetFieldValue<T>(_instance, field);
}
public void SetFieldValue<T>(MonoClassField* field, in T value)
{
_scriptClass.SetFieldValue<T>(_instance, field, value);
}
public void CopyEditorFieldsTo(ScriptInstance target)
{
for (let (fieldName, field) in ScriptClass.Fields)
{
}
}
}
+12 -4
View File
@@ -98,6 +98,8 @@ namespace GlitchyEngine.World
targetComponent.Instance = new ScriptInstance(sourceComponent.Instance.ScriptClass);
targetComponent.Instance..ReleaseRef();
sourceComponent.Instance.CopyEditorFieldsTo(targetComponent.Instance);
}
// Copy transforms
@@ -263,17 +265,23 @@ namespace GlitchyEngine.World
script.Instance.[Friend]OnUpdate(gameTime);
}
/*}
if (mode.HasFlag(.Runtime) || mode.HasFlag(.Editor))
{*/
// Run scripts
for (var (entity, script) in _ecsWorld.Enumerate<ScriptComponent>())
{
if (!script.InInstantiated)
{
ScriptEngine.InitializeInstance(Entity(entity, this), script);
script.Instance.InvokeOnCreate();
if (mode.HasFlag(.Runtime))
script.Instance.InvokeOnCreate();
}
script.Instance.InvokeOnUpdate(gameTime.DeltaTime);
if (mode.HasFlag(.Runtime))
script.Instance.InvokeOnUpdate(gameTime.DeltaTime);
}
}