public static void ExportToStringFunction(StringBuilder sb, ClassDefine classNode, Language lanaguage) { sb.AppendLine(""); string toString = string.Empty; toString += "\"" + classNode.desc + "(" + classNode.name + "):"; //成员变量 List <FieldDefine> fieldList = classNode.fieldList; foreach (var fieldNode in fieldList) { toString += fieldNode.desc + "(\"+" + GetDebugString(fieldNode, lanaguage) + " + \")"; } //ToString函数 if (lanaguage == Language.CSharp) { toString = "return @" + toString + "\";"; } else if (lanaguage == Language.Java) { toString = "return " + toString + "\";"; } if (Language.CSharp == lanaguage) { sb.AppendIndentLine("public override string ToString()"); } else if (lanaguage == Language.Java) { sb.AppendIndentLine("public String toString()"); } AddLeftBrace(sb, lanaguage); sb.AppendIndentLine(toString); AddRightBrace(sb, lanaguage); }
public static void ExportEncodeFunction(StringBuilder sb, ClassDefine classNode, Language language) { sb.AppendLine(""); if (language == Language.CSharp) { if (classNode.exportStatic) { sb.AppendIndentLine(string.Format("public static void {0}Encode(BufferBuilder bb,{0} value)", classNode.name)); } else { sb.AppendIndentLine("public void Encode(BufferBuilder bb)"); } } else if (language == Language.Java) { sb.AppendIndentLine("public void encode(BufferBuilder bb)"); } AddLeftBrace(sb, language); foreach (var fieldNode in classNode.fieldList) { string fieldName = fieldNode.name; if (classNode.exportStatic) { fieldName = "value." + fieldName; } string functionName = CodeExporterHelper.GetFunctionStatement(language, CodeMode.Encode, fieldNode.decoration, fieldName, true); sb.AppendIndentLine(functionName); } AddRightBrace(sb, language); }
//导出类型的序列化辅助函数 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 void ExportClassName(StringBuilder sb, ClassDefine classNode, Language lanaguage) { string className = classNode.name; //注释 sb.AppendIndentLine("//" + classNode.desc + ExportProtocolConstant.GenerateComment); if (lanaguage == Language.Java) { sb.AppendIndentLine("public class " + className); } else if (lanaguage == Language.CSharp) { sb.AppendIndentLine("public class " + className);// + genericStatement); } }
//增加命名空间:xxx.xxx.xxx 这种形式,然后xxx都是小写 public static bool ExportNamespace(StringBuilder sb, ClassDefine classNode, Language language) { string namesapceStatement = BufferCodeExporter.GetNamespaceStatement(language, classNode.decoration, NamespaceMode.Declare); HashSet <string> usingNamesapceSet = new HashSet <string>(); foreach (var fieldNode in classNode.fieldList) { var usingNamespaces = GetNamespaces(language, fieldNode.decoration, globalClassDefines); foreach (var usingNamespace in usingNamespaces) { if (!usingNamesapceSet.Contains(usingNamespace)) { usingNamesapceSet.Add(usingNamespace); } } } if (language == Language.Java) { sb.AppendIndentLine(namesapceStatement); sb.AppendLine(""); foreach (var usingNamespace in usingNamesapceSet) { if (!string.IsNullOrEmpty(usingNamespace)) { sb.AppendIndentLine(BufferCodeExporter.GetNamespaceStatement(language, usingNamespace, NamespaceMode.Using)); } } } else if (language == Language.CSharp) { foreach (var usingNamespace in usingNamesapceSet) { if (!string.IsNullOrEmpty(usingNamespace)) { sb.AppendIndentLine(BufferCodeExporter.GetNamespaceStatement(language, usingNamespace, NamespaceMode.Using)); } } sb.AppendLine(""); sb.AppendIndentLine(namesapceStatement); AddLeftBrace(sb, language); return(true); } return(false); }
public static void ExportClassMember(StringBuilder sb, ClassDefine classNode, Language language) { //成员变量 foreach (var fieldNode in classNode.fieldList) { sb.AppendIndentLine(fieldNode.ExportCode(language, FieldType.ClassField)); } sb.AppendLine(""); //构造函数 string constructorName = classNode.name; /*int start = constructorName.IndexOf("<"); * int end = constructorName.LastIndexOf(">"); * if (start != -1 && end != -1) * { * constructorName = constructorName.Substring(0, start); * }*/ sb.AppendIndentLine(string.Format("public {0}()", constructorName)); AddLeftBrace(sb, language); AddRightBrace(sb, language); }
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 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()); }