AccountFileInfo.cs 9.7 KB

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