Ejemplo n.º 1
0
        private static void _GenCSharp(bool isTs, CSStruct code, List <IRow> rows)
        {
            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);
        }
Ejemplo 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);
            }
        }