AccountFileInfo.cs 11 KB

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