LocalSaver.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Xml;
  7. using LitJson;
  8. public class LocalSaver
  9. {
  10. public const string KEY_ID = "ida";
  11. public const string KEY_NO_BINDING = "nb";
  12. public const string KEY_EQUIPED = "eq";
  13. public const string KEY_EQUIPED_STYLE = "eqs";
  14. public const string KEY_CHAT_IDS = "ci";
  15. public const string KEY_CHAT = "ch";
  16. public const string KEY_CHAT_TEMP_ID = "ct";
  17. public LocalSaver ()
  18. {
  19. }
  20. private static LocalSaver instance;
  21. public static LocalSaver GetInstance()
  22. {
  23. if(instance == null)
  24. instance = new LocalSaver();
  25. return instance;
  26. }
  27. public void DeleteAll()
  28. {
  29. PlayerPrefsManager.DeleteAll();
  30. }
  31. public int LoadId()
  32. {
  33. if (PlayerPrefsManager.HasKey (KEY_ID))
  34. return PlayerPrefsManager.GetInt (KEY_ID);
  35. return 0;
  36. }
  37. public void SaveId(int id)
  38. {
  39. PlayerPrefsManager.Set (KEY_ID, id);
  40. }
  41. public int noAutoBinding
  42. {
  43. set
  44. {
  45. PlayerPrefsManager.Set (KEY_NO_BINDING + Session.myUserId, value);
  46. }
  47. get
  48. {
  49. if (PlayerPrefsManager.HasKey (KEY_NO_BINDING + Session.myUserId))
  50. return PlayerPrefsManager.GetInt (KEY_NO_BINDING + Session.myUserId);
  51. return 0;
  52. }
  53. }
  54. private string GetChatIdsKey(int userId)
  55. {
  56. return KEY_CHAT_IDS + "_" + userId;
  57. }
  58. private string GetChatKey(int id, int userId)
  59. {
  60. return KEY_CHAT + "_" + userId + "_" + id;
  61. }
  62. public int NewTempChatId(int userId)
  63. {
  64. string key = KEY_CHAT_TEMP_ID + "_" + userId;
  65. int tempId = PlayerPrefsManager.GetInt (key) + 1;
  66. PlayerPrefsManager.Set (key, tempId);
  67. return -tempId;
  68. }
  69. public void AddChat(ChatMsg chatMsg, int userId)
  70. {
  71. string keyId = GetChatKey (chatMsg.id, userId);
  72. PlayerPrefsManager.Set (keyId, chatMsg.GetJson().ToJson());
  73. AddChatId (chatMsg.id, userId);
  74. }
  75. private void AddChatId(int id, int userId)
  76. {
  77. string key = GetChatIdsKey (userId);
  78. string chatIds = PlayerPrefsManager.GetString (key);
  79. if (StringUtil.Empty (chatIds)) {
  80. chatIds = id.ToString ();
  81. } else {
  82. chatIds += "|" + id.ToString ();
  83. }
  84. PlayerPrefsManager.Set (key, chatIds);
  85. if(!HasChatId(id))
  86. chatIdDict.Add (id, false);
  87. }
  88. public void RemoveChat(int id, int userId)
  89. {
  90. string key = GetChatIdsKey (userId);
  91. string keyChat = GetChatKey (id, userId);
  92. string chatIds = PlayerPrefsManager.GetString (key);
  93. int index = chatIds.IndexOf (id.ToString());
  94. if(index >= 0)
  95. {
  96. if (chatIds.Length > id.ToString ().Length) {
  97. if (index + id.ToString ().Length == chatIds.Length) {
  98. chatIds = chatIds.Replace ("|" + id.ToString (), "");
  99. } else {
  100. chatIds = chatIds.Replace (id.ToString () + "|", "");
  101. }
  102. } else {
  103. chatIds = "";
  104. }
  105. PlayerPrefsManager.Set (key, chatIds);
  106. PlayerPrefsManager.DeleteKey (keyChat);
  107. chatIdDict.Remove (id);
  108. }
  109. }
  110. public void ReplaceChat(int origin, ChatMsg replaceMsg, int userId)
  111. {
  112. string originChatKey = KEY_CHAT + "_" + userId + "_" + origin;
  113. string replaceChatKey = KEY_CHAT + "_" + userId + "_" + replaceMsg.id;
  114. string key = KEY_CHAT_IDS + "_" + userId.ToString ();
  115. string oldChatIds = PlayerPrefsManager.GetString (key);
  116. string newChatIds = oldChatIds.Replace (origin.ToString(), replaceMsg.id.ToString());
  117. if (!newChatIds.Equals (oldChatIds)) {
  118. PlayerPrefsManager.DeleteKey (originChatKey);
  119. PlayerPrefsManager.Set (key, newChatIds);
  120. PlayerPrefsManager.Set (replaceChatKey, replaceMsg.GetJson().ToJson());
  121. chatIdDict.Remove (origin);
  122. if(!HasChatId(replaceMsg.id))
  123. chatIdDict.Add (replaceMsg.id, false);
  124. }
  125. }
  126. public JsonData LoadChat(int userId)
  127. {
  128. chatIdDict.Clear ();
  129. JsonData list = JsonMapper.ToObject ("[]");
  130. string key = GetChatIdsKey (userId);
  131. if (!PlayerPrefsManager.HasKey (key))
  132. return list;
  133. string idStr = PlayerPrefsManager.GetString (key);
  134. int[] idArr = StringUtil.SplitToInt (idStr, '|');
  135. for(int i=0; i<idArr.Length; i++)
  136. {
  137. int id = idArr [i];
  138. string keyChat = GetChatKey (id, userId);
  139. if(PlayerPrefsManager.HasKey(keyChat))
  140. {
  141. string chatStr = PlayerPrefsManager.GetString (keyChat);
  142. try
  143. {
  144. JsonData json = JsonMapper.ToObject(chatStr);
  145. list.Add (json);
  146. }
  147. catch(System.Exception e)
  148. {
  149. Debuger.LogException (e);
  150. }
  151. }
  152. if(!HasChatId(id))
  153. chatIdDict.Add (id, false);
  154. }
  155. return list;
  156. }
  157. private Dictionary<int, bool> chatIdDict = new Dictionary<int, bool>();
  158. public bool HasChatId(int id)
  159. {
  160. return chatIdDict.ContainsKey (id);
  161. }
  162. private string GetEquipedKey(int userId, int craftId)
  163. {
  164. return KEY_EQUIPED + "_" + userId + "_" + craftId;
  165. }
  166. public List<int> LoadEquiped(int userId, int craftId)
  167. {
  168. List<int> list = new List<int> ();
  169. string key = GetEquipedKey (userId, craftId);
  170. if (PlayerPrefsManager.HasKey (key)) {
  171. string value = PlayerPrefsManager.GetString (key);
  172. Debuger.Log ("Load Equiped [" + key + "] : " + value);
  173. int[] valueArr = StringUtil.SplitToInt (value, '|');
  174. for(int i=0; i<valueArr.Length; i++)
  175. {
  176. list.Add (valueArr[i]);
  177. }
  178. }
  179. return list;
  180. }
  181. public void SaveEquiped(int craftId, List<Equipment> list)
  182. {
  183. string str = "";
  184. for(int i=0; i<list.Count; i++)
  185. {
  186. if (i > 0)
  187. str += "|";
  188. Equipment equipment = list [i];
  189. if(equipment != null)
  190. str += list [i].GetData ().id;
  191. else
  192. str += "0";
  193. }
  194. string key = GetEquipedKey (Session.myUserId, craftId);
  195. Debuger.Log ("Save Equiped [" + key + "] : "+str);
  196. PlayerPrefsManager.Set (key, str);
  197. }
  198. private string GetEquipedStyleKey(int userId, int craftId)
  199. {
  200. return KEY_EQUIPED_STYLE + "_" + userId + "_" + craftId;
  201. }
  202. public EquipedData.StyleType LoadEquipedStyle(int userId, int craftId)
  203. {
  204. string key = GetEquipedStyleKey (userId, craftId);
  205. if(!PlayerPrefsManager.HasKey(key))
  206. {
  207. return EquipedData.StyleType.Recommand;
  208. }
  209. int equipedStyleCode = PlayerPrefsManager.GetInt (key);
  210. EquipedData.StyleType equipedStyle = EquipedData.GetSytleTypeByCode (equipedStyleCode);
  211. Debuger.Log ("Load EquipedStyle [" + key + "] : "+equipedStyle);
  212. return equipedStyle;
  213. }
  214. public void SaveEquipedStyle(int userId, int craftId, EquipedData.StyleType equipedStyle)
  215. {
  216. string key = GetEquipedStyleKey (userId, craftId);
  217. Debuger.Log ("Save EquipedStyle [" + key + "] : "+equipedStyle);
  218. PlayerPrefsManager.Set (key, equipedStyle.GetHashCode ());
  219. }
  220. }