From 88f3f85cbdcfba83c8b322fc4bd5defeed537b0d Mon Sep 17 00:00:00 2001 From: kimura Date: Mon, 2 Aug 2021 16:32:09 +0900 Subject: [PATCH] =?UTF-8?q?DataManager=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Assets/MyGame/Scripts/GameDataManager.cs | 271 ++++++++++++++++++ .../MyGame/Scripts/GameDataManager.cs.meta | 3 + 2 files changed, 274 insertions(+) create mode 100644 popcorn/Assets/MyGame/Scripts/GameDataManager.cs create mode 100644 popcorn/Assets/MyGame/Scripts/GameDataManager.cs.meta diff --git a/popcorn/Assets/MyGame/Scripts/GameDataManager.cs b/popcorn/Assets/MyGame/Scripts/GameDataManager.cs new file mode 100644 index 00000000..ced32190 --- /dev/null +++ b/popcorn/Assets/MyGame/Scripts/GameDataManager.cs @@ -0,0 +1,271 @@ +using UnityEngine; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; + +public enum AdRewardType { + GachaCoin = 1, + Avatar = 2, + Count, +} + +[DataContract(Name="KeyValueOfintint")] +public sealed class KeyValueOfintint { + [DataMember] + public int Key; + [DataMember] + public int Value; + + public KeyValueOfintint(KeyValuePair value){ + Key = value.Key; + Value = value.Value; + } +} + +[DataContract] +public sealed class GameData { + [DataMember(Name = "Data0")] + public long beginPlayTime = DateTime.UtcNow.ToBinary(); + [DataMember(Name = "Data1")] + public string lastPlayedVersion = Const.DefaultAppVersion; + [DataMember(Name = "Data2")] + public long lastPlayTime = DateTime.UtcNow.ToBinary(); // 最終プレイ日時 + [DataMember(Name = "Data3")] + public int dailyLoginCount = 0; + [DataMember(Name = "Data4")] + public int adCount; + [DataMember(Name = "Data5")] + public int stage = 1; + // [DataMember(Name = "Data6")] + // public int avatarId = Const.DefaultAvatarId; + [DataMember(Name = "Data7")] + public int coin; + // [DataMember(Name = "Data8")] + // private int[] avatarIdArray; + // public List avatarIdList; + // [DataMember(Name = "Data9")] + // public int gachaCount; + // [DataMember(Name = "Data10")] + // private int[] newAvatarIdArray; + // public List newAvatarIdList; + // [DataMember(Name = "Data11")] + // public bool isRandomAvatar; + [DataMember(Name = "Data12")] + public List<(PlantLineType type, CornFieldRank level)> PlantLineTypes; + [DataMember(Name = "Data13")] + public CornFieldRank FieldLevel = CornFieldRank.Rank1; + [DataMember(Name = "Data14")] + public int MachineLevel = 1; + [DataMember(Name = "Data15")] + public List SeedlingDataList = new List(); + [DataMember(Name = "Data16")] public int cornSeed; + + // public void ChangeAvatar(AvatarData avatarData){ + // newAvatarIdList.Remove(avatarData.id); + // } + // public void AddAvatar(AvatarData avatarData){ + // if(HasAvatar(avatarData)) return ; + // avatarIdList.Add(avatarData.id); + // newAvatarIdList.Add(avatarData.id); + // } + // public bool HasAvatar(AvatarData avatarData){ + // return avatarIdList.Exists(id => id == avatarData.id); + // } + // public void SetRandom(bool isRandom, AvatarCategory category){ + // } + // public bool GetRandom(AvatarCategory category){ + // return false; + // } + + + +// [OnDeserialized] Unityだと機能しない + public void OnDeserialized(){ + PreDeserialize(); + + // if(avatarIdList.Count < 1){ + // avatarIdList.Add(Const.DefaultAvatarId); + // } + // 追加した要素の初期化用 + // TODO Release前にまっさらにする + } + public void PreDeserialize(){ + // avatarIdList = avatarIdArray == null ? new List() : avatarIdArray.ToList(); + // newAvatarIdList = newAvatarIdArray == null ? new List() : newAvatarIdArray.ToList(); + // lastAdRewardTimeList = lastAdRewardTimeArray == null ? new List() : lastAdRewardTimeArray.ToList(); + } + private Dictionary ArrayToDictionary(KeyValueOfintint[] array){ + var dictionary = new Dictionary(); + if(array != null && array.Length > 0){ + foreach(var kv in array){ + dictionary.Add(kv.Key, kv.Value); + } + } + return dictionary; + } + public void PreSerialize(){ + // avatarIdArray = avatarIdList.ToArray(); + // newAvatarIdArray = newAvatarIdList.ToArray(); + // lastAdRewardTimeArray = lastAdRewardTimeList.ToArray(); + } + private KeyValueOfintint[] DictionaryToArray(Dictionary dictionary){ + var array = new KeyValueOfintint[dictionary.Count]; + int index = 0; + foreach(var kvp in dictionary){ + array[index++] = new KeyValueOfintint(kvp); + } + return array; + } +} + +public static class GameDataManager { + + public enum LoadState { + NoFile, + ExistsClashed, + DeserializeFailed, + Old, + Succeeded, + } + +// private static readonly int DiskFullTextId = 201; +// private static readonly int UnauthorizedAccessExceptionTextId = 202; + + private static GameData dataCache = null; + public static GameData GameData { + get{ return dataCache ?? (dataCache = LoadGameData()); } + } + public static bool IsLoaded { + get{ return dataCache != null; } + } + public static bool ExistsGameData(){ + return UsayaStorageManager.Exists(UsayaStorageFilename.Main_Data, Const.GameDataTag) || UsayaStorageManager.ExistsBackup(UsayaStorageFilename.Main_Data, Const.GameDataTag); + } + private static GameData LoadGameData(){ + GameData data = null; + var loadState = LoadState.ExistsClashed; + try{ + if(UsayaStorageManager.Exists(UsayaStorageFilename.Main_Data, Const.GameDataTag)){ + loadState = LoadState.DeserializeFailed; + data = StringToGameData(UsayaStorageManager.Load(UsayaStorageFilename.Main_Data, Const.GameDataTag)); + loadState = LoadState.Succeeded; + }else{ + loadState = LoadState.NoFile; + } + }catch(Exception){ + }finally{ + var backup = LoadGameDataBackup(loadState); + if(loadState != LoadState.Succeeded || (data.stage < backup.stage)){ + data = backup; + if(loadState == LoadState.Succeeded){ +// AnalyticsManager.DataLoadWarning(LoadState.Old); + } + } + } + data.OnDeserialized(); + + if(Application.version.CompareVersion(data.lastPlayedVersion) != 0){ + data.lastPlayedVersion = Application.version; + } + return data; + } + private static GameData LoadGameDataBackup(LoadState mainLoadState){ + GameData result = null; + var loadState = LoadState.ExistsClashed; + try{ + if(UsayaStorageManager.ExistsBackup(UsayaStorageFilename.Main_Data, Const.GameDataTag)){ + loadState = LoadState.DeserializeFailed; + result = StringToGameData(UsayaStorageManager.LoadBackup(UsayaStorageFilename.Main_Data, Const.GameDataTag)); + loadState = LoadState.Succeeded; + if(mainLoadState != LoadState.Succeeded){ +// AnalyticsManager.DataLoadWarning(mainLoadState); + } + }else{ + loadState = LoadState.NoFile; + } + }catch(Exception){ + }finally{ + if(mainLoadState != LoadState.Succeeded && loadState != LoadState.NoFile){ +// AnalyticsManager.DataLoadWarning(mainLoadState, loadState); + } + if(loadState != LoadState.Succeeded){ + result = CreateData(); + } + } + return result; + } + + private static GameData CreateData(){ + var data = new GameData(); + data.lastPlayedVersion = Application.version; + return data; + } + public static bool OverWriteGameData(string dataString){ + try{ + dataCache = StringToGameData(dataString); + return true; + }catch{ + return false; + } + } + public static void SaveGameData(){ + string data = GameDataToString(GameData); + bool isMainSaved = false; + Exception exceptionMain = null; + try{ + UsayaStorageManager.Save(UsayaStorageFilename.Main_Data, Const.GameDataTag, data); + isMainSaved = true; + }catch(Exception e){ + // TODO ログサーバが出来たらExceptionを送りたい + exceptionMain = e; + }finally{ + bool isBackupSaved = false; + Exception exceptionBackup = null; + try{ + UsayaStorageManager.SaveBackup(UsayaStorageFilename.Main_Data, Const.GameDataTag, data); + isBackupSaved = true; + }catch(Exception e){ + // TODO ログサーバが出来たらExceptionを送りたい + exceptionBackup = e; + }finally{ + if(!isMainSaved && !isBackupSaved){ +// AnalyticsManager.DataSaveWarning("GameData", SaveState.MainAndBackupFailed); +// SaveFailedDialogManager.ShowDialog(exceptionMain, exceptionBackup); + }else if(!isMainSaved){ +// AnalyticsManager.DataSaveWarning("GameData", SaveState.MainFailed); + }else if(!isBackupSaved){ +// AnalyticsManager.DataSaveWarning("GameData", SaveState.BackupFailed); + } + } + } + } + public static string GetGameDataString(){ + return GameDataToString(GameData); + } + private static string GameDataToString(GameData data){ + data.PreSerialize(); + return AvoEx.AesEncryptor.Encrypt( + StringExtensions.DataToXML(data) + .Replace("KeyValueOfintint", "KVOii") + .Replace("KeyValueOfintlong", "KVOil") + .Replace("KeyValueOfintInfiniteFloat", "KVOiIF") +// .Compression() + ); + } + private static GameData StringToGameData(string str){ + str = AvoEx.AesEncryptor.DecryptString(str) +// .DeCompression() + .Replace("KVOii", "KeyValueOfintint") + .Replace("KVOil", "KeyValueOfintlong") + .Replace("KVOiIF", "KeyValueOfintInfiniteFloat"); + return StringExtensions.XMLToData(str); + } + +#if UNITY_EDITOR || DEVELOPMENT_BUILD + public static void ResetData(){ + dataCache = CreateData(); + dataCache.OnDeserialized(); + } +#endif +} diff --git a/popcorn/Assets/MyGame/Scripts/GameDataManager.cs.meta b/popcorn/Assets/MyGame/Scripts/GameDataManager.cs.meta new file mode 100644 index 00000000..6223cb55 --- /dev/null +++ b/popcorn/Assets/MyGame/Scripts/GameDataManager.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: fed351b203484d798684ff10710c9ca7 +timeCreated: 1627822558 \ No newline at end of file