Пример #1
0
        public static CsvData GetValidCsvData(string csvText, GlobalCCSettings gSettings)
        {
            var csvAsList = CsvParser.ReadAsList(csvText);

            CsvData csvData = new CsvData();

            csvData.SetFromList(csvAsList);

            // END マーカーが有効な場合は END 以下を無視するようにする.
            if (gSettings.isEndMarkerEnabled)
            {
                int    endMarkerIndex = gSettings.columnIndexOfEndMarker;
                string marker         = gSettings.endMarker;

                if (endMarkerIndex < 0 || csvData.col <= endMarkerIndex)
                {
                    throw new Exception("無効な columnIndexOfEndMarker です: " + endMarkerIndex);
                }

                for (int i = 0; i < csvData.row; i++)
                {
                    if (csvData.Get(i, endMarkerIndex).Trim() == marker)
                    {
                        csvData = csvData.Slice(0, i);
                        break;
                    }
                }
            }

            // 無効な列を除外する
            csvData = csvData.SliceColumn(gSettings.columnIndexOfTableStart);

            return(csvData);
        }
Пример #2
0
        public static void CreateOneAssets(CsvConverterSettings.Setting s, GlobalCCSettings gSettings,
                                           string settingPath)
        {
            try
            {
                CsvConverter.CreateAssets(s, gSettings, settingPath);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            EditorUtility.ClearProgressBar();
        }
Пример #3
0
        public static void CreateAllAssets(CsvConverterSettings.Setting[] setting, GlobalCCSettings gSettings,
                                           string[] settingPaths)
        {
            try
            {
                for (int i = 0; i < setting.Length; i++)
                {
                    CsvConverter.CreateAssets(setting[i], gSettings, settingPaths[i]);
                }
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            EditorUtility.ClearProgressBar();
        }
Пример #4
0
        public static void GenerateOneCode(CsvConverterSettings.Setting s, GlobalCCSettings gSettings,
                                           string settingPath)
        {
            show_progress(s.className, 0, 0, 1);

            try
            {
                CsvConverter.GenerateCode(s, gSettings, settingPath);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            show_progress(s.className, 1, 1, 1);

            EditorUtility.ClearProgressBar();
        }
Пример #5
0
        public static void GenerateAllCode(CsvConverterSettings.Setting[] setting, GlobalCCSettings gSettings,
                                           string[] settingPaths)
        {
            int i = 0;

            try
            {
                foreach (CsvConverterSettings.Setting s in setting)
                {
                    show_progress(s.className, (float)i / setting.Length, i, setting.Length);
                    CsvConverter.GenerateCode(s, gSettings, settingPaths[i]);
                    i++;
                    show_progress(s.className, (float)i / setting.Length, i, setting.Length);
                }
            }
            catch (Exception e)
            {
                Debug.LogException(e);
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            EditorUtility.ClearProgressBar();
        }
Пример #6
0
        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);
        }
Пример #7
0
        private void OnGUI()
        {
            GUILayout.Space(6f);
            CsvConverterSettings.Setting[] setting        = null;
            CsvConverterSettings[]         parentSettings = null;
            string[] paths = null;

            if (cachedAllSettings != null)
            {
                setting        = cachedAllSettings;
                paths          = cachedAllSettingsPath;
                parentSettings = cachedAllParentSettings;
            }

            // 検索ボックスを表示
            GUILayout.BeginHorizontal();
            searchTxt = SearchField(searchTxt);
            searchTxt = searchTxt.ToLower();
            GUILayout.EndHorizontal();

            if (setting != null)
            {
                scrollPosition = GUILayout.BeginScrollView(scrollPosition);

                for (int i = 0; i < setting.Length; i++)
                {
                    var s    = setting[i];
                    var path = paths[i];

                    // 設定が削除されている場合などに対応
                    if (s == null)
                    {
                        continue;
                    }

                    // 検索ワードチェック
                    if (!string.IsNullOrEmpty(searchTxt))
                    {
                        if (s.tableGenerate)
                        {
                            if (!searchTxt.IsSubsequence(s.tableAssetName.ToLower()))
                            {
                                continue;
                            }
                        }
                        else
                        {
                            if (!searchTxt.IsSubsequence(s.className.ToLower()))
                            {
                                continue;
                            }
                        }
                    }

                    GUILayout.BeginHorizontal("box");

                    if (s.tableGenerate)
                    {
                        if (GUILayout.Button(s.tableAssetName, "Label"))
                        {
                            EditorGUIUtility.PingObject(parentSettings[i].GetInstanceID());
                            GUIUtility.ExitGUI();
                        }
                    }
                    else
                    {
                        if (GUILayout.Button(s.className, "Label"))
                        {
                            EditorGUIUtility.PingObject(parentSettings[i].GetInstanceID());
                            GUIUtility.ExitGUI();
                        }
                    }

                    if (s.useGSPlugin)
                    {
                        if (GUILayout.Button("DownLoad", GUILayout.Width(110)))
                        {
                            GSPlugin.GSPluginSettings.Sheet sheet = new GSPlugin.GSPluginSettings.Sheet();
                            sheet.sheetId = s.sheetID;
                            sheet.gid     = s.gid;

                            string absolutePath   = CCLogic.GetFilePathRelativesToAssets(path, s.csvFilePath);
                            string downloadFolder = Path.GetDirectoryName(absolutePath);

                            // 先頭の Assets を削除する
                            if (downloadFolder.StartsWith("Assets/"))
                            {
                                sheet.downloadFolder = downloadFolder.Substring(7);
                            }
                            else if (downloadFolder.Equals("Assets"))
                            {
                                sheet.downloadFolder = "";
                            }
                            else
                            {
                                Debug.LogError("unexpected downloadFolder: " + downloadFolder);
                            }
                            sheet.fileName = Path.GetFileNameWithoutExtension(absolutePath);
                            sheet.isCsv    = true;

                            GSPlugin.GSEditorWindow.DownloadOne(sheet);
                        }
                    }

                    GUI.enabled = s.canGenerateCode;
                    if (GUILayout.Button("Generate Code", GUILayout.Width(110)) && !isDownloading)
                    {
                        GlobalCCSettings gSettings = CCLogic.GetGlobalSettings();
                        isDownloading = true;
                        GenerateOneCode(s, gSettings, path);
                        isDownloading = false;

                        GUIUtility.ExitGUI();
                    }

                    GUI.enabled = s.canCreateAsset;

                    if (GUILayout.Button("Create Assets", GUILayout.Width(110)) && !isDownloading)
                    {
                        GlobalCCSettings gSettings = CCLogic.GetGlobalSettings();
                        isDownloading = true;
                        CreateOneAssets(s, gSettings, path);
                        isDownloading = false;

                        GUIUtility.ExitGUI();
                    }

                    GUI.enabled = true;

                    {
                        string mainOutputPath = CCLogic.GetMainOutputPath(s, path);

                        if (mainOutputPath != null)
                        {
                            Object outputRef = AssetDatabase.LoadAssetAtPath <Object>(mainOutputPath);

                            EditorGUI.BeginDisabledGroup(true);
                            EditorGUILayout.ObjectField(outputRef, typeof(Object), false, GUILayout.Width(100));
                            EditorGUI.EndDisabledGroup();
                        }
                    }

                    GUILayout.EndHorizontal();
                }

                GUILayout.EndScrollView();

                GUILayout.BeginHorizontal("box");
                if (GUILayout.Button("Generate All Codes", "LargeButtonMid") && !isDownloading)
                {
                    GlobalCCSettings gSettings = CCLogic.GetGlobalSettings();
                    isDownloading = true;
                    GenerateAllCode(setting, gSettings, paths);
                    isDownloading = false;

                    GUIUtility.ExitGUI();
                }

                if (GUILayout.Button("Create All Assets", "LargeButtonMid") && !isDownloading)
                {
                    GlobalCCSettings gSettings = CCLogic.GetGlobalSettings();
                    isDownloading = true;
                    CreateAllAssets(setting, gSettings, paths);
                    isDownloading = false;

                    GUIUtility.ExitGUI();
                }

                GUILayout.EndHorizontal();
            }
        }
Пример #8
0
        public static void GenerateCode(CsvConverterSettings.Setting s, GlobalCCSettings gSettings, string settingPath)
        {
            string    csvPath   = CCLogic.GetFilePathRelativesToAssets(settingPath, s.csvFilePath);
            TextAsset textAsset = AssetDatabase.LoadAssetAtPath <TextAsset>(csvPath);

            if (textAsset == null)
            {
                Debug.LogError("Not found : " + csvPath);
                return;
            }

            string directoryPath = CCLogic.GetFullPath(settingPath, s.destination);

            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);

                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.tableGenerate && s.onlyTableCreate);

                    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();
        }
Пример #9
0
        public static void CreateAssets(CsvConverterSettings.Setting s, GlobalCCSettings gSettings, string settingPath)
        {
            string    csvPath   = CCLogic.GetFilePathRelativesToAssets(settingPath, s.csvFilePath);
            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))
                    {
                        customAssetTypes[i] = type;
                    }
                    else
                    {
                        return;
                    }
                }

                assetsGenerator.SetCustomAssetTypes(customAssetTypes);
            }

            // 生成する各要素の class type を取得
            Type assetType;

            if (!TryGetTypeWithError(s.className, out assetType))
            {
                return;
            }

            // class のフィールド名と一致しないものは除外する.
            for (int j = 0; j < fields.Length; j++)
            {
                if (!fields[j].isValid)
                {
                    continue;
                }

                // フィールド名が配列表の場合は [] の部分を削除する
                // 例) names[2] => names
                string    fieldName = fields[j].fieldNameWithoutIndexing;
                FieldInfo info      = assetType.GetField(fieldName);

                if (info == null)
                {
                    Debug.LogWarningFormat("{0} に存在しないフィールド \"{1}\" を無視", s.className, fieldName);
                    fields[j].isValid = false;
                }
            }

            // テーブルを生成する場合は、生成するテーブル class type を取得
            Type tableType = null;

            if (s.tableGenerate)
            {
                if (!TryGetTypeWithError(s.tableClassName, out tableType))
                {
                    return;
                }
            }

            if (s.tableGenerate)
            {
                assetsGenerator.Setup(assetType, tableType, settingPath);
            }
            else
            {
                assetsGenerator.Setup(assetType, settingPath);
            }

            // アセットを作成する.
            for (int i = 0; i < assetsGenerator.contentRowCount; i++)
            {
                int        line       = i + 2 + 1;
                ResultType resultType = assetsGenerator.CreateCsvAssetAt(i);

                if ((resultType & ResultType.SkipNoKey) != 0)
                {
                    Debug.LogWarningFormat("{0} line {1}: key が存在しない行をスキップしました", s.className, line);
                }

                int total = assetsGenerator.contentRowCount;
                if (total <= 10 || i == total - 1 || i % (total / 10) == 0)
                {
                    float progress = (float)i / total;
                    EditorUtility.DisplayProgressBar("Progress", $"Creating {s.className} ({i + 1}/{total})", progress);
                }
            }

            // 結果を出力して保存する.
            var result = assetsGenerator.result;

            if (s.tableGenerate)
            {
                EditorUtility.SetDirty(result.tableInstance);
                Debug.Log($"Create \"{Path.Combine(assetsGenerator.dstFolder, s.tableAssetName)}.asset\"");
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();

            Debug.Log($"生成された総行数: {result.createdRowCount}");

            if (result.tableInstance != null)
            {
                EditorGUIUtility.PingObject(result.tableInstance.GetInstanceID());
            }

            EditorUtility.ClearProgressBar();
        }