Last active
May 14, 2020 14:55
-
-
Save korchoon/b2687c200f32fd09eec06ef2f6b3f008 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
using UnityEngine; | |
namespace Asset_Cleaner.Serialization { | |
public static class PersistenceUtils { | |
public static void Load(ref Config result) { | |
var serializable = Deserialize(); | |
AufSerializableData.OnDeserialize(in serializable, ref result); | |
} | |
public static void Save(in Config src) { | |
AufSerializableData.OnSerialize(in src, out var serializable); | |
var json = JsonUtility.ToJson(serializable); | |
File.WriteAllText(Path, json); | |
} | |
static AufSerializableData Deserialize() { | |
AufSerializableData serializableData; | |
string json; | |
if (!File.Exists(Path)) { | |
// not exists - write new | |
serializableData = AufSerializableData.Default(); | |
json = JsonUtility.ToJson(serializableData); | |
File.WriteAllText(Path, json); | |
} | |
else { | |
// exists | |
json = File.ReadAllText(Path); | |
if (string.IsNullOrEmpty(json)) { | |
// but corrupted - overwrite with new | |
serializableData = AufSerializableData.Default(); | |
json = JsonUtility.ToJson(serializableData); | |
File.WriteAllText(Path, json); | |
} | |
serializableData = JsonUtility.FromJson<AufSerializableData>(json); | |
if (serializableData.Valid()) | |
return serializableData; | |
serializableData = AufSerializableData.Default(); | |
json = JsonUtility.ToJson(serializableData); | |
File.WriteAllText(Path, json); | |
} | |
return serializableData; | |
} | |
static string Path => $"{Application.temporaryCachePath}/AssetCleaner_{AufSerializableData.CurrentVersion}.json"; | |
// [MenuItem("Tools/LogPath")] | |
static void Log() { | |
Debug.Log(Application.temporaryCachePath); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment