feat: support uuid type in code generation

This commit is contained in:
ChillerDragon 2024-04-19 10:37:32 +08:00
parent 4c434752dc
commit 87fe5f3990

View file

@ -223,6 +223,8 @@ def gen_unpack_members(msg: NetMessageJson) -> str:
continue
elif member['type']['kind'] == 'flags': # TODO: think about flags
unpacker = 'int() # TODO: this is a flag'
elif member['type']['kind'] == 'uuid':
unpacker = 'raw(16)'
elif member['type']['kind'] == 'optional':
if member['type']['inner']['kind'] == 'string':
if member['type']['inner']['disallow_cc']:
@ -285,6 +287,8 @@ def pack_field(member: NetMessageMemberJson) -> str:
return f"b''.join([pack_{packer}(x) for x in {field}])"
elif member['type']['kind'] == 'flags': # TODO: think about flags
packer = 'int'
elif member['type']['kind'] == 'uuid':
return f'self.{name}'
elif member['type']['kind'] == 'optional':
packer = 'int'
if member['type']['inner']['kind'] == 'string':
@ -492,6 +496,8 @@ class CodeGenerator():
packer_deps.append('SANITIZE_CC')
elif member['type']['inner']['kind'] in ('int32', 'tick'):
packer_deps.append('pack_int')
elif member['type']['kind'] == 'uuid':
pass
else:
raise ValueError(f"Error: unknown type {member['type']}")
res: str = ''
@ -745,6 +751,9 @@ def match_connless{self.protocol_version}(msg_id: bytes, data: bytes) -> Connles
elif member['type']['kind'] == 'flags': # TODO: think about flags
ftype = 'int'
default = '0'
elif member['type']['kind'] == 'uuid':
ftype = 'bytes'
default = "b'\xbc\xb4\x3b\xf5\x42\x7c\x36\xd8\xb5\xb8\x79\x75\xc8\xc0\x6a\xa1'"
elif member['type']['kind'] == 'optional':
if member['type']['inner']['kind'] == 'string':
ftype = 'str'
@ -971,6 +980,8 @@ def match_connless{self.protocol_version}(msg_id: bytes, data: bytes) -> Connles
ftype = ''
elif member['type']['kind'] == 'flags': # TODO: think about flags
ftype = 'int'
elif member['type']['kind'] == 'uuid':
ftype = 'bytes'
elif member['type']['kind'] == 'optional':
if member['type']['inner']['kind'] == 'string':
ftype = 'Optional[str]'
@ -1132,6 +1143,10 @@ def main() -> None:
'../../libtw2/gamenet/generate/spec/teeworlds-0.6.json',
'6'
)
# SpecInfo(
# '../../libtw2/gamenet/generate/spec/ddnet-17.2.1.json',
# '_ddnet_17_2_1'
# )
]
for spec_info in spec_infos:
spec_file = os.path.join(