AccountFileInfo.cs 7.8 KB

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