Expect the sha256 field to be 32 bytes

This commit is contained in:
ChillerDragon 2023-04-16 08:15:55 +02:00
parent e3411f7f98
commit 8b0cce28be
2 changed files with 18 additions and 13 deletions

View file

@ -174,10 +174,12 @@ def generate_msg(msg: NetMessageJson, game: Literal['game', 'system']) -> None:
if member['type']['kind'] == 'string':
ftype = 'str'
default = "'default'"
elif member['type']['kind'] in \
('raw', 'sha256', 'rest'): # TODO: rest sha256 and raw
elif member['type']['kind'] == 'rest':
ftype = 'bytes'
default = "b'\\x00'"
elif member['type']['kind'] == 'sha256':
ftype = 'Annotated[bytes, 32]'
default = "bytes(32)"
elif member['type']['kind'] == 'data':
ftype = 'bytes'
default = "b'\\x00'"
@ -257,9 +259,10 @@ def generate_msg(msg: NetMessageJson, game: Literal['game', 'system']) -> None:
ftype = 'int'
if member['type']['kind'] == 'string':
ftype = 'str'
elif member['type']['kind'] in \
('raw', 'sha256', 'rest'): # TODO: sha256 and raw
elif member['type']['kind'] == 'rest':
ftype = 'bytes'
elif member['type']['kind'] == 'sha256':
ftype = 'Annotated[bytes, 32]'
elif member['type']['kind'] == 'data':
ftype = 'bytes'
if member['type']['size'] == 'specified_before':
@ -323,9 +326,10 @@ def gen_unpack_members(msg: NetMessageJson) -> str:
unpacker = 'str(SANITIZE_CC)'
else:
unpacker = 'str()'
elif member['type']['kind'] in \
('raw', 'sha256', 'rest'): # TODO: do we need to fix size for sha256?
elif member['type']['kind'] == 'rest':
unpacker = 'raw()'
elif member['type']['kind'] == 'sha256':
unpacker = 'raw(32)'
elif member['type']['kind'] == 'data':
if member['type']['size'] == 'specified_before':
res += ' self.data_size = unpacker.get_int()\n'
@ -395,9 +399,10 @@ def get_dependencies(msg: NetMessageJson) -> str:
packer_deps.append('pack_str')
if member['type']['disallow_cc']:
packer_deps.append('SANITIZE_CC')
elif member['type']['kind'] in \
('raw', 'sha256', 'rest'):
elif member['type']['kind'] == 'rest':
pass
elif member['type']['kind'] == 'sha256':
typing_deps.append('Annotated')
elif member['type']['kind'] == 'data':
if member['type']['size'] == 'specified_before':
typing_deps.append('Optional')
@ -457,8 +462,7 @@ def pack_field(member: NetMessageMemberJson) -> str:
packer = 'int'
if member['type']['kind'] == 'string':
packer = 'str'
elif member['type']['kind'] in \
('raw', 'sha256', 'rest'): # TODO: raw sha256 rest
elif member['type']['kind'] in ('sha256', 'rest'):
return f'self.{name}'
elif member['type']['kind'] == 'data':
if member['type']['size'] == 'specified_before':

View file

@ -4,6 +4,7 @@ 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, pack_str
from typing import Annotated
class MsgMapChange(PrettyPrint):
def __init__(
@ -13,7 +14,7 @@ class MsgMapChange(PrettyPrint):
size: int = 0,
num_response_chunks_per_request: int = 0,
chunk_size: int = 0,
sha256: bytes = b'\x00'
sha256: Annotated[bytes, 32] = bytes(32)
) -> None:
self.message_name = 'map_change'
self.system_message = True
@ -24,7 +25,7 @@ class MsgMapChange(PrettyPrint):
self.size: int = size
self.num_response_chunks_per_request: int = num_response_chunks_per_request
self.chunk_size: int = chunk_size
self.sha256: bytes = sha256
self.sha256: Annotated[bytes, 32] = sha256
# first byte of data
# has to be the first byte of the message payload
@ -36,7 +37,7 @@ class MsgMapChange(PrettyPrint):
self.size = unpacker.get_int()
self.num_response_chunks_per_request = unpacker.get_int()
self.chunk_size = unpacker.get_int()
self.sha256 = unpacker.get_raw()
self.sha256 = unpacker.get_raw(32)
return True
def pack(self) -> bytes: