public static void CreateAssets(ConvertSetting s, GlobalCCSettings gSettings) { string settingPath = s.GetDirectoryPath(); string csvPath = CCLogic.GetFilePathRelativesToAssets(settingPath, s.GetCsvPath(gSettings)); TextAsset textAsset = AssetDatabase.LoadAssetAtPath <TextAsset>(csvPath); if (textAsset == null) { Debug.LogError("Not found : " + csvPath); return; } if (s.isEnum) { return; } // csv ファイルから読み込み CsvData csv = CsvLogic.GetValidCsvData(textAsset.text, gSettings); CsvData contents = csv.Slice(gSettings.rowIndexOfContentStart); Field[] fields = CsvLogic.GetFieldsFromHeader(csv, gSettings); // アセットを生成する. AssetsGenerator assetsGenerator = new AssetsGenerator(s, fields, contents); // カスタムアセットタイプを設定する // これはプロジェクト固有のアセットをテーブルでセット出来るようにする. { Type[] customAssetTypes = new Type[gSettings.customAssetTypes.Length]; for (int i = 0; i < customAssetTypes.Length; i++) { if (TryGetTypeWithError(gSettings.customAssetTypes[i], out var type, s.checkFullyQualifiedName)) { customAssetTypes[i] = type; }
/// <summary> /// 指定の sheetId, gid のシートを CsvData としてダウンロードする. /// /// Google Spreadsheet URL に2つのリクエストを送る。 /// まず一つ目は sheetId に対する複数の worksheet を持つ URL へのリクエスト。 /// 二つ目は その sheetId の中で特定の gid を持つ URL へのリクエスト。 /// </summary> public IEnumerator LoadGS(string sheetId, string gid, string apiKey = "", bool useV4 = true) { isSuccess = false; if (useV4) { var coroutine = GSLoaderV4.LoadCsvData(sheetId, gid, apiKey); yield return(coroutine); loadedCsvData = GSLoaderV4.csvData; isSuccess = loadedCsvData != null; } // V3 old api else { // Load spread sheet yield return(EditorCoroutineRunner.StartCoroutine(downloadWorksheets(sheetId))); if (_worksheets == null) { Debug.LogError("Failed to download worksheets"); yield break; } // Load each worksheet foreach (GSWorksheet sheet in _worksheets) { if (sheet.gid == gid) { yield return(EditorCoroutineRunner.StartCoroutine(LoadCsvData(sheet))); yield break; } } } }
public static Field[] GetFieldsFromHeader(CsvData grid) { var fields = new Field[grid.col]; for (int i = 0; i < fields.Length; i++) { fields[i] = new Field(); } // get field names; for (int col = 0; col < grid.col; col++) { string fieldName = grid.Get(0, col); fieldName = fieldName.Trim(); if (fieldName == string.Empty) { fields[col].isValid = false; continue; } fields[col].fieldName = fieldName; } // set field types; for (int col = 0; col < grid.col; col++) { if (!fields[col].isValid) { continue; } fields[col].typeName = fields[col].fieldName; } return(fields); }
public IEnumerator LoadCsvData(GSWorksheet sheet) { string title = sheet.title; string url = sheet.link; UnityWebRequest req = UnityWebRequest.Get(url); var op = req.SendWebRequest(); while (!op.isDone) { yield return(null); } if (req.isNetworkError || req.isHttpError) { Debug.Log(req.error); yield break; } else if (req.responseCode != 200) { yield break; } string resJson = req.downloadHandler.text; resJson = resJson.Replace("$", "_d_"); GSResponse response = JsonUtility.FromJson <GSResponse>(resJson); if (response == null || response.feed == null) { yield break; } List <List <string> > cellList = new List <List <string> >(); foreach (GSResponse.Entry entry in response.feed.entry) { var cell = entry.gs_d_cell; int row = cell.row; int col = cell.col; string inputValue = cell._d_t; while (cellList.Count < row) { cellList.Add(new List <string>()); } while (cellList[row - 1].Count < col) { cellList[row - 1].Add(string.Empty); } cellList[row - 1][col - 1] = inputValue; } CsvData csv = new CsvData(); csv.SetFromList(cellList); this.loadedCsvData = csv; this.isSuccess = true; }
public GSLoader() { isSuccess = false; loadedCsvData = null; }
public static IEnumerator Download(GSPluginSettings.Sheet ss, string settingDir) { previousDownloadSuccess = false; string sheetId = ss.sheetId; string gid = ss.gid; string label = "Downloading " + ss.targetPath; EditorCoroutineRunner.UpdateUILabel(label); var gsLoader = new GSLoader(); var globalSettings = CCLogic.GetGlobalSettings(); string apiKey = globalSettings.apiKey; bool useV4 = globalSettings.useV4; yield return(EditorCoroutineRunner.StartCoroutine(gsLoader.LoadGS(sheetId, gid, apiKey, useV4))); if (!gsLoader.isSuccess) { Debug.Log("Failed to load spreadsheet data."); yield break; } CsvData csvData = gsLoader.loadedCsvData; string targetPathRelativeToAssets = ss.GetFilePathRelativesToAssets(settingDir); if (csvData != null) { if (ss.isCsv) { string targetDir = Path.GetDirectoryName(targetPathRelativeToAssets); if (!Directory.Exists(targetDir)) { try { Directory.CreateDirectory(targetDir); Debug.Log("指定のフォルダが存在しないため、作成しました: " + targetDir); } catch (Exception e) { Debug.LogError("指定のフォルダの作成に失敗: " + e.Message); } // Debug.LogError("指定のフォルダは存在しません: " + targetDir); // return; } using (var s = new StreamWriter(targetPathRelativeToAssets)) { s.Write(csvData.ToString()); } } else { // AssetDatabase.CreateAsset(csvData, targetPathRelativeToAssets); Debug.LogError("CsvData の書き出しには未対応になりました"); } if (ss.verbose) { Debug.Log("Write " + ss.targetPath); } previousDownloadSuccess = true; AssetDatabase.Refresh(); } else { Debug.LogError("Fails for " + ss.ToString()); } }
public static void GenerateCode(ConvertSetting s, GlobalCCSettings gSettings) { string settingPath = s.GetDirectoryPath(); string csvPath = CCLogic.GetFilePathRelativesToAssets(settingPath, s.GetCsvPath(gSettings)); TextAsset textAsset = AssetDatabase.LoadAssetAtPath <TextAsset>(csvPath); if (textAsset == null) { Debug.LogError("Not found : " + csvPath); return; } string directoryPath = CCLogic.GetFullPath(settingPath, s.codeDestination); if (!Directory.Exists(directoryPath)) { Debug.LogError("Not found directory: " + directoryPath); return; } CsvData csv = CsvLogic.GetValidCsvData(textAsset.text, gSettings); if (s.isEnum) { CsvData headers = csv.Slice(gSettings.rowIndexOfName, gSettings.rowIndexOfName + 1); CsvData contents = csv.Slice(gSettings.rowIndexOfEnumContentStart); string code = EnumGenerator.Generate(s.className, headers, contents, s.verbose); string filePath = Path.Combine(directoryPath, s.className + ".cs"); using (StreamWriter writer = File.CreateText(filePath)) { writer.WriteLine(code); } Debug.LogFormat("Create \"{0}\"", filePath); } else { Field[] fields = CsvLogic.GetFieldsFromHeader(csv, gSettings); if (s.classGenerate) { string code = ClassGenerator.GenerateClass(s.className, fields, s.IsPureClass); string filePath = Path.Combine(directoryPath, s.className + ".cs"); using (StreamWriter writer = File.CreateText(filePath)) { writer.WriteLine(code); } Debug.LogFormat("Create \"{0}\"", filePath); } if (s.tableClassGenerate) { int[] keyIndexes = ClassGenerator.FindKeyIndexes(s, fields); string[] keys = s.keys; Field[] key = null; if (keyIndexes.Length > 0) { List <Field> keyFieldList = new List <Field>(); for (int i = 0; i < keyIndexes.Length; i++) { keyFieldList.Add(fields[keyIndexes[i]]); } key = keyFieldList.ToArray(); } string code = ClassGenerator.GenerateTableClass(s, s.TableClassName, key); string filePath = Path.Combine(directoryPath, s.TableClassName + ".cs"); using (StreamWriter writer = File.CreateText(filePath)) { writer.WriteLine(code); } Debug.LogFormat("Create \"{0}\"", filePath); } } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); }
public static Field[] GetFieldsFromHeader(CsvData csv, GlobalCCSettings gSettings) { CsvData nameHeaders = csv.Slice(gSettings.rowIndexOfName, gSettings.rowIndexOfName + 1); CsvData typeHeaders = csv.Slice(gSettings.rowIndexOfType, gSettings.rowIndexOfType + 1); var fields = new Field[csv.col]; for (int i = 0; i < fields.Length; i++) { fields[i] = new Field(); } // enabled check if (gSettings.rowIndexOfEnabledColumn != -1) { CsvData enabledHeaders = csv.Slice(gSettings.rowIndexOfEnabledColumn, gSettings.rowIndexOfEnabledColumn + 1); for (int col = 0; col < csv.col; col++) { string enabledCell = enabledHeaders.Get(0, col); if (!GetEnabledColumn(enabledCell)) { fields[col].isValid = false; } } } // get field names; for (int col = 0; col < csv.col; col++) { string fieldName = nameHeaders.Get(0, col); fieldName = fieldName.Trim(); if (fieldName == string.Empty) { fields[col].isValid = false; continue; } fields[col].fieldName = fieldName; } // set field types; for (int col = 0; col < csv.col; col++) { if (!fields[col].isValid) { continue; } string typeName = typeHeaders.Get(0, col).Trim(); if (typeName == string.Empty) { fields[col].isValid = false; continue; } fields[col].typeName = typeName; } return(fields); }
public AssetsGenerator(ConvertSetting _setting, Field[] _fields, CsvData _content) { setting = _setting; fields = _fields; content = _content; }
public static string Generate(string name, CsvData header, CsvData contents, bool verbose = false) { Field[] fields = GetFieldsFromHeader(header); if (verbose) { Debug.Log("header fields: " + fields.ToString <Field>()); } string classData = ""; classData += "public enum " + name + "\n"; classData += "{\n"; for (int i = 0; i < contents.row; i++) { int line = i + 2; string eid = ""; int value = -1; bool isOkEid = false; bool isOkValue = false; for (int j = 0; j < contents.col; j++) { Field f = fields[j]; if (!f.isValid) { continue; } if (f.fieldName == ID_NAME) { eid = contents.Get(i, j).Trim(); if (!string.IsNullOrWhiteSpace(eid)) { isOkEid = true; } } else if (f.fieldName == VALUE_NAME) { isOkValue = true; string vs = contents.Get(i, j).Trim(); if (!int.TryParse(vs, out value)) { Debug.LogWarningFormat("{0} line {1}: int に変換出来ない値です: \"{2}\"", name, line, vs); isOkValue = false; continue; } } } if (!isOkEid || !isOkValue) { if (verbose) { Debug.Log(i + ": INVALID : " + contents.content[i].ToString()); } continue; } if (verbose) { Debug.Log(i + ": OK : " + contents.content[i].ToString()); } classData += string.Format(FIELD_FORMAT, eid, value); } classData += "}"; return(classData); }