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; } } }
void ExportFC_InnerClass(ref StringBuilder szOut, int nSpaceCount, bool bExportReadWriteFunc) { int nSize = m_ChildMsgDesc.Count; for (int i = 0; i < nSize; ++i) { PBMessage pChild = m_ChildMsgDesc[i]; pChild.ExportFCScript(ref szOut, nSpaceCount, bExportReadWriteFunc); } }
public void ExportFC(string szPath, bool bExportReadWriteFunc) { // 先删除该目录下的文件 FCClassWrap.DeletePath(szPath); System.IO.Directory.CreateDirectory(szPath); StringBuilder szFileData = new StringBuilder(1024 * 1024 * 4); PBMessageFile pMsgFile = null; PBMessage pMsg = null; string szPathName = string.Empty, szFileName = string.Empty; int nFileCount = m_MessageFiles.Count; int nClassCount = 0; // 先导出PBTpye, 已经内置了,不需要导出了 //szFileData.Append("enum PB_ZipType\r\n"); //szFileData.Append("{\r\n"); //szFileData.Append(" PB_Zip_Varint = 0,\r\n"); //szFileData.Append(" PB_Zip_Fixed = 1,\r\n"); //szFileData.Append(" PB_Zip_ZigZag = 2,\r\n"); //szFileData.Append("};\r\n"); //szPathName = szPath + "FCProtobufType.cs"; //System.IO.File.WriteAllText(szPathName, szFileData.ToString()); for (int i = 0; i < nFileCount; ++i) { pMsgFile = m_MessageFiles[i]; szFileData.Clear(); for (int j = 0; j < pMsgFile.m_Enums.Count; ++j) { pMsgFile.m_Enums[j].ExportFC(ref szFileData, 0); } nClassCount = pMsgFile.m_Messages.Count; for (int j = 0; j < nClassCount; ++j) { pMsg = pMsgFile.m_Messages[j]; pMsg.ExportFCScript(ref szFileData, 0, bExportReadWriteFunc); } szFileName = pMsgFile.m_szFileName; szFileName += ".cs"; szPathName = szPath; szPathName += szFileName; // 保存 //System.IO.File.WriteAllText(szPathName, szFileData.ToString()); SaveUTF8File(szPathName, szFileData.ToString()); } }
static int get_data(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); PBMessage obj = (PBMessage)o; byte[] ret = obj.data; LuaDLL.tolua_pushlstring(L, ret, ret.Length); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o, "attempt to index data on a nil value")); } }
static int get_playerId(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); PBMessage obj = (PBMessage)o; long ret = obj.playerId; LuaDLL.tolua_pushint64(L, ret); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o, "attempt to index playerId on a nil value")); } }
static int set_len(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); PBMessage obj = (PBMessage)o; int arg0 = (int)LuaDLL.luaL_checknumber(L, 2); obj.len = arg0; return(0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o, "attempt to index len on a nil value")); } }
static int set_data(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); PBMessage obj = (PBMessage)o; byte[] arg0 = ToLua.CheckByteBuffer(L, 2); obj.data = arg0; return(0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o, "attempt to index data on a nil value")); } }
static int set_playerId(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); PBMessage obj = (PBMessage)o; long arg0 = LuaDLL.tolua_checkint64(L, 2); obj.playerId = arg0; return(0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o, "attempt to index playerId on a nil value")); } }
static int get_cmd(IntPtr L) { object o = null; try { o = ToLua.ToObject(L, 1); PBMessage obj = (PBMessage)o; int ret = obj.cmd; LuaDLL.lua_pushinteger(L, ret); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e, o, "attempt to index cmd on a nil value")); } }
IEnumerator tick() { while (true) { PBMessage pbs = GameSocket.Instance.getMsg(); if (pbs != null) { callLuaNetFun(pbs); } yield return(new WaitForEndOfFrame()); frameIndex++; //SkillMgr.Instance.tick(frameIndex); if (luaFunc != null) { luaFunc.Call(frameIndex); } } }
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); }
protected PBMessage ParseMessage(List <pb_lex_words> aWords, ref int nStart, int nEnd) { PBMessage pChild = null; int nLeft = PBLex.FindFirstWords(aWords, pb_lex_words_type.lex_left_brace, pb_lex_words_type.lex_left_brace, nStart, nEnd); int nRight = PBLex.FindNextWords(aWords, pb_lex_words_type.lex_left_brace, pb_lex_words_type.lex_right_brace, nStart, nEnd); if (nLeft < nRight && nLeft > nStart) { // 找到了 message xxx{ } if (nStart + 2 == nLeft) { pb_lex_words name_class = aWords[nStart + 1]; pChild = new PBMessage(); pChild.m_szClassName = name_class.GetString(); pChild.Parse(aWords, nLeft + 1, nRight); } nStart = nRight; } return(pChild); }
static int _CreatePBMessage(IntPtr L) { try { int count = LuaDLL.lua_gettop(L); if (count == 0) { PBMessage obj = new PBMessage(); ToLua.PushObject(L, obj); return(1); } else { return(LuaDLL.luaL_throw(L, "invalid arguments to ctor method: PBMessage.New")); } } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
/// <summary> /// 拆完整的包 /// </summary> /// <param name="packet"></param> private void decoderPacket(byte[] packet) { ByteBuffer byteBuffer = ByteBuffer.Allocate(packet); short headFlag = byteBuffer.ReadShort(); short len = byteBuffer.ReadShort(); short code = byteBuffer.ReadShort(); long playerId = byteBuffer.ReadLong(); int encryId = byteBuffer.ReadInt(); len -= HEAD_LEN; byte[] data = new byte[len]; byteBuffer.ReadBytes(data, 0, len); //测试反序列化 //pb.PlayerSnapShootMsg msg = ProtobufSerializer.DeSerialize<pb.PlayerSnapShootMsg>(data); //UnityEngine.Debug.LogError("msg user name " + msg.username); PBMessage pbs = new PBMessage(); pbs.playerId = playerId; pbs.cmd = code; pbs.data = data; pbQueue.Enqueue(pbs); }
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)); }
/// <summary> /// call lua net fun /// </summary> /// <param name="pbs"></param> private void callLuaNetFun(PBMessage pbs) { netFun.Call(pbs); }
public void ParseFile(string szPathName) { if (!System.IO.File.Exists(szPathName)) { return; } string szFileData = System.IO.File.ReadAllText(szPathName); if (string.IsNullOrEmpty(szFileData)) { return; } string szFileName = DistillFileName(szPathName); m_szCurFileName = szFileName; pb_lex_words_contain contain = new pb_lex_words_contain(); pb_lex_string_ptr fileDataPtr = new pb_lex_string_ptr(szFileData); PBLex.AnylseProtoFile(contain, fileDataPtr, szFileName); PBMessageFile pMessageFile = new PBMessageFile(); pMessageFile.m_szFileName = szFileName; int nSize = contain.m_Words.Count; for (int i = 0; i < nSize; ++i) { pb_lex_words words = contain.m_Words[i]; switch (words.m_type) { case pb_lex_words_type.lex_enum: { PBEnumDesc pEnum = ParseEnum(contain.m_Words, ref i, nSize); if (pEnum != null) { pMessageFile.m_Enums.Add(pEnum); } } break; case pb_lex_words_type.lex_message: { PBMessage pMessage = ParseMessage(contain.m_Words, ref i, nSize); if (pMessage != null) { pMessageFile.m_Messages.Add(pMessage); } } break; default: break; } } if (pMessageFile.m_Messages.Count == 0) { return; } m_MessageFiles.Add(pMessageFile); }
protected void Parse(List <pb_lex_words> aWords, int nStart, int nEnd) { int nPB_ID = 0; int nFind = -1; for (int i = nStart; i < nEnd; ++i) { pb_lex_words words = aWords[i]; if (words.m_type == pb_lex_words_type.lex_enum) { PBEnumDesc pEnum = ParseEnum(aWords, ref i, nEnd); if (pEnum != null) { m_Enums.Add(pEnum); } } else if (words.m_type == pb_lex_words_type.lex_message) { PBMessage pChild = ParseMessage(aWords, ref i, nEnd); if (pChild != null) { m_ChildMsgDesc.Add(pChild); } } else if (words.m_type == pb_lex_words_type.lex_oneof) { ParseOneOf(aWords, ref i, nEnd); } else if (words.m_type == pb_lex_words_type.lex_repeated) { // repeated type xxx = id; // List<type> xxxx = id; if (i + 5 < aWords.Count && aWords[i + 3].m_type == pb_lex_words_type.lex_set && aWords[i + 5].m_type == pb_lex_words_type.lex_semicolon ) { pb_lex_words key_words = aWords[i + 1]; pb_lex_words name_words = aWords[i + 2]; pb_lex_words id_words = aWords[i + 4]; nPB_ID = PBLex.GetNumber(id_words); nFind = FindChild(nPB_ID); if (nFind != -1) { i += 5; continue; } MessageItem Item = new MessageItem(); Item.m_item_type = PBValueType.Value_Array; Item.m_key.m_type = PBLex.GetPBType(key_words); Item.m_key.m_szType = key_words.GetString(); Item.m_value = Item.m_key; Item.m_pOneOfDesc = m_pCurOneOf; Item.m_szName = name_words.GetString(); Item.m_ID = nPB_ID; m_Member.Add(Item); if (m_pCurOneOf != null) { m_pCurOneOf.m_Childs.Add(Item); } i += 5; } } else if (words.m_type == pb_lex_words_type.lex_map) { // map<key, value> xxx = id; if (i + 9 < aWords.Count && aWords[i + 1].m_type == pb_lex_words_type.lex_less && aWords[i + 3].m_type == pb_lex_words_type.lex_comma && aWords[i + 5].m_type == pb_lex_words_type.lex_greate && aWords[i + 7].m_type == pb_lex_words_type.lex_set && aWords[i + 9].m_type == pb_lex_words_type.lex_semicolon ) { pb_lex_words key_words = aWords[i + 2]; pb_lex_words value_words = aWords[i + 4]; pb_lex_words name_words = aWords[i + 6]; pb_lex_words id_words = aWords[i + 8]; nPB_ID = PBLex.GetNumber(id_words); nFind = FindChild(nPB_ID); if (nFind != -1) { i += 8; continue; } MessageItem Item = new MessageItem(); Item.m_item_type = PBValueType.Value_Map; Item.m_key.m_type = PBLex.GetPBType(key_words); Item.m_key.m_szType = key_words.GetString(); Item.m_value = Item.m_key; Item.m_value.m_type = PBLex.GetPBType(value_words); Item.m_value.m_szType = value_words.GetString(); Item.m_pOneOfDesc = m_pCurOneOf; Item.m_szName = name_words.GetString(); Item.m_ID = PBLex.GetNumber(id_words); m_Member.Add(Item); if (m_pCurOneOf != null) { m_pCurOneOf.m_Childs.Add(Item); } i += 8; } } else { // xxx type = xx; // xxx type = xx [default = xx] if (i + 5 < aWords.Count && aWords[i + 2].m_type == pb_lex_words_type.lex_set) { // 是成员定义 int nSemicolon = PBLex.FindFirstWords(aWords, pb_lex_words_type.lex_none, pb_lex_words_type.lex_semicolon, i, nEnd); if (nSemicolon != -1) { pb_lex_words key_words = aWords[i + 0]; pb_lex_words name_words = aWords[i + 1]; pb_lex_words id_words = aWords[i + 3]; nPB_ID = PBLex.GetNumber(id_words); nFind = FindChild(nPB_ID); if (nFind != -1) { i = nSemicolon; continue; } MessageItem Item = new MessageItem(); Item.m_item_type = PBValueType.Value_Base; Item.m_key.m_type = PBLex.GetPBType(key_words); Item.m_key.m_szType = key_words.GetString(); Item.m_value = Item.m_key; Item.m_pOneOfDesc = m_pCurOneOf; Item.m_szName = name_words.GetString(); Item.m_ID = PBLex.GetNumber(id_words); // 找默认值 xxx type = xx [default = xx] if (i + 8 < aWords.Count && aWords[i + 4].m_type == pb_lex_words_type.lex_bracket_1 && aWords[i + 5].m_type == pb_lex_words_type.lex_default && aWords[i + 6].m_type == pb_lex_words_type.lex_set && aWords[i + 8].m_type == pb_lex_words_type.lex_bracket_2 && aWords[i + 7].m_type == pb_lex_words_type.lex_value ) { if (Item.m_value.m_type != pb_type.pb_string) { Item.m_default = aWords[i + 7].GetString(); } } m_Member.Add(Item); if (m_pCurOneOf != null) { m_pCurOneOf.m_Childs.Add(Item); } i = nSemicolon; } } } } }
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); }