AccountFileInfo.cs 11 KB

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