//----------------------内部调用------------------------- #region 内部调用 /// <summary> /// 生成Enum脚本 /// </summary> void sWriteFile(EnumFileData fileData) { if (fileData.mIsCS) { //生成C#脚本 StringBuilder sb = new StringBuilder(); NetCmdXmlParser.sWriteFileHeadComment(sb, fileData.mClassName, fileData.mDescribe); //生成注释的文件头 sWriteCSFileContent(sb, fileData); //将解析到的xml生成string,并赋值到sb //写文件 string outFilePath = NetCmdXmlParser.targetFileBasePath + enumOutFilePath + fileData.mClassName + ".cs"; NetCmdXmlParser.sWriteStringToFile(outFilePath, sb); } if (fileData.mIsLua) { //生成Lua脚本 StringBuilder lua_sb = new StringBuilder(); NetCmdXmlParser.sWriteLuaFileHeadComment(lua_sb, fileData.mClassName, fileData.mDescribe); //生成注释的文件头 sWriteLuaFileContent(lua_sb, fileData); //将解析到的xml生成string,并赋值到sb lua_sb.AppendLine(); //写文件 string outLuaFilePath = NetCmdXmlParser.targetLuaFileBasePath + enumOutFilePath + enumLuaFileName + ".lua"; NetCmdXmlParser.sWriteStringToFile(outLuaFilePath, lua_sb, true); //所有Enum都在一个文件里,统一引用即可,不用再次引用 } }
//----------------------内部调用------------------------- #region 内部调用 /// <summary> /// 生成Data脚本 /// </summary> void sWriteFile(DataStructFileData fileData) { if (fileData.mIsCS) { //生成C#脚本 StringBuilder sb = new StringBuilder(); NetCmdXmlParser.sWriteFileHeadComment(sb, fileData.mClassName, fileData.mDescribe); //生成注释的文件头 NetCmdXmlParser.sWriteFileHeadNameSpace(sb); //写命名空间 sWriteCSFileContent(sb, fileData); //将解析到的xml生成string,并赋值到sb //写文件 string outFilePath = NetCmdXmlParser.targetFileBasePath + paramOutFilePath + fileData.mClassName + ".cs"; //生成文件路径 NetCmdXmlParser.sWriteStringToFile(outFilePath, sb); //生成文件 } if (fileData.mIsLua) { //生成Lua脚本 StringBuilder lua_sb = new StringBuilder(); NetCmdXmlParser.sWriteLuaFileHeadComment(lua_sb, fileData.mClassName, fileData.mDescribe); //生成注释的文件头 sWriteLuaFileContent(lua_sb, fileData); //将解析到的xml生成string,并赋值到lua_sb //写Lua文件 string outLuaFilePath = NetCmdXmlParser.targetLuaFileBasePath + paramOutFilePath + fileData.mClassName + ".lua"; //生成文件路径 NetCmdXmlParser.sWriteStringToFile(outLuaFilePath, lua_sb); //生成文件 //放入NetCmdLuaHandler中进行引用,以便加载 LuaScriptParserTool.AddLuaRequireString(constPackageName + "." + fileData.mClassName); } }
//----------------------内部调用------------------------- #region 内部调用 /// <summary> /// 生成Const脚本 /// </summary> void sWriteFile(ConstFileData fileData) { //生成C#脚本 StringBuilder sb = new StringBuilder(); NetCmdXmlParser.sWriteFileHeadComment(sb, fileData.mClassName, "网络消息所用常量"); //生成注释的文件头 NetCmdXmlParser.sWriteFileHeadNameSpace(sb); //写命名空间 sWriteCSFileContent(sb, fileData); //将解析到的xml生成string,并赋值到sb //写文件 string outFilePath = NetCmdXmlParser.targetFileBasePath + constOutFilePath + fileData.mClassName + ".cs"; NetCmdXmlParser.sWriteStringToFile(outFilePath, sb); //生成Lua脚本 StringBuilder lua_sb = new StringBuilder(); NetCmdXmlParser.sWriteLuaFileHeadComment(lua_sb, fileData.mClassName, "网络消息所用常量"); //生成注释的文件头 sWriteLuaFileContent(lua_sb, fileData); //将解析到的xml生成string,并赋值到sb //写文件 string outLuaFilePath = NetCmdXmlParser.targetLuaFileBasePath + constOutFilePath + fileData.mClassName + ".lua"; NetCmdXmlParser.sWriteStringToFile(outLuaFilePath, lua_sb); //放入NetCmdLuaHandler中进行引用,以便加载 LuaScriptParserTool.AddLuaRequireString(constPackageName + "." + fileData.mClassName); //加入const的引用 }
//----------------------内部调用------------------------- #region 内部调用 /// <summary> /// 生成base脚本 /// </summary> void sWriteFile(AllBaseCmdFileData fileData) { if (fileData.mIsCS) { //生成C#脚本 StringBuilder sb = new StringBuilder(); NetCmdXmlParser.sWriteFileHeadComment(sb, fileData.mClassName, fileData.mDescribe); //生成注释的文件头 NetCmdXmlParser.sWriteFileHeadNameSpace(sb); //写命名空间引用 sWriteCSFileContent(sb, fileData); //将解析到的xml生成string,并赋值到sb //写文件 string outFilePath = NetCmdXmlParser.targetFileBasePath + baseCmdOutFilePath + fileData.mClassName + ".cs"; NetCmdXmlParser.sWriteStringToFile(outFilePath, sb); } if (fileData.mIsLua) { //生成Lua脚本 StringBuilder lua_db = new StringBuilder(); NetCmdXmlParser.sWriteLuaFileHeadComment(lua_db, fileData.mClassName, fileData.mDescribe); //生成注释的文件头 sWriteLuaFileContent(lua_db, fileData); //将解析到的xml生成string,并赋值到sb //写Lua文件 string outLuaFilePath = NetCmdXmlParser.targetLuaFileBasePath + baseCmdOutFilePath + fileData.mClassName + ".lua"; NetCmdXmlParser.sWriteStringToFile(outLuaFilePath, lua_db); //放入NetCmdLuaHandler中进行引用,以便加载 LuaScriptParserTool.AddLuaRequireString(LuaScriptParserTool.CmdBaseDirectory + "." + fileData.mClassName); } }
//写消息处理方法 public static void sWriteNetHandlerFile() { //C# StringBuilder sb = new StringBuilder(); NetCmdXmlParser.sWriteFileHeadComment(sb, className, "handler总文件"); //总的handle注释 NetCmdXmlParser.sWriteFileHeadNameSpace(sb); //加入命名空间 sWriteCSFileContent(sb, mCmdBigDic); //解析字典生成string //写文件 string outFilePath = NetCmdXmlParser.targetFileBasePath + "/" + className + ".cs"; NetCmdXmlParser.sWriteStringToFile(outFilePath, sb); //Lua StringBuilder sb_lua = new StringBuilder(); NetCmdXmlParser.sWriteLuaFileHeadComment(sb_lua, LuaHandler, "handler总文件"); //总的handle注释 sWriteLuaFileContent(sb_lua, mCmdBigDic); //解析字典生成string //写文件 string outFilePath_lua = NetCmdXmlParser.targetLuaFileBasePath + "/" + LuaHandler + ".lua"; NetCmdXmlParser.sWriteStringToFile(outFilePath_lua, sb_lua); }
//解析所有的 消息cmd的xml文件 public static void ParseAllCmdXml(string name = "") { CmdHandlerFileTool.CreateTypeDic(); string[] _fils = Directory.GetFiles(CmdXmlPath, "*.xml", SearchOption.AllDirectories); //获取当前工程下的所有xml foreach (string filePath in _fils) { if (filePath.EndsWith("xml")) { if (!string.IsNullOrEmpty(name)) { if (!filePath.EndsWith("\\" + name + ".xml")) { continue; } else { Debug.Log("找到文件了"); } } string targetFilePath = filePath.Replace("\\", "/"); string fileName = targetFilePath.Substring(targetFilePath.LastIndexOf("/") + 1); string filePreName = fileName.Substring(0, fileName.IndexOf("_")); //获取当前数据类型 ICmdXmlParser targetParser = null; if (filePreName == "Const") { targetParser = new ConstXmlParser(); } else if (filePreName == "Data") { targetParser = new DataXmlParser(); } else if (filePreName == "Enum") { targetParser = new EnumXmlParser(); } else if (filePreName == "Base") { targetParser = new BaseXmlParser(); } else if (filePreName == "Cmd") { targetParser = new CmdXmlParser(); } if (targetParser != null) { if (fileName.Contains("stMapDataMapScreenMobileClientCmd")) //这个协议复杂度过高,无法自动生成,忽略掉 { continue; } //解析当前文件 targetParser.ParserXml(filePath, !string.IsNullOrEmpty(name)); } } } //处理最后所有的 消息分发 写文件 if (string.IsNullOrEmpty(name)) { //创建总的handler文件 CmdHandlerFileTool.sWriteNetHandlerFile(); //创建lua的stNullClientCmd文件,其实不创建也可 StringBuilder file = new StringBuilder(); file.Append(LuaScriptParserTool.CreateNullClientCmd()); //创建消息根节点 string outFilePath_lua = NetCmdXmlParser.targetLuaFileBasePath + "/" + LuaScriptParserTool.CmdBaseDirectory + "/stNullClientCmd.lua"; NetCmdXmlParser.sWriteStringToFile(outFilePath_lua, file); } }
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); } }