//解析基础类型 根据类型 数组 string public static void CmdParamParserArrayByString_CS(string bufferStr, ParamDataOutString outStrData, ParamData tarPa) { if (tarPa.mArrayLen != "") //定长数组 { string tarArrLen = tarPa.mArrayLen; int arrLenOut = 0; if (!int.TryParse(tarArrLen, out arrLenOut)) //不是数字 { tarArrLen = "(int)" + tarPa.mArrayLen; } string strDel = "public string[] {0} = new string[{1}]; \t\t\t\t// {2}"; outStrData.mOutDeclareString.Add(string.Format(strDel, tarPa.mName, tarArrLen, tarPa.mComment)); //字符串 长度 string tarStrLen = tarPa.mStringLen; int strLenOut = 0; if (!int.TryParse(tarStrLen, out strLenOut)) //不是数字 { string strPack1 = "for(int i = 0; i < {0}.Length; ++i)"; outStrData.mOutPackString.Add(string.Format(strPack1, tarPa.mName)); string strPack2 = "\t{0}.WriteString({1}[i], (uint){2});"; outStrData.mOutPackString.Add(string.Format(strPack2, bufferStr, tarPa.mName, tarPa.mStringLen)); string strUnpack1 = "for(int i = 0; i < {0}.Length; ++i)"; outStrData.mOutUnpackString.Add(string.Format(strUnpack1, tarPa.mName)); string strUnpack2 = "\t{0}[i] = {1}.ReadString((int){2});"; outStrData.mOutUnpackString.Add(string.Format(strUnpack2, tarPa.mName, bufferStr, tarPa.mStringLen)); } else { string strPack1 = "for(int i = 0; i < {0}.Length; ++i)"; outStrData.mOutPackString.Add(string.Format(strPack1, tarPa.mName)); string strPack2 = "\t{0}.WriteString({1}[i] , {2});"; outStrData.mOutPackString.Add(string.Format(strPack2, bufferStr, tarPa.mName, tarPa.mStringLen)); string strUnpack1 = "for(int i = 0; i < {0}.Length; ++i)"; outStrData.mOutUnpackString.Add(string.Format(strUnpack1, tarPa.mName)); string strUnpack2 = "\t{0}[i] = {1}.ReadString({2});"; outStrData.mOutUnpackString.Add(string.Format(strUnpack2, tarPa.mName, bufferStr, tarPa.mStringLen)); } } else if (tarPa.mArrayDynLen != "") //变长数组 { string strDel = "public string[] {0} = null; \t\t\t\t// {1}"; outStrData.mOutDeclareString.Add(string.Format(strDel, tarPa.mName, tarPa.mComment)); //字符串 长度 string tarStrLen = tarPa.mStringLen; int strLenOut = 0; if (!int.TryParse(tarStrLen, out strLenOut)) //不是数字 { string strPack1 = "for(int i = 0; i < {0}.Length; ++i)"; outStrData.mOutPackString.Add(string.Format(strPack1, tarPa.mName)); string strPack2 = "\t{0}.WriteString({1}[i], (uint){2});"; outStrData.mOutPackString.Add(string.Format(strPack2, bufferStr, tarPa.mName, tarPa.mStringLen)); string strUnpack1 = "{0} = new string[{1}];"; outStrData.mOutUnpackString.Add(string.Format(strUnpack1, tarPa.mName, tarPa.mArrayDynLen)); string strUnpack2 = "for(int i = 0; i < {0}.Length; ++i)"; outStrData.mOutUnpackString.Add(string.Format(strUnpack2, tarPa.mName)); string strUnpack3 = "\t{0}[i] = {1}.ReadString( (int){2} );"; outStrData.mOutUnpackString.Add(string.Format(strUnpack3, tarPa.mName, bufferStr, tarPa.mStringLen)); } else { string strPack1 = "for(int i = 0; i < {0}.Length; ++i)"; outStrData.mOutPackString.Add(string.Format(strPack1, tarPa.mName)); string strPack2 = "\t{0}.WriteString({1}[i] , {2});"; outStrData.mOutPackString.Add(string.Format(strPack2, bufferStr, tarPa.mName, tarPa.mStringLen)); string strUnpack1 = "{0} = new string[{1}];"; outStrData.mOutUnpackString.Add(string.Format(strUnpack1, tarPa.mName, tarPa.mArrayDynLen)); string strUnpack2 = "for(int i = 0; i < {0}.Length; ++i)"; outStrData.mOutUnpackString.Add(string.Format(strUnpack2, tarPa.mName)); string strUnpack3 = "\t{0}[i] = {1}.ReadString({2});"; outStrData.mOutUnpackString.Add(string.Format(strUnpack3, tarPa.mName, bufferStr, tarPa.mStringLen)); } } else { Debug.LogError("导出消息数组类型长度错误:" + mCurExportFileName); } }
//解析基础类型 根据类型 public static void CmdParamParserByBaseType_CS(string baseTypeStr, string bufferStr, ParamDataOutString outStrData, ParamData tarPa) { string bType = baseTypeStr.ToLower(); string strDel = "public {0} {1} = {2}; \t\t\t\t//{3}"; outStrData.mOutDeclareString.Add(string.Format(strDel, bType, tarPa.mName, tarPa.mValue, tarPa.mComment)); string strPack = "{0}.Write{1}({2});"; outStrData.mOutPackString.Add(string.Format(strPack, bufferStr, baseTypeStr, tarPa.mName)); string strUnpack = "{0} = {1}.Read{2}();"; outStrData.mOutUnpackString.Add(string.Format(strUnpack, tarPa.mName, bufferStr, baseTypeStr)); }
//解析基础类型 根据类型 数组 public static void CmdParamParserArrayByBaseType_CS(string baseTypeStr, string bufferStr, ParamDataOutString outStrData, ParamData tarPa) { string bType = baseTypeStr.ToLower(); if (tarPa.mArrayLen != "") //定长数组 { string tarStrLen = tarPa.mArrayLen; int strLenOut = 0; if (!int.TryParse(tarStrLen, out strLenOut)) //不是数字 { tarStrLen = "(int)" + tarPa.mArrayLen; } string strDel = "public {0}[] {1} = new {2}[{3}]; \t\t\t\t// {4}"; outStrData.mOutDeclareString.Add(string.Format(strDel, bType, tarPa.mName, bType, tarStrLen, tarPa.mComment)); string strPack1 = "for(int i = 0; i < {0}.Length; ++i)"; outStrData.mOutPackString.Add(string.Format(strPack1, tarPa.mName)); string strPack2 = "\t{0}.Write{1}({2}[i]);"; outStrData.mOutPackString.Add(string.Format(strPack2, bufferStr, baseTypeStr, tarPa.mName)); string strUnpack1 = "for(int i = 0; i < {0}.Length; ++i)"; outStrData.mOutUnpackString.Add(string.Format(strUnpack1, tarPa.mName)); string strUnpack2 = "\t{0}[i] = {1}.Read{2}();"; outStrData.mOutUnpackString.Add(string.Format(strUnpack2, tarPa.mName, bufferStr, baseTypeStr)); } else if (tarPa.mArrayDynLen != "") //变长数组 { string strDel = "public {0}[] {1} = null; \t\t\t\t// {2}"; outStrData.mOutDeclareString.Add(string.Format(strDel, bType, tarPa.mName, tarPa.mComment)); string strPack1 = "for(int i = 0; i < {0}; ++i)"; outStrData.mOutPackString.Add(string.Format(strPack1, tarPa.mArrayDynLen)); string strPack2 = "\t{0}.Write{1}({2}[i]);"; outStrData.mOutPackString.Add(string.Format(strPack2, bufferStr, baseTypeStr, tarPa.mName)); string strUnpack1 = "{0} = new {1}[{2}];"; outStrData.mOutUnpackString.Add(string.Format(strUnpack1, tarPa.mName, bType, tarPa.mArrayDynLen)); string strUnpack2 = "for(int i = 0; i < {0}; ++i)"; outStrData.mOutUnpackString.Add(string.Format(strUnpack2, tarPa.mArrayDynLen)); string strUnpack3 = "\t{0}[i] = {1}.Read{2}();"; outStrData.mOutUnpackString.Add(string.Format(strUnpack3, tarPa.mName, bufferStr, baseTypeStr)); } else { Debug.LogError("导出消息数组类型长度错误:" + mCurExportFileName); } }
//解析 变量声明 部分 C#文件 public static ParamDataOutString CmdParamParserDeclare_CS(ParamData tarPa, string fileName, bool isCmdType = false, bool isLua = false) { mCurExportFileName = fileName; ParamDataOutString outDataString = new ParamDataOutString(); string packageParamName = "byteBuffer"; if (isCmdType && !isLua) { packageParamName = "mByteBuffer"; } switch (tarPa.mParamType) { case EParamType.eParamType_Byte: CmdParamParserByBaseType_CS("Byte", packageParamName, outDataString, tarPa); break; case EParamType.eParamType_Short: CmdParamParserByBaseType_CS("Short", packageParamName, outDataString, tarPa); break; case EParamType.eParamType_UShort: CmdParamParserByBaseType_CS("UShort", packageParamName, outDataString, tarPa); break; case EParamType.eParamType_Int: CmdParamParserByBaseType_CS("Int", packageParamName, outDataString, tarPa); break; case EParamType.eParamType_UInt: CmdParamParserByBaseType_CS("UInt", packageParamName, outDataString, tarPa); break; case EParamType.eParamType_Long: CmdParamParserByBaseType_CS("Long", packageParamName, outDataString, tarPa); break; case EParamType.eParamType_ULong: CmdParamParserByBaseType_CS("ULong", packageParamName, outDataString, tarPa); break; case EParamType.eParamType_String: CmdParamParserByString_CS(packageParamName, outDataString, tarPa); break; case EParamType.eParamType_OtherData: CmdParamParserByClass_CS(packageParamName, outDataString, tarPa); break; case EParamType.eParamType_Byte_Array: CmdParamParserArrayByByte_CS(packageParamName, outDataString, tarPa); break; case EParamType.eParamType_Short_Array: CmdParamParserArrayByBaseType_CS("Short", packageParamName, outDataString, tarPa); break; case EParamType.eParamType_UShort_Array: CmdParamParserArrayByBaseType_CS("UShort", packageParamName, outDataString, tarPa); break; case EParamType.eParamType_Int_Array: CmdParamParserArrayByBaseType_CS("Int", packageParamName, outDataString, tarPa); break; case EParamType.eParamType_UInt_Array: CmdParamParserArrayByBaseType_CS("UInt", packageParamName, outDataString, tarPa); break; case EParamType.eParamType_Long_Array: CmdParamParserArrayByBaseType_CS("Long", packageParamName, outDataString, tarPa); break; case EParamType.eParamType_ULong_Array: CmdParamParserArrayByBaseType_CS("ULong", packageParamName, outDataString, tarPa); break; case EParamType.eParamType_String_Array: CmdParamParserArrayByString_CS(packageParamName, outDataString, tarPa); break; case EParamType.eParamType_OtherData_Array: CmdParamParserArrayByClass_CS(packageParamName, outDataString, tarPa); break; default: break; } return(outDataString); }
/// <summary> /// Lua 将解析到的xml生成string,并赋值到sb /// </summary> void sWriteLuaFileContent(StringBuilder sb, DataStructFileData fileData) { List <string> declareStringList = new List <string>(); List <string> ctorStringList = new List <string>(); List <string> packStringList = new List <string>(); List <string> unpackStringList = new List <string>(); for (int i = 0; i < fileData.mParamList.Count; ++i) { ParamDataOutString outString = CmdParserComUitls.CmdParamParserDeclare_Lua(fileData.mParamList[i], fileData.mClassName); declareStringList.AddRange(outString.mOutDeclareString.ToArray()); ctorStringList.AddRange(outString.mOutCtorString.ToArray()); packStringList.AddRange(outString.mOutPackString.ToArray()); unpackStringList.AddRange(outString.mOutUnpackString.ToArray()); } if (fileData.mFather == "NULL" || fileData.mFather == null) { sb.AppendLine(fileData.mClassName + " = class(\"" + fileData.mClassName + "\");"); } else { sb.AppendLine(fileData.mClassName + " = class(\"" + fileData.mClassName + "\"," + fileData.mFather + ");"); } sb.AppendLine(); //声明 for (int j = 0; j < declareStringList.Count; ++j) { sb.AppendLine("\t" + declareStringList[j].Replace("self", fileData.mClassName)); } sb.AppendLine(); //ctor sb.Append("function " + fileData.mClassName + ":ctor()"); sb.AppendLine(); for (int i = 0; i < ctorStringList.Count; i++) { sb.Append("\n" + ctorStringList[i]); } sb.Append("\nend\n"); sb.AppendLine(); //PackData 打包 sb.AppendLine("function " + fileData.mClassName + ":PackData(byteBuffer)"); if (fileData.mFather != "NULL" & fileData.mFather != null) { sb.AppendLine("\t " + fileData.mClassName + ".super:PackData(byteBuffer);"); } for (int k = 0; k < packStringList.Count; ++k) { sb.AppendLine("\t\t" + packStringList[k].Replace("self.byteBuffer", "byteBuffer")); } sb.Append("end"); sb.AppendLine(); //UnPackData sb.AppendLine("function " + fileData.mClassName + ":UnPackData(byteBuffer)"); if (fileData.mFather != "NULL" & fileData.mFather != null) { sb.AppendLine("\t " + fileData.mClassName + ".super.UnPackData(self,byteBuffer);"); } for (int m = 0; m < unpackStringList.Count; ++m) { sb.AppendLine("\t\t" + unpackStringList[m].Replace("self.byteBuffer", "byteBuffer")); } sb.Append("\nend\n"); sb.AppendLine(); }
/// <summary> /// C# 将解析到的xml生成string,并赋值到sb /// </summary> void sWriteCSFileContent(StringBuilder sb, DataStructFileData fileData) { List <string> declareStringList = new List <string>(); List <string> packStringList = new List <string>(); List <string> unpackStringList = new List <string>(); for (int i = 0; i < fileData.mParamList.Count; ++i) { ParamDataOutString outString = CmdParserComUitls.CmdParamParserDeclare_CS(fileData.mParamList[i], fileData.mClassName); //将ParamData转为string declareStringList.AddRange(outString.mOutDeclareString.ToArray()); //赋值 声明string packStringList.AddRange(outString.mOutPackString.ToArray()); //赋值 写入string unpackStringList.AddRange(outString.mOutUnpackString.ToArray()); //赋值 读取string } if (fileData.mFather == "NULL" || fileData.mFather == null) { sb.AppendLine("public class " + fileData.mClassName); } else { sb.AppendLine("public class " + fileData.mClassName + " : " + fileData.mFather); } sb.AppendLine("{"); //声明变量 for (int j = 0; j < declareStringList.Count; ++j) { sb.AppendLine("\t" + declareStringList[j]); } sb.AppendLine(); //PackData 写数据 if (fileData.mFather != "NULL" & fileData.mFather != null) { sb.AppendLine("\tpublic override void PackData(ByteBuffer byteBuffer)"); sb.AppendLine("\t{"); sb.AppendLine("\t\tbase.PackData(byteBuffer);"); } else { sb.AppendLine("\tpublic virtual void PackData(ByteBuffer byteBuffer)"); sb.AppendLine("\t{"); } for (int k = 0; k < packStringList.Count; ++k) { sb.AppendLine("\t\t" + packStringList[k]); } sb.AppendLine("\t}"); sb.AppendLine(); //UnPackData 读数据 if (fileData.mFather != "NULL" & fileData.mFather != null) { sb.AppendLine("\tpublic override void UnPackData(ByteBuffer byteBuffer)"); sb.AppendLine("\t{"); sb.AppendLine("\t\tbase.UnPackData(byteBuffer);"); } else { sb.AppendLine("\tpublic virtual void UnPackData(ByteBuffer byteBuffer)"); sb.AppendLine("\t{"); } for (int m = 0; m < unpackStringList.Count; ++m) { sb.AppendLine("\t\t" + unpackStringList[m]); } sb.AppendLine("\t}"); sb.AppendLine("}"); sb.AppendLine(); }
public void sWriteLuaFileContent(string fileType, CmdStructFileData fileData) { StringBuilder sb = new StringBuilder(); NetCmdXmlParser.sWriteLuaFileHeadComment(sb, fileData.mClassName, fileData.mDescribe); //生成注释的文件头 //所需XML数据 List <string> declareStringList = new List <string>(); List <string> packStringList = new List <string>(); List <string> unpackStringList = new List <string>(); List <string> ctorStringList = new List <string>(); //初始化字符串 for (int i = 0; i < fileData.mCmdList.Count; ++i) { ParamDataOutString outString = CmdParserComUitls.CmdParamParserDeclare_Lua(fileData.mCmdList[i], fileData.mClassName, true); declareStringList.AddRange(outString.mOutDeclareString.ToArray()); packStringList.AddRange(outString.mOutPackString.ToArray()); unpackStringList.AddRange(outString.mOutUnpackString.ToArray()); ctorStringList.AddRange(outString.mOutCtorString.ToArray()); } //lua的文件直接生成到对应根目录下,防止lua打包时生成过多零碎文件 /* * string filePathName = ""; * if (fileData.mPathName != null) * { * filePathName = fileData.mPathName + "/"; * } */ string outFilePath = ""; //生成文件位置 bool _isWrite = false; switch (fileType) { case "Lua_C": outFilePath = NetCmdXmlParser.targetLuaFileBasePath + cmdOutFilePath_C + fileData.mClassName + "_C.lua"; sWriteLuaFileContent_C(sb, fileData, declareStringList, packStringList); _isWrite = true; break; case "Lua_S": outFilePath = NetCmdXmlParser.targetLuaFileBasePath + cmdOutFilePath_S + fileData.mClassName + "_S.lua"; LuaScriptParserTool.AddLuaRequireString(cmdOutFilePathName_S + "." + fileData.mClassName + "_S"); sWriteLuaFileContent_S(sb, fileData, declareStringList, unpackStringList, ctorStringList); _isWrite = true; break; case "Lua_Handler": outFilePath = NetCmdXmlParser.targetLuaFileBasePath + cmdOutFilePath_Handler + fileData.mClassName + "_Handler.lua"; LuaScriptParserTool.AddLuaRequireString(cmdOutFilePathName_Handler + "." + fileData.mClassName + "_Handler"); if (!File.Exists(outFilePath)) { sWriteLuaFileContent_Handler(sb, fileData); _isWrite = true; } else { return; } break; } if (_isWrite) { NetCmdXmlParser.sWriteStringToFile(outFilePath, sb); } }
void sWriteCSFileContent(string fileType, CmdStructFileData fileData) { StringBuilder sb = new StringBuilder(); NetCmdXmlParser.sWriteFileHeadComment(sb, fileData.mClassName, fileData.mDescribe); //生成注释的文件头 NetCmdXmlParser.sWriteFileHeadNameSpace(sb); //生成命名空间 //所需XML数据 List <string> declareStringList = new List <string>(); List <string> packStringList = new List <string>(); List <string> unpackStringList = new List <string>(); List <string> ctorStringList = new List <string>(); //初始化字符串 for (int i = 0; i < fileData.mCmdList.Count; ++i) { ParamDataOutString outString = CmdParserComUitls.CmdParamParserDeclare_CS(fileData.mCmdList[i], fileData.mClassName, true); declareStringList.AddRange(outString.mOutDeclareString.ToArray()); packStringList.AddRange(outString.mOutPackString.ToArray()); unpackStringList.AddRange(outString.mOutUnpackString.ToArray()); ctorStringList.AddRange(outString.mOutCtorString.ToArray()); } string filePathName = ""; if (fileData.mPathName != null) { filePathName = fileData.mPathName + "/"; } string outFilePath = ""; //生成文件位置 bool _isWrite = false; switch (fileType) { case "C#_C": outFilePath = NetCmdXmlParser.targetFileBasePath + cmdOutFilePath_C + filePathName + fileData.mClassName + "_C.cs"; sWriteCSFileContent_C(sb, fileData, declareStringList, packStringList); _isWrite = true; break; case "C#_S": outFilePath = NetCmdXmlParser.targetFileBasePath + cmdOutFilePath_S + filePathName + fileData.mClassName + "_S.cs"; sWriteCSFileContent_S(sb, fileData, declareStringList, unpackStringList, ctorStringList); _isWrite = true; break; case "C#_Handler": outFilePath = NetCmdXmlParser.targetFileBasePath + cmdOutFilePath_Handler + filePathName + fileData.mClassName + "_Handler.cs"; if (!File.Exists(outFilePath)) { sWriteCSFileContent_Handler(sb, fileData); _isWrite = true; } else { return; } break; } if (_isWrite) { NetCmdXmlParser.sWriteStringToFile(outFilePath, sb); } }