You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

156 lines
4.0 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using SUISS.Storage;
  4. using UnityEngine;
  5. namespace SUISSEngine
  6. {
  7. public class Serializing : MonoBehaviour
  8. {
  9. public static string DictToString(Dictionary<string, object> dict)
  10. {
  11. string text = string.Empty;
  12. foreach (KeyValuePair<string, object> keyValuePair in dict)
  13. {
  14. text += ((text.Length <= 0) ? "{ " : ", ");
  15. text += string.Format("\"{0}\" => \"{1}\"", keyValuePair.Key, keyValuePair.Value);
  16. }
  17. return text + " }";
  18. }
  19. private void Start()
  20. {
  21. if (!this.started)
  22. {
  23. this.Deserialize();
  24. }
  25. }
  26. public void Serialize()
  27. {
  28. if (!this.readOnly)
  29. {
  30. if (this.isDeserializing)
  31. {
  32. this.pendingSerialize = true;
  33. return;
  34. }
  35. if (this.StorageKey != null && this.StorageKey.Length > 0)
  36. {
  37. if (!this.started)
  38. {
  39. this.Deserialize();
  40. }
  41. this.isSerializing = true;
  42. Storage storage = Storage.Get(this.StorageLifecycle);
  43. Dictionary<string, object> value;
  44. if (storage.Root.ContainsKey(this.StorageKey))
  45. {
  46. value = (Dictionary<string, object>)storage.Root[this.StorageKey];
  47. }
  48. else
  49. {
  50. value = new Dictionary<string, object>();
  51. storage.Root[this.StorageKey] = value;
  52. }
  53. base.SendMessage("OnSerialize", value, SendMessageOptions.DontRequireReceiver);
  54. base.SendMessage("OnSerialized", SendMessageOptions.DontRequireReceiver);
  55. this.isSerializing = false;
  56. }
  57. else
  58. {
  59. UnityEngine.Debug.LogWarning(string.Format("Serializing with empty StorageKey in {0}.", base.name));
  60. }
  61. if (this.pendingDeserialize)
  62. {
  63. this.pendingDeserialize = false;
  64. this.Deserialize();
  65. }
  66. }
  67. }
  68. public void Deserialize()
  69. {
  70. if (this.isSerializing)
  71. {
  72. this.pendingDeserialize = true;
  73. return;
  74. }
  75. this.started = true;
  76. if (this.StorageKey != null && this.StorageKey.Length > 0)
  77. {
  78. this.isDeserializing = true;
  79. Storage storage = Storage.Get(this.StorageLifecycle);
  80. if (storage.Root.ContainsKey(this.StorageKey))
  81. {
  82. Dictionary<string, object> value = (Dictionary<string, object>)storage.Root[this.StorageKey];
  83. base.SendMessage("OnDeserialize", value, SendMessageOptions.DontRequireReceiver);
  84. }
  85. base.SendMessage("OnDeserialized", SendMessageOptions.DontRequireReceiver);
  86. this.isDeserializing = false;
  87. }
  88. else
  89. {
  90. UnityEngine.Debug.LogWarning(string.Format("Deserializing with empty StorageKey in {0}.", base.name));
  91. }
  92. if (this.pendingSerialize)
  93. {
  94. this.pendingSerialize = false;
  95. this.Serialize();
  96. }
  97. }
  98. public void Import(Dictionary<string, object> values)
  99. {
  100. if (this.StorageKey != null && this.StorageKey.Length > 0)
  101. {
  102. Storage storage = Storage.Get(this.StorageLifecycle);
  103. storage.Root[this.StorageKey] = values;
  104. this.Deserialize();
  105. }
  106. else
  107. {
  108. UnityEngine.Debug.LogWarning(string.Format("Importing with empty StorageKey in {0}.", base.name));
  109. }
  110. }
  111. public void Delete()
  112. {
  113. if (this.StorageKey != null && this.StorageKey.Length > 0)
  114. {
  115. Storage storage = Storage.Get(this.StorageLifecycle);
  116. storage.Root.Remove(this.StorageKey);
  117. }
  118. }
  119. public void Rename(string newKey)
  120. {
  121. Dictionary<string, object> value = new Dictionary<string, object>();
  122. Storage storage = Storage.Get(this.StorageLifecycle);
  123. if (this.StorageKey != null && this.StorageKey.Length > 0 && storage.Root.ContainsKey(this.StorageKey))
  124. {
  125. value = (Dictionary<string, object>)storage.Root[this.StorageKey];
  126. storage.Root.Remove(this.StorageKey);
  127. }
  128. this.StorageKey = newKey;
  129. storage.Root[this.StorageKey] = value;
  130. }
  131. public StorageLifecycle StorageLifecycle = StorageLifecycle.Game;
  132. public string StorageKey;
  133. public bool readOnly;
  134. private bool started;
  135. private bool isDeserializing;
  136. private bool isSerializing;
  137. private bool pendingSerialize;
  138. private bool pendingDeserialize;
  139. }
  140. }