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; #if UNITY_WEIXINMINIGAME using WeChatWASM; #endif public class AccountFileInfo : Singleton { public string persistentDataPath = Application.persistentDataPath + "/playerData.txt"; public PlayerData playerData = new PlayerData(); /// /// 索引,用于快速查找 /// private Dictionary itemIndexMap = new Dictionary(); public string fileName = "/playerData.txt"; [System.Serializable] public class PlayerData { public List ItemListData = new List(); /// /// 关卡进度 /// public int levelBattle = 1; /// /// 是否全部阵亡一次 /// public bool isAllHeroDie; /// /// 装备的GUID /// public long eqGUID = 0; /// /// 战斗引导下标 /// public int combatGuideIndex = 0; // /// // /// 后备英雄(共享等级) // /// // public List HeroListInBackDatas = new List(); } /// /// 重建索引表 /// 主要给道具用,比较多,调用频繁 /// private void RestoreIndexMap() { itemIndexMap.Clear(); for (int i = 0; i < playerData.ItemListData.Count; i++) { itemIndexMap[playerData.ItemListData[i].guid] = i; } } /// /// 加载玩家数据,一切数据:从这里开始 /// 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 //初始化索引 RestoreIndexMap(); } private int lastHeroIdx = 0; private int lastItemIdx = 0; /// /// 保存item数据 /// /// /// 是否立即保存 /// count为0时,是否删除 public void SaveItemData(ItemInfo itemInfo, bool now = true) { if (itemIndexMap.TryGetValue(itemInfo.guid, out int index)) { // //count为零且需要立即删除时,移除Data并移除索引(也有count为0,不删除的情况,就走else的逻辑) // if (itemInfo.count.Value == 0 && zeroDel) // { // playerData.ItemListData.RemoveAt(index); // itemIndexMap.Remove(itemInfo.guid); // } // else // { // // 快速找到索引,直接修改 // playerData.ItemListData[index] = itemInfo.ToItemData(); // } // 快速找到索引,直接修改 playerData.ItemListData[index] = itemInfo.ToItemData(); } else { foreach (ItemData itemData in playerData.ItemListData) { if (itemData.guid == itemInfo.guid) { LogTool.Error("??? guid重复了" + itemInfo.itemID + "-" + itemData.guid); } } // 添加新数据并更新索引表 playerData.ItemListData.Add(itemInfo.ToItemData()); itemIndexMap[itemInfo.guid] = playerData.ItemListData.Count - 1; } if (now) { SavePlayerData(); } // 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 SaveEqGUID() // { // SavePlayerData(); // } /// /// 清除空数据,重建索引 /// private void CleanEmptyData() { // 从后往前遍历,这样删除元素,可以边循环,边删除 for (int i = playerData.ItemListData.Count - 1; i >= 0; i--) { // string ss = ""; ItemData itemData = playerData.ItemListData[i]; if (itemData.itemCount == 0) { //不是装备,删除 if (itemData.eqData == null || itemData.eqData.zyEqId == 0) { playerData.ItemListData.RemoveAt(i); // ss = "删除道具:" + itemData.guid; } else if (itemData.eqData != null && itemData.eqData.zyEqId != 0 && !itemData.eqData.isEquip) { // ss = "删除装备:" + itemData.guid; playerData.ItemListData.RemoveAt(i); } // LogTool.Log(ss); } } RestoreIndexMap(); } public void SavePlayerData(bool clean = false) { if (clean) { CleanEmptyData(); } 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 } /// /// 不要服务器的话,这里初始化玩家的起始数据 /// public void ClearInitPlayerData() { ItemData coin = new ItemData(GlobalParam.Item_Coin_ID, 0); ItemData diamond = new ItemData(GlobalParam.Item_Diamond_ID); ItemData heroExp = new ItemData(GlobalParam.Item_HeroExp_ID, 0); playerData.ItemListData.Add(coin); playerData.ItemListData.Add(diamond); playerData.ItemListData.Add(heroExp); // BagController.Instance.AddCoin(10000); // playerData.HeroListData.Add(heroData2); // playerData.HeroListData.Add(heroData3); // playerData.HeroListData.Add(heroData4); SavePlayerData(); } [System.Serializable] public class HeroData { /// /// 英雄基础信息 /// public int heroModelId; /// /// 英雄等级 /// public int heroPowerId; } [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; } } /// /// 装备数据 /// [System.Serializable] public class EqData { // public string guid; // public int count; /// /// HeroBasicEquipConfig ID /// public int zyEqId; // public int dropLv; public int quality; /// /// 是否穿了(职业装备) /// public bool isEquip; // /// // /// 穿在哪个职业身上 // /// // public int zy; } }