using Fie.Object; using Fie.Scene; using GameDataEditor; using System.Collections.Generic; using System.Linq; using UnityEngine; namespace Fie.Manager { [FieManagerExists(FieManagerExistSceneFlag.NEVER_DESTROY)] public sealed class FieSaveManager : FieManagerBehaviour { private static string password = string.Empty; private static string directory = "FieData"; private static string fileName = "FieSaveData.bin"; private static string backupFileName = "FieSaveData_Backup.bin"; public static List debugSkills = new List(); public Dictionary snapshottedExpList = new Dictionary(); public Dictionary currentExpList = new Dictionary(); private FieSaveData _onMemorySaveData = new FieSaveData(); public FieSaveData onMemorySaveData { get { return _onMemorySaveData; } set { _onMemorySaveData = value; } } protected override void StartUpEntity() { LoadAllSaveData(); FieManagerBehaviour.I.FiePreparedForLoadSceneEvent += I_FiePreparedForLoadSceneEvent; } private void I_FiePreparedForLoadSceneEvent(FieSceneBase targetScene) { SaveAllData(); } private void LoadAllSaveData() { TryToLoad(ref _onMemorySaveData.LanguageCode, "LanguageCode"); TryToLoad(ref _onMemorySaveData.PlayerLevel, "PlayerLevel"); TryToLoad(ref _onMemorySaveData.LastLoginedUserName, "LastLoginedUserName"); TryToLoad(ref _onMemorySaveData.LastLoginedPasswordPassword, "LastLoginedPasswordPassword"); TryToLoadDictionary(ref _onMemorySaveData.CharacterExp, "CharacterExp"); TryToLoadDictionary(ref _onMemorySaveData.CharacterSkillPoint, "CharacterSkillPoint"); TryToLoadDictionary(ref _onMemorySaveData.PromotedCount, "PromotedCount"); TryToLoadList(ref _onMemorySaveData.unlockedSkills, "SkillTreeUnlocked"); TryToLoadDictionary(ref _onMemorySaveData.AchivemenetProgress, "AchivemenetProgress"); ValidateSaveDataByMasterData(); } private void ValidateSaveDataByMasterData() { bool flag = false; if (_onMemorySaveData.CharacterExp == null) { _onMemorySaveData.CharacterExp = new Dictionary(); flag = true; } if (_onMemorySaveData.CharacterSkillPoint == null) { _onMemorySaveData.CharacterSkillPoint = new Dictionary(); flag = true; } if (_onMemorySaveData.PromotedCount == null) { _onMemorySaveData.PromotedCount = new Dictionary(); flag = true; } Dictionary allMasterData = FieMasterData.I.GetAllMasterData(); foreach (KeyValuePair item in allMasterData) { if (!_onMemorySaveData.CharacterExp.ContainsKey(item.Value.ID)) { _onMemorySaveData.CharacterExp[item.Value.ID] = 0; flag = true; } if (!_onMemorySaveData.CharacterSkillPoint.ContainsKey(item.Value.ID)) { _onMemorySaveData.CharacterSkillPoint[item.Value.ID] = 0; flag = true; } if (!_onMemorySaveData.PromotedCount.ContainsKey(item.Value.ID)) { _onMemorySaveData.PromotedCount[item.Value.ID] = 0; flag = true; } } if (_onMemorySaveData.AchivemenetProgress == null) { _onMemorySaveData.AchivemenetProgress = new Dictionary(); flag = true; } Dictionary allMasterData2 = FieMasterData.I.GetAllMasterData(); foreach (KeyValuePair item2 in allMasterData2) { if (!_onMemorySaveData.AchivemenetProgress.ContainsKey(item2.Value.ID)) { _onMemorySaveData.AchivemenetProgress[item2.Value.ID] = 0; flag = true; } } if (flag) { SaveAllData(); } } internal FieGameCharacterBuildData GetGameCharacterBuildData(GDEGameCharacterTypeData gameCharacterTypeData) { FieGameCharacterBuildData result = default(FieGameCharacterBuildData); result.levelInfo = GetCharacterLevelInfo(gameCharacterTypeData); result.skillPoint = onMemorySaveData.CharacterSkillPoint[gameCharacterTypeData.ID]; result.promotedCount = onMemorySaveData.PromotedCount[gameCharacterTypeData.ID]; return result; } private void SaveAllData() { Save(_onMemorySaveData.LanguageCode, "LanguageCode"); Save(_onMemorySaveData.PlayerLevel, "PlayerLevel"); Save(_onMemorySaveData.LastLoginedUserName, "LastLoginedUserName"); Save(_onMemorySaveData.LastLoginedPasswordPassword, "LastLoginedPasswordPassword"); SaveDictionary(_onMemorySaveData.CharacterExp, "CharacterExp"); SaveDictionary(_onMemorySaveData.CharacterSkillPoint, "CharacterSkillPoint"); SaveDictionary(_onMemorySaveData.PromotedCount, "PromotedCount"); SaveList(_onMemorySaveData.unlockedSkills, "SkillTreeUnlocked"); SaveDictionary(_onMemorySaveData.AchivemenetProgress, "AchivemenetProgress"); } private void Save(T value, string variableName) { ES2.Save(value, GetIdentifier(variableName)); ES2.Save(value, GetIdentifier(variableName, isBackupMode: true)); } private void SaveList(List value, string variableName) { ES2.Save(value, GetIdentifier(variableName)); ES2.Save(value, GetIdentifier(variableName, isBackupMode: true)); } private void SaveDictionary(Dictionary value, string variableName) { ES2.Save(value, GetIdentifier(variableName)); ES2.Save(value, GetIdentifier(variableName, isBackupMode: true)); } private bool IsExists(string variableName, bool isBackupMode = false) { return ES2.Exists(GetIdentifier(variableName, isBackupMode)); } private void TryToLoad(ref T variable, string variableName) { if (IsExists(variableName)) { try { variable = ES2.Load(GetIdentifier(variableName)); return; } catch { } if (IsExists(variableName, isBackupMode: true)) { Debug.LogWarning("The save data was not found at master save data. FiE save system will try to load that from backup. The variable name :" + variableName); try { variable = ES2.Load(GetIdentifier(variableName, isBackupMode: true)); } catch { Debug.LogWarning("Faild to load a save data from backup. The save data was completery broken. It might be initialized. The variable name :" + variableName); } } else { Debug.Log("The variable dose not exist on save datas. It might be initialize. The valiable name : " + variableName); } } } private void TryToLoadList(ref List variable, string variableName) { if (IsExists(variableName)) { try { variable = ES2.LoadList(GetIdentifier(variableName)); return; } catch { } if (IsExists(variableName, isBackupMode: true)) { Debug.LogWarning("The save data was not found at master save data. FiE save system will try to load that from backup. The variable name :" + variableName); try { variable = ES2.LoadList(GetIdentifier(variableName, isBackupMode: true)); } catch { Debug.LogWarning("Failed to load a save data from backup. The save data was completery broken. It might be initialized. The variable name :" + variableName); } } else { Debug.Log("The variable dose not exist on save datas. It might be initialize. The valiable name : " + variableName); } } } private void TryToLoadDictionary(ref Dictionary variable, string variableName) { if (IsExists(variableName)) { try { variable = ES2.LoadDictionary(GetIdentifier(variableName)); return; } catch { } if (IsExists(variableName, isBackupMode: true)) { Debug.LogWarning("The save data was not found at master save data. FiE save system will try to load that from backup. The variable name :" + variableName); try { variable = ES2.LoadDictionary(GetIdentifier(variableName, isBackupMode: true)); } catch { Debug.LogWarning("Failed to load a save data from backup. The save data was completery broken. It might be initialized. The variable name :" + variableName); } } else { Debug.Log("The variable dose not exist on save datas. It might be initialize. The valiable name : " + variableName); } } } private string GetIdentifier(string variableName, bool isBackupMode = false) { return directory + "/" + ((!isBackupMode) ? fileName : backupFileName) + "?tag=" + variableName; } private void OnApplicationQuit() { SaveAllData(); } private void OnDestroy() { SaveAllData(); } public void UnlockSkills(params GDESkillTreeData[] unlockSkills) { if (unlockSkills != null && unlockSkills.Length > 0) { for (int i = 0; i < unlockSkills.Length; i++) { if (!_onMemorySaveData.unlockedSkills.Contains(unlockSkills[i].ID)) { _onMemorySaveData.unlockedSkills.Add(unlockSkills[i].ID); } } SaveAllData(); } } public void LockSkills(params GDESkillTreeData[] lockSkills) { if (lockSkills != null && lockSkills.Length > 0 && _onMemorySaveData.unlockedSkills.Count > 0) { List list = new List(); for (int i = 0; i < lockSkills.Length; i++) { if (_onMemorySaveData.unlockedSkills.Contains(lockSkills[i].ID)) { list.Add(lockSkills[i].ID); } } if (list.Count > 0) { for (int j = 0; j < list.Count; j++) { _onMemorySaveData.unlockedSkills.Remove(list[j]); } } SaveAllData(); } } public FieLevelInfo GetCharacterLevelInfo(GDEGameCharacterTypeData gameCharacterTypeData) { return GetCharacterLevelInfo((FieConstValues.FieGameCharacter)gameCharacterTypeData.ID); } public FieLevelInfo GetCharacterLevelInfo(FieConstValues.FieGameCharacter gameCharacterTypeID) { FieLevelInfo result = default(FieLevelInfo); if (!_onMemorySaveData.CharacterExp.ContainsKey((int)gameCharacterTypeID)) { return result; } int totalExp = _onMemorySaveData.CharacterExp[(int)gameCharacterTypeID]; return GetLevelInfoByTotalExp(totalExp); } public void IncreaseOrReduceSkillPoints(GDEGameCharacterTypeData gameCharacterTypeData, int skillPoint) { if (gameCharacterTypeData != null && !(gameCharacterTypeData.Key == string.Empty) && _onMemorySaveData.CharacterSkillPoint.ContainsKey(gameCharacterTypeData.ID)) { int num = _onMemorySaveData.CharacterSkillPoint[gameCharacterTypeData.ID] + skillPoint; if (num < 0) { Debug.LogError("Setted skill point is out of range. Calculated Skill Point : " + skillPoint.ToString()); } num = Mathf.Max(num, 0); _onMemorySaveData.CharacterSkillPoint[gameCharacterTypeData.ID] = num; SaveAllData(); } } public void ApplyBuildDataFromSaveData(int gameCharacterID, ref GDESkillTreeData[] _unlockedSkills) { if (debugSkills != null && debugSkills.Count > 0) { _unlockedSkills = (from skill in debugSkills where skill.GameCharacterType.ID == gameCharacterID select skill).ToArray(); } else { _unlockedSkills = FieMasterData.FindMasterDataList(delegate(GDESkillTreeData data) { for (int i = 0; i < _onMemorySaveData.unlockedSkills.Count; i++) { if (data.GameCharacterType.ID == gameCharacterID && _onMemorySaveData.unlockedSkills[i] == data.ID) { return true; } } return false; }).ToArray(); } } private FieLevelInfo GetDefaultLevelInfoData() { FieLevelInfo result = default(FieLevelInfo); result.level = 1; result.levelCap = 1; result.totalExp = 0; result.requiredExpToNextLevel = 1; result.currentExpToNextLevel = 1; return result; } public FieLevelInfo GetLevelInfoByTotalExp(int totalExp) { FieLevelInfo defaultLevelInfoData = GetDefaultLevelInfoData(); defaultLevelInfoData.totalExp = totalExp; Dictionary allMasterData = FieMasterData.I.GetAllMasterData(); if (allMasterData == null || allMasterData.Count <= 0) { return defaultLevelInfoData; } IEnumerable source = from data in allMasterData orderby data.Value.Level select data.Value; GDELevelTableData[] array = source.ToArray(); if (array == null || array.Length <= 0) { return defaultLevelInfoData; } defaultLevelInfoData.levelCap = array[array.Length - 1].Level; int num = 0; int num2 = 0; bool flag = false; int num3 = 0; for (int i = 0; i < array.Length; i++) { num3 += array[i].ExpRequirement; if (!flag) { num2 = num; num += array[i].ExpRequirement; if (totalExp >= num) { defaultLevelInfoData.level = array[i].Level; } else { flag = true; } } } totalExp = Mathf.Min(totalExp, num3); if (defaultLevelInfoData.level == defaultLevelInfoData.levelCap) { defaultLevelInfoData.requiredExpToNextLevel = 0; defaultLevelInfoData.currentExpToNextLevel = 0; } else { defaultLevelInfoData.requiredExpToNextLevel = num - num2; defaultLevelInfoData.currentExpToNextLevel = totalExp - num2; } return defaultLevelInfoData; } public void ResetCurrentGameData() { currentExpList = new Dictionary(); snapshottedExpList = new Dictionary(); } public void SnapshotCurrentExp(FieGameCharacter gameCharacter, int currentExp) { if (!(gameCharacter == null) && gameCharacter.ownerUser != null) { if (!snapshottedExpList.ContainsKey(gameCharacter.ownerUser.userHash)) { snapshottedExpList[gameCharacter.ownerUser.userHash] = 0; } snapshottedExpList[gameCharacter.ownerUser.userHash] = currentExp; } } public void AddCurrentGameExp(FieGameCharacter gameCharacter, int exp) { if (!(gameCharacter == null) && gameCharacter.ownerUser != null) { if (!currentExpList.ContainsKey(gameCharacter.ownerUser.userHash)) { currentExpList[gameCharacter.ownerUser.userHash] = 0; } Dictionary dictionary; string userHash; (dictionary = currentExpList)[userHash = gameCharacter.ownerUser.userHash] = dictionary[userHash] + exp; } } public Dictionary GetSnapShottedExp() { return snapshottedExpList; } public Dictionary GetCurrentGameExp() { Dictionary dictionary = new Dictionary(); foreach (KeyValuePair currentExp in currentExpList) { if (FieManagerBehaviour.I.getUserNumberByHash(currentExp.Key) != -1) { dictionary.Add(currentExp.Key, currentExp.Value); } } if (dictionary.Count <= 0) { return dictionary; } IOrderedEnumerable> source = from x in dictionary orderby x.Value descending select x; return source.ToDictionary((KeyValuePair pair) => pair.Key, (KeyValuePair pair) => pair.Value); } public void FlushExpToSaveData(FieGameCharacter _gainExpTarget) { if (!(_gainExpTarget == null)) { int num = 0; Dictionary currentGameExp = GetCurrentGameExp(); if (currentGameExp != null && currentGameExp.Count() > 0) { foreach (KeyValuePair item in currentGameExp) { num += item.Value; } if (num > 0) { FieLevelInfo levelInfoByTotalExp = GetLevelInfoByTotalExp(_onMemorySaveData.CharacterExp[(int)_gainExpTarget.getGameCharacterID()]); FieLevelInfo levelInfoByTotalExp2 = GetLevelInfoByTotalExp(_onMemorySaveData.CharacterExp[(int)_gainExpTarget.getGameCharacterID()] + num); if (levelInfoByTotalExp.level < levelInfoByTotalExp2.level) { List list = new List(); for (int i = levelInfoByTotalExp.level; i < levelInfoByTotalExp2.level; i++) { list.Add(i + 1); } int num2 = 0; Dictionary allMasterData = FieMasterData.I.GetAllMasterData(); foreach (int item2 in list) { foreach (KeyValuePair item3 in allMasterData) { if (item3.Value.Level == item2) { num2 += item3.Value.SkillPoint; } } } Dictionary characterSkillPoint; int gameCharacterID; (characterSkillPoint = _onMemorySaveData.CharacterSkillPoint)[gameCharacterID = (int)_gainExpTarget.getGameCharacterID()] = characterSkillPoint[gameCharacterID] + num2; } _onMemorySaveData.CharacterExp[(int)_gainExpTarget.getGameCharacterID()] = levelInfoByTotalExp2.totalExp; SaveAllData(); } } } } public void PromoteGameCharacter(FieConstValues.FieGameCharacter gamecharacterId) { if (onMemorySaveData.CharacterExp.ContainsKey((int)gamecharacterId)) { bool flag = false; int totalExp = onMemorySaveData.CharacterExp[(int)gamecharacterId]; FieLevelInfo levelInfoByTotalExp = GetLevelInfoByTotalExp(totalExp); if (levelInfoByTotalExp.level == levelInfoByTotalExp.levelCap) { flag = true; } if (flag) { onMemorySaveData.CharacterExp[(int)gamecharacterId] = 0; onMemorySaveData.CharacterSkillPoint[(int)gamecharacterId] = 0; onMemorySaveData.unlockedSkills.Clear(); Dictionary promotedCount; int key; (promotedCount = onMemorySaveData.PromotedCount)[key = (int)gamecharacterId] = promotedCount[key] + 1; onMemorySaveData.PlayerLevel++; SaveAllData(); } } } } }