AccountFileInfo.cs 11 KB

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