AccountFileInfo.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. using WeChatWASM;
  11. public class AccountFileInfo : Singleton<AccountFileInfo>
  12. {
  13. public string persistentDataPath = Application.persistentDataPath + "/playerData.txt";
  14. public PlayerData playerData = new PlayerData();
  15. /// <summary>
  16. /// 索引,用于快速查找
  17. /// </summary>
  18. private Dictionary<string, int> itemIndexMap = new Dictionary<string, int>();
  19. public string fileName = "/playerData.txt";
  20. [System.Serializable]
  21. public class PlayerData
  22. {
  23. public List<ItemData> ItemListData = new List<ItemData>();
  24. /// <summary>
  25. /// 英雄数据
  26. /// </summary>
  27. public List<HeroData> HeroListData = new List<HeroData>();
  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. /// <summary>
  50. /// 初始化索引表
  51. /// 主要给道具用,比较多,调用频繁
  52. /// </summary>
  53. private void InitializeIndexMap()
  54. {
  55. itemIndexMap.Clear();
  56. for (int i = 0; i < playerData.ItemListData.Count; i++)
  57. {
  58. itemIndexMap[playerData.ItemListData[i].guid] = i;
  59. }
  60. }
  61. /// <summary>
  62. /// 加载玩家数据,一切数据:从这里开始
  63. /// </summary>
  64. public void LoadPlayerData()
  65. {
  66. #if UNITY_WEIXINMINIGAME && !UNITY_EDITOR
  67. persistentDataPath = WX.env.USER_DATA_PATH + fileName;
  68. WXFileSystemManager wxFileSystemManager = WX.GetFileSystemManager();
  69. if (wxFileSystemManager.AccessSync(persistentDataPath).Equals("access:ok"))
  70. {
  71. string data = wxFileSystemManager.ReadFileSync(persistentDataPath, "utf8");
  72. playerData = new PlayerData();
  73. JsonUtility.FromJsonOverwrite(data, playerData);
  74. }
  75. else
  76. {
  77. ClearInitPlayerData();
  78. SavePlayerData();
  79. }
  80. #else
  81. if (!File.Exists(persistentDataPath))
  82. {
  83. LogTool.Log("没有文件: " + persistentDataPath);
  84. ClearInitPlayerData();
  85. // File.Create(persistentDataPath).Close();
  86. }
  87. LogTool.Log("读取=文件: " + persistentDataPath);
  88. StreamReader sr = File.OpenText(persistentDataPath);
  89. string data = sr.ReadToEnd();
  90. sr.Close();
  91. playerData = new PlayerData();
  92. JsonUtility.FromJsonOverwrite(data, playerData);
  93. #endif
  94. //初始化索引
  95. InitializeIndexMap();
  96. }
  97. private int lastHeroIdx = 0;
  98. /// <summary>
  99. /// 保存英雄数据
  100. /// </summary>
  101. /// <param name="heroInfo"></param>
  102. public void SaveHeroData(HeroInfo heroInfo)
  103. {
  104. var lastHeroData = playerData.HeroListData[lastHeroIdx];
  105. if (heroInfo.modelID == lastHeroData.heroModelId)
  106. {
  107. playerData.HeroListData[lastHeroIdx] = heroInfo.ToHeroData();
  108. SavePlayerData();
  109. return;
  110. }
  111. for (int i = 0; i < playerData.HeroListData.Count; i++)
  112. {
  113. HeroData heroData = playerData.HeroListData[i];
  114. if (heroData.heroModelId == heroInfo.modelID)
  115. {
  116. playerData.HeroListData[i] = heroInfo.ToHeroData();
  117. //存下来,用于快速查找
  118. lastHeroIdx = i;
  119. SavePlayerData();
  120. return;
  121. }
  122. }
  123. }
  124. private int lastItemIdx = 0;
  125. /// <summary>
  126. /// 保存item数据
  127. /// </summary>
  128. /// <param name="itemInfo"></param>
  129. public void SaveItemData(ItemInfo itemInfo)
  130. {
  131. if (itemIndexMap.TryGetValue(itemInfo.guid, out int index))
  132. {
  133. // 快速找到索引,直接修改
  134. playerData.ItemListData[index] = itemInfo.ToItemData();
  135. }
  136. else
  137. {
  138. // 添加新数据并更新索引表
  139. playerData.ItemListData.Add(itemInfo.ToItemData());
  140. itemIndexMap[itemInfo.guid] = playerData.ItemListData.Count - 1;
  141. }
  142. SavePlayerData();
  143. // if (itemInfo.guid == playerData.ItemListData[lastItemIdx].guid)
  144. // {
  145. // playerData.ItemListData[lastItemIdx] = itemInfo.ToItemData();
  146. // SavePlayerData();
  147. // return;
  148. // }
  149. //
  150. // for (int i = 0; i < playerData.ItemListData.Count; i++)
  151. // {
  152. // ItemData itemData = playerData.ItemListData[i];
  153. // if (itemData.guid == itemInfo.guid)
  154. // {
  155. // playerData.ItemListData[i] = itemInfo.ToItemData();
  156. // //存下来,用于快速查找
  157. // lastItemIdx = i;
  158. // SavePlayerData();
  159. // return;
  160. // }
  161. // }
  162. // playerData.ItemListData.Add(itemInfo.ToItemData());
  163. // SavePlayerData();
  164. }
  165. // public void SaveEqGUID()
  166. // {
  167. // SavePlayerData();
  168. // }
  169. public void SavePlayerData()
  170. {
  171. if (!string.IsNullOrEmpty(persistentDataPath))
  172. {
  173. string playerSettingJson = JsonManager.ToJson(playerData);
  174. #if UNITY_WEIXINMINIGAME&& !UNITY_EDITOR
  175. WXFileSystemManager wxFileSystemManager = WX.GetFileSystemManager();
  176. wxFileSystemManager.WriteFileSync(persistentDataPath, playerSettingJson, "utf8");
  177. #else
  178. File.WriteAllText(persistentDataPath, playerSettingJson);
  179. #endif
  180. }
  181. }
  182. public void DeleteFile(string filePath)
  183. {
  184. #if UNITY_WEIXINMINIGAME&& !UNITY_EDITOR
  185. playerData = new PlayerData();
  186. SavePlayerData();
  187. ClearInitPlayerData();
  188. #else
  189. if (File.Exists(filePath))
  190. {
  191. File.Delete(filePath); // 删除文件
  192. LogTool.Log($"文件已删除:{filePath}");
  193. }
  194. else
  195. {
  196. LogTool.Log($"文件不存在:{filePath}");
  197. }
  198. #endif
  199. }
  200. /// <summary>
  201. /// 不要服务器的话,这里初始化玩家的起始数据
  202. /// </summary>
  203. public void ClearInitPlayerData()
  204. {
  205. ItemData coin = new ItemData(GlobalParam.Item_Coin_ID);
  206. ItemData diamond = new ItemData(GlobalParam.Item_Diamond_ID);
  207. ItemData heroExp = new ItemData(GlobalParam.Item_HeroExp_ID);
  208. playerData.ItemListData.Add(coin);
  209. playerData.ItemListData.Add(diamond);
  210. playerData.ItemListData.Add(heroExp);
  211. HeroData heroData1 = new HeroData
  212. {
  213. heroModelId = 105,
  214. heroPowerId = 1,
  215. heroPromoteId = 3,
  216. isLead = true,
  217. };
  218. playerData.HeroListData.Add(heroData1);
  219. // playerData.HeroListData.Add(heroData2);
  220. // playerData.HeroListData.Add(heroData3);
  221. // playerData.HeroListData.Add(heroData4);
  222. SavePlayerData();
  223. }
  224. [System.Serializable]
  225. public class HeroData
  226. {
  227. /// <summary>
  228. /// 英雄基础信息
  229. /// </summary>
  230. public int heroModelId;
  231. /// <summary>
  232. /// 英雄等级
  233. /// </summary>
  234. public int heroPowerId;
  235. /// <summary>
  236. /// 星级
  237. /// </summary>
  238. public int heroPromoteId;
  239. /// <summary>
  240. /// 是否为主力
  241. /// </summary>
  242. public bool isLead;
  243. }
  244. [System.Serializable]
  245. public class ItemData
  246. {
  247. public int itemId;
  248. public long itemCount;
  249. public string guid;
  250. public EqData eqData;
  251. public ItemData(int itemId, long itemCount = 0, string guid = "")
  252. {
  253. this.itemId = itemId;
  254. this.itemCount = itemCount;
  255. if (string.IsNullOrEmpty(guid))
  256. {
  257. this.guid = itemId.ToString();
  258. }
  259. else
  260. {
  261. this.guid = guid;
  262. }
  263. // eqData = null;
  264. }
  265. }
  266. /// <summary>
  267. /// 装备数据
  268. /// </summary>
  269. [System.Serializable]
  270. public class EqData
  271. {
  272. // public string guid;
  273. // public int count;
  274. public int zyEqId;
  275. // public int dropLv;
  276. public int quality;
  277. /// <summary>
  278. /// 是否穿了(职业装备)
  279. /// </summary>
  280. public bool isEquip;
  281. // /// <summary>
  282. // /// 穿在哪个职业身上
  283. // /// </summary>
  284. // public int zy;
  285. }
  286. }