Skip to content

Commit

Permalink
changed json serialization, added yaml serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
dducode committed Oct 18, 2024
1 parent a4e1c34 commit e2d94af
Show file tree
Hide file tree
Showing 30 changed files with 224 additions and 30 deletions.
12 changes: 12 additions & 0 deletions Editor/SaveSystemSettingsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ internal class SaveSystemSettingsProvider {
private SerializedProperty m_encryptProperty;
private SerializedProperty m_encryptionSettingsProperty;
private SerializedProperty m_cacheSizeProperty;
private SerializedProperty m_jsonSerializationSettingsProperty;


[SettingsProvider]
Expand Down Expand Up @@ -134,6 +135,9 @@ private void InitializeCommonSettings () {
nameof(SaveSystemSettings.encryptionSettings)
);
m_cacheSizeProperty = m_serializedSettings.FindProperty(nameof(SaveSystemSettings.cacheSize));
m_jsonSerializationSettingsProperty = m_serializedSettings.FindProperty(
nameof(SaveSystemSettings.jsonSerializationSettings)
);
}


Expand Down Expand Up @@ -171,6 +175,7 @@ private void DrawCommonSettings () {

EditorGUILayout.PropertyField(m_serializerType);
EditorGUILayout.PropertyField(m_cacheSizeProperty);
DrawJsonSerializationSettings();
DrawCompressionSettings();
DrawEncryptionSettings();

Expand Down Expand Up @@ -218,6 +223,13 @@ private void DrawCheckpointsSettings () {
}


private void DrawJsonSerializationSettings () {
var serializerType = (SerializerType)m_serializerType.enumValueIndex;
if (serializerType is SerializerType.JSONSerializer)
EditorGUILayout.PropertyField(m_jsonSerializationSettingsProperty);
}


private void DrawCompressionSettings () {
EditorGUILayout.PropertyField(m_compressProperty);

Expand Down
8 changes: 8 additions & 0 deletions Plugins.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Plugins/Newtonsoft.Json.dll
Binary file not shown.
33 changes: 33 additions & 0 deletions Plugins/Newtonsoft.Json.dll.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added Plugins/YamlDotNet.dll
Binary file not shown.
33 changes: 33 additions & 0 deletions Plugins/YamlDotNet.dll.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions Runtime/ISaveData.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
namespace SaveSystemPackage {
using Newtonsoft.Json;
using YamlDotNet.Serialization;

namespace SaveSystemPackage {

public interface ISaveData {

public bool IsEmpty { get; }
[YamlIgnore, JsonIgnore]
public bool IsEmpty => false;

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
namespace SaveSystemPackage.Serialization {
namespace SaveSystemPackage.Internal {

public class CodeFormats {
internal class CodeFormats {

public const string XML = "xml";
public const string JSON = "json";
public const string Binary = "bin";
public const string YAML = "yaml";

}

Expand Down
File renamed without changes.
5 changes: 4 additions & 1 deletion Runtime/SaveSystem.Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,14 @@ private ISerializer SelectSerializer (SaveSystemSettings settings) {
serializer = new BinarySerializer();
break;
case SerializerType.JSONSerializer:
serializer = new JsonSerializer();
serializer = new JsonSerializer(settings.jsonSerializationSettings);
break;
case SerializerType.XMLSerializer:
serializer = new XmlSerializer();
break;
case SerializerType.YAMLSerializer:
serializer = new YamlSerializer();
break;
case SerializerType.Custom:
serializer = null;
break;
Expand Down
2 changes: 0 additions & 2 deletions Runtime/SerializableData/ColorData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public struct ColorData : ISaveData {
public float b;
public float a;

public bool IsEmpty => false;


public ColorData (float r, float g, float b, float a) {
this.r = r;
Expand Down
3 changes: 3 additions & 0 deletions Runtime/SerializableData/DataBatch.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System;
using Newtonsoft.Json;
using YamlDotNet.Serialization;

namespace SaveSystemPackage.SerializableData {

[Serializable]
public class DataBatch<TData> : Map<string, TData>, ISaveData where TData : ISaveData {

[YamlIgnore, JsonIgnore]
public bool IsEmpty => Count == 0;

}
Expand Down
3 changes: 3 additions & 0 deletions Runtime/SerializableData/ProfileData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using Newtonsoft.Json;
using YamlDotNet.Serialization;

namespace SaveSystemPackage.SerializableData {

Expand All @@ -9,6 +11,7 @@ public struct ProfileData : ISaveData {
public string name;
public string iconId;

[YamlIgnore, JsonIgnore]
public bool IsEmpty =>
string.IsNullOrEmpty(id)
&& string.IsNullOrEmpty(name)
Expand Down
4 changes: 4 additions & 0 deletions Runtime/SerializableData/ProfilesManagerData.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using System;
using Newtonsoft.Json;
using YamlDotNet.Serialization;

namespace SaveSystemPackage.SerializableData {

[Serializable]
public struct ProfilesManagerData : ISaveData {

public Map<string, string> profilesMap;

[YamlIgnore, JsonIgnore]
public bool IsEmpty => profilesMap.Count == 0;

}
Expand Down
2 changes: 0 additions & 2 deletions Runtime/SerializableData/QuaternionData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public struct QuaternionData : ISaveData {
public float z;
public float w;

public bool IsEmpty => false;


public QuaternionData (float x, float y, float z, float w) {
this.x = x;
Expand Down
2 changes: 0 additions & 2 deletions Runtime/SerializableData/RigidbodyData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ public struct RigidbodyData : ISaveData {
public Vector3Data angularVelocity;
public bool isKinematic;

public bool IsEmpty => false;


public static implicit operator RigidbodyData (Rigidbody rigidbody) {
return new RigidbodyData {
Expand Down
2 changes: 0 additions & 2 deletions Runtime/SerializableData/TransformData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public struct TransformData : ISaveData {

public Vector3Data scale;

public bool IsEmpty => false;


public static implicit operator TransformData (Transform transform) {
return new TransformData {
Expand Down
2 changes: 0 additions & 2 deletions Runtime/SerializableData/Vector2Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ public struct Vector2Data : ISaveData {
public float x;
public float y;

public bool IsEmpty => false;


public Vector2Data (float x, float y) {
this.x = x;
Expand Down
2 changes: 0 additions & 2 deletions Runtime/SerializableData/Vector3Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ public struct Vector3Data : ISaveData {
public float y;
public float z;

public bool IsEmpty => false;


public Vector3Data (float x, float y, float z) {
this.x = x;
Expand Down
2 changes: 0 additions & 2 deletions Runtime/SerializableData/Vector4Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public struct Vector4Data : ISaveData {
public float z;
public float w;

public bool IsEmpty => false;


public Vector4Data (float x, float y, float z, float w) {
this.x = x;
Expand Down
2 changes: 2 additions & 0 deletions Runtime/Serialization/BinarySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using SaveSystemPackage.Internal;

namespace SaveSystemPackage.Serialization {

Expand All @@ -23,6 +24,7 @@ public byte[] Serialize<TData> ([NotNull] TData data) where TData : ISaveData {
public TData Deserialize<TData> (byte[] data) where TData : ISaveData {
if (data == null || data.Length == 0)
return default;

using var stream = new MemoryStream(data);
var formatter = new BinaryFormatter();
return (TData)formatter.Deserialize(stream);
Expand Down
14 changes: 14 additions & 0 deletions Runtime/Serialization/JsonSerializationSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using Newtonsoft.Json;

namespace SaveSystemPackage.Serialization {

[Serializable]
public class JsonSerializationSettings {

public Formatting Formatting;
public ReferenceLoopHandling ReferenceLoopHandling;

}

}
3 changes: 3 additions & 0 deletions Runtime/Serialization/JsonSerializationSettings.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 32 additions & 3 deletions Runtime/Serialization/JsonSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,52 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
using UnityEngine;
using Newtonsoft.Json;
using SaveSystemPackage.Internal;

namespace SaveSystemPackage.Serialization {

public class JsonSerializer : ISerializer {

private readonly JsonSerializationSettings m_settings;


public JsonSerializer (JsonSerializationSettings settings = null) {
m_settings = settings ?? new JsonSerializationSettings();
}


public byte[] Serialize<TData> ([NotNull] TData data) where TData : ISaveData {
if (data == null)
throw new ArgumentNullException(nameof(data));
return data.IsEmpty ? Array.Empty<byte>() : Encoding.UTF8.GetBytes(JsonUtility.ToJson(data, true));
if (data.IsEmpty)
return Array.Empty<byte>();

var stream = new MemoryStream();

using (var writer = new StreamWriter(stream, Encoding.UTF8)) {
var serializer = new Newtonsoft.Json.JsonSerializer {
Formatting = m_settings.Formatting,
ReferenceLoopHandling = m_settings.ReferenceLoopHandling
};
serializer.Serialize(writer, data);
}

return stream.ToArray();
}


public TData Deserialize<TData> (byte[] data) where TData : ISaveData {
if (data == null || data.Length == 0)
return default;
return JsonUtility.FromJson<TData>(Encoding.UTF8.GetString(data));

using var reader = new StreamReader(new MemoryStream(data), Encoding.UTF8);
var serializer = new Newtonsoft.Json.JsonSerializer {
Formatting = m_settings.Formatting,
ReferenceLoopHandling = m_settings.ReferenceLoopHandling
};
return serializer.Deserialize<TData>(new JsonTextReader(reader));
}


Expand Down
Loading

0 comments on commit e2d94af

Please sign in to comment.