static public string toLuaMsgAttributeSet(PBAttribute attribute, string pbModularName) { if (attribute.name == "cmd") { return(toLuaMsgAttributeSetCMDTp.Replace("$pbModularName$", pbModularName).Replace("$Default$", attribute.Default)); } else if (attribute.type == "sint32") { return(toLuaMsgAttributeSetINTTp.Replace("$attributeName$", attribute.name).Replace("$Notes$", attribute.Notes)); } return(toLuaMsgEnumSetINTTp.Replace("$attributeName$", attribute.name).Replace("$Notes$", attribute.Notes)); //return "err"; }
static public string toLuaWarnMsgAttribute(PBMessage pbMessage) { string warnMsgAttribute = ""; foreach (PBElement pElement in pbMessage.child) { if (pElement.pb_type != PBElement.type_PBAttribute) { continue; } if (pElement.name == "result") { continue; } //"\t\twarn('$msgName$.$attributeName$'..msg.$attributeName$);"; PBAttribute attribute = (PBAttribute)pElement; warnMsgAttribute += warnMsgAttributeTp.Replace("$msgName$", pbMessage.name).Replace("$attributeName$", attribute.name); } return(warnMsgAttribute); }
static public string toLuaMsgCheckResult(PBMessage pbMessage) { string checkResult = checkResultTp; string checkResultItem = ""; foreach (PBElement pElement in pbMessage.child) { if (pElement.pb_type != PBElement.type_PBAttribute) { continue; } PBAttribute attribute = (PBAttribute)pElement; if (attribute.name == "result") { PBElement pbEnum = pbMessage.getChildByName(attribute.type); if (pbEnum == null) { Debug.LogError("not find !!!! = > " + attribute.type); } else { /* * static public string checkResultItemTp = "\tif msg.result == $enumValue$ then\n"+ * "\t\twarn('$Notes$');\n"+ * "\tend\n"; */ foreach (PBElement pbKeyValue in pbEnum.child) { PBKeyValue keyValue = (PBKeyValue)pbKeyValue; checkResultItem += checkResultItemTp.Replace("$enumValue$", keyValue.value).Replace("$Notes$", keyValue.Notes); } } } } return(checkResult.Replace("$checkResultItem$", checkResultItem)); }
static public void toLuaMsgCtrl(string protoPath, PBFile pbFile) { init(); //string pbModularName = protoPath.Replace(Application.dataPath +"/Lua/pblua/","").Replace(".proto","").ToLower() + "_pb"; string pbModularName = Path.GetFileName(protoPath).ToLower() + "_pb"; string luaModularName = pbModularName.Substring(0, 1).ToUpper() + pbModularName.Substring(1, pbModularName.Length - 1).Replace("_pb", ""); string luaModularCtrlName = luaModularName + "MsgCtrl"; string luaModularCtrlFileName = luaModularName + "MsgCtrl"; string sendFunList = ""; string handleMsg = ""; foreach (PBElement chlidPBElement in pbFile.child) { //1. if (chlidPBElement.pb_type == PBElement.type_PBMessage && chlidPBElement.name.IndexOf("Usr") != -1) { string Params = ""; string InitMsg = initMsgTp.Replace("$pbModularName$", pbModularName).Replace("$msgName$", chlidPBElement.name); foreach (PBElement pElement in chlidPBElement.child) { if (pElement.pb_type == PBElement.type_PBAttribute) { PBAttribute attribute = (PBAttribute)pElement; if (attribute.name != "cmd") { Params += "," + attribute.name; } InitMsg += toLuaMsgAttributeSet(attribute, pbModularName); } /* * if(pElement.pb_type == PBElement.type_PBEnum){ * PBEnum pbEnum = (PBEnum)pElement; * Params += ","+pbEnum.name ; * toLuaMsgEnumSet(pbEnum,pbModularName); * }*/ } if (Params.Length > 0) { Params = Params.Substring(1, Params.Length - 1); } string funCode = sendFunTp.Replace("$Notes$", chlidPBElement.Notes).Replace("$luaModularCtrlName$", luaModularCtrlName).Replace("$MsgName$", chlidPBElement.name).Replace("$Params$", Params).Replace("$InitMsg$", InitMsg); Debug.LogWarning(funCode); if (funCode.StartsWith("--//申请收获")) { Debug.Log("xxx"); } sendFunList += funCode; } //2. if (chlidPBElement.pb_type == PBElement.type_PBMessage && chlidPBElement.name.IndexOf("Svr") != -1) { string checkResult = toLuaMsgCheckResult((PBMessage)chlidPBElement); string warnMsgAttribute = toLuaWarnMsgAttribute((PBMessage)chlidPBElement); if (chlidPBElement.name == "S_MsgSvrCollectionEnterFailed") { Debug.LogWarning(chlidPBElement.name); } handleMsg += handleMsgTp.Replace("$pbModularName$", pbModularName).Replace("$Default$", ((PBAttribute)chlidPBElement.getChildByName("cmd")).Default).Replace("$msgName$", chlidPBElement.name).Replace("$warnMsgAttribute$", warnMsgAttribute).Replace("$checkResult$", checkResult); } } string onPBMsgFunCode = onPBMsgFunTp.Replace("$luaModularCtrlName$", luaModularCtrlName).Replace("$handleMsg$", handleMsg); Debug.LogWarning(onPBMsgFunCode); newFileMsgCtrl(luaModularCtrlFileName, luaMsgCtrlCode.Replace("$proto$", pbFile.code).Replace("$pbModularName$", pbModularName).Replace("$luaModularCtrlName$", luaModularCtrlName).Replace("$sendFunList$", sendFunList).Replace("$onPBMsgFunCode$", onPBMsgFunCode) ); }
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); }