mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Start of Script Instance deserialization
- Also deserialize when stopping the game to restore pre play state - Fixed ancient bug where ClearDictionaryAndDeleteKeys falsly deletes the dictionary - Added ClearDictionaryAndDeleteValues helper mixin
This commit is contained in:
@@ -67,7 +67,17 @@ namespace GlitchyEngine
|
||||
{
|
||||
for (var value in dictionary)
|
||||
delete value.key;
|
||||
delete dictionary;
|
||||
dictionary.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
public static mixin ClearDictionaryAndDeleteValues(var dictionary)
|
||||
{
|
||||
if (dictionary != null)
|
||||
{
|
||||
for (var value in dictionary)
|
||||
delete value.value;
|
||||
dictionary.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -960,11 +960,7 @@ static class ScriptEngine
|
||||
if (script.ScriptClass.ClassName != "SerializationTest")
|
||||
continue;
|
||||
|
||||
SerializedObject object = new SerializedObject(allObjects);
|
||||
|
||||
object.Id = script.EntityId;
|
||||
object.AllObjects = allObjects;
|
||||
|
||||
SerializedObject object = new SerializedObject(allObjects, script.EntityId);
|
||||
object.Serialize(script);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -559,6 +559,20 @@ static class ScriptGlue
|
||||
newContext = Internal.UnsafeCastToPtr(newObject);
|
||||
newId = newObject.Id;
|
||||
}
|
||||
|
||||
[RegisterCall("ScriptGlue::Serialization_DeserializeField")]
|
||||
public static void Serialization_DeserializeField(void* internalContext, SerializationType expectedType, MonoString* fieldName, uint8* target)
|
||||
{
|
||||
SerializedObject context = Internal.UnsafeCastToObject(internalContext) as SerializedObject;
|
||||
|
||||
Log.EngineLogger.AssertDebug(context != null);
|
||||
|
||||
char8* name = Mono.mono_string_to_utf8(fieldName);
|
||||
|
||||
context.GetField(StringView(name), expectedType, target);
|
||||
|
||||
Mono.mono_free(name);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -58,6 +58,10 @@ public enum SerializationType : int32
|
||||
return 16;
|
||||
case .EntityReference, .ComponentReference, .ObjectReference:
|
||||
return sizeof(UUID);
|
||||
case .String:
|
||||
return sizeof(StringView);
|
||||
case .Enum:
|
||||
return sizeof(StringView);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
@@ -74,16 +78,23 @@ class SerializedObject
|
||||
|
||||
public append Dictionary<StringView, (SerializationType PrimitiveType, uint8[16] Data)> Fields = .();
|
||||
|
||||
public this(Dictionary<UUID, SerializedObject> allObjects)
|
||||
public this(Dictionary<UUID, SerializedObject> allObjects, UUID? id = null)
|
||||
{
|
||||
AllObjects = allObjects;
|
||||
|
||||
// Make sure we have no duplicate keys
|
||||
repeat
|
||||
if (id == null)
|
||||
{
|
||||
Id = UUID.Create();
|
||||
// Make sure we have no duplicate keys
|
||||
repeat
|
||||
{
|
||||
Id = UUID.Create();
|
||||
}
|
||||
while (AllObjects.ContainsKey(Id));
|
||||
}
|
||||
else
|
||||
{
|
||||
Id = id.Value;
|
||||
}
|
||||
while (AllObjects.ContainsKey(Id));
|
||||
|
||||
AllObjects.Add(Id, this);
|
||||
}
|
||||
@@ -140,6 +151,33 @@ class SerializedObject
|
||||
Fields.Add(nameCopy, (primitiveType, data));
|
||||
}
|
||||
|
||||
public void GetField(StringView fieldName, SerializationType expectedType, uint8* target)
|
||||
{
|
||||
if (!Fields.TryGetValue(fieldName, let field))
|
||||
return;
|
||||
|
||||
if (expectedType != field.PrimitiveType)
|
||||
return;
|
||||
|
||||
switch (field.PrimitiveType)
|
||||
{
|
||||
case .String, .Enum:
|
||||
#unwarn
|
||||
StringView view = *(StringView*)&field.Data;
|
||||
|
||||
char8* stringPtr = view.Ptr;
|
||||
int stringLen = view.Length;
|
||||
|
||||
// We just pass the raw utf8-Pointer and length to C#
|
||||
Internal.MemCpy(target, &stringPtr, sizeof(void*));
|
||||
Internal.MemCpy(target + 8, &stringLen, sizeof(int));
|
||||
default:
|
||||
// Most values can simply be copied, the conversion will be done in C#
|
||||
#unwarn
|
||||
Internal.MemCpy(target, &field.Data, 16);
|
||||
}
|
||||
}
|
||||
|
||||
typealias SerializeMethod = function MonoObject*(MonoObject* entity, void* contextPtr, MonoException** exception);
|
||||
typealias DeserializeMethod = function MonoObject*(MonoObject* entity, void* contextPtr, MonoException** exception);
|
||||
|
||||
@@ -158,7 +196,7 @@ class SerializedObject
|
||||
|
||||
public void Deserialize(ScriptInstance scriptInstance)
|
||||
{
|
||||
DeserializeMethod deserialize = (DeserializeMethod)ScriptEngine.Classes.EntitySerializer.GetMethodThunk("Serialize", 2);
|
||||
DeserializeMethod deserialize = (DeserializeMethod)ScriptEngine.Classes.EntitySerializer.GetMethodThunk("Deserialize", 2);
|
||||
|
||||
void* thisPtr = Internal.UnsafeCastToPtr(this);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user