bta-proxy/tools/mkpacketfile.py

42 lines
1.3 KiB
Python
Raw Permalink 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')
2023-08-26 01:12:19 +03:00
slots = tuple([
arg.split(':')[0].split('?')[0]
for arg in fields
])
f.write(f' __slots__ = {slots!r}\n')
2023-08-24 17:40:23 +03:00
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:])