AccountFileInfo.cs 8.2 KB

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