Serialize/Deserialize simple classes

This commit is contained in:
Simon Lübeß
2023-12-25 20:08:48 +01:00
parent 7bd69c162f
commit acefbb58bd
5 changed files with 168 additions and 72 deletions
+1 -1
View File
@@ -857,7 +857,7 @@ static class ScriptEngine
for (let (id, script) in _entityScriptInstances)
{
SerializedObject object = new SerializedObject(allObjects, script.EntityId);
SerializedObject object = new SerializedObject(allObjects, script.ScriptClass.FullName, script.EntityId);
object.Serialize(script);
}
}
+31 -2
View File
@@ -550,13 +550,17 @@ static class ScriptGlue
}
[RegisterCall("ScriptGlue::Serialization_CreateObject")]
static void Serialization_CreateObject(void* currentContext, out void* newContext, out UUID newId)
static void Serialization_CreateObject(void* currentContext, MonoString* typeName, out void* newContext, out UUID newId)
{
SerializedObject context = Internal.UnsafeCastToObject(currentContext) as SerializedObject;
Log.EngineLogger.AssertDebug(context != null);
char8* rawTypeName = Mono.mono_string_to_utf8(typeName);
SerializedObject newObject = new SerializedObject(context.AllObjects, StringView(rawTypeName));
SerializedObject newObject = new SerializedObject(context.AllObjects);
Mono.mono_free(rawTypeName);
newContext = Internal.UnsafeCastToPtr(newObject);
newId = newObject.Id;
@@ -575,6 +579,31 @@ static class ScriptGlue
Mono.mono_free(name);
}
[RegisterCall("ScriptGlue::Serialization_GetObject")]
public static void Serialization_GetObject(void* internalContext, UUID id, out void* objectContext)
{
SerializedObject context = Internal.UnsafeCastToObject(internalContext) as SerializedObject;
Log.EngineLogger.AssertDebug(context != null);
objectContext = null;
if (!context.AllObjects.TryGetValue(id, let foundObject))
return;
objectContext = Internal.UnsafeCastToPtr(foundObject);
}
[RegisterCall("ScriptGlue::Serialization_GetObjectTypeName")]
public static void Serialization_GetObjectTypeName(void* internalContext, out MonoString* fullTypeName)
{
SerializedObject context = Internal.UnsafeCastToObject(internalContext) as SerializedObject;
Log.EngineLogger.AssertDebug(context != null);
fullTypeName = Mono.mono_string_new(ScriptEngine.[Friend]s_AppDomain, context.TypeName);
}
#endregion
@@ -70,16 +70,24 @@ public enum SerializationType : int32
class SerializedObject
{
/// ID used to identify the object represented by this SerializedObject. In case that the represented object is a
/// Script Instance, the ID is the UUID of the Entity, random otherwise.
/// Todo: This sucks because we don't know all UUIDs beforehand and might accidentally assign the ID of an Entity to some class
public UUID Id;
public String TypeName ~ delete:append _;
public Dictionary<UUID, SerializedObject> AllObjects;
private List<String> _ownedString = new List<String>() ~ DeleteContainerAndItems!(_);
public append Dictionary<StringView, (SerializationType PrimitiveType, uint8[16] Data)> Fields = .();
public this(Dictionary<UUID, SerializedObject> allObjects, UUID? id = null)
[AllowAppend]
public this(Dictionary<UUID, SerializedObject> allObjects, StringView? typeName, UUID? id = null)
{
String typeNameCopy = append String(typeName.Value);
AllObjects = allObjects;
if (id == null)
@@ -97,6 +105,8 @@ class SerializedObject
}
AllObjects.Add(Id, this);
TypeName = typeNameCopy;
}
public void AddField(StringView name, SerializationType primitiveType, MonoObject* value)