public void read1(string codep) { string code = codep.Trim(); for (int i = 0; i < code.Length; i++) { Debug.LogWarning("index --------> " + i); if (i == 1834) { Debug.LogWarning("index --------> " + i); } int nextPBEnumIndex = code.IndexOf("enum", i); int nextPBMessageIndex = code.IndexOf("message", i); if (nextPBEnumIndex == -1 && nextPBMessageIndex == -1) { return; } if (nextPBEnumIndex == -1 && nextPBMessageIndex > -1) { int blockStart = code.IndexOf("{", nextPBMessageIndex); int messageEndIndex = FindEnd(code, blockStart + 1); //code.IndexOf("}",nextPBMessageIndex); PBMessage pbMessage = new PBMessage(code.Substring(nextPBMessageIndex, (messageEndIndex - nextPBMessageIndex) + 1)); i = messageEndIndex + 1; continue; } if (nextPBEnumIndex > -1 && nextPBMessageIndex == -1) { int blockStart = code.IndexOf("{", nextPBEnumIndex); int enumEndIndex = FindEnd(code, blockStart + 1); //code.IndexOf("}",nextPBEnumIndex); PBEnum pbEnum = new PBEnum(code.Substring(nextPBEnumIndex, (enumEndIndex - nextPBEnumIndex) + 1)); i = enumEndIndex + 1; continue; } if (nextPBMessageIndex < nextPBEnumIndex) { int blockStart = code.IndexOf("{", nextPBMessageIndex); int messageEndIndex = FindEnd(code, blockStart + 1); //code.IndexOf("}",nextPBMessageIndex); PBMessage pbMessage = new PBMessage(code.Substring(nextPBMessageIndex, (messageEndIndex - nextPBMessageIndex) + 1)); i = messageEndIndex + 1; } else { int blockStart = code.IndexOf("{", nextPBEnumIndex); int enumEndIndex = FindEnd(code, blockStart + 1); //code.IndexOf("}",nextPBEnumIndex); PBEnum pbEnum = new PBEnum(code.Substring(nextPBEnumIndex, (enumEndIndex - nextPBEnumIndex) + 1)); i = enumEndIndex + 1; } } }
static void pbFileInfo() { string protoPath = Application.dataPath + "/Resources/proto/addressbook.proto"; PBFile.curPath = "/Users/zhuyuu3d/Downloads"; protoPath = PBFile.curPath + "/ZTest.proto"; PBFile.curPath = "C:/Users/Administrator.PC-20171226GAHY/Desktop/proto"; protoPath = "C:/Users/Administrator.PC-20171226GAHY/Desktop/proto/ZBank.proto"; string LuaCodeOutPath = "/Project/Editor/LuaCodeOut/"; string protoTxt; //using (StreamReader readerTemplentPath = new StreamReader(protoPath, Encoding.GetEncoding("GBK"))) using (StreamReader readerTemplentPath = new StreamReader(protoPath, Encoding.GetEncoding("utf-8"))) { protoTxt = readerTemplentPath.ReadToEnd(); readerTemplentPath.Close(); } string ModuleName = Path.GetFileName(protoPath).Replace(".proto", ""); PBFile pbFile = new PBFile(protoTxt); pbFile.read(); Debug.Log(">> package = " + pbFile.package); StringBuilder CmdKeyList = new StringBuilder(); StringBuilder CmdMappingList = new StringBuilder(); StringBuilder CmdSendFunList = new StringBuilder(); StringBuilder CmdReceiveList = new StringBuilder(); StringBuilder CmdAddlistenerList = new StringBuilder(); StringBuilder CmdRemlistenerList = new StringBuilder(); //pbFile.debug (); //循环 poto->Element 文件定义 foreach (PBElement chlidPBElement in pbFile.child) { Debug.LogFormat("消息 >> name = {0},pb_type = {1}", chlidPBElement.name, chlidPBElement.pb_type); //循环 poto->Element->Enum foreach (PBElement msgPBElement in chlidPBElement.child) { Debug.LogFormat("消息字段 >> >> name = {0},pb_type = {1}", msgPBElement.name, msgPBElement.pb_type); if (msgPBElement.pb_type == PBElement.type_PBEnum && msgPBElement.name.Equals("enumID")) { //消息 ID 枚举 PBEnum PBEnum = (PBEnum)msgPBElement; foreach (PBElement EnumPBElement in msgPBElement.child) { if (EnumPBElement is PBKeyValue) { PBKeyValue cmdID = (EnumPBElement as PBKeyValue); Debug.LogFormat(">> >> >> ID = {0},value = {1}", (EnumPBElement as PBKeyValue).key, (EnumPBElement as PBKeyValue).value); //string tp = "$MsgName$ = sib($ID$),"; CmdKeyList.AppendLine("\t$MsgName$ = sib($ID$),".Replace("$MsgName$", chlidPBElement.name).Replace("$ID$", cmdID.value)); } } } } CmdMappingList.AppendLine("\tpbtable [Cmd.$MsgName$] = \"$Package$.$MsgName$\"".Replace("$MsgName$", chlidPBElement.name).Replace("$Package$", pbFile.package)); //循环 poto->Element.name == CS_ if (chlidPBElement.name.StartsWith("CS_")) { CmdSendFunList.Append("function $ModuleName$ModuleData.send_$MsgName$(".Replace("$MsgName$", chlidPBElement.name).Replace("$ModuleName$", ModuleName)); //循环 poto->Element->Attribute //参数列表 int i = 0; foreach (PBElement msgPBElement in chlidPBElement.child) { if (msgPBElement.pb_type == PBElement.type_PBAttribute) { CmdSendFunList.Append(i > 0 ? "," + msgPBElement.name : msgPBElement.name); i++; } } CmdSendFunList.AppendLine(")"); CmdSendFunList.AppendLine("\tlocal " + chlidPBElement.name + " = {}"); //循环 poto->Element->Attribute foreach (PBElement msgPBElement in chlidPBElement.child) { if (msgPBElement.pb_type == PBElement.type_PBAttribute) { CmdSendFunList.AppendLine("\t" + chlidPBElement.name + "." + msgPBElement.name + " = " + msgPBElement.name); } } CmdSendFunList.AppendLine(string.Format("\tGameState.tcpClinet.sendmsg(Cmd.{0},{1})", chlidPBElement.name, chlidPBElement.name)); CmdSendFunList.AppendLine("end"); } //循环 poto->Element.name == SC_ if (chlidPBElement.name.StartsWith("SC_")) { CmdAddlistenerList.AppendLine("\ttcpClinet.addlistener(Cmd.$MsgName$,$ModuleName$ModuleData.on_msg)".Replace("$ModuleName$", ModuleName).Replace("$MsgName$", chlidPBElement.name)); CmdRemlistenerList.AppendLine("\ttcpClinet.removelistener(Cmd.$MsgName$,$ModuleName$ModuleData.on_msg)".Replace("$ModuleName$", ModuleName).Replace("$MsgName$", chlidPBElement.name)); CmdReceiveList.AppendLine("\tif key == Cmd.$MsgName$ then".Replace("$MsgName$", chlidPBElement.name)); CmdReceiveList.AppendLine("\t\tprint(\"$ModuleName$ >> on_msg >> user_para >> \".. Cmd.$MsgName$)\n".Replace("$ModuleName$", ModuleName).Replace("$MsgName$", chlidPBElement.name)); CmdReceiveList.AppendLine("\t\t$ModuleName$ModuleData.$MsgName$ = decode".Replace("$ModuleName$", ModuleName).Replace("$MsgName$", chlidPBElement.name)); foreach (PBElement msgPBElement in chlidPBElement.child) { if (msgPBElement.pb_type == PBElement.type_PBAttribute) { CmdReceiveList.AppendLine("\t\tprint(\"$MsgName$.$Attribute$ \".. decode.$Attribute$)").Replace("$MsgName$", chlidPBElement.name).Replace("$Attribute$", msgPBElement.name); } } CmdReceiveList.AppendLine("\tend"); } } /* * -- $ModuleName$ * -- $MsgName$ * -- $Package$ * -- $ID$ */ Dictionary <string, string> repkey = new Dictionary <string, string> (); repkey["$ModuleName$"] = ModuleName; repkey ["$CmdKeyList$"] = CmdKeyList.ToString(); repkey ["$CmdMappingList$"] = CmdMappingList.ToString(); repkey ["$CmdReceiveList$"] = CmdReceiveList.ToString(); repkey ["$CmdAddlistenerList$"] = CmdAddlistenerList.ToString(); repkey ["$CmdRemlistenerList$"] = CmdRemlistenerList.ToString(); repkey ["$CmdSendFunList$"] = CmdSendFunList.ToString(); repkey ["$Package$"] = pbFile.package; //"$MsgName$ = sib($ID$)," //$CmdKeyList$ string luaFilePath = Application.dataPath + string.Format("{0}/DataModules/{1}ModuleData.lua", LuaCodeOutPath, repkey ["$ModuleName$"]); CreateLuaCodeByTp(repkey, Application.dataPath + "/Project/Editor/LuaCodeTp/TP_ModuleData.lua", luaFilePath); }
static public string toLuaMsgEnumSet(PBEnum pbEnum, string pbModularName) { // toLuaMsgEnumSetINTTp = "$MessageName$_$EnumTypeName$_$EnumAttributeName$_ENUM" return(toLuaMsgEnumSetINTTp.Replace("$attributeName$", pbEnum.name).Replace("$Notes$", pbEnum.Notes)); }
public void read() { List <CodeKeyword> codekeyStack = new List <CodeKeyword>(); List <CodeSymbol> symbolStack = new List <CodeSymbol>(); List <PBElement> elementStack = new List <PBElement>(); elementStack.Add(this); if (charArr == null) { charArr = code.ToCharArray(); } PBElement curPBElement = null; int startIndex = 0; for (int i = 0; i < code.Length; i++) { startIndex = i; CodeSymbol curSymbol = FindNextSymbol(code, startIndex); int Index = curSymbol.Index; if (Index > 0) { //跳过注释 if (curSymbol.Symbol == '/') { if (charArr [curSymbol.Index + 1] == '*') { while (Index < charArr.Length) { curSymbol = FindNextSymbol(code, Index + 1, '/'); Index = curSymbol.Index; if (Index == -1) { Debug.LogErrorFormat("{0} not find '/' end", name); return; } if (charArr [curSymbol.Index - 1] == '*') { break; } } i = Index; Debug.LogWarningFormat("{0} jump /**/ index {1} ", name, Index); continue; } } string key = code.Substring(startIndex, Index - startIndex); if (curPBElement != null && key.StartsWith("//")) { // curPBElement.Notes = key; } if (key == "") { } else { //关键字压入 codekeyStack.Add(new CodeKeyword(startIndex, key)); } if (key.Equals("import")) { //PBFile pbFile = new PBFile (); PBImportFile pushPBImportFile = new PBImportFile(); pushPBImportFile.startIndex = startIndex; elementStack.Add(pushPBImportFile); curPBElement = pushPBImportFile; } else if (key.Equals("package")) { PBPackage pushPBPackage = new PBPackage(); pushPBPackage.startIndex = startIndex; elementStack.Add(pushPBPackage); curPBElement = pushPBPackage; } else if (key.Equals("required") || key.Equals("optional") || key.Equals("repeated")) { PBAttribute pushPBAttribute = new PBAttribute(); pushPBAttribute.startIndex = startIndex; elementStack.Add(pushPBAttribute); curPBElement = pushPBAttribute; } else //enum压入 if (key == "enum") { PBEnum pushPBElement = new PBEnum(); pushPBElement.pbFile = this; pushPBElement.name = key; pushPBElement.startIndex = Index; elementStack.Add(pushPBElement); curPBElement = pushPBElement; } else //message压入 if (key == "message") { PBMessage pushPBMessage = new PBMessage(); pushPBMessage.pbFile = this; pushPBMessage.name = key; pushPBMessage.startIndex = Index; elementStack.Add(pushPBMessage); curPBElement = pushPBMessage; if (codekeyStack.Count >= 2 && codekeyStack[codekeyStack.Count - 2].value.StartsWith("//")) { pushPBMessage.Notes = codekeyStack[codekeyStack.Count - 2].value; } } /* * if(curSymbol.Symbol == '='){ * string pname = (string)codekeyStack[codekeyStack.Count-1]; * Debug.LogWarning(" >> pname :))))) --------> " + pname); * }else */ if (curSymbol.Symbol == '{') { CodeKeyword ptype = codekeyStack[codekeyStack.Count - 2]; CodeKeyword pname = codekeyStack[codekeyStack.Count - 1]; //Debug.LogWarning(" >> Index :))))) --------> " + pname.Index + ", >> pname :))))) --------> " + pname.value ); //压入符号栈 symbolStack.Add(curSymbol); } else if (curSymbol.Symbol == '}') { //块结束 CodeSymbol pushSymbol = symbolStack[symbolStack.Count - 1]; if (pushSymbol.Symbol == '{') { symbolStack.RemoveAt(symbolStack.Count - 1); //弹出符号栈 PBElement popUpPBElement = elementStack[elementStack.Count - 1]; popUpPBElement.endIndex = Index; popUpPBElement.cut(code); elementStack.RemoveAt(elementStack.Count - 1); if (elementStack.Count > 0) { elementStack[elementStack.Count - 1].addChild(popUpPBElement); } } else { Debug.LogError("Symbol Error { "); } } else if (curSymbol.Symbol == '[') { symbolStack.Add(curSymbol); PBKeyValue pushPBKeyValue = new PBKeyValue(); pushPBKeyValue.startIndex = Index; elementStack.Add(pushPBKeyValue); } else if (curSymbol.Symbol == ']') { CodeSymbol pushSymbol = symbolStack[symbolStack.Count - 1]; if (pushSymbol.Symbol == '[') { symbolStack.RemoveAt(symbolStack.Count - 1); //弹出 PBElement popUpPBElement = elementStack[elementStack.Count - 1]; popUpPBElement.endIndex = Index; popUpPBElement.cut(code); elementStack.RemoveAt(elementStack.Count - 1); if (elementStack.Count > 0) { elementStack[elementStack.Count - 1].addChild(popUpPBElement); } } else { Debug.LogError("Symbol Error [ "); } } else if (curSymbol.Symbol == ';') { //弹出 PBElement popUpPBElement = elementStack[elementStack.Count - 1]; if (popUpPBElement.pb_type == PBElement.type_PBPackage) { popUpPBElement.endIndex = Index; popUpPBElement.cut(code); this.package = (popUpPBElement as PBPackage).pbPackageName; elementStack.RemoveAt(elementStack.Count - 1); } else if (popUpPBElement.pb_type == PBElement.type_PBImportFile) { popUpPBElement.endIndex = Index; popUpPBElement.cut(code); PBFile pbFile = new PBFile(); pbFile.name = (popUpPBElement as PBImportFile).pbFilePath; pbFile.readFile(pbFile.name); pbFile.read(); PBFileImportDic.Add(pbFile.name, pbFile); elementStack.RemoveAt(elementStack.Count - 1); } else //枚举 属性 if (popUpPBElement.pb_type == PBElement.type_PBEnum) { PBKeyValue PBKeyValue = new PBKeyValue(); PBKeyValue.key = codekeyStack[codekeyStack.Count - 2].value; PBKeyValue.value = codekeyStack[codekeyStack.Count - 1].value; PBKeyValue.startIndex = codekeyStack[codekeyStack.Count - 2].Index; PBKeyValue.endIndex = Index; PBKeyValue.cut(code); popUpPBElement.addChild(PBKeyValue); curPBElement = PBKeyValue; codekeyStack.RemoveAt(codekeyStack.Count - 1); codekeyStack.RemoveAt(codekeyStack.Count - 1); CodeSymbol nextSymbol = FindNextSymbol(code, Index + 1); string nextKey = code.Substring(Index + 1, nextSymbol.Index - (Index + 1)); if (curPBElement != null && nextKey.StartsWith("//")) { curPBElement.Notes = nextKey; } } else //message 属性 if (popUpPBElement.pb_type == PBElement.type_PBAttribute) { popUpPBElement.endIndex = Index; popUpPBElement.cut(code); elementStack.RemoveAt(elementStack.Count - 1); curPBElement = popUpPBElement; if (elementStack.Count > 0) { elementStack[elementStack.Count - 1].addChild(popUpPBElement); } CodeSymbol nextSymbol = PBFile.FindNextSymbol(code, Index + 1); string nextKey = code.Substring(Index + 1, nextSymbol.Index - (Index + 1)); if (curPBElement != null && nextKey.StartsWith("//")) { curPBElement.Notes = nextKey; } } } } i = Index; } //getAllChild(this); }