123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using Core.Utility;
- using Fort23.UTool;
- using GameLogic.Bag;
- using GameLogic.Hero;
- using UnityEngine;
- using Utility;
- using WeChatWASM;
- public class AccountFileInfo : Singleton<AccountFileInfo>
- {
- public string persistentDataPath = Application.persistentDataPath + "/playerData.txt";
- public PlayerData playerData = new PlayerData();
- public string fileName = "/playerData.txt";
- [System.Serializable]
- public class PlayerData
- {
- public List<ItemData> ItemListData = new List<ItemData>();
- /// <summary>
- /// 英雄数据
- /// </summary>
- public List<HeroData> HeroListData = new List<HeroData>();
- /// <summary>
- /// 装备列表
- /// </summary>
- // public List<EqData> EqListData = new List<EqData>();
- /// <summary>
- /// 关卡进度
- /// </summary>
- public int levelBattle = 1;
- /// <summary>
- /// 是否全部阵亡一次
- /// </summary>
- public bool isAllHeroDie;
-
- /// <summary>
- /// 装备的GUID
- /// </summary>
- public long eqGUID = 0;
- /// <summary>
- /// 战斗引导下标
- /// </summary>
- public int combatGuideIndex = 0;
- // /// <summary>
- // /// 后备英雄(共享等级)
- // /// </summary>
- // public List<HeroData> HeroListInBackDatas = new List<HeroData>();
- }
-
-
-
- public void LoadPlayerData()
- {
- #if UNITY_WEIXINMINIGAME && !UNITY_EDITOR
- persistentDataPath = WX.env.USER_DATA_PATH + fileName;
- WXFileSystemManager wxFileSystemManager = WX.GetFileSystemManager();
- if (wxFileSystemManager.AccessSync(persistentDataPath).Equals("access:ok"))
- {
- string data = wxFileSystemManager.ReadFileSync(persistentDataPath, "utf8");
- playerData = new PlayerData();
- JsonUtility.FromJsonOverwrite(data, playerData);
- }
- else
- {
- ClearInitPlayerData();
- SavePlayerData();
- }
- #else
- if (!File.Exists(persistentDataPath))
- {
- LogTool.Log("没有文件: " + persistentDataPath);
- ClearInitPlayerData();
- // File.Create(persistentDataPath).Close();
- }
- LogTool.Log("读取=文件: " + persistentDataPath);
- StreamReader sr = File.OpenText(persistentDataPath);
- string data = sr.ReadToEnd();
- sr.Close();
- playerData = new PlayerData();
- JsonUtility.FromJsonOverwrite(data, playerData);
- #endif
- }
- private int lastHeroIdx = 0;
- /// <summary>
- /// 保存英雄数据
- /// </summary>
- /// <param name="heroInfo"></param>
- public void SaveHeroData(HeroInfo heroInfo)
- {
- var lastHeroData = playerData.HeroListData[lastHeroIdx];
- if (heroInfo.modelID == lastHeroData.heroModelId)
- {
- playerData.HeroListData[lastHeroIdx] = heroInfo.ToHeroData();
- SavePlayerData();
- return;
- }
- for (int i = 0; i < playerData.HeroListData.Count; i++)
- {
- HeroData heroData = playerData.HeroListData[i];
- if (heroData.heroModelId == heroInfo.modelID)
- {
- playerData.HeroListData[i] = heroInfo.ToHeroData();
- //存下来,用于快速查找
- lastHeroIdx = i;
- SavePlayerData();
- return;
- }
- }
- }
- private int lastItemIdx = 0;
- /// <summary>
- /// 保存item数据
- /// </summary>
- /// <param name="itemInfo"></param>
- public void SaveItemData(ItemInfo itemInfo)
- {
- if (itemInfo.guid == playerData.ItemListData[lastItemIdx].guid)
- {
- playerData.ItemListData[lastItemIdx] = itemInfo.ToItemData();
- SavePlayerData();
- return;
- }
- for (int i = 0; i < playerData.ItemListData.Count; i++)
- {
- ItemData itemData = playerData.ItemListData[i];
- if (itemData.guid == itemInfo.guid)
- {
- playerData.ItemListData[i] = itemInfo.ToItemData();
- //存下来,用于快速查找
- lastItemIdx = i;
- SavePlayerData();
- return;
- }
- }
-
- playerData.ItemListData.Add(itemInfo.ToItemData());
- SavePlayerData();
- }
- public void SaveEqData()
- {
-
- }
- public void SaveEqGUID()
- {
- SavePlayerData();
- }
- public void SavePlayerData()
- {
- if (!string.IsNullOrEmpty(persistentDataPath))
- {
- string playerSettingJson = JsonManager.ToJson(playerData);
- #if UNITY_WEIXINMINIGAME&& !UNITY_EDITOR
- WXFileSystemManager wxFileSystemManager = WX.GetFileSystemManager();
- wxFileSystemManager.WriteFileSync(persistentDataPath, playerSettingJson, "utf8");
- #else
- File.WriteAllText(persistentDataPath, playerSettingJson);
- #endif
- }
- }
- public void DeleteFile(string filePath)
- {
- #if UNITY_WEIXINMINIGAME&& !UNITY_EDITOR
- playerData = new PlayerData();
- SavePlayerData();
- ClearInitPlayerData();
- #else
- if (File.Exists(filePath))
- {
- File.Delete(filePath); // 删除文件
- LogTool.Log($"文件已删除:{filePath}");
- }
- else
- {
- LogTool.Log($"文件不存在:{filePath}");
- }
- #endif
- }
- /// <summary>
- /// 不要服务器的话,这里初始化玩家的起始数据
- /// </summary>
- public void ClearInitPlayerData()
- {
- ItemData coin = new ItemData(GlobalParam.Item_Coin_ID);
- ItemData diamond = new ItemData(GlobalParam.Item_Diamond_ID);
- ItemData heroExp = new ItemData(GlobalParam.Item_HeroExp_ID);
- playerData.ItemListData.Add(coin);
- playerData.ItemListData.Add(diamond);
- playerData.ItemListData.Add(heroExp);
- HeroData heroData1 = new HeroData
- {
- heroModelId = 105,
- heroPowerId = 1,
- heroPromoteId = 3,
- isLead = true,
- };
-
- playerData.HeroListData.Add(heroData1);
- // playerData.HeroListData.Add(heroData2);
- // playerData.HeroListData.Add(heroData3);
- // playerData.HeroListData.Add(heroData4);
- SavePlayerData();
- }
- [System.Serializable]
- public class HeroData
- {
- /// <summary>
- /// 英雄基础信息
- /// </summary>
- public int heroModelId;
- /// <summary>
- /// 英雄等级
- /// </summary>
- public int heroPowerId;
- /// <summary>
- /// 星级
- /// </summary>
- public int heroPromoteId;
- /// <summary>
- /// 是否为主力
- /// </summary>
- public bool isLead;
- }
- [System.Serializable]
- public class ItemData
- {
- public int itemId;
- public long itemCount;
- public string guid;
- public EqData eqData;
- public ItemData(int itemId, long itemCount = 0, string guid = "")
- {
- this.itemId = itemId;
- this.itemCount = itemCount;
- if (string.IsNullOrEmpty(guid))
- {
- this.guid = itemId.ToString();
- }
- else
- {
- this.guid = guid;
- }
- // eqData = null;
- }
- }
-
- /// <summary>
- /// 装备数据
- /// </summary>
- [System.Serializable]
- public class EqData
- {
- // public string guid;
- // public int count;
- public int itemConfigID;
- public int dropLv;
- public int quality;
- /// <summary>
- /// 是否穿了(职业装备)
- /// </summary>
- public bool isEquip;
-
- // /// <summary>
- // /// 穿在哪个职业身上
- // /// </summary>
- // public int zy;
- }
- }
|