Fix undo for custom serialized objects (eg. LocalizedString)
This commit is contained in:
@@ -4,6 +4,8 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using FlaxEditor.Utilities;
|
||||
using FlaxEngine;
|
||||
using Newtonsoft.Json;
|
||||
using JsonSerializer = FlaxEngine.Json.JsonSerializer;
|
||||
|
||||
namespace FlaxEditor.History
|
||||
{
|
||||
@@ -114,6 +116,8 @@ namespace FlaxEditor.History
|
||||
public object TargetInstance;
|
||||
}
|
||||
|
||||
internal static JsonSerializerSettings JsonSettings;
|
||||
|
||||
// For objects that cannot be referenced in undo action like: FlaxEngine.Object or SceneGraphNode we store them in DataStorage,
|
||||
// otherwise here:
|
||||
private readonly object TargetInstance;
|
||||
@@ -177,6 +181,24 @@ namespace FlaxEditor.History
|
||||
};
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override DataStorage Data
|
||||
{
|
||||
protected set
|
||||
{
|
||||
// Inject objects typename serialization to prevent data type mismatch when loading from saved state
|
||||
var settings = JsonSettings;
|
||||
if (settings == null)
|
||||
{
|
||||
settings = JsonSerializer.CreateDefaultSettings(false);
|
||||
settings.TypeNameHandling = TypeNameHandling.All;
|
||||
JsonSettings = settings;
|
||||
}
|
||||
_data = JsonConvert.SerializeObject(value, Formatting.Indented, settings);
|
||||
//Editor.Log(_data);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override string ActionString { get; }
|
||||
|
||||
|
||||
@@ -374,6 +374,7 @@ namespace FlaxEditor.SceneGraph
|
||||
/// <summary>
|
||||
/// Gets or sets the node state.
|
||||
/// </summary>
|
||||
[NoSerialize]
|
||||
public virtual StateData State
|
||||
{
|
||||
get => throw new NotImplementedException();
|
||||
|
||||
@@ -4,7 +4,6 @@ using System;
|
||||
using FlaxEditor.SceneGraph;
|
||||
using FlaxEngine;
|
||||
using Newtonsoft.Json;
|
||||
using JsonSerializer = FlaxEngine.Json.JsonSerializer;
|
||||
|
||||
namespace FlaxEditor
|
||||
{
|
||||
@@ -28,7 +27,6 @@ namespace FlaxEditor
|
||||
var id = Guid.Parse((string)reader.Value);
|
||||
return SceneGraphFactory.FindNode(id);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -56,19 +54,11 @@ namespace FlaxEditor
|
||||
/// <summary>
|
||||
/// Gets or sets the serialized undo data.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The data.
|
||||
/// </value>
|
||||
[NoSerialize]
|
||||
public TData Data
|
||||
public virtual TData Data
|
||||
{
|
||||
get => JsonConvert.DeserializeObject<TData>(_data, JsonSerializer.Settings);
|
||||
protected set => _data = JsonConvert.SerializeObject(value, Formatting.None, JsonSerializer.Settings);
|
||||
/*protected set
|
||||
{
|
||||
_data = JsonConvert.SerializeObject(value, Formatting.Indented, JsonSerializer.Settings);
|
||||
Debug.Info(_data);
|
||||
}*/
|
||||
get => JsonConvert.DeserializeObject<TData>(_data, FlaxEngine.Json.JsonSerializer.Settings);
|
||||
protected set => _data = JsonConvert.SerializeObject(value, Formatting.None, FlaxEngine.Json.JsonSerializer.Settings);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
||||
@@ -236,6 +236,13 @@ namespace FlaxEngine.Json
|
||||
else
|
||||
{
|
||||
writer.WriteStartObject();
|
||||
#if FLAX_EDITOR
|
||||
if ((serializer.TypeNameHandling & TypeNameHandling.Objects) == TypeNameHandling.Objects)
|
||||
{
|
||||
writer.WritePropertyName("$type");
|
||||
writer.WriteValue("FlaxEngine.LocalizedString, FlaxEngine.CSharp");
|
||||
}
|
||||
#endif
|
||||
writer.WritePropertyName("Id");
|
||||
writer.WriteValue(str.Id);
|
||||
writer.WritePropertyName("Value");
|
||||
|
||||
Reference in New Issue
Block a user