//导出类型的序列化辅助函数 public static string ExportHelperFunctionClass(ClassDefine helperClass, List <string> types, Language language) { HashSet <string> allItertorType = new HashSet <string>(); foreach (string type in types) { if (!allItertorType.Contains(type)) { allItertorType.Add(type); } List <string> nestTypes = GetAllNestType(type); foreach (string nestType in nestTypes) { if (!allItertorType.Contains(nestType)) { allItertorType.Add(nestType); } } FieldDefine fd = new FieldDefine(); fd.decoration = type; helperClass.fieldList.Add(fd); } StringBuilder sb = new StringBuilder(); ExportClassHelper.indent = 0; bool hasLeftBrace = ExportClassHelper.ExportNamespace(sb, helperClass, language); ExportClassHelper.ExportClassName(sb, helperClass, language); ExportClassHelper.AddLeftBrace(sb, language); //function sb.AppendLine(""); foreach (var type in allItertorType) { string function = CodeExporterHelper.GenerateFunctionCode(language, type, CodeMode.Decode); if (!string.IsNullOrEmpty(function)) { sb.AppendIndentLine(function); } if (ExportConfigConstant.GenerateSerializeCode) { function = CodeExporterHelper.GenerateFunctionCode(language, type, CodeMode.Encode); if (!string.IsNullOrEmpty(function)) { sb.AppendIndentLine(function); } } } sb.AppendLine(""); //class end ExportClassHelper.AddRightBrace(sb, language); //namespace end if (hasLeftBrace) { ExportClassHelper.AddRightBrace(sb, language); } return(sb.ToString()); }
public static string ExportConfigDeserializeClass(ClassDefine deserializerClass, List <string> types, Language language) { StringBuilder sb = new StringBuilder(); ExportClassHelper.indent = 0; FieldDefine fd = new FieldDefine(); fd.decoration = "Dictionary<int,object>"; deserializerClass.fieldList.Add(fd); bool hasLeftBrace = ExportClassHelper.ExportNamespace(sb, deserializerClass, language); ExportClassHelper.ExportClassName(sb, deserializerClass, language); ExportClassHelper.AddLeftBrace(sb, language); sb.AppendLine(""); if (language == Language.CSharp) { sb.AppendIndentLine("public static void Deserialize(BufferBuilder bb)"); } else if (language == Language.Java) { } ExportClassHelper.AddLeftBrace(sb, language); foreach (var type in types) { sb.AppendIndentLine(type + ".LoadTable(bb);"); } ExportClassHelper.AddRightBrace(sb, language); //class end ExportClassHelper.AddRightBrace(sb, language); //namespace end if (hasLeftBrace) { ExportClassHelper.AddRightBrace(sb, language); } return(sb.ToString()); }
public static string GetDebugString(FieldDefine field, Language lanaguage) { return(string.Empty); }
public static void DoExport() { string configClassOutputPath = EditorUtils.AssetPath2FilePath(ExportConfigConstant.ConfigClassOutputPath); string excelConfigPath = EditorUtils.AssetPath2FilePath(ExportConfigConstant.ExcelConfigFolderPath); Directory.CreateDirectory(configClassOutputPath); ClassDefine helperClass = new ClassDefine(); List <string> functionList = new List <string>(); helperClass.name = "SerializeHelper"; ClassDefine deserializeClass = new ClassDefine(); deserializeClass.name = "DeserializeHelper"; List <string> typeList = new List <string>(); byte[] data = new byte[1024]; BufferBuilder bb = new BufferBuilder(data); XMLNodeList nodeList = ConfigExporter.GetConfigDataXml(excelConfigPath + ExportConfigConstant.Excel2ConfigClassFileName); int objectTotal = 0; string test = string.Empty; int count = 0; foreach (XMLNode node in nodeList) { count++; string excelFileName = node.GetValue("@file"); Debug.Log("excelFileName:" + excelFileName + " Application.dataPath:" + UnityEngine.Application.dataPath); EditorUtility.DisplayProgressBar("Export Config", "Export " + excelFileName, count / (float)nodeList.Count); string clientCsFileName = node.GetValue("@client"); string excelFilePath = excelConfigPath + excelFileName + ".xlsx"; if (!File.Exists(excelFilePath) || string.IsNullOrEmpty(clientCsFileName)) { Debug.LogError(excelFilePath + " " + clientCsFileName); continue; } /* * if (!clientCsFileName.Contains("SoundConfig")) * { * continue; * }*/ Excel excelParser = new Excel(excelFilePath); Worksheet workSheet = excelParser.Read(0); excelParser.Dispose(); int rowCount = workSheet.RowCount; int columnCount = workSheet.ColumnCount; ClassDefine classDefine = new ClassDefine(); classDefine.desc = excelFileName; classDefine.name = clientCsFileName; string deserializeType = classDefine.name + CodeGenConstants.ComplexType[0]; typeList.Add(deserializeType); functionList.Add(deserializeType); int objectCount = rowCount - ExportConfigConstant.ExcelDataBeginRow; objectTotal += objectCount; test += clientCsFileName + "," + objectCount + "\n"; bb.PutBool(false); if (excelFileName.Contains("q全局配置表")) { bb.Put7BitEncodeInt(1); } else { bb.Put7BitEncodeInt(objectCount); } Debug.LogError(clientCsFileName + " row: " + rowCount + " column:" + columnCount + " nodeList cnt:" + nodeList.Count); if (excelFileName.Contains("q全局配置表")) { for (int column = 0; column < columnCount; column++) { if (column != 0) { bb.PutBool(false); } for (int row = 6; row < rowCount; row++) { string name = workSheet.GetCell(row, 1) .Replace(" ", ""); string type = workSheet.GetCell(row, 2) .Replace(" ", ""); type = type.Replace("String", "string"); //Debug.LogError("column:" + column + " name:" + name + " type:" + type); if (string.IsNullOrEmpty(type) || string.IsNullOrEmpty(name)) { continue; } if (column == 0) //先读取配置头 { FieldDefine fieldDefine = new FieldDefine(); fieldDefine.desc = workSheet.GetCell(row, 0).Trim(); //.ToString(); fieldDefine.desc = fieldDefine.desc.Replace("\n", "").Replace("\r", ""); fieldDefine.name = name; //.ToString(); fieldDefine.decoration = type; classDefine.fieldList.Add(fieldDefine); if (!functionList.Contains(fieldDefine.decoration)) { functionList.Add(fieldDefine.decoration); } } else //遍历读取数据,序列化保存数据 { string value = workSheet.GetCell(row, column); if (value == null) { value = string.Empty; } //这里要注释掉 value = value.Replace(";", ",").Replace(";", ","); value = value.Replace(",", ","); value = value.Trim(); try { object serializeValue = ExportConfigHelper.String2Object(type, value); ExportSerializeHelper.SerializeObject(bb, serializeValue, type); } catch (Exception ex) { Debug.LogError("cell(" + row + "," + column + "):" + value + " type:" + type); Debug.LogError(ex); } } } if (column == 0) { column = 2; } } } else { Debug.Log("rowCount:" + rowCount); for (int row = 0; row < rowCount; row++) { if (row != 0) { bb.PutBool(false); } for (int column = ExportConfigConstant.ExcelDataBeginColumn; column < columnCount; column++) { string name = workSheet.GetCell(ExportConfigConstant.ClientFieldNameRow, column) .Replace(" ", ""); string type = workSheet.GetCell(ExportConfigConstant.ClientFieldTypeRow, column) .Replace(" ", ""); type = type.Replace("String", "string"); //Debug.LogError("column:" + column + " name:" + name + " type:" + type); if (string.IsNullOrEmpty(type) || string.IsNullOrEmpty(name)) { continue; } if (row == 0) //先读取配置头 { FieldDefine fieldDefine = new FieldDefine(); fieldDefine.desc = workSheet.GetCell(ExportConfigConstant.ClientFieldDescRow, column).Trim(); //.ToString(); fieldDefine.desc = fieldDefine.desc.Replace("\n", "").Replace("\r", ""); fieldDefine.name = name; //.ToString(); fieldDefine.decoration = type; classDefine.fieldList.Add(fieldDefine); if (!functionList.Contains(fieldDefine.decoration)) { functionList.Add(fieldDefine.decoration); } } else //遍历读取数据,序列化保存数据 { string value = workSheet.GetCell(row, column); if (value == null) { value = string.Empty; } //这里要注释掉 value = value.Replace(";", ",").Replace(";", ","); value = value.Replace(" ", ""); value = value.Replace(",", ","); try { object serializeValue = ExportConfigHelper.String2Object(type, value); ExportSerializeHelper.SerializeObject(bb, serializeValue, type); } catch (Exception ex) { Debug.LogError("cell(" + row + "," + column + "):" + value + " type:" + type); Debug.LogError(ex); } } } if (row == 0) { row = ExportConfigConstant.ExcelDataBeginRow - 1; } } } //导出配置类 File.WriteAllText(configClassOutputPath + classDefine.name + ".cs", classDefine.ExportCode(Language.CSharp, 0)); //break; //Debug.LogError(classDefine.ExportCode(Language.CSharp, 0)); } EditorUtility.ClearProgressBar(); Debug.LogError("position:" + bb.Position + " serialize object count:" + objectTotal + " :" + test); //辅助函数,全局序列化函数 string deserializeClassCode = ExportDeserializeHelper.ExportDeserializeClass(deserializeClass, typeList, Language.CSharp); string helperClassCode = ExportDeserializeHelper.ExportHelperFunctionClass(helperClass, functionList, Language.CSharp); File.WriteAllText(configClassOutputPath + deserializeClass.name + ".cs", deserializeClassCode); File.WriteAllText(configClassOutputPath + helperClass.name + ".cs", helperClassCode); File.WriteAllBytes(configClassOutputPath + ExportConfigConstant.SerializeFileName, bb.ToArray()); }
//旧版本 public static string ExportDeserializeClass(ClassDefine deserializerClass, List <string> types, Language language) { StringBuilder sb = new StringBuilder(); ExportClassHelper.indent = 0; FieldDefine fd = new FieldDefine(); fd.decoration = "Dictionary<int,object>"; deserializerClass.fieldList.Add(fd); bool hasLeftBrace = ExportClassHelper.ExportNamespace(sb, deserializerClass, language); ExportClassHelper.ExportClassName(sb, deserializerClass, language); ExportClassHelper.AddLeftBrace(sb, language); sb.AppendLine(""); if (language == Language.CSharp) { sb.AppendIndentLine("public static Dictionary<string,Dictionary<int,object>> Deserialize(BufferBuilder bb)"); } else if (language == Language.Java) { sb.AppendIndentLine("public Dictionary<string,Dictionary<int,object>> deserialize(BufferBuilder bb)"); } ExportClassHelper.AddLeftBrace(sb, language); sb.AppendIndentLine("Dictionary<string,Dictionary<int,object>> valueDicts= new Dictionary<string,Dictionary<int,object>>();"); sb.AppendIndentLine("Dictionary<int,object> valueDict;"); foreach (var type in types) { string elementType = CodeExporterHelper.GetArrayElementTypeName(type); sb.AppendIndentLine("valueDict = new Dictionary<int,object>();"); string valuesName = elementType + "Values"; string function = CodeExporterHelper.GetFunctionStatement(language, CodeMode.Decode, type, valuesName, true); if (!string.IsNullOrEmpty(function)) { sb.AppendIndentLine(type + " " + function); } sb.AppendIndentLine("for(int i=0;i<" + valuesName + ".Length;i++)"); ExportClassHelper.AddLeftBrace(sb, language); if (type.Contains("GlobalConfig")) { sb.AppendIndentLine("valueDict.Add(i+1," + valuesName + "[i]);"); } else { sb.AppendIndentLine("valueDict.Add(" + valuesName + "[i].id," + valuesName + "[i]);"); } ExportClassHelper.AddRightBrace(sb, language); sb.AppendIndentLine("valueDicts.Add(\"" + elementType + "\",valueDict);"); } sb.AppendLine(""); sb.AppendIndentLine("return valueDicts;"); ExportClassHelper.AddRightBrace(sb, language); //class end ExportClassHelper.AddRightBrace(sb, language); //namespace end if (hasLeftBrace) { ExportClassHelper.AddRightBrace(sb, language); } return(sb.ToString()); }