123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- using UnityEngine;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Xml;
- using LitJson;
- public class LocalSaver
- {
- public const string KEY_ID = "ida";
- public const string KEY_NO_BINDING = "nb";
- public const string KEY_EQUIPED = "eq";
- public const string KEY_EQUIPED_STYLE = "eqs";
- public const string KEY_CHAT_IDS = "ci";
- public const string KEY_CHAT = "ch";
- public const string KEY_CHAT_TEMP_ID = "ct";
- public LocalSaver ()
- {
- }
- private static LocalSaver instance;
- public static LocalSaver GetInstance()
- {
- if(instance == null)
- instance = new LocalSaver();
- return instance;
- }
- public void DeleteAll()
- {
- PlayerPrefsManager.DeleteAll();
- }
- public int LoadId()
- {
- if (PlayerPrefsManager.HasKey (KEY_ID))
- return PlayerPrefsManager.GetInt (KEY_ID);
- return 0;
- }
- public void SaveId(int id)
- {
- PlayerPrefsManager.Set (KEY_ID, id);
- }
- public int noAutoBinding
- {
- set
- {
- PlayerPrefsManager.Set (KEY_NO_BINDING + Session.myUserId, value);
- }
- get
- {
- if (PlayerPrefsManager.HasKey (KEY_NO_BINDING + Session.myUserId))
- return PlayerPrefsManager.GetInt (KEY_NO_BINDING + Session.myUserId);
- return 0;
- }
- }
- private string GetChatIdsKey(int userId)
- {
- return KEY_CHAT_IDS + "_" + userId;
- }
- private string GetChatKey(int id, int userId)
- {
- return KEY_CHAT + "_" + userId + "_" + id;
- }
- public int NewTempChatId(int userId)
- {
- string key = KEY_CHAT_TEMP_ID + "_" + userId;
- int tempId = PlayerPrefsManager.GetInt (key) + 1;
- PlayerPrefsManager.Set (key, tempId);
- return -tempId;
- }
- public void AddChat(ChatMsg chatMsg, int userId)
- {
- string keyId = GetChatKey (chatMsg.id, userId);
- PlayerPrefsManager.Set (keyId, chatMsg.GetJson().ToJson());
- AddChatId (chatMsg.id, userId);
- }
- private void AddChatId(int id, int userId)
- {
- string key = GetChatIdsKey (userId);
- string chatIds = PlayerPrefsManager.GetString (key);
- if (StringUtil.Empty (chatIds)) {
- chatIds = id.ToString ();
- } else {
- chatIds += "|" + id.ToString ();
- }
- PlayerPrefsManager.Set (key, chatIds);
- if(!HasChatId(id))
- chatIdDict.Add (id, false);
- }
- public void RemoveChat(int id, int userId)
- {
- string key = GetChatIdsKey (userId);
- string keyChat = GetChatKey (id, userId);
- string chatIds = PlayerPrefsManager.GetString (key);
- int index = chatIds.IndexOf (id.ToString());
- if(index >= 0)
- {
- if (chatIds.Length > id.ToString ().Length) {
- if (index + id.ToString ().Length == chatIds.Length) {
- chatIds = chatIds.Replace ("|" + id.ToString (), "");
- } else {
- chatIds = chatIds.Replace (id.ToString () + "|", "");
- }
- } else {
- chatIds = "";
- }
- PlayerPrefsManager.Set (key, chatIds);
- PlayerPrefsManager.DeleteKey (keyChat);
- chatIdDict.Remove (id);
- }
- }
- public void ReplaceChat(int origin, ChatMsg replaceMsg, int userId)
- {
- string originChatKey = KEY_CHAT + "_" + userId + "_" + origin;
- string replaceChatKey = KEY_CHAT + "_" + userId + "_" + replaceMsg.id;
- string key = KEY_CHAT_IDS + "_" + userId.ToString ();
- string oldChatIds = PlayerPrefsManager.GetString (key);
- string newChatIds = oldChatIds.Replace (origin.ToString(), replaceMsg.id.ToString());
- if (!newChatIds.Equals (oldChatIds)) {
- PlayerPrefsManager.DeleteKey (originChatKey);
- PlayerPrefsManager.Set (key, newChatIds);
- PlayerPrefsManager.Set (replaceChatKey, replaceMsg.GetJson().ToJson());
- chatIdDict.Remove (origin);
- if(!HasChatId(replaceMsg.id))
- chatIdDict.Add (replaceMsg.id, false);
- }
- }
- public JsonData LoadChat(int userId)
- {
- chatIdDict.Clear ();
- JsonData list = JsonMapper.ToObject ("[]");
- string key = GetChatIdsKey (userId);
- if (!PlayerPrefsManager.HasKey (key))
- return list;
- string idStr = PlayerPrefsManager.GetString (key);
- int[] idArr = StringUtil.SplitToInt (idStr, '|');
- for(int i=0; i<idArr.Length; i++)
- {
- int id = idArr [i];
- string keyChat = GetChatKey (id, userId);
- if(PlayerPrefsManager.HasKey(keyChat))
- {
- string chatStr = PlayerPrefsManager.GetString (keyChat);
- try
- {
- JsonData json = JsonMapper.ToObject(chatStr);
- list.Add (json);
- }
- catch(System.Exception e)
- {
- Debuger.LogException (e);
- }
- }
- if(!HasChatId(id))
- chatIdDict.Add (id, false);
- }
- return list;
- }
- private Dictionary<int, bool> chatIdDict = new Dictionary<int, bool>();
- public bool HasChatId(int id)
- {
- return chatIdDict.ContainsKey (id);
- }
- private string GetEquipedKey(int userId, int craftId)
- {
- return KEY_EQUIPED + "_" + userId + "_" + craftId;
- }
- public List<int> LoadEquiped(int userId, int craftId)
- {
- List<int> list = new List<int> ();
- string key = GetEquipedKey (userId, craftId);
- if (PlayerPrefsManager.HasKey (key)) {
- string value = PlayerPrefsManager.GetString (key);
- Debuger.Log ("Load Equiped [" + key + "] : " + value);
- int[] valueArr = StringUtil.SplitToInt (value, '|');
- for(int i=0; i<valueArr.Length; i++)
- {
- list.Add (valueArr[i]);
- }
- }
- return list;
- }
- public void SaveEquiped(int craftId, List<Equipment> list)
- {
- string str = "";
- for(int i=0; i<list.Count; i++)
- {
- if (i > 0)
- str += "|";
- Equipment equipment = list [i];
- if(equipment != null)
- str += list [i].GetData ().id;
- else
- str += "0";
- }
- string key = GetEquipedKey (Session.myUserId, craftId);
- Debuger.Log ("Save Equiped [" + key + "] : "+str);
- PlayerPrefsManager.Set (key, str);
- }
- private string GetEquipedStyleKey(int userId, int craftId)
- {
- return KEY_EQUIPED_STYLE + "_" + userId + "_" + craftId;
- }
- public EquipedData.StyleType LoadEquipedStyle(int userId, int craftId)
- {
- string key = GetEquipedStyleKey (userId, craftId);
- if(!PlayerPrefsManager.HasKey(key))
- {
- return EquipedData.StyleType.Recommand;
- }
- int equipedStyleCode = PlayerPrefsManager.GetInt (key);
- EquipedData.StyleType equipedStyle = EquipedData.GetSytleTypeByCode (equipedStyleCode);
- Debuger.Log ("Load EquipedStyle [" + key + "] : "+equipedStyle);
- return equipedStyle;
- }
- public void SaveEquipedStyle(int userId, int craftId, EquipedData.StyleType equipedStyle)
- {
- string key = GetEquipedStyleKey (userId, craftId);
- Debuger.Log ("Save EquipedStyle [" + key + "] : "+equipedStyle);
- PlayerPrefsManager.Set (key, equipedStyle.GetHashCode ());
- }
- }
|