Reuse various serialization code

This commit is contained in:
Wojtek Figat
2024-03-10 23:28:16 +01:00
parent b00b5bed00
commit 4a5ded0849
2 changed files with 10 additions and 48 deletions

View File

@@ -13,7 +13,6 @@
#else
// Cached methods (FlaxEngine.CSharp.dll is loaded only once)
MMethod* UIControl_Serialize = nullptr;
MMethod* UIControl_SerializeDiff = nullptr;
MMethod* UIControl_Deserialize = nullptr;
MMethod* UIControl_ParentChanged = nullptr;
MMethod* UIControl_TransformChanged = nullptr;
@@ -39,10 +38,10 @@ UIControl::UIControl(const SpawnParams& params)
: Actor(params)
{
#if !COMPILE_WITHOUT_CSHARP
Platform::MemoryBarrier();
if (UIControl_Serialize == nullptr)
{
MClass* mclass = GetClass();
UIControl_SerializeDiff = mclass->GetMethod("SerializeDiff", 2);
UIControl_Deserialize = mclass->GetMethod("Deserialize", 2);
UIControl_ParentChanged = mclass->GetMethod("ParentChanged");
UIControl_TransformChanged = mclass->GetMethod("TransformChanged");
@@ -50,7 +49,8 @@ UIControl::UIControl(const SpawnParams& params)
UIControl_ActiveInTreeChanged = mclass->GetMethod("ActiveInTreeChanged");
UIControl_BeginPlay = mclass->GetMethod("BeginPlay");
UIControl_EndPlay = mclass->GetMethod("EndPlay");
UIControl_Serialize = mclass->GetMethod("Serialize", 1);
UIControl_Serialize = mclass->GetMethod("Serialize", 2);
Platform::MemoryBarrier();
}
#endif
}
@@ -82,8 +82,7 @@ void UIControl::Serialize(SerializeStream& stream, const void* otherObj)
params[0] = &controlType;
params[1] = other ? other->GetOrCreateManagedInstance() : nullptr;
MObject* exception = nullptr;
const auto method = other ? UIControl_SerializeDiff : UIControl_Serialize;
const auto invokeResultStr = (MString*)method->Invoke(GetOrCreateManagedInstance(), params, &exception);
const auto invokeResultStr = (MString*)UIControl_Serialize->Invoke(GetOrCreateManagedInstance(), params, &exception);
if (exception)
{
MException ex(exception);

View File

@@ -314,15 +314,15 @@ namespace FlaxEngine
return FallbackParentGetDelegate?.Invoke(this);
}
internal string Serialize(out string controlType)
internal string Serialize(out string controlType, UIControl other)
{
if (_control == null)
{
controlType = null;
return null;
}
var type = _control.GetType();
var noDiff = other._control == null || other._control.GetType() != type;
JsonSerializer jsonSerializer = JsonSerializer.CreateDefault(Json.JsonSerializer.Settings);
jsonSerializer.Formatting = Formatting.Indented;
@@ -344,47 +344,10 @@ namespace FlaxEngine
JsonSerializerInternalWriter serializerWriter = new JsonSerializerInternalWriter(jsonSerializer);
serializerWriter.Serialize(jsonWriter, _control, type);
}
controlType = type.FullName;
return sw.ToString();
}
internal string SerializeDiff(out string controlType, UIControl other)
{
if (_control == null)
{
controlType = null;
return null;
}
var type = _control.GetType();
if (other._control == null || other._control.GetType() != type)
{
return Serialize(out controlType);
}
JsonSerializer jsonSerializer = JsonSerializer.CreateDefault(Json.JsonSerializer.Settings);
jsonSerializer.Formatting = Formatting.Indented;
StringBuilder sb = new StringBuilder(1024);
StringWriter sw = new StringWriter(sb, CultureInfo.InvariantCulture);
using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
{
// Prepare writer settings
jsonWriter.IndentChar = '\t';
jsonWriter.Indentation = 1;
jsonWriter.Formatting = jsonSerializer.Formatting;
jsonWriter.DateFormatHandling = jsonSerializer.DateFormatHandling;
jsonWriter.DateTimeZoneHandling = jsonSerializer.DateTimeZoneHandling;
jsonWriter.FloatFormatHandling = jsonSerializer.FloatFormatHandling;
jsonWriter.StringEscapeHandling = jsonSerializer.StringEscapeHandling;
jsonWriter.Culture = jsonSerializer.Culture;
jsonWriter.DateFormatString = jsonSerializer.DateFormatString;
JsonSerializerInternalWriter serializerWriter = new JsonSerializerInternalWriter(jsonSerializer);
serializerWriter.SerializeDiff(jsonWriter, _control, type, other._control);
if (noDiff)
serializerWriter.Serialize(jsonWriter, _control, type);
else
serializerWriter.SerializeDiff(jsonWriter, _control, type, other._control);
}
controlType = string.Empty;