AccountFileInfo.cs 9.9 KB

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