Esempio n. 1
0
        public static void GenCodeWithFile(string excelPath, bool isTs)
        {
            var fileName = LTUtils.GetFileName(excelPath);

            if (fileName.StartsWith("~"))
            {
                // excel临时文件自动跳过
                return;
            }

            if (fileName.EndsWith("const"))
            {
                _DoExportConstConfig(excelPath, isTs);
            }
            else
            {
                _DoExportNormalConfig(excelPath, isTs);
            }
        }
Esempio n. 2
0
        public static void GenCodeWithFile(string excelPath, bool isTs)
        {
            var fileName = LTUtils.GetFileName(excelPath);

            if (fileName.StartsWith("~"))
            {
                // excel临时文件自动跳过
                return;
            }


            var workbook = ReadExcel(excelPath);
            var sheet1   = workbook.GetSheetAt(0);
            var code     = new CSStruct();

            code.nameSpace = isTs ? LTEditorData.instance.ts_excelNameSpace : LTEditorData.instance.excelNameSpace;
            code.className = LTUtils.ConvertNameToFormat(fileName);
            code.fileds    = new List <CSFiled>();

            var comments = new List <string>();
            var types    = new List <string>();
            var names    = new List <string>();

            const int commentIndex = 0;
            const int typeIndex    = 1;
            const int nameIndex    = 2;

            _ReadPorps(sheet1.GetRow(commentIndex), comments);
            _ReadPorps(sheet1.GetRow(typeIndex), types);
            _ReadPorps(sheet1.GetRow(nameIndex), names);

            if (comments.Count != types.Count ||
                types.Count != names.Count)
            {
                Debug.LogError("格式不正确");
                return;
            }

            for (int i = 0; i < comments.Count; ++i)
            {
                var comment = comments[i];
                var type    = types[i];
                var name    = names[i];
                code.fileds.Add(new CSFiled()
                {
                    name = name, mtype = type, region = comment, index = i
                });
            }

            var  rows  = new List <IRow>();
            var  index = 3;
            IRow row   = null;

            while ((row = sheet1.GetRow(index++)) != null)
            {
                rows.Add(row);
            }

            if (isTs)
            {
                var codeSb = new StringBuilder();
                // 写入命名空间
                codeSb.AppendLine("export namespace {0} {".ReplaceAll("{0}", code.className));

                // 写入结构体
                codeSb.AppendLine(code.GetTSClass());

                var tsSavePath = LTEditorData.instance.ts_configCodeSavePath;
                if (tsSavePath.StartsWith("/"))
                {
                    tsSavePath = Application.dataPath + tsSavePath;
                }
                tsSavePath = tsSavePath + "/" + code.className + ".ts";
                WriteStrToFile(codeSb.ToString(), tsSavePath);

                // 写入数据
                var minJson      = _GenJson(code, rows, isTs);
                var jsonSavePath = LTEditorData.instance.ts_configJsonSavePath;
                if (jsonSavePath.StartsWith("/"))
                {
                    jsonSavePath = Application.dataPath + jsonSavePath;
                }
                jsonSavePath = jsonSavePath + "/" + code.className + ".json";
                WriteStrToFile(minJson, jsonSavePath);

                Debug.Log("[TS模式]配置生成完成");
                Debug.LogFormat("ts:{0},json:{1}", tsSavePath, jsonSavePath);
            }
            else
            {
                var minJson      = _GenJson(code, rows, isTs);
                var formatJson   = JsonFormatterPlus.JsonFormatter.Format(minJson);
                var jsonSavePath = LTEditorData.instance.configDataSavePath;
                if (jsonSavePath.StartsWith("/"))
                {
                    jsonSavePath = Application.dataPath + jsonSavePath;
                }
                var jsonPath = jsonSavePath + "/" + code.className + ".json";
                WriteStrToFile(formatJson, jsonPath);

                var codeSavePath = LTEditorData.instance.configCodeSavePath;
                if (codeSavePath.StartsWith("/"))
                {
                    codeSavePath = Application.dataPath + codeSavePath;
                }
                var codePath = codeSavePath + "/" + code.className + ".cs";

                var cachePath = jsonPath.Replace(Application.dataPath, "Assets");
                WriteStrToFile(code.GetString(cachePath), codePath);

                Debug.Log("[C#模式]配置生成完成");
                Debug.LogFormat("code:{0}", codePath);
                Debug.LogFormat("json:{0}", jsonPath);
            }
        }
Esempio n. 3
0
        private static void _DoExportNormalConfig(string excelPath, bool isTs)
        {
            var fileName = LTUtils.GetFileName(excelPath);

            var workbook = ReadExcel(excelPath);
            var sheet1   = workbook.GetSheetAt(0);
            var code     = new CSStruct();

            code.nameSpace = isTs ? "NoName" : LTEditorData.instance.excelNameSpace;
            code.className = LTUtils.ConvertNameToFormat(fileName);
            code.fileds    = new List <CSFiled>();
            code.isConst   = false;

            var comments = new List <string>();
            var types    = new List <string>();
            var names    = new List <string>();

            const int commentIndex = 0;
            const int typeIndex    = 1;
            const int nameIndex    = 2;

            _ReadPorps(sheet1.GetRow(commentIndex), comments);
            _ReadPorps(sheet1.GetRow(typeIndex), types);
            _ReadPorps(sheet1.GetRow(nameIndex), names);

            if (comments.Count != types.Count ||
                types.Count != names.Count)
            {
                Debug.LogError("格式不正确");
                return;
            }

            for (int i = 0; i < comments.Count; ++i)
            {
                var comment = comments[i];
                var type    = types[i];
                var name    = names[i];
                code.fileds.Add(new CSFiled()
                {
                    name = name, mtype = type, region = comment, index = i
                });
            }

            var  rows  = new List <IRow>();
            var  index = 3;
            IRow row   = null;

            while ((row = sheet1.GetRow(index++)) != null)
            {
                rows.Add(row);
            }

            if (isTs)
            {
                _GenTs(isTs, code, rows);
            }
            else
            {
                _GenCSharp(isTs, code, rows);
            }
        }
Esempio n. 4
0
        private static void _DoExportConstConfig(string excelPath, bool isTs)
        {
            var fileName = LTUtils.GetFileName(excelPath);

            var workbook = ReadExcel(excelPath);
            var sheet1   = workbook.GetSheetAt(0);
            var code     = new CSStruct();

            code.nameSpace = isTs ? "NoName" : LTEditorData.instance.excelNameSpace;
            code.className = LTUtils.ConvertNameToFormat(fileName);
            code.fileds    = new List <CSFiled>();
            code.isConst   = true;

            var startIndex = 1;
            var rows       = new List <IRow>();
            var row        = sheet1.GetRow(startIndex);

            while (row != null)
            {
                code.fileds.Add(new CSFiled()
                {
                    index  = startIndex,
                    mtype  = row.GetCell(1).StringCellValue,
                    region = row.GetCell(3).StringCellValue,
                    name   = row.GetCell(0).StringCellValue
                });
                rows.Add(row);
                startIndex++;
                row = sheet1.GetRow(startIndex);
            }

            if (isTs)
            {
                var codeSb = new StringBuilder();
                // 写入命名空间
                codeSb.AppendLine("export namespace {0} {".ReplaceAll("{0}", code.className));

                // 写入结构体
                codeSb.AppendLine(code.GetTSClass());

                var tsSavePath = LTEditorData.instance.ts_configCodeSavePath;
                if (tsSavePath.StartsWith("/"))
                {
                    tsSavePath = Application.dataPath + tsSavePath;
                }
                tsSavePath = tsSavePath + "/" + code.className + ".ts";
                WriteStrToFile(codeSb.ToString(), tsSavePath);

                // 写入数据
                var minJson      = _GenJson(code, rows, isTs);
                var jsonSavePath = LTEditorData.instance.ts_configJsonSavePath;
                if (jsonSavePath.StartsWith("/"))
                {
                    jsonSavePath = Application.dataPath + jsonSavePath;
                }
                jsonSavePath = jsonSavePath + "/" + code.className + ".json";
                WriteStrToFile(minJson, jsonSavePath);

                Debug.Log("[TS模式]配置生成完成");
                Debug.LogFormat("ts:{0},json:{1}", tsSavePath, jsonSavePath);
            }
            else
            {
                Debug.LogError("暂未支持");
            }
        }