123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- 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<AccountFileInfo>
- {
- public string persistentDataPath = Application.persistentDataPath + "/playerData.txt";
- public PlayerData playerData = new PlayerData();
- /// <summary>
- /// 索引,用于快速查找
- /// </summary>
- private Dictionary<string, int> itemIndexMap = new Dictionary<string, int>();
- public string fileName = "/playerData.txt";
- [System.Serializable]
- public class PlayerData
- {
- public int divineSenseLevel = 1;
-
- public List<EventData> events = new List<EventData>();
-
- public List<ItemData> ItemListData = new List<ItemData>();
-
- /// <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 class EventData
- {
- public int eventID ;
- //完成cishu
- public int completeCount = 0;
- }
- /// <summary>
- /// 重建索引表
- /// 主要给道具用,比较多,调用频繁
- /// </summary>
- private void RestoreIndexMap()
- {
- itemIndexMap.Clear();
- for (int i = 0; i < playerData.ItemListData.Count; i++)
- {
- itemIndexMap[playerData.ItemListData[i].guid] = i;
- }
- }
- /// <summary>
- /// 加载玩家数据,一切数据:从这里开始
- /// </summary>
- 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;
- /// <summary>
- /// 保存item数据
- /// </summary>
- /// <param name="itemInfo"></param>
- /// <param name="now">是否立即保存</param>
- /// <param name="zeroDel">count为0时,是否删除</param>
- 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();
- // }
- /// <summary>
- /// 清除空数据,重建索引
- /// </summary>
- 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
- }
- /// <summary>
- /// 不要服务器的话,这里初始化玩家的起始数据
- /// </summary>
- 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
- {
- /// <summary>
- /// 英雄基础信息
- /// </summary>
- public int heroModelId;
- /// <summary>
- /// 英雄等级
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 装备数据
- /// </summary>
- [System.Serializable]
- public class EqData
- {
- // public string guid;
- // public int count;
- /// <summary>
- /// HeroBasicEquipConfig ID
- /// </summary>
- public int zyEqId;
- // public int dropLv;
- public int quality;
- /// <summary>
- /// 是否穿了(职业装备)
- /// </summary>
- public bool isEquip;
- // /// <summary>
- // /// 穿在哪个职业身上
- // /// </summary>
- // public int zy;
- }
- }
|