public void ParseChatMessage(Packet _packet) { Write("data: " + Tools.ByteArrayToString(_packet.data)); Write("hex: " + hex); string[] split = hex.Split(new string[] { "00" }, StringSplitOptions.None); Write("split.Length[" + split.Length + "] data.Length[" + _packet.data.Length + "] _length[" + _packet.length + "]"); //foreach (string s in split) //{ // Write("split: " + s); //} // Get Name string name = split[split.Length - 2]; if (name.Length - 2 < 0) name = name.Substring(0, name.Length); else name = name.Substring(0, name.Length - 2); // Get Message string message = split[split.Length - 1]; if (message.Length - 2 < 0) message = message.Substring(0, message.Length); else message = message.Substring(0, message.Length - 2); // Translate from Hex to String this.name = Tools.HexStringToString(name); this.message = Tools.HexStringToString(message); Write("Message[" + GetFullMessage() + "]"); //Upload(GetFullMessage()); }
/// Parses a player name. int ParseName(int index, Packet _packet, Player player) { string s = ""; int i = 0; for (i = index; i < _packet.data.Length; i++) { if (_packet.data[i] == 0) { if (_packet.data[i + 1] == 0) // player has no clan { i += 8; // advance index by 8 bytes break; } } else if (_packet.data[i] == 2 || _packet.data[i] == 3) // clan tag found (tag length is revealed 2 bytes before the tag (either a 2 or 3)) { if (_packet.data[i + 1] == 0) { i = ParseClan(i, _packet, player); // parse clan name and return index for the next player break; } } else // continue building name { char c = Convert.ToChar(_packet.data[i]); s += c; } } player.SetName(s); return i; }
public GameInterface.PacketType AnalyzePacketPattern(Packet _packet) { //if (_length < 1400) // return GameInterface.PacketType.Ignore; //string hex = BitConverter.ToString(_packet.data).Replace("-", string.Empty); //Write("----- NEW ANALYZE -----"); //Tools.WriteSeparator(); //Write("byte (first 32): " + Tools.ByteArrayToString(_packet.data).Substring(0, 32)); //Write("hex (first 32): " + _packet.hex.Substring(0, 32)); //Write("byte: " + Tools.ByteArrayToString(_packet.data)); //Write("hex: " + _packet.hex); //Tools.WriteSeparator(); if (_packet.hex.Substring(_packet.hex.Length - 2, 2) != "02") return GameInterface.PacketType.Ignore; bool isGlobalJoinPacket = IsPacketGlobalJoin(_packet); bool isGlobalChatPacket = IsPacketGlobalChat(_packet); //Tools.WriteSeparator(); //Write("- Finished"); if (isGlobalJoinPacket) return GameInterface.PacketType.JoinedGlobal; else if (isGlobalChatPacket) return GameInterface.PacketType.ChatMessage; else return GameInterface.PacketType.Ignore; }
Packet CreatePacket(byte[] _data, int _length) { Packet packet = new Packet(); packet.data = _data; Tools.TrimTrailingBytes(ref packet.data, 0); packet.hex = BitConverter.ToString(packet.data).Replace("-", String.Empty); packet.length = _length; packet.packetType = packetParser.GetPacketType(packet); packet.Log(); return packet; }
/// Parses a clan for a player int ParseClan(int index, Packet _packet, Player player) { string s = ""; int i = 0; int len = Convert.ToInt32(_packet.data[index]); // clan tag len for (i = index + 2; i < index + 2 + len; i++) { char c = Convert.ToChar(_packet.data[i]); s += c; } player.SetClan(s); i += 6; // advance index by 6 bytes return i; }
public void ParseGlobalChannelJoin(Packet _packet, ref List<Player> playerList) { int index = 17; // Start at the 17th byte while (index < _packet.data.Length) { Player p = new Player(); index = ParseName(index, _packet, p); playerList.Add(p); // Exit loop if three consecutive zeros appear if (_packet.data.Length >= index + 2) { if (_packet.data[index] == 0 && _packet.data[index + 1] == 0 && _packet.data[index + 2] == 0) break; } } }
public GameInterface.PacketType GetPacketType(Packet _packet) { return packetAnalyzer.AnalyzePacketPattern(_packet); }
bool IsPacketGlobalJoin(Packet _packet) { int highestIndex = 99; int checks = 0; int matches = 0; if (_packet.hex.Length < highestIndex) return false; try { CheckPosition(_packet.hex, 0, "" + 0, false, ref checks, ref matches); CheckPosition(_packet.hex, 1, "" + 0, false, ref checks, ref matches); CheckPosition(_packet.hex, 12, "" + 0, true, ref checks, ref matches); CheckPosition(_packet.hex, 13, "" + 0, true, ref checks, ref matches); CheckPosition(_packet.hex, 14, "" + 0, true, ref checks, ref matches); CheckPosition(_packet.hex, 15, "" + 0, true, ref checks, ref matches); CheckPosition(_packet.hex, 17, "" + 0, false, ref checks, ref matches); CheckPosition(_packet.hex, 25, "" + 0, false, ref checks, ref matches); CheckPosition(_packet.hex, 31, "" + 0, false, ref checks, ref matches); } catch (Exception ex) { Write("Exception " + ex); return false; } if ((float)matches / (float)checks >= accuracy) Write("GlobalJoined Scored: " + (float)((float)matches / (float)checks) + ". (Length=" + _packet.length + ") (Checks=" + checks + ") (Matches=" + matches + ")"); return ((float)matches / (float)checks >= accuracy); }
void HandlePacket(Packet packet) { switch (packet.packetType) { default: Write("Unknown packet."); break; case PacketType.Ignore: //Write("Ignored packet."); break; case PacketType.JoinedGlobal: playerList.Clear(); packetParser.ParseGlobalChannelJoin(packet, ref playerList); Write("Global has " + playerList.Count + " players."); break; case PacketType.ChatMessage: ChatMessage cm = new ChatMessage(); cm.ParseChatMessage(packet); break; } }