AccountFileInfo.cs 7.7 KB

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