/// <summary> /// 将类信息转为基本的C#类代码 /// </summary> /// <param name="info"></param> private static void TranslateConfigCSharpClassCode(ClassTemplateInfo info) { StringBuilder sb = info.sb; int row = info.notesRow; int col = info.dataStartCol; object notes = info.collect[row][col]; sb.Append("using ShipDock.Config;\r\n\r\n"); sb.Append("using ShipDock.Tools;\r\n\r\n"); sb.Append("namespace StaticConfig\r\n"); sb.Append("{\r\n"); sb.Append(" public partial class %cls% : IConfig\r\n".Replace(info.rplClsName, info.className)); sb.Append(" {\r\n"); sb.Append(" /// <summary>\r\n"); sb.Append(" /// %notes%\r\n").Replace(info.notes, notes.ToString()); sb.Append(" /// <summary>\r\n"); sb.Append(" public %type% %id%;\r\n\r\n").Replace(info.typeName, info.typeValue).Replace(info.idKeyName, info.IDName); //ID sb.Append(" %codes%\r\n"); //此处容纳其他代码 sb.Append(" public string CRCValue { get; }\r\n\r\n"); sb.Append(" public %type% GetID()\r\n").Replace(info.typeName, info.typeValue); sb.Append(" {\r\n"); sb.Append(" return id;\r\n"); sb.Append(" }\r\n\r\n"); sb.Append(" public void Parse(ByteBuffer buffer)\r\n"); sb.Append(" {\r\n"); sb.Append(" %parseCode%\r\n"); sb.Append(" }\r\n\r\n"); sb.Append(" }\r\n"); sb.Append("}\r\n"); }
/// <summary> /// 读取表数据,生成对应的数组 /// </summary> /// <param name="filePath">excel文件全路径</param> /// <returns>Item数组</returns> public static void CreateItemArrayWithExcel(string filePath, out string relativeName, ref string log) { InitExeclDefs(); log += filePath + "开始解析... \r\n"; DataRowCollection collect = ReadExcel(filePath, out int rowSize, out int colSize);//获得表数据 ExeclDefination defination = Setting.classDefine; int col = defination.column, row = defination.row; string className = collect[row][col].ToString();//类名 defination = Setting.generateFileName; col = defination.column; row = defination.row; string fileName = collect[row][col].ToString();//生成的配置文件名 fileName = string.IsNullOrEmpty(fileName) ? className : fileName; defination = Setting.classType; col = defination.column; row = defination.row; string classType = collect[row][col].ToString();//类模板的类别 defination = Setting.IDFieldName; col = defination.column; row = defination.row; string IDName = collect[row][col].ToString();//id字段的名称 defination = Setting.dataStart; int dataStartRow = defination.row; int dataStartCol = defination.column; Debug.Log(rowSize); string cellData; for (int i = dataStartRow; i < rowSize; i++) { cellData = collect[i][dataStartCol].ToString(); if (string.IsNullOrEmpty(cellData)) { rowSize = i;//找到数据起始列为空的一行,作为数据最终行 Debug.Log(rowSize); break; } else { } } StringBuilder sb = new StringBuilder(); int typeRow = Setting.dataType.row; //类型名所在行 int keyRow = Setting.keyFieldDef.row; //字段名所在行 int notesRow = Setting.noteFieldDef.row; //注释所在行 DataRow rowData = collect[typeRow]; object colData = rowData[dataStartCol]; object IDTypeCell = collect[typeRow][dataStartCol]; string typeValue = GetCSharpForTypeValueCode(IDTypeCell.ToString()); ClassTemplateInfo info = new ClassTemplateInfo { sb = sb, collect = collect, className = className, notesRow = notesRow, dataStartCol = dataStartCol, typeValue = typeValue, IDName = IDName, }; switch (classType) { case "mapper": TranslateConfigCSharpClassCode(info); break; case "const": //常量类 sb.Append("namespace StaticConfig"); sb.Append("{"); sb.Append(" public static class %cls%".Replace(info.rplClsName, className)); sb.Append(" {"); sb.Append(" }"); sb.Append("}"); break; } string field, notes, valueInitCode; string CSharpScript = sb.ToString(); sb.Clear(); object item; string parserCode = IDName.Append(GetCSharpForSetValueCode(IDTypeCell.ToString())); //获取到翻译后的 id 赋值语句 for (int i = dataStartCol + 1; i < colSize; i++) //从id列顺延下一个字段开始构建剩余的脚本 { item = collect[typeRow][i]; //类型 typeValue = item.ToString(); if (string.IsNullOrEmpty(typeValue)) { colSize = i;//读取到无效的表头,此前的所有字段为完整的类成员字段,并更新到列数 break; } else { } typeValue = GetCSharpForTypeValueCode(typeValue); item = collect[keyRow][i];//字段名 field = item.ToString(); notes = collect[notesRow][i].ToString(); valueInitCode = collect[typeRow][i].ToString(); sb.Append("/// <summary>"); sb.Append("\r\n /// %notes%") .Replace(info.notes, notes); sb.Append("\r\n /// <summary>\r\n "); sb.Append("public %type% %fieldName%;\r\n") .Replace(info.typeName, typeValue) .Replace(info.fieldName, field); sb.Append(" "); parserCode = parserCode.Append(field, GetCSharpForSetValueCode(valueInitCode));//获取到翻译后的其他成员字段赋值语句 } CSharpScript = CSharpScript.Replace(info.codes, sb.ToString()); //合成类成员字段的声明语句 CSharpScript = CSharpScript.Replace(info.parseCode, parserCode); //合成类成员字段的初始化赋值语句 sb.Clear(); object ID; string IDValue; DataRow itemRow; ByteBuffer byteBuffer = ByteBuffer.Allocate(0); int dataRealSize = rowSize - dataStartRow; byteBuffer.WriteInt(dataRealSize); for (int i = dataStartRow; i < rowSize; i++) { itemRow = collect[i]; ID = itemRow[dataStartCol]; IDValue = ID.ToString(); if (string.IsNullOrEmpty(IDValue)) { Debug.LogWarning("Config ID invalid in row " + i + ", it do not allow empty, config bytes parse will be break."); break; } else { Debug.Log("Writing in ID = " + IDValue.ToString()); for (int j = dataStartCol; j < colSize; j++)//从id列顺延下一个字段开始构建剩余的脚本 { item = collect[typeRow][j]; typeValue = item.ToString(); //字段类型 field = itemRow[j].ToString(); //字段数据 WriteField(ref byteBuffer, typeValue, field, collect[keyRow][j].ToString()); } } } Debug.Log("Write finished.. length = " + byteBuffer.GetCapacity()); string path = GetCSharpCodeFilePath(className); FileOperater.WriteUTF8Text(CSharpScript, path);//生成代码 path = GetConfigFilePath(fileName, out relativeName); FileOperater.WriteBytes(byteBuffer.ToArray(), path);//生成配置数据文件 }