Remove snap message from generation

currently blocked by broken huffman decompression
This commit is contained in:
ChillerDragon 2023-05-15 11:24:42 +02:00
parent fb12b0a498
commit 81cee81e94
4 changed files with 102 additions and 3 deletions

View file

@ -1059,6 +1059,8 @@ class CodeGenerator():
for msg in game_messages:
self.generate_msg(msg, 'game')
for msg in system_messages:
if msg['name'] == ['snap']:
continue
self.generate_msg(msg, 'system')
for connless_msg in connless_messages:
self.generate_msg_connless(connless_msg)

View file

@ -0,0 +1,70 @@
from twnet_parser.packet import parse7, TwPacket
from twnet_parser.messages7.game.sv_game_info import MsgSvGameInfo
def test_parse_7_real_game_client_snap() -> None:
"""
The first packet the server sends that contains a snapshot
it also contains a sv_game_info and a sv_client_info message
This is the raw compressed data copied from a tcpdump
of a ruby client talking to a vanilla 0.7.5 server
"""
packet_header_raw = b'\x10\x04\x03\xcc\xde\xe6\x57'
packet_payload_raw = b'\x4a\x36\x4c\xed\xe1\x47\xde\x4c\x3d\x8c\x18\x4e\xb6\xd4\x16' \
b'\x43\x39\x6b\x3a\xc7\x51\x74\xc4\x09\x70\x5a\x89\x4e\x75\x99\x6b' \
b'\xd1\x5c\x50\x9e\x13\x6d\xa9\x2d\xe2\x44\x5b\xe4\xda\x5d\x3b\x94' \
b'\x4f\xa7\x50\xc2\x51\xae\x9d\x13\x39\x0a\x4e\x9c\x98\x4e\xa1\x84' \
b'\xa3\x5c\x3b\x27\x72\x14\x9c\x38\x31\x9d\x42\x09\x47\xb9\x76\x4e' \
b'\xe4\x28\x38\x71\xe2\xff\x2f\xf8\x98\xe2\x60\xe1\x5e\x0b\x15\xee' \
b'\xf1\xfc\x28\x9a\xea\x59\xc0\x2a\x30\x69\x47\xcf\x0e\xab\x00\xd6' \
b'\x06\x3d\x1b\x58\x09\xb0\x4c\x0b\xcf\xcd\x24\xbd\x49\x7d\x8f\x98' \
b'\x34\x65\x52\xdf\x63\xc2\xaa\x32\xa9\xef\xf9\xc3\x3a\xb0\xf2\x9e' \
b'\xdc\xa4\x63\xd2\xe9\x39\xb0\x8e\x49\x07\x3d\x7a\x93\xf4\x26\x9d' \
b'\x9e\x3b\x58\x05\x26\x1d\xf4\xd4\xb0\x0e\xac\xd3\x33\x05\xab\xc0' \
b'\x24\x3d\x7a\x5c\x93\xd0\x60\xd5\xc2\x66\xac\x0f\xd7\x02\x16\x42' \
b'\xc5\x0b\xc4\x95\x86\x85\xc0\x7a\xeb\xeb\xf1\xfe\xc9\x8a\x1b'
data = packet_header_raw + packet_payload_raw
# verified with libtw2's dissector
expected_decompressed = \
b'\x00\x04\x03\xcc\xde\xe6\x57\x40\x06\x09\x26\x00\x14\x00\x00\x01' \
b'\x41\x09\x0a\x24\x00\x01\x00\x72\x75\x62\x79\x20\x67\x61\x6d\x65' \
b'\x72\x00\x00\x40\x73\x70\x69\x6b\x79\x00\x64\x75\x6f\x64\x6f\x6e' \
b'\x6e\x79\x00\x00\x73\x74\x61\x6e\x64\x61\x72\x64\x00\x73\x74\x61' \
b'\x6e\x64\x61\x72\x64\x00\x73\x74\x61\x6e\x64\x61\x72\x64\x00\x00' \
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x17\x11\xba' \
b'\x03\xbb\x03\x9a\x85\x04\x8d\x02\x00\x11\x00\x04\x03\xb0\x1c\x90' \
b'\x05\x01\x04\x05\xb0\x1c\xb0\x06\x01\x04\x06\xb0\x1a\xb0\x09\x03' \
b'\x04\x07\x90\x0e\x90\x0a\x00\x04\x08\x90\x11\x90\x0a\x00\x04\x09' \
b'\xb0\x13\x90\x0a\x00\x04\x0b\xb0\x0d\xb0\x0c\x00\x04\x0c\x90\x0d' \
b'\x90\x0d\x00\x04\x0d\xb0\x0d\x90\x0d\x01\x04\x0e\x90\x0e\x90\x0d' \
b'\x00\x04\x0f\xb0\x1c\x90\x0d\x01\x04\x10\xb0\x0d\xb0\x0d\x00\x04' \
b'\x11\xb0\x1c\x90\x0e\x01\x04\x12\x90\x1b\xb0\x10\x02\x06\x00\x00' \
b'\x01\x00\x0a\x00\xb2\x03\xb0\x18\xb1\x04\x00\x80\x02\x00\x00\x00' \
b'\x40\x00\x00\xb0\x18\xb0\x04\x00\x00\x0a\x00\x0a\x01\x00\x00\x00' \
b'\x0b\x00\x08\x00\x00'
packet: TwPacket = parse7(data)
assert packet.version == '0.7'
assert packet.header.token == b'\xcc\xde\xe6\x57'
assert packet.header.num_chunks == 3
assert packet.header.ack == 4
assert packet.header.flags.control is False
assert packet.header.flags.compression is True
assert packet.header.flags.resend is False
assert packet.header.flags.connless is False
assert packet.payload_raw == packet_payload_raw
# TODO: uncomment when decompression is fixed
# assert packet.payload_decompressed == bytearray(expected_decompressed)
# assert len(packet.messages) == 3
# msg: MsgSvGameInfo = packet.messages[0]
# assert msg.system_message is False
# assert msg.message_name == 'sv_game_info'

View file

@ -1,10 +1,10 @@
# generated by scripts/generate_messages.py
from typing import Literal, Optional
from twnet_parser.pretty_print import PrettyPrint
from twnet_parser.packer import Unpacker
from twnet_parser.chunk_header import ChunkHeader
from twnet_parser.packer import pack_int
from typing import Literal, Optional
from twnet_parser.snapshot7 import Snapshot
class MsgSnap(PrettyPrint):
def __init__(
@ -31,6 +31,7 @@ class MsgSnap(PrettyPrint):
self.crc: int = crc
self.data_size: int = data_size if data_size else len(data)
self.data: bytes = data
self.snapshot = Snapshot()
# first byte of data
# has to be the first byte of the message payload
@ -44,6 +45,7 @@ class MsgSnap(PrettyPrint):
self.crc = unpacker.get_int()
self.data_size = unpacker.get_int()
self.data = unpacker.get_raw(self.data_size)
self.snapshot.unpack(self.data)
return True
def pack(self) -> bytes:
@ -53,4 +55,4 @@ class MsgSnap(PrettyPrint):
pack_int(self.part) + \
pack_int(self.crc) + \
pack_int(self.data_size) + \
self.data
self.snapshot.pack()

25
twnet_parser/snapshot7.py Normal file
View file

@ -0,0 +1,25 @@
from twnet_parser.packer import Unpacker
class Snapshot:
def __init__(
self,
num_removed_items: int = 0,
num_item_deltas: int = 0
) -> None:
self.num_removed_items: int = num_removed_items
self.num_item_deltas: int = num_item_deltas
# expects the int compressed
# data field of the snap message
# not the whole snap message with crc
# and the other fields
def unpack(self, data: bytes) -> bool:
unpacker = Unpacker(data)
self.num_removed_items = unpacker.get_int()
self.num_item_deltas = unpacker.get_int()
unpacker.get_int() # unused by tw 0.7 NumTempItems
return True
def pack(self) -> bytes:
return b''