|
- using System;
- using System.Collections.Generic;
- using SUISS.Storage;
- using UnityEngine;
-
- namespace SUISSEngine
- {
- public class Serializing : MonoBehaviour
- {
- public static string DictToString(Dictionary<string, object> dict)
- {
- string text = string.Empty;
- foreach (KeyValuePair<string, object> keyValuePair in dict)
- {
- text += ((text.Length <= 0) ? "{ " : ", ");
- text += string.Format("\"{0}\" => \"{1}\"", keyValuePair.Key, keyValuePair.Value);
- }
- return text + " }";
- }
-
- private void Start()
- {
- if (!this.started)
- {
- this.Deserialize();
- }
- }
-
- public void Serialize()
- {
- if (!this.readOnly)
- {
- if (this.isDeserializing)
- {
- this.pendingSerialize = true;
- return;
- }
- if (this.StorageKey != null && this.StorageKey.Length > 0)
- {
- if (!this.started)
- {
- this.Deserialize();
- }
- this.isSerializing = true;
- Storage storage = Storage.Get(this.StorageLifecycle);
- Dictionary<string, object> value;
- if (storage.Root.ContainsKey(this.StorageKey))
- {
- value = (Dictionary<string, object>)storage.Root[this.StorageKey];
- }
- else
- {
- value = new Dictionary<string, object>();
- storage.Root[this.StorageKey] = value;
- }
- base.SendMessage("OnSerialize", value, SendMessageOptions.DontRequireReceiver);
- base.SendMessage("OnSerialized", SendMessageOptions.DontRequireReceiver);
- this.isSerializing = false;
- }
- else
- {
- UnityEngine.Debug.LogWarning(string.Format("Serializing with empty StorageKey in {0}.", base.name));
- }
- if (this.pendingDeserialize)
- {
- this.pendingDeserialize = false;
- this.Deserialize();
- }
- }
- }
-
- public void Deserialize()
- {
- if (this.isSerializing)
- {
- this.pendingDeserialize = true;
- return;
- }
- this.started = true;
- if (this.StorageKey != null && this.StorageKey.Length > 0)
- {
- this.isDeserializing = true;
- Storage storage = Storage.Get(this.StorageLifecycle);
- if (storage.Root.ContainsKey(this.StorageKey))
- {
- Dictionary<string, object> value = (Dictionary<string, object>)storage.Root[this.StorageKey];
- base.SendMessage("OnDeserialize", value, SendMessageOptions.DontRequireReceiver);
- }
- base.SendMessage("OnDeserialized", SendMessageOptions.DontRequireReceiver);
- this.isDeserializing = false;
- }
- else
- {
- UnityEngine.Debug.LogWarning(string.Format("Deserializing with empty StorageKey in {0}.", base.name));
- }
- if (this.pendingSerialize)
- {
- this.pendingSerialize = false;
- this.Serialize();
- }
- }
-
- public void Import(Dictionary<string, object> values)
- {
- if (this.StorageKey != null && this.StorageKey.Length > 0)
- {
- Storage storage = Storage.Get(this.StorageLifecycle);
- storage.Root[this.StorageKey] = values;
- this.Deserialize();
- }
- else
- {
- UnityEngine.Debug.LogWarning(string.Format("Importing with empty StorageKey in {0}.", base.name));
- }
- }
-
- public void Delete()
- {
- if (this.StorageKey != null && this.StorageKey.Length > 0)
- {
- Storage storage = Storage.Get(this.StorageLifecycle);
- storage.Root.Remove(this.StorageKey);
- }
- }
-
- public void Rename(string newKey)
- {
- Dictionary<string, object> value = new Dictionary<string, object>();
- Storage storage = Storage.Get(this.StorageLifecycle);
- if (this.StorageKey != null && this.StorageKey.Length > 0 && storage.Root.ContainsKey(this.StorageKey))
- {
- value = (Dictionary<string, object>)storage.Root[this.StorageKey];
- storage.Root.Remove(this.StorageKey);
- }
- this.StorageKey = newKey;
- storage.Root[this.StorageKey] = value;
- }
-
- public StorageLifecycle StorageLifecycle = StorageLifecycle.Game;
-
- public string StorageKey;
-
- public bool readOnly;
-
- private bool started;
-
- private bool isDeserializing;
-
- private bool isSerializing;
-
- private bool pendingSerialize;
-
- private bool pendingDeserialize;
- }
- }
|