AccountFileInfo.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 SaveEqData()
  136. {
  137. }
  138. public void SaveEqGUID()
  139. {
  140. SavePlayerData();
  141. }
  142. public void SavePlayerData()
  143. {
  144. if (!string.IsNullOrEmpty(persistentDataPath))
  145. {
  146. string playerSettingJson = JsonManager.ToJson(playerData);
  147. #if UNITY_WEIXINMINIGAME&& !UNITY_EDITOR
  148. WXFileSystemManager wxFileSystemManager = WX.GetFileSystemManager();
  149. wxFileSystemManager.WriteFileSync(persistentDataPath, playerSettingJson, "utf8");
  150. #else
  151. File.WriteAllText(persistentDataPath, playerSettingJson);
  152. #endif
  153. }
  154. }
  155. public void DeleteFile(string filePath)
  156. {
  157. #if UNITY_WEIXINMINIGAME&& !UNITY_EDITOR
  158. playerData = new PlayerData();
  159. SavePlayerData();
  160. ClearInitPlayerData();
  161. #else
  162. if (File.Exists(filePath))
  163. {
  164. File.Delete(filePath); // 删除文件
  165. LogTool.Log($"文件已删除:{filePath}");
  166. }
  167. else
  168. {
  169. LogTool.Log($"文件不存在:{filePath}");
  170. }
  171. #endif
  172. }
  173. /// <summary>
  174. /// 不要服务器的话,这里初始化玩家的起始数据
  175. /// </summary>
  176. public void ClearInitPlayerData()
  177. {
  178. ItemData coin = new ItemData(GlobalParam.Item_Coin_ID);
  179. ItemData diamond = new ItemData(GlobalParam.Item_Diamond_ID);
  180. ItemData heroExp = new ItemData(GlobalParam.Item_HeroExp_ID);
  181. playerData.ItemListData.Add(coin);
  182. playerData.ItemListData.Add(diamond);
  183. playerData.ItemListData.Add(heroExp);
  184. HeroData heroData1 = new HeroData
  185. {
  186. heroModelId = 105,
  187. heroPowerId = 1,
  188. heroPromoteId = 3,
  189. isLead = true,
  190. };
  191. playerData.HeroListData.Add(heroData1);
  192. // playerData.HeroListData.Add(heroData2);
  193. // playerData.HeroListData.Add(heroData3);
  194. // playerData.HeroListData.Add(heroData4);
  195. SavePlayerData();
  196. }
  197. [System.Serializable]
  198. public class HeroData
  199. {
  200. /// <summary>
  201. /// 英雄基础信息
  202. /// </summary>
  203. public int heroModelId;
  204. /// <summary>
  205. /// 英雄等级
  206. /// </summary>
  207. public int heroPowerId;
  208. /// <summary>
  209. /// 星级
  210. /// </summary>
  211. public int heroPromoteId;
  212. /// <summary>
  213. /// 是否为主力
  214. /// </summary>
  215. public bool isLead;
  216. }
  217. [System.Serializable]
  218. public class ItemData
  219. {
  220. public int itemId;
  221. public long itemCount;
  222. public string guid;
  223. public EqData eqData;
  224. public ItemData(int itemId, long itemCount = 0, string guid = "")
  225. {
  226. this.itemId = itemId;
  227. this.itemCount = itemCount;
  228. if (string.IsNullOrEmpty(guid))
  229. {
  230. this.guid = itemId.ToString();
  231. }
  232. else
  233. {
  234. this.guid = guid;
  235. }
  236. // eqData = null;
  237. }
  238. }
  239. /// <summary>
  240. /// 装备数据
  241. /// </summary>
  242. [System.Serializable]
  243. public class EqData
  244. {
  245. // public string guid;
  246. // public int count;
  247. public int itemConfigID;
  248. public int dropLv;
  249. public int quality;
  250. /// <summary>
  251. /// 是否穿了(职业装备)
  252. /// </summary>
  253. public bool isEquip;
  254. // /// <summary>
  255. // /// 穿在哪个职业身上
  256. // /// </summary>
  257. // public int zy;
  258. }
  259. }