I2BasePersistentStorage.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using UnityEngine;
  5. namespace I2.Loc
  6. {
  7. public abstract class I2BasePersistentStorage
  8. {
  9. public virtual void SetSetting_String(string key, string value)
  10. {
  11. try
  12. {
  13. int length = value.Length;
  14. int num = 8000;
  15. if (length <= num)
  16. {
  17. PlayerPrefs.SetString(key, value);
  18. }
  19. else
  20. {
  21. int num2 = Mathf.CeilToInt((float)length / (float)num);
  22. for (int i = 0; i < num2; i++)
  23. {
  24. int num3 = num * i;
  25. PlayerPrefs.SetString(string.Format("[I2split]{0}{1}", i, key), value.Substring(num3, Mathf.Min(num, length - num3)));
  26. }
  27. PlayerPrefs.SetString(key, "[$I2#@div$]" + num2);
  28. }
  29. }
  30. catch (Exception)
  31. {
  32. Debug.LogError("Error saving PlayerPrefs " + key);
  33. }
  34. }
  35. public virtual string GetSetting_String(string key, string defaultValue)
  36. {
  37. string result;
  38. try
  39. {
  40. string text = PlayerPrefs.GetString(key, defaultValue);
  41. if (!string.IsNullOrEmpty(text) && text.StartsWith("[I2split]"))
  42. {
  43. int num = int.Parse(text.Substring("[I2split]".Length));
  44. text = string.Empty;
  45. for (int i = 0; i < num; i++)
  46. {
  47. text += PlayerPrefs.GetString(string.Format("[I2split]{0}{1}", i, key), string.Empty);
  48. }
  49. }
  50. result = text;
  51. }
  52. catch (Exception)
  53. {
  54. Debug.LogError("Error loading PlayerPrefs " + key);
  55. result = defaultValue;
  56. }
  57. return result;
  58. }
  59. public virtual void DeleteSetting(string key)
  60. {
  61. try
  62. {
  63. string @string = PlayerPrefs.GetString(key, null);
  64. if (!string.IsNullOrEmpty(@string) && @string.StartsWith("[I2split]"))
  65. {
  66. int num = int.Parse(@string.Substring("[I2split]".Length));
  67. for (int i = 0; i < num; i++)
  68. {
  69. PlayerPrefs.DeleteKey(string.Format("[I2split]{0}{1}", i, key));
  70. }
  71. }
  72. PlayerPrefs.DeleteKey(key);
  73. }
  74. catch (Exception)
  75. {
  76. Debug.LogError("Error deleting PlayerPrefs " + key);
  77. }
  78. }
  79. public virtual void ForceSaveSettings()
  80. {
  81. PlayerPrefs.Save();
  82. }
  83. public virtual bool HasSetting(string key)
  84. {
  85. return PlayerPrefs.HasKey(key);
  86. }
  87. public virtual bool CanAccessFiles()
  88. {
  89. return true;
  90. }
  91. private string UpdateFilename(PersistentStorage.eFileType fileType, string fileName)
  92. {
  93. if (fileType != PersistentStorage.eFileType.Persistent)
  94. {
  95. if (fileType != PersistentStorage.eFileType.Temporal)
  96. {
  97. if (fileType == PersistentStorage.eFileType.Streaming)
  98. {
  99. fileName = Application.streamingAssetsPath + "/" + fileName;
  100. }
  101. }
  102. else
  103. {
  104. fileName = Application.temporaryCachePath + "/" + fileName;
  105. }
  106. }
  107. else
  108. {
  109. fileName = Application.persistentDataPath + "/" + fileName;
  110. }
  111. return fileName;
  112. }
  113. public virtual bool SaveFile(PersistentStorage.eFileType fileType, string fileName, string data, bool logExceptions = true)
  114. {
  115. if (!this.CanAccessFiles())
  116. {
  117. return false;
  118. }
  119. bool result;
  120. try
  121. {
  122. fileName = this.UpdateFilename(fileType, fileName);
  123. File.WriteAllText(fileName, data, Encoding.UTF8);
  124. result = true;
  125. }
  126. catch (Exception ex)
  127. {
  128. if (logExceptions)
  129. {
  130. Debug.LogError(string.Concat(new object[]
  131. {
  132. "Error saving file '",
  133. fileName,
  134. "'\n",
  135. ex
  136. }));
  137. }
  138. result = false;
  139. }
  140. return result;
  141. }
  142. public virtual string LoadFile(PersistentStorage.eFileType fileType, string fileName, bool logExceptions = true)
  143. {
  144. if (!this.CanAccessFiles())
  145. {
  146. return null;
  147. }
  148. string result;
  149. try
  150. {
  151. fileName = this.UpdateFilename(fileType, fileName);
  152. result = File.ReadAllText(fileName, Encoding.UTF8);
  153. }
  154. catch (Exception ex)
  155. {
  156. if (logExceptions)
  157. {
  158. Debug.LogError(string.Concat(new object[]
  159. {
  160. "Error loading file '",
  161. fileName,
  162. "'\n",
  163. ex
  164. }));
  165. }
  166. result = null;
  167. }
  168. return result;
  169. }
  170. public virtual bool DeleteFile(PersistentStorage.eFileType fileType, string fileName, bool logExceptions = true)
  171. {
  172. if (!this.CanAccessFiles())
  173. {
  174. return false;
  175. }
  176. bool result;
  177. try
  178. {
  179. fileName = this.UpdateFilename(fileType, fileName);
  180. File.Delete(fileName);
  181. result = true;
  182. }
  183. catch (Exception ex)
  184. {
  185. if (logExceptions)
  186. {
  187. Debug.LogError(string.Concat(new object[]
  188. {
  189. "Error deleting file '",
  190. fileName,
  191. "'\n",
  192. ex
  193. }));
  194. }
  195. result = false;
  196. }
  197. return result;
  198. }
  199. public virtual bool HasFile(PersistentStorage.eFileType fileType, string fileName, bool logExceptions = true)
  200. {
  201. if (!this.CanAccessFiles())
  202. {
  203. return false;
  204. }
  205. bool result;
  206. try
  207. {
  208. fileName = this.UpdateFilename(fileType, fileName);
  209. result = File.Exists(fileName);
  210. }
  211. catch (Exception ex)
  212. {
  213. if (logExceptions)
  214. {
  215. Debug.LogError(string.Concat(new object[]
  216. {
  217. "Error requesting file '",
  218. fileName,
  219. "'\n",
  220. ex
  221. }));
  222. }
  223. result = false;
  224. }
  225. return result;
  226. }
  227. }
  228. }