diff --git a/popcorn/Assets/MyGame/Editor/DataDownloader.cs b/popcorn/Assets/MyGame/Editor/DataDownloader.cs new file mode 100644 index 00000000..bbdb8cdb --- /dev/null +++ b/popcorn/Assets/MyGame/Editor/DataDownloader.cs @@ -0,0 +1,132 @@ +using UnityEditor; +using UnityEngine; +using System; +using System.IO; +using System.Collections.Generic; + +public class DataDownloader { + + private static readonly string WebServiceUrl = "https://script.google.com/macros/s/AKfycbyenMGJNEy3M_P37lWZavGe0gwLMI91qiew12XE6kmnsLdcwAI/exec"; + private static readonly string SpreadsheetId = "15ReD1nkGL60QdvJdGOYbjwRXMPIBclhXD5vCWOmbmew"; + private static readonly string LangSpreadsheetId = "1Eq7VNB2MqzdotBwaf71dqZ1MIU_HrEjPxIfNBeFlTr8"; + private static readonly string Password = "hogehogehogeo"; + private static readonly string DirectoryPath = "Assets/MyGame/Resources/Data"; + + // Googleに短時間アクセスで怒られるので仕方なくSleep + private static readonly int SleepTime = 1000; + + [MenuItem("Tools/SpreadsheetTool/DataUpdate")] + static void DataUpdate(){ + List> downloadedDataList = new List>(); + + int count = Const.BaseSheetNameList.Length; + bool isError = false; + foreach(Const.SheetData baseNameData in Const.BaseSheetNameList){ + string fileName = baseNameData.name; + string connectionString = CreateConnectionString(fileName); + DataDownload(connectionString, fileName, (str, data) => { + downloadedDataList.Add(Tuple.Create(fileName, data)); + if(--count <= 0){ + if(!isError){ + Debug.Log("DataDownload is Done!!"); + int result = SaveDownloadedDataList(downloadedDataList, DirectoryPath); + if(result > 0){ + File.WriteAllText(string.Format("{0}/DownloadDate.txt", DirectoryPath), AvoEx.AesEncryptor.Encrypt(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"))); + } + AssetDatabase.Refresh(); + } else{ + Debug.LogError("エラーデータがダウンロードされたので、再度DataUpdateを行なって下さい。"); + } + } + }); + System.Threading.Thread.Sleep(SleepTime); + } + } + + [MenuItem("Tools/SpreadsheetTool/LanguageDataUpdate")] + static void LanguageDataUpdate(){ + List> downloadedDataList = new List>(); + + int count = Const.LanguageSheetNameList.Length; + bool isError = false; + + foreach(Const.SheetData LanguageData in Const.LanguageSheetNameList){ + string fileName = LanguageData.name; + string connectionString = CreateConnectionString(LangSpreadsheetId,fileName); + DataDownload(connectionString, fileName, (str, data) => { + downloadedDataList.Add(Tuple.Create(fileName, data)); + if(--count <= 0){ + if(!isError){ + Debug.Log("DataDownload is Done!!"); + int result = SaveDownloadedDataList(downloadedDataList, DirectoryPath); + if(result > 0){ + File.WriteAllText(string.Format("{0}/DownloadDate.txt", DirectoryPath), AvoEx.AesEncryptor.Encrypt(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"))); + } + AssetDatabase.Refresh(); + } else{ + Debug.LogError("エラーデータがダウンロードされたので、再度DataUpdateを行なって下さい。"); + } + } + }); + System.Threading.Thread.Sleep(SleepTime); + } + } + + private static string CreateConnectionString(string sheetName){ + return CreateConnectionString(SpreadsheetId, sheetName); + } + + private static string CreateConnectionString(string spreadsheetId, string sheetName){ + return WebServiceUrl + "?ssid=" + spreadsheetId + "&sheet=" + sheetName + "&pass=" + Password + "&action=GetData"; + } + + private static void DataDownload(string connectionString, string fileName, Action callback){ + Debug.LogFormat ("BeginDataDownload {0}", fileName); + WWW www = new WWW(connectionString); + + ContinuationManager.Add(() => www.isDone, () => { + if(!string.IsNullOrEmpty(www.error)){ + Debug.Log("WWW failed: " + www.error); + } + Debug.LogFormat("EndDataDownload {0}", fileName); + string result = www.text; + // Keyが空白なセル情報を破棄 + while(result.Contains(",\"\":")){ + int begin = result.IndexOf(",\"\":"); + int end = result.IndexOf("}", begin); + result = result.Remove(begin, end - begin); + } + while(result.Contains(",{\"\":")){ + int begin = result.IndexOf(",{\"\":"); + int end = result.IndexOf("}", begin) + 1; + result = result.Remove(begin, end - begin); + } + Debug.Log(result); + callback(result, AvoEx.AesEncryptor.Encrypt(result)); + }); + } + + private static int SaveDownloadedDataList(List> downloadedDataList, string directoryPath){ + if(!Directory.Exists(directoryPath)){ + Directory.CreateDirectory(directoryPath); + } + int updateCount = 0; + Debug.Log("BeginDataUpdate"); + foreach(var data in downloadedDataList){ + string path = string.Format(directoryPath + "/{0}.txt", data.Item1); + string newData = AvoEx.AesEncryptor.DecryptString(data.Item2); + string oldData = ""; + try { + oldData = AvoEx.AesEncryptor.DecryptString(File.ReadAllText(path)); + } catch(Exception){ + } + if(newData != oldData){ + ++updateCount; + Debug.Log("Update " + data.Item1); + File.WriteAllText(path, data.Item2); + } + } + Debug.Log("EndDataUpdate updateCount:" + updateCount); + return updateCount; + } +} diff --git a/popcorn/Assets/MyGame/Editor/DataDownloader.cs.meta b/popcorn/Assets/MyGame/Editor/DataDownloader.cs.meta new file mode 100644 index 00000000..ff5e17ec --- /dev/null +++ b/popcorn/Assets/MyGame/Editor/DataDownloader.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 2578de5608cf4a5aad2276cf86270849 +timeCreated: 1631181808 \ No newline at end of file diff --git a/popcorn/Assets/MyGame/Scenes/shopping/Scripts/ShopItemPurchaseButton.cs b/popcorn/Assets/MyGame/Scenes/shopping/Scripts/ShopItemPurchaseButton.cs index 0839382c..cf37159d 100644 --- a/popcorn/Assets/MyGame/Scenes/shopping/Scripts/ShopItemPurchaseButton.cs +++ b/popcorn/Assets/MyGame/Scenes/shopping/Scripts/ShopItemPurchaseButton.cs @@ -3,11 +3,6 @@ using UniRx; using UnityEngine; using UnityEngine.UI; -public enum ShopItemType -{ - Consumable, - NonConsumable, -} public class ShopItemPurchaseButton : MonoBehaviour { private ShopItemType itemType; diff --git a/popcorn/Assets/MyGame/Scripts/Const.cs b/popcorn/Assets/MyGame/Scripts/Const.cs index 37d6ea45..f7ff9631 100644 --- a/popcorn/Assets/MyGame/Scripts/Const.cs +++ b/popcorn/Assets/MyGame/Scripts/Const.cs @@ -6,6 +6,8 @@ public static class Const { public static readonly string DefaultAppVersion = "0.1.0"; + // parameter + // tag public static readonly string GameDataTag = "GameData"; public static readonly string VibrationTag = "Vibration"; @@ -16,4 +18,42 @@ public static class Const { public static readonly string LineURL = "http://nav.cx/aVZ8nEK"; public static readonly string FacebookURL = "https://www.facebook.com/usayaApp/"; public static readonly string TwitterURL = "https://twitter.com/usayaApp"; + + // sheet + public static readonly string AvatarDataSheet = "AvatarData"; + public static readonly string ShopDataSheet = "ShopData"; + + // language + public static readonly string LangJapanDataSheet = "LangJapanData"; + public static readonly string LangEnglishDataSheet = "LangEnglishData"; + public static readonly string LangChineseTraditionaldDataSheet = "LangChineseTraditionaldData"; + public static readonly string LangChineseSimplifiedDataSheet = "LangChineseSimplifiedData"; + public static readonly string UILangJapanDataSheet = "UILangJapanData"; + public static readonly string UILangEnglishDataSheet = "UILangEnglishData"; + public static readonly string UILangChineseTraditionaldDataSheet = "UILangChineseTraditionaldData"; + public static readonly string UILangChineseSimplifiedDataSheet = "UILangChineseSimplifiedData"; + + public sealed class SheetData { + public string name; + public string className; + public SheetData(string name) : this(name, name){} + public SheetData(string name, string className){ + this.name = name; + this.className = className; + } + } + public static readonly SheetData[] BaseSheetNameList = new SheetData[]{ + // new SheetData(Const.AvatarDataSheet), + new SheetData(Const.ShopDataSheet), + }; + public static readonly SheetData[] LanguageSheetNameList = new SheetData[]{ + new SheetData(Const.LangJapanDataSheet, "LanguageData"), + // new SheetData(Const.LangEnglishDataSheet, "LanguageData"), + // new SheetData(Const.LangChineseTraditionaldDataSheet, "LanguageData"), + // new SheetData(Const.LangChineseSimplifiedDataSheet, "LanguageData"), + new SheetData(Const.UILangJapanDataSheet, "LanguageData"), + // new SheetData(Const.UILangEnglishDataSheet, "LanguageData"), + // new SheetData(Const.UILangChineseTraditionaldDataSheet, "LanguageData"), + // new SheetData(Const.UILangChineseSimplifiedDataSheet, "LanguageData"), + }; } \ No newline at end of file diff --git a/popcorn/Assets/MyGame/Scripts/SpreadsheetData.cs b/popcorn/Assets/MyGame/Scripts/SpreadsheetData.cs new file mode 100644 index 00000000..4134a5a6 --- /dev/null +++ b/popcorn/Assets/MyGame/Scripts/SpreadsheetData.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + +public enum ItemCategory +{ + Material = 1, + Recipe = 2, + Pan = 3, +} + +public enum ShopItemType +{ + Consumable, + NonConsumable, +} + +[Serializable] +public sealed class ShopData { + public int id; + public int nameId; + public string name; + public int price; + public string text; + public int category; + public int consumeType; + public string prefabName; + public ItemCategory Category => (ItemCategory)category; + public ShopItemType ConsumeType => (ShopItemType)consumeType; +} \ No newline at end of file diff --git a/popcorn/Assets/MyGame/Scripts/SpreadsheetData.cs.meta b/popcorn/Assets/MyGame/Scripts/SpreadsheetData.cs.meta new file mode 100644 index 00000000..c7b32794 --- /dev/null +++ b/popcorn/Assets/MyGame/Scripts/SpreadsheetData.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: b66f3f846b214a52bb597ea8a8b2aca6 +timeCreated: 1631174370 \ No newline at end of file diff --git a/popcorn/Assets/MyGame/Scripts/SpreadsheetDataManager.cs b/popcorn/Assets/MyGame/Scripts/SpreadsheetDataManager.cs new file mode 100644 index 00000000..dc5824f9 --- /dev/null +++ b/popcorn/Assets/MyGame/Scripts/SpreadsheetDataManager.cs @@ -0,0 +1,79 @@ +using UnityEngine; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Reflection; +using System.Linq; +using AvoEx; + +public sealed class SpreadsheetDataManager : SingletonMonoBehaviour { + + private Dictionary> dataDictionary = new Dictionary>(); + + private string downloadDate; + public string DownloadDateString { + get{ return downloadDate; } + } + + void Awake(){ + if(CheckInstance()) return ; + downloadDate = LoadData("DownloadDate"); + } + + public bool LoadAndCache(string worksheetName){ + string typeName = typeof(T).ToString(); + string json = LoadData(worksheetName); + dataDictionary[worksheetName] = JsonHelper.GetJsonArray(json, typeName).ToList(); + return dataDictionary.ContainsKey(worksheetName); + } + public bool ExistsDataList(string worksheetName){ + return dataDictionary.ContainsKey(worksheetName); + } + public List GetBaseDataList(string worksheetName){ + if(!ExistsDataList(worksheetName)) LoadAndCache(worksheetName); + // HACK + return dataDictionary[worksheetName].Cast().ToList(); + } +#if UNITY_EDITOR + public static List Load(string worksheetName){ + string typeName = typeof(T).ToString(); + string json = LoadData(worksheetName); + return JsonHelper.GetJsonArray(json, typeName).Cast().ToList(); + } + private static string LoadData(string assetName){ +#else + private string LoadData(string assetName){ +#endif + string path = "Data/" + assetName; + TextAsset textAsset = Resources.Load(path); + return AesEncryptor.DecryptString(textAsset.text); + } +} + +public static class JsonHelper{ + public static object[] GetJsonArray(string json, string typeName){ + return GetJsonArray(json, Type.GetType(typeName)); + } + public static object[] GetJsonArray(string json, Type type){ + // JsonHelper型 + Type jh = typeof(JsonHelper); + // GetJsonArray Method + MethodInfo mi = jh.GetMethod("JsonToArray"); + // GetJsonArray Method + MethodInfo miConstructed = mi.MakeGenericMethod(type); + + string[] args = { json }; + object result = miConstructed.Invoke(null, args); + + return ((IEnumerable)result).Cast().ToArray(); + } + public static T[] JsonToArray(string json){ + string newJson = "{ \"array\": " + json + "}"; + return JsonUtility.FromJson>(newJson).array; + } + + [Serializable] + private class Wrapper{ + public T[] array = null; + } +} diff --git a/popcorn/Assets/MyGame/Scripts/SpreadsheetDataManager.cs.meta b/popcorn/Assets/MyGame/Scripts/SpreadsheetDataManager.cs.meta new file mode 100644 index 00000000..751c172a --- /dev/null +++ b/popcorn/Assets/MyGame/Scripts/SpreadsheetDataManager.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 992ca0f9e68a447b8f3d9ea096088ae4 +timeCreated: 1631173558 \ No newline at end of file