AccountFileInfo.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Core.Utility;
  5. using Fort23.UTool;
  6. using GameLogic.Bag;
  7. using GameLogic.Hero;
  8. using UnityEngine;
  9. using Utility;
  10. #if UNITY_WEIXINMINIGAME
  11. using WeChatWASM;
  12. #endif
  13. public class AccountFileInfo : Singleton<AccountFileInfo>
  14. {
  15. public string persistentDataPath = Application.persistentDataPath + "/playerData.txt";
  16. public PlayerData playerData = new PlayerData();
  17. /// <summary>
  18. /// 索引,用于快速查找
  19. /// </summary>
  20. private Dictionary<string, int> itemIndexMap = new Dictionary<string, int>();
  21. public string fileName = "/playerData.txt";
  22. [System.Serializable]
  23. public class PlayerData
  24. {
  25. //地图探索进度
  26. public int senceExplorationProgress;
  27. public int divineSenseLevel = 1;
  28. public int divineSensePoint = 0;
  29. public int divineSenseexp = 0;
  30. public List<EventData> events = new List<EventData>();
  31. public List<ItemData> ItemListData = new List<ItemData>();
  32. /// <summary>
  33. /// 关卡进度
  34. /// </summary>
  35. public int levelBattle = 1;
  36. /// <summary>
  37. /// 是否全部阵亡一次
  38. /// </summary>
  39. public bool isAllHeroDie;
  40. /// <summary>
  41. /// 装备的GUID
  42. /// </summary>
  43. public long eqGUID = 0;
  44. /// <summary>
  45. /// 战斗引导下标
  46. /// </summary>
  47. public int combatGuideIndex = 0;
  48. // /// <summary>
  49. // /// 后备英雄(共享等级)
  50. // /// </summary>
  51. // public List<HeroData> HeroListInBackDatas = new List<HeroData>();
  52. }
  53. public class EventData
  54. {
  55. public int eventID ;
  56. //完成cishu
  57. public int completeCount = 0;
  58. }
  59. /// <summary>
  60. /// 重建索引表
  61. /// 主要给道具用,比较多,调用频繁
  62. /// </summary>
  63. private void RestoreIndexMap()
  64. {
  65. itemIndexMap.Clear();
  66. for (int i = 0; i < playerData.ItemListData.Count; i++)
  67. {
  68. itemIndexMap[playerData.ItemListData[i].guid] = i;
  69. }
  70. }
  71. /// <summary>
  72. /// 加载玩家数据,一切数据:从这里开始
  73. /// </summary>
  74. public void LoadPlayerData()
  75. {
  76. #if UNITY_WEIXINMINIGAME && !UNITY_EDITOR
  77. persistentDataPath = WX.env.USER_DATA_PATH + fileName;
  78. WXFileSystemManager wxFileSystemManager = WX.GetFileSystemManager();
  79. if (wxFileSystemManager.AccessSync(persistentDataPath).Equals("access:ok"))
  80. {
  81. string data = wxFileSystemManager.ReadFileSync(persistentDataPath, "utf8");
  82. playerData = new PlayerData();
  83. JsonUtility.FromJsonOverwrite(data, playerData);
  84. }
  85. else
  86. {
  87. ClearInitPlayerData();
  88. SavePlayerData();
  89. }
  90. #else
  91. if (!File.Exists(persistentDataPath))
  92. {
  93. LogTool.Log("没有文件: " + persistentDataPath);
  94. ClearInitPlayerData();
  95. // File.Create(persistentDataPath).Close();
  96. }
  97. LogTool.Log("读取=文件: " + persistentDataPath);
  98. StreamReader sr = File.OpenText(persistentDataPath);
  99. string data = sr.ReadToEnd();
  100. sr.Close();
  101. playerData = new PlayerData();
  102. JsonUtility.FromJsonOverwrite(data, playerData);
  103. #endif
  104. //初始化索引
  105. RestoreIndexMap();
  106. }
  107. private int lastHeroIdx = 0;
  108. private int lastItemIdx = 0;
  109. /// <summary>
  110. /// 保存item数据
  111. /// </summary>
  112. /// <param name="itemInfo"></param>
  113. /// <param name="now">是否立即保存</param>
  114. /// <param name="zeroDel">count为0时,是否删除</param>
  115. public void SaveItemData(ItemInfo itemInfo, bool now = true)
  116. {
  117. if (itemIndexMap.TryGetValue(itemInfo.guid, out int index))
  118. {
  119. // //count为零且需要立即删除时,移除Data并移除索引(也有count为0,不删除的情况,就走else的逻辑)
  120. // if (itemInfo.count.Value == 0 && zeroDel)
  121. // {
  122. // playerData.ItemListData.RemoveAt(index);
  123. // itemIndexMap.Remove(itemInfo.guid);
  124. // }
  125. // else
  126. // {
  127. // // 快速找到索引,直接修改
  128. // playerData.ItemListData[index] = itemInfo.ToItemData();
  129. // }
  130. // 快速找到索引,直接修改
  131. playerData.ItemListData[index] = itemInfo.ToItemData();
  132. }
  133. else
  134. {
  135. foreach (ItemData itemData in playerData.ItemListData)
  136. {
  137. if (itemData.guid == itemInfo.guid)
  138. {
  139. LogTool.Error("??? guid重复了" + itemInfo.itemID + "-" + itemData.guid);
  140. }
  141. }
  142. // 添加新数据并更新索引表
  143. playerData.ItemListData.Add(itemInfo.ToItemData());
  144. itemIndexMap[itemInfo.guid] = playerData.ItemListData.Count - 1;
  145. }
  146. if (now)
  147. {
  148. SavePlayerData();
  149. }
  150. // if (itemInfo.guid == playerData.ItemListData[lastItemIdx].guid)
  151. // {
  152. // playerData.ItemListData[lastItemIdx] = itemInfo.ToItemData();
  153. // SavePlayerData();
  154. // return;
  155. // }
  156. //
  157. // for (int i = 0; i < playerData.ItemListData.Count; i++)
  158. // {
  159. // ItemData itemData = playerData.ItemListData[i];
  160. // if (itemData.guid == itemInfo.guid)
  161. // {
  162. // playerData.ItemListData[i] = itemInfo.ToItemData();
  163. // //存下来,用于快速查找
  164. // lastItemIdx = i;
  165. // SavePlayerData();
  166. // return;
  167. // }
  168. // }
  169. // playerData.ItemListData.Add(itemInfo.ToItemData());
  170. // SavePlayerData();
  171. }
  172. // public void SaveEqGUID()
  173. // {
  174. // SavePlayerData();
  175. // }
  176. /// <summary>
  177. /// 清除空数据,重建索引
  178. /// </summary>
  179. private void CleanEmptyData()
  180. {
  181. // 从后往前遍历,这样删除元素,可以边循环,边删除
  182. for (int i = playerData.ItemListData.Count - 1; i >= 0; i--)
  183. {
  184. // string ss = "";
  185. ItemData itemData = playerData.ItemListData[i];
  186. if (itemData.itemCount == 0)
  187. {
  188. //不是装备,删除
  189. if (itemData.eqData == null || itemData.eqData.zyEqId == 0)
  190. {
  191. playerData.ItemListData.RemoveAt(i);
  192. // ss = "删除道具:" + itemData.guid;
  193. }
  194. else if (itemData.eqData != null && itemData.eqData.zyEqId != 0 && !itemData.eqData.isEquip)
  195. {
  196. // ss = "删除装备:" + itemData.guid;
  197. playerData.ItemListData.RemoveAt(i);
  198. }
  199. // LogTool.Log(ss);
  200. }
  201. }
  202. RestoreIndexMap();
  203. }
  204. public void SavePlayerData(bool clean = false)
  205. {
  206. if (clean)
  207. {
  208. CleanEmptyData();
  209. }
  210. if (!string.IsNullOrEmpty(persistentDataPath))
  211. {
  212. string playerSettingJson = JsonManager.ToJson(playerData);
  213. #if UNITY_WEIXINMINIGAME&& !UNITY_EDITOR
  214. WXFileSystemManager wxFileSystemManager = WX.GetFileSystemManager();
  215. wxFileSystemManager.WriteFileSync(persistentDataPath, playerSettingJson, "utf8");
  216. #else
  217. File.WriteAllText(persistentDataPath, playerSettingJson);
  218. #endif
  219. }
  220. }
  221. public void DeleteFile(string filePath)
  222. {
  223. #if UNITY_WEIXINMINIGAME&& !UNITY_EDITOR
  224. playerData = new PlayerData();
  225. SavePlayerData();
  226. ClearInitPlayerData();
  227. #else
  228. if (File.Exists(filePath))
  229. {
  230. File.Delete(filePath); // 删除文件
  231. LogTool.Log($"文件已删除:{filePath}");
  232. }
  233. else
  234. {
  235. LogTool.Log($"文件不存在:{filePath}");
  236. }
  237. #endif
  238. }
  239. /// <summary>
  240. /// 不要服务器的话,这里初始化玩家的起始数据
  241. /// </summary>
  242. public void ClearInitPlayerData()
  243. {
  244. ItemData coin = new ItemData(GlobalParam.Item_Coin_ID, 0);
  245. ItemData diamond = new ItemData(GlobalParam.Item_Diamond_ID);
  246. ItemData heroExp = new ItemData(GlobalParam.Item_HeroExp_ID, 0);
  247. playerData.ItemListData.Add(coin);
  248. playerData.ItemListData.Add(diamond);
  249. playerData.ItemListData.Add(heroExp);
  250. // BagController.Instance.AddCoin(10000);
  251. // playerData.HeroListData.Add(heroData2);
  252. // playerData.HeroListData.Add(heroData3);
  253. // playerData.HeroListData.Add(heroData4);
  254. SavePlayerData();
  255. }
  256. [System.Serializable]
  257. public class HeroData
  258. {
  259. /// <summary>
  260. /// 英雄基础信息
  261. /// </summary>
  262. public int heroModelId;
  263. /// <summary>
  264. /// 英雄等级
  265. /// </summary>
  266. public int heroPowerId;
  267. }
  268. [System.Serializable]
  269. public class ItemData
  270. {
  271. public int itemId;
  272. public long itemCount;
  273. public string guid;
  274. public EqData eqData;
  275. public ItemData(int itemId, long itemCount = 0, string guid = "")
  276. {
  277. this.itemId = itemId;
  278. this.itemCount = itemCount;
  279. if (string.IsNullOrEmpty(guid))
  280. {
  281. this.guid = itemId.ToString();
  282. }
  283. else
  284. {
  285. this.guid = guid;
  286. }
  287. // eqData = null;
  288. }
  289. }
  290. /// <summary>
  291. /// 装备数据
  292. /// </summary>
  293. [System.Serializable]
  294. public class EqData
  295. {
  296. // public string guid;
  297. // public int count;
  298. /// <summary>
  299. /// HeroBasicEquipConfig ID
  300. /// </summary>
  301. public int zyEqId;
  302. // public int dropLv;
  303. public int quality;
  304. /// <summary>
  305. /// 是否穿了(职业装备)
  306. /// </summary>
  307. public bool isEquip;
  308. // /// <summary>
  309. // /// 穿在哪个职业身上
  310. // /// </summary>
  311. // public int zy;
  312. }
  313. }