More snap preperation in generate script
This commit is contained in:
parent
08f71f60a2
commit
6fef3f1abb
|
@ -720,6 +720,21 @@ class CodeGenerator():
|
|||
out_file.write(',\n'.join(args) + '\n')
|
||||
out_file.write(' ) -> None:\n')
|
||||
|
||||
def generate_snap_obj7(self, obj: SnapObjJson) -> None:
|
||||
name_snake = name_to_snake(obj['name'])
|
||||
name_camel = name_to_camel(obj['name'])
|
||||
dirname = os.path.dirname(__file__)
|
||||
file_path= os.path.join(
|
||||
dirname,
|
||||
f'../twnet_parser/snap/07/',
|
||||
f'{name_snake}.py')
|
||||
with open(file_path, 'w') as out_file:
|
||||
print(f"Generating {file_path} ...")
|
||||
out_file.write('# generated by scripts/generate_messages.py\n')
|
||||
out_file.write('\n')
|
||||
out_file.write('from twnet_parser.pretty_print import PrettyPrint\n')
|
||||
out_file.write(f'class Obj{name_camel}(PrettyPrint):\n')
|
||||
|
||||
def generate_msg_connless(
|
||||
self,
|
||||
msg: NetConnlessJson
|
||||
|
@ -782,6 +797,73 @@ class CodeGenerator():
|
|||
out_file.write(' def pack(self) -> bytes:\n')
|
||||
out_file.write(gen_pack_return_connless7(msg))
|
||||
|
||||
def generate_field_assignments_in_initialize(
|
||||
self,
|
||||
msg: NetMessageJson,
|
||||
out_file
|
||||
) -> None:
|
||||
for member in msg['members']:
|
||||
# {
|
||||
# 'name': ['message'],
|
||||
# 'type': {
|
||||
# 'kind': 'string',
|
||||
# 'disallow_cc': False
|
||||
# }
|
||||
# }
|
||||
ftype = 'int'
|
||||
if member['type']['kind'] == 'string':
|
||||
ftype = 'str'
|
||||
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':
|
||||
out_file.write(" " \
|
||||
"self.data_size: int =" \
|
||||
" data_size if data_size else len(data)\n")
|
||||
else:
|
||||
raise ValueError(f"Error: unknown data size {member['type']}")
|
||||
# {"name": ["mode"], "type": {"kind": "enum", "enum": ["chat"]}},
|
||||
elif member['type']['kind'] == 'enum':
|
||||
ftype = 'int'
|
||||
elif member['type']['kind'] in ('int32', 'tick'):
|
||||
ftype = 'int'
|
||||
elif member['type']['kind'] == 'boolean':
|
||||
ftype = 'bool'
|
||||
elif member['type']['kind'] == 'tune_param':
|
||||
ftype = 'float'
|
||||
elif member['type']['kind'] == 'snapshot_object':
|
||||
# TODO: think about snapshot_object
|
||||
ftype = 'int'
|
||||
elif member['type']['kind'] == 'array':
|
||||
# Array type annotations are so annoyingly long
|
||||
# also there is a planned refactor
|
||||
# https://gitlab.com/teeworlds-network/twnet_parser/-/issues/4
|
||||
# so inherit type from constructor arguments
|
||||
ftype = ''
|
||||
elif member['type']['kind'] == 'flags': # TODO: think about flags
|
||||
ftype = 'int'
|
||||
elif member['type']['kind'] == 'optional':
|
||||
if member['type']['inner']['kind'] == 'string':
|
||||
ftype = 'str'
|
||||
elif member['type']['inner']['kind'] in ('int32', 'tick'):
|
||||
ftype = 'int'
|
||||
else:
|
||||
raise \
|
||||
ValueError( \
|
||||
f"Error: unknown optional type {member['type']}")
|
||||
else:
|
||||
raise ValueError(f"Error: unknown type {member['type']}")
|
||||
name = name_to_snake(member["name"])
|
||||
if ftype != '':
|
||||
ftype = f': {ftype}'
|
||||
if member['type']['kind'] == 'enum':
|
||||
out_file.write(f" self.{name}{ftype} = {name}\n")
|
||||
else:
|
||||
out_file.write(f" self.{name}{ftype} = {name}\n")
|
||||
|
||||
def generate_msg(
|
||||
self,
|
||||
msg: NetMessageJson,
|
||||
|
@ -816,67 +898,7 @@ class CodeGenerator():
|
|||
out_file.write(f" self.message_id: int = {msg['id']}\n")
|
||||
out_file.write(" self.header: ChunkHeader = chunk_header\n")
|
||||
out_file.write('\n')
|
||||
for member in msg['members']:
|
||||
# {
|
||||
# 'name': ['message'],
|
||||
# 'type': {
|
||||
# 'kind': 'string',
|
||||
# 'disallow_cc': False
|
||||
# }
|
||||
# }
|
||||
ftype = 'int'
|
||||
if member['type']['kind'] == 'string':
|
||||
ftype = 'str'
|
||||
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':
|
||||
out_file.write(" " \
|
||||
"self.data_size: int =" \
|
||||
" data_size if data_size else len(data)\n")
|
||||
else:
|
||||
raise ValueError(f"Error: unknown data size {member['type']}")
|
||||
# {"name": ["mode"], "type": {"kind": "enum", "enum": ["chat"]}},
|
||||
elif member['type']['kind'] == 'enum':
|
||||
ftype = 'int'
|
||||
elif member['type']['kind'] in ('int32', 'tick'):
|
||||
ftype = 'int'
|
||||
elif member['type']['kind'] == 'boolean':
|
||||
ftype = 'bool'
|
||||
elif member['type']['kind'] == 'tune_param':
|
||||
ftype = 'float'
|
||||
elif member['type']['kind'] == 'snapshot_object':
|
||||
# TODO: think about snapshot_object
|
||||
ftype = 'int'
|
||||
elif member['type']['kind'] == 'array':
|
||||
# Array type annotations are so annoyingly long
|
||||
# also there is a planned refactor
|
||||
# https://gitlab.com/teeworlds-network/twnet_parser/-/issues/4
|
||||
# so inherit type from constructor arguments
|
||||
ftype = ''
|
||||
elif member['type']['kind'] == 'flags': # TODO: think about flags
|
||||
ftype = 'int'
|
||||
elif member['type']['kind'] == 'optional':
|
||||
if member['type']['inner']['kind'] == 'string':
|
||||
ftype = 'str'
|
||||
elif member['type']['inner']['kind'] in ('int32', 'tick'):
|
||||
ftype = 'int'
|
||||
else:
|
||||
raise \
|
||||
ValueError( \
|
||||
f"Error: unknown optional type {member['type']}")
|
||||
else:
|
||||
raise ValueError(f"Error: unknown type {member['type']}")
|
||||
name = name_to_snake(member["name"])
|
||||
if ftype != '':
|
||||
ftype = f': {ftype}'
|
||||
if member['type']['kind'] == 'enum':
|
||||
out_file.write(f" self.{name}{ftype} = {name}\n")
|
||||
else:
|
||||
out_file.write(f" self.{name}{ftype} = {name}\n")
|
||||
self.generate_field_assignments_in_initialize(msg, out_file)
|
||||
out_file.write('\n')
|
||||
out_file.write(' # first byte of data\n')
|
||||
out_file.write(' # has to be the first byte of the message payload\n')
|
||||
|
@ -944,6 +966,7 @@ class CodeGenerator():
|
|||
game_messages: list[NetMessageJson] = spec_data['game_messages']
|
||||
system_messages: list[NetMessageJson] = spec_data['system_messages']
|
||||
connless_messages: list[NetConnlessJson] = spec_data['connless_messages']
|
||||
snapshot_objects: list[SnapObjJson] = spec_data['snapshot_objects']
|
||||
self.gen_enum_file7()
|
||||
gen_match_file7('game', game_messages)
|
||||
gen_match_file7('system', system_messages)
|
||||
|
@ -954,6 +977,8 @@ class CodeGenerator():
|
|||
self.generate_msg(msg, 'system')
|
||||
for connless_msg in connless_messages:
|
||||
self.generate_msg_connless(connless_msg)
|
||||
# for obj in snapshot_objects:
|
||||
# self.generate_snap_obj7(obj)
|
||||
|
||||
def main() -> None:
|
||||
dirname = os.path.dirname(__file__)
|
||||
|
|
Loading…
Reference in a new issue