PersistentStorage.cs 9.8 KB

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