AccountFileInfo.cs 10 KB

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