Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Runtime.Serialization.Formatters.Binary;
  4. using System.Threading;
  5. using Facebook.Unity;
  6. using Islands.Upgrades;
  7. using Logging.amplitude;
  8. using utils;
  9. using Ui;
  10. using UnityEditor;
  11. using UnityEngine;
  12.  
  13. namespace User
  14. {
  15. public class UserFactory
  16. {
  17. public static UserFactory instance = null;
  18. static Mutex SaveWriteMutex = new Mutex();
  19. private User CurrentUser = null;
  20. private string SaveName = "/UserSave.gd";
  21. private string SaveNameBackup = "/UserSaveBackup.gd";
  22.  
  23. public string StoragePath
  24. {
  25. get
  26. {
  27. var path = Application.persistentDataPath;
  28. return path;
  29. }
  30. }
  31. public static UserFactory GetInstance()
  32. {
  33. if (instance == null)
  34. {
  35. instance = new UserFactory();
  36. }
  37.  
  38. return instance;
  39. }
  40.  
  41. public User GetCurrentUser()
  42. {
  43. if (CurrentUser == null)
  44. {
  45. CurrentUser = Load();
  46. }
  47.  
  48. return CurrentUser;
  49. }
  50.  
  51. public static User User()
  52. {
  53. return GetInstance().GetCurrentUser();
  54. }
  55.  
  56. public void Save(User UserForSave)
  57. {
  58. SaveWriteMutex.WaitOne();
  59. CurrentUser = UserForSave;
  60. try
  61. {
  62. var file = File.Create(this.StoragePath + SaveName);
  63.  
  64. BinaryFormatter bf = new BinaryFormatter();
  65. bf.Serialize(file, UserForSave);
  66. file.Close();
  67. if (CheckSaveFile(this.StoragePath + SaveName))
  68. {
  69. ChangeBackUp();
  70. }
  71. }
  72. catch (Exception ex)
  73. {
  74. Debug.Log("Save File Error");
  75. var path = this.StoragePath + "/CrushReport" + DateTime.Now.TotalSeconds() +".txt";
  76. using (StreamWriter sw = File.CreateText(path))
  77. {
  78. sw.Write(ex.Message + "\n" + ex.StackTrace + "\n" + ex.ToString());
  79. sw.Flush();
  80. }
  81. }
  82. SaveWriteMutex.ReleaseMutex();
  83. }
  84.  
  85. private void ChangeBackUp()
  86. {
  87. if (File.Exists(this.StoragePath + SaveNameBackup))
  88. {
  89. File.Copy(this.StoragePath +SaveName, this.StoragePath + SaveNameBackup, true);
  90. }
  91. else
  92. {
  93. File.Copy(this.StoragePath +SaveName, this.StoragePath + SaveNameBackup);
  94. }
  95. }
  96.  
  97. private bool CheckSaveFile(string FilePath)
  98. {
  99. try
  100. {
  101. var file = File.Open(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
  102. BinaryFormatter bf = new BinaryFormatter();
  103. var loadedUser = (User) bf.Deserialize(file);
  104. file.Close();
  105. }
  106. catch (Exception ex)
  107. {
  108. return false;
  109. }
  110.  
  111. return true;
  112. }
  113.  
  114. public void RemoveSave(string SavePath = null)
  115. {
  116. if (SavePath == null)
  117. {
  118. SavePath = SaveName;
  119. }
  120.  
  121. File.Delete(this.StoragePath + SavePath);
  122. #if UNITY_EDITOR
  123. UnityEditor.AssetDatabase.Refresh();
  124. #endif
  125. CurrentUser = null;
  126. }
  127.  
  128. private User Load()
  129. {
  130. User loadedUser = null;
  131.  
  132. try
  133. {
  134. var file = File.Open(this.StoragePath + SaveName, FileMode.OpenOrCreate);
  135. BinaryFormatter bf = new BinaryFormatter();
  136. loadedUser = (User) bf.Deserialize(file);
  137. file.Close();
  138. }
  139. catch (Exception ex)
  140. {
  141. if (File.Exists(this.StoragePath + SaveNameBackup))
  142. {
  143. var file = File.Open(this.StoragePath + SaveNameBackup, FileMode.OpenOrCreate);
  144. BinaryFormatter bf = new BinaryFormatter();
  145. loadedUser = (User) bf.Deserialize(file);
  146. file.Close();
  147. }
  148. }
  149.  
  150. Debug.Log("LOAD " + this.StoragePath + SaveName);
  151. if (loadedUser == null)
  152. {
  153. loadedUser = this.CreateUser();
  154. }
  155. else
  156. {
  157. loadedUser.IsStored = true;
  158. }
  159.  
  160.  
  161. return loadedUser;
  162. }
  163.  
  164.  
  165. protected User CreateUser()
  166. {
  167. var user = new User();
  168. user.TimestampToGiveSpins = TimeUtil.unixTimestamp;
  169. user.RegisterTime = TimeUtil.unixTimestamp;
  170. user.UpgradeVersionId = GameMono.Instance.UpgradeMilestones.VersionId;
  171. //Set defulat Values
  172. foreach (Resource id in Enum.GetValues(typeof(Resource)))
  173. {
  174.  
  175. var resCfg = GameMono.Instance.GetItemById(id);
  176. if (resCfg!=null)
  177. {
  178. user.Res.SetResourceCount(resCfg.id, resCfg.DefaultValue);
  179. }
  180. }
  181.  
  182. return user;
  183. }
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement