Reuse various serialization code

This commit is contained in:
Wojtek Figat
2024-03-08 14:26:48 +01:00
parent f4a352ec8d
commit 584c073df1
3 changed files with 23 additions and 118 deletions

View File

@@ -246,15 +246,7 @@ namespace FlaxEngine.Json
/// <returns>The output json string.</returns> /// <returns>The output json string.</returns>
public static string Serialize(object obj, bool isManagedOnly = false) public static string Serialize(object obj, bool isManagedOnly = false)
{ {
Type type = obj.GetType(); return Serialize(obj, obj.GetType(), isManagedOnly);
var cache = isManagedOnly ? CacheManagedOnly.Value : Cache.Value;
Current.Value = cache;
cache.WriteBegin();
cache.SerializerWriter.Serialize(cache.JsonWriter, obj, type);
cache.WriteEnd();
return cache.StringBuilder.ToString();
} }
/// <summary> /// <summary>

View File

@@ -12,7 +12,6 @@
#else #else
// Cached methods (FlaxEngine.CSharp.dll is loaded only once) // Cached methods (FlaxEngine.CSharp.dll is loaded only once)
MMethod* UICanvas_Serialize = nullptr; MMethod* UICanvas_Serialize = nullptr;
MMethod* UICanvas_SerializeDiff = nullptr;
MMethod* UICanvas_Deserialize = nullptr; MMethod* UICanvas_Deserialize = nullptr;
MMethod* UICanvas_PostDeserialize = nullptr; MMethod* UICanvas_PostDeserialize = nullptr;
MMethod* UICanvas_Enable = nullptr; MMethod* UICanvas_Enable = nullptr;
@@ -45,7 +44,6 @@ UICanvas::UICanvas(const SpawnParams& params)
if (UICanvas_Serialize == nullptr) if (UICanvas_Serialize == nullptr)
{ {
MClass* mclass = GetClass(); MClass* mclass = GetClass();
UICanvas_SerializeDiff = mclass->GetMethod("SerializeDiff", 1);
UICanvas_Deserialize = mclass->GetMethod("Deserialize", 1); UICanvas_Deserialize = mclass->GetMethod("Deserialize", 1);
UICanvas_PostDeserialize = mclass->GetMethod("PostDeserialize"); UICanvas_PostDeserialize = mclass->GetMethod("PostDeserialize");
UICanvas_Enable = mclass->GetMethod("Enable"); UICanvas_Enable = mclass->GetMethod("Enable");
@@ -55,7 +53,7 @@ UICanvas::UICanvas(const SpawnParams& params)
#endif #endif
UICanvas_EndPlay = mclass->GetMethod("EndPlay"); UICanvas_EndPlay = mclass->GetMethod("EndPlay");
UICanvas_ParentChanged = mclass->GetMethod("ParentChanged"); UICanvas_ParentChanged = mclass->GetMethod("ParentChanged");
UICanvas_Serialize = mclass->GetMethod("Serialize"); UICanvas_Serialize = mclass->GetMethod("Serialize", 1);
Platform::MemoryBarrier(); Platform::MemoryBarrier();
} }
#endif #endif
@@ -83,8 +81,7 @@ void UICanvas::Serialize(SerializeStream& stream, const void* otherObj)
void* params[1]; void* params[1];
params[0] = other ? other->GetOrCreateManagedInstance() : nullptr; params[0] = other ? other->GetOrCreateManagedInstance() : nullptr;
MObject* exception = nullptr; MObject* exception = nullptr;
auto method = other ? UICanvas_SerializeDiff : UICanvas_Serialize; auto invokeResultStr = (MString*)UICanvas_Serialize->Invoke(GetOrCreateManagedInstance(), params, &exception);
auto invokeResultStr = (MString*)method->Invoke(GetOrCreateManagedInstance(), params, &exception);
if (exception) if (exception)
{ {
MException ex(exception); MException ex(exception);

View File

@@ -575,8 +575,9 @@ namespace FlaxEngine
} }
} }
internal string Serialize() internal string Serialize(UICanvas other)
{ {
bool noOther = other == null;
StringBuilder sb = new StringBuilder(256); StringBuilder sb = new StringBuilder(256);
StringWriter sw = new StringWriter(sb, CultureInfo.InvariantCulture); StringWriter sw = new StringWriter(sb, CultureInfo.InvariantCulture);
using (JsonTextWriter jsonWriter = new JsonTextWriter(sw)) using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
@@ -587,137 +588,52 @@ namespace FlaxEngine
jsonWriter.WriteStartObject(); jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("RenderMode"); if (noOther || _renderMode != other._renderMode)
jsonWriter.WriteValue(_renderMode);
jsonWriter.WritePropertyName("RenderLocation");
jsonWriter.WriteValue(RenderLocation);
jsonWriter.WritePropertyName("Order");
jsonWriter.WriteValue(Order);
jsonWriter.WritePropertyName("ReceivesEvents");
jsonWriter.WriteValue(ReceivesEvents);
jsonWriter.WritePropertyName("IgnoreDepth");
jsonWriter.WriteValue(IgnoreDepth);
jsonWriter.WritePropertyName("RenderCamera");
jsonWriter.WriteValue(Json.JsonSerializer.GetStringID(RenderCamera));
jsonWriter.WritePropertyName("Distance");
jsonWriter.WriteValue(Distance);
if (RenderMode == CanvasRenderMode.WorldSpace || RenderMode == CanvasRenderMode.WorldSpaceFaceCamera)
{
jsonWriter.WritePropertyName("Size");
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("X");
jsonWriter.WriteValue(Size.X);
jsonWriter.WritePropertyName("Y");
jsonWriter.WriteValue(Size.Y);
jsonWriter.WriteEndObject();
}
jsonWriter.WritePropertyName("NavigationInputRepeatDelay");
jsonWriter.WriteValue(NavigationInputRepeatDelay);
jsonWriter.WritePropertyName("NavigationInputRepeatRate");
jsonWriter.WriteValue(NavigationInputRepeatRate);
jsonWriter.WritePropertyName("NavigateUp");
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("Name");
jsonWriter.WriteValue(NavigateUp.Name);
jsonWriter.WriteEndObject();
jsonWriter.WritePropertyName("NavigateDown");
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("Name");
jsonWriter.WriteValue(NavigateDown.Name);
jsonWriter.WriteEndObject();
jsonWriter.WritePropertyName("NavigateLeft");
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("Name");
jsonWriter.WriteValue(NavigateLeft.Name);
jsonWriter.WriteEndObject();
jsonWriter.WritePropertyName("NavigateRight");
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("Name");
jsonWriter.WriteValue(NavigateRight.Name);
jsonWriter.WriteEndObject();
jsonWriter.WritePropertyName("NavigateSubmit");
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("Name");
jsonWriter.WriteValue(NavigateSubmit.Name);
jsonWriter.WriteEndObject();
jsonWriter.WriteEndObject();
}
return sw.ToString();
}
internal string SerializeDiff(UICanvas other)
{
StringBuilder sb = new StringBuilder(256);
StringWriter sw = new StringWriter(sb, CultureInfo.InvariantCulture);
using (JsonTextWriter jsonWriter = new JsonTextWriter(sw))
{
jsonWriter.IndentChar = '\t';
jsonWriter.Indentation = 1;
jsonWriter.Formatting = Formatting.Indented;
jsonWriter.WriteStartObject();
if (_renderMode != other._renderMode)
{ {
jsonWriter.WritePropertyName("RenderMode"); jsonWriter.WritePropertyName("RenderMode");
jsonWriter.WriteValue(_renderMode); jsonWriter.WriteValue(_renderMode);
} }
if (RenderLocation != other.RenderLocation) if (noOther || RenderLocation != other.RenderLocation)
{ {
jsonWriter.WritePropertyName("RenderLocation"); jsonWriter.WritePropertyName("RenderLocation");
jsonWriter.WriteValue(RenderLocation); jsonWriter.WriteValue(RenderLocation);
} }
if (Order != other.Order) if (noOther || Order != other.Order)
{ {
jsonWriter.WritePropertyName("Order"); jsonWriter.WritePropertyName("Order");
jsonWriter.WriteValue(Order); jsonWriter.WriteValue(Order);
} }
if (ReceivesEvents != other.ReceivesEvents) if (noOther || ReceivesEvents != other.ReceivesEvents)
{ {
jsonWriter.WritePropertyName("ReceivesEvents"); jsonWriter.WritePropertyName("ReceivesEvents");
jsonWriter.WriteValue(ReceivesEvents); jsonWriter.WriteValue(ReceivesEvents);
} }
if (IgnoreDepth != other.IgnoreDepth) if (noOther || IgnoreDepth != other.IgnoreDepth)
{ {
jsonWriter.WritePropertyName("IgnoreDepth"); jsonWriter.WritePropertyName("IgnoreDepth");
jsonWriter.WriteValue(IgnoreDepth); jsonWriter.WriteValue(IgnoreDepth);
} }
if (RenderCamera != other.RenderCamera) if (noOther || RenderCamera != other.RenderCamera)
{ {
jsonWriter.WritePropertyName("RenderCamera"); jsonWriter.WritePropertyName("RenderCamera");
jsonWriter.WriteValue(Json.JsonSerializer.GetStringID(RenderCamera)); jsonWriter.WriteValue(Json.JsonSerializer.GetStringID(RenderCamera));
} }
if (Mathf.Abs(Distance - other.Distance) > Mathf.Epsilon) if (noOther || Mathf.Abs(Distance - other.Distance) > Mathf.Epsilon)
{ {
jsonWriter.WritePropertyName("Distance"); jsonWriter.WritePropertyName("Distance");
jsonWriter.WriteValue(Distance); jsonWriter.WriteValue(Distance);
} }
if ((RenderMode == CanvasRenderMode.WorldSpace || bool saveSize = RenderMode == CanvasRenderMode.WorldSpace || RenderMode == CanvasRenderMode.WorldSpaceFaceCamera;
RenderMode == CanvasRenderMode.WorldSpaceFaceCamera || if (!noOther)
other.RenderMode == CanvasRenderMode.WorldSpace || saveSize = (saveSize || other.RenderMode == CanvasRenderMode.WorldSpace || other.RenderMode == CanvasRenderMode.WorldSpaceFaceCamera) && Size != other.Size;
other.RenderMode == CanvasRenderMode.WorldSpaceFaceCamera) && Size != other.Size) if (saveSize)
{ {
jsonWriter.WritePropertyName("Size"); jsonWriter.WritePropertyName("Size");
jsonWriter.WriteStartObject(); jsonWriter.WriteStartObject();
@@ -728,17 +644,17 @@ namespace FlaxEngine
jsonWriter.WriteEndObject(); jsonWriter.WriteEndObject();
} }
if (!Mathf.NearEqual(NavigationInputRepeatDelay, other.NavigationInputRepeatDelay)) if (noOther || !Mathf.NearEqual(NavigationInputRepeatDelay, other.NavigationInputRepeatDelay))
{ {
jsonWriter.WritePropertyName("NavigationInputRepeatDelay"); jsonWriter.WritePropertyName("NavigationInputRepeatDelay");
jsonWriter.WriteValue(NavigationInputRepeatDelay); jsonWriter.WriteValue(NavigationInputRepeatDelay);
} }
if (!Mathf.NearEqual(NavigationInputRepeatRate, other.NavigationInputRepeatRate)) if (noOther || !Mathf.NearEqual(NavigationInputRepeatRate, other.NavigationInputRepeatRate))
{ {
jsonWriter.WritePropertyName("NavigationInputRepeatRate"); jsonWriter.WritePropertyName("NavigationInputRepeatRate");
jsonWriter.WriteValue(NavigationInputRepeatRate); jsonWriter.WriteValue(NavigationInputRepeatRate);
} }
if (NavigateUp.Name != other.NavigateUp.Name) if (noOther || NavigateUp.Name != other.NavigateUp.Name)
{ {
jsonWriter.WritePropertyName("NavigateUp"); jsonWriter.WritePropertyName("NavigateUp");
jsonWriter.WriteStartObject(); jsonWriter.WriteStartObject();
@@ -746,7 +662,7 @@ namespace FlaxEngine
jsonWriter.WriteValue(NavigateUp.Name); jsonWriter.WriteValue(NavigateUp.Name);
jsonWriter.WriteEndObject(); jsonWriter.WriteEndObject();
} }
if (NavigateDown.Name != other.NavigateDown.Name) if (noOther || NavigateDown.Name != other.NavigateDown.Name)
{ {
jsonWriter.WritePropertyName("NavigateDown"); jsonWriter.WritePropertyName("NavigateDown");
jsonWriter.WriteStartObject(); jsonWriter.WriteStartObject();
@@ -754,7 +670,7 @@ namespace FlaxEngine
jsonWriter.WriteValue(NavigateDown.Name); jsonWriter.WriteValue(NavigateDown.Name);
jsonWriter.WriteEndObject(); jsonWriter.WriteEndObject();
} }
if (NavigateLeft.Name != other.NavigateLeft.Name) if (noOther || NavigateLeft.Name != other.NavigateLeft.Name)
{ {
jsonWriter.WritePropertyName("NavigateLeft"); jsonWriter.WritePropertyName("NavigateLeft");
jsonWriter.WriteStartObject(); jsonWriter.WriteStartObject();
@@ -762,7 +678,7 @@ namespace FlaxEngine
jsonWriter.WriteValue(NavigateLeft.Name); jsonWriter.WriteValue(NavigateLeft.Name);
jsonWriter.WriteEndObject(); jsonWriter.WriteEndObject();
} }
if (NavigateRight.Name != other.NavigateRight.Name) if (noOther || NavigateRight.Name != other.NavigateRight.Name)
{ {
jsonWriter.WritePropertyName("NavigateRight"); jsonWriter.WritePropertyName("NavigateRight");
jsonWriter.WriteStartObject(); jsonWriter.WriteStartObject();
@@ -770,7 +686,7 @@ namespace FlaxEngine
jsonWriter.WriteValue(NavigateRight.Name); jsonWriter.WriteValue(NavigateRight.Name);
jsonWriter.WriteEndObject(); jsonWriter.WriteEndObject();
} }
if (NavigateSubmit.Name != other.NavigateSubmit.Name) if (noOther || NavigateSubmit.Name != other.NavigateSubmit.Name)
{ {
jsonWriter.WritePropertyName("NavigateSubmit"); jsonWriter.WritePropertyName("NavigateSubmit");
jsonWriter.WriteStartObject(); jsonWriter.WriteStartObject();