bta-proxy/tools/mkpacketfile.py

37 lines
1.1 KiB
Python
Raw Normal View History

2023-08-24 17:40:23 +03:00
from sys import argv
from pathlib import Path
from re import findall
from typing import Any
def main(argv: list[str]):
packetname, *fields = argv
packet_id = int(findall(r'\d+', packetname)[0])
with open(Path('bta_proxy/packets/') / f'{packetname.lower()}.py', 'w') as f:
f.write(f'from .base import Packet\n')
f.write(f'\n')
f.write(f'class {packetname}(Packet, packet_id={packet_id}):\n')
f.write(f' FIELDS = [\n')
for field in fields:
args: list[Any]
name, typename, *args = field.split(':')
for i, arg in enumerate(args):
try:
args[i] = int(arg)
except ValueError:
pass
if args:
args_str = repr((typename, *args))
f.write(f' ({name!r}, {args_str}),\n')
else:
f.write(f' ({name!r}, {typename!r}),\n')
f.write(f' ]\n')
with open('bta_proxy/packets/__init__.py', 'a') as f:
f.write(f'from .{packetname.lower()} import {packetname}\n')
if __name__ == '__main__':
main(argv[1:])