No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

228 líneas
6.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Text;
  5. using SUISS.Core;
  6. using SUISS.Storage;
  7. using UnityEngine;
  8. public class CloudStorage
  9. {
  10. public CloudStorage()
  11. {
  12. this.Deserialize();
  13. Storage.NewGameEvent += this.OnNewGame;
  14. }
  15. public bool CloudStorageAvailable
  16. {
  17. get
  18. {
  19. return false;
  20. }
  21. }
  22. public bool Authenticated
  23. {
  24. get
  25. {
  26. return false;
  27. }
  28. }
  29. public CloudGameState LastCloudGameState { get; private set; }
  30. public ConflictResolver.ConflictSolution LastResolutionResult { get; private set; }
  31. private bool CanPush
  32. {
  33. get
  34. {
  35. return this.Authenticated && SingletonMonobehaviour<CIGGameState>.IsAvailable && this.LastResolutionResult == ConflictResolver.ConflictSolution.None;
  36. }
  37. }
  38. private LocalGameState LocalGameState
  39. {
  40. get
  41. {
  42. return new LocalGameState(new Version("3.0.6"), this._saveStateGuid, this._installGuid, Storage.Get(StorageLifecycle.Game).UpdateTimestamp == DateTime.MinValue);
  43. }
  44. }
  45. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  46. public event CloudStorage.ConflictResolutionCompleteEventHandler ConflictResolutionComplete;
  47. private void FireConflictResolutionCompleteEvent(ConflictResolver.ConflictSolution result)
  48. {
  49. if (this.ConflictResolutionComplete != null)
  50. {
  51. this.ConflictResolutionComplete(result);
  52. }
  53. }
  54. //[DebuggerBrowsable(DebuggerBrowsableState.Never)]
  55. public event CloudStorage.ConflictResolvedEventHandler ConflictResolved;
  56. private void FireConflictResolvedEvent()
  57. {
  58. if (this.ConflictResolved != null)
  59. {
  60. this.ConflictResolved();
  61. }
  62. }
  63. public void Authenticate(Action<bool> onComplete)
  64. {
  65. {
  66. onComplete(false);
  67. }
  68. }
  69. public void Deauthenticate()
  70. {
  71. }
  72. public void Push()
  73. {
  74. if (!this.CanPush)
  75. {
  76. return;
  77. }
  78. this._saveStateGuid = Guid.NewGuid().ToString();
  79. this.Serialize();
  80. Dictionary<string, object> storage = new Dictionary<string, object>
  81. {
  82. {
  83. "Game",
  84. Storage.Get(StorageLifecycle.Game).Root
  85. },
  86. {
  87. "GameVersion",
  88. "3.0.6"
  89. },
  90. {
  91. "Level",
  92. SingletonMonobehaviour<CIGGameState>.Instance.Level
  93. },
  94. {
  95. "SaveStateGuid",
  96. this._saveStateGuid
  97. },
  98. {
  99. "SaveGuid",
  100. this._saveGuid
  101. },
  102. {
  103. "InstallGuid",
  104. this._installGuid
  105. }
  106. };
  107. string s = Storage.DictToString(storage);
  108. byte[] bytes = Encoding.UTF8.GetBytes(s);
  109. }
  110. public void Pull(Action<bool> onComplete)
  111. {
  112. if (!this.Authenticated)
  113. {
  114. onComplete(false);
  115. return;
  116. }
  117. }
  118. public void OnConflictResolved()
  119. {
  120. this.LastResolutionResult = ConflictResolver.ConflictSolution.None;
  121. this.FireConflictResolvedEvent();
  122. }
  123. public void LoadLastCloudSaveGame()
  124. {
  125. if (this.LastCloudGameState != null)
  126. {
  127. this._saveGuid = this.LastCloudGameState.SaveGuid;
  128. this.Serialize();
  129. StorageController.ReplaceGameDictionary(this.LastCloudGameState.SaveGame);
  130. }
  131. }
  132. private void Serialize()
  133. {
  134. Dictionary<string, object> dictionary = Storage.Get(StorageLifecycle.Forever).GetDictionary("CloudStorage");
  135. dictionary["SaveGuid"] = this._saveGuid;
  136. dictionary["SaveStateGuid"] = this._saveStateGuid;
  137. dictionary["InstallGuid"] = this._installGuid;
  138. }
  139. private void Deserialize()
  140. {
  141. Dictionary<string, object> dictionary = Storage.Get(StorageLifecycle.Forever).GetDictionary("CloudStorage");
  142. this._saveGuid = dictionary.GetString("SaveGuid", Guid.NewGuid().ToString());
  143. this._saveStateGuid = dictionary.GetString("SaveStateGuid", null);
  144. this._installGuid = dictionary.GetString("InstallGuid", Guid.NewGuid().ToString());
  145. }
  146. private void OnNewGame()
  147. {
  148. this._saveGuid = Guid.NewGuid().ToString();
  149. this.Serialize();
  150. }
  151. private void OnPullCompleted(Dictionary<string, object> cloudStorageDict, Action<bool> onComplete)
  152. {
  153. this.LastCloudGameState = this.GetCloudGameState(cloudStorageDict);
  154. if (this.LastCloudGameState != null)
  155. {
  156. this.LastResolutionResult = ConflictResolver.Resolve(this.LocalGameState, this.LastCloudGameState);
  157. this.FireConflictResolutionCompleteEvent(this.LastResolutionResult);
  158. onComplete(true);
  159. }
  160. else
  161. {
  162. UnityEngine.Debug.LogErrorFormat("{0} Cloud gamestate is null. Cloud data may have been invalid.", new object[]
  163. {
  164. "[Cloud Save]"
  165. });
  166. onComplete(false);
  167. }
  168. }
  169. private CloudGameState GetCloudGameState(Dictionary<string, object> cloudStorageDict)
  170. {
  171. Dictionary<string, object> gameDict = (Dictionary<string, object>)cloudStorageDict.Get("Game", null);
  172. string @string = cloudStorageDict.GetString("SaveGuid", null);
  173. string string2 = cloudStorageDict.GetString("SaveStateGuid", null);
  174. string string3 = cloudStorageDict.GetString("InstallGuid", null);
  175. int @int = cloudStorageDict.GetInt("Level", 1);
  176. Version gameVersion = new Version(cloudStorageDict.GetString("GameVersion", "0.0.0"));
  177. return new CloudGameState(gameDict, @string, string2, string3, @int, gameVersion);
  178. }
  179. private const string LogPrefix = "[Cloud Save]";
  180. private const string GameRootKey = "Game";
  181. private const string CloudStorageKey = "CloudStorage";
  182. private const string GameVersionKey = "GameVersion";
  183. private const string LevelKey = "Level";
  184. private const string SaveStateGuidKey = "SaveStateGuid";
  185. private const string SaveGuidKey = "SaveGuid";
  186. private const string InstallGuidKey = "InstallGuid";
  187. private string _saveGuid;
  188. private string _saveStateGuid;
  189. private string _installGuid;
  190. public delegate void ConflictResolutionCompleteEventHandler(ConflictResolver.ConflictSolution result);
  191. public delegate void ConflictResolvedEventHandler();
  192. }