diff options
| author | mat <github@matdoes.dev> | 2022-05-07 17:59:03 -0500 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-05-07 17:59:03 -0500 |
| commit | aa3ba64aa45abcafb6aa9c1981730d3270a3b0a5 (patch) | |
| tree | 39a8c31afb3fff311c86b12cf8a38d9424273986 /data-code-generator | |
| parent | 7b61d41f867090a120ca2493d4a499e3d08b018f (diff) | |
| download | azalea-drasl-aa3ba64aa45abcafb6aa9c1981730d3270a3b0a5.tar.xz | |
actually generate a packet!
Diffstat (limited to 'data-code-generator')
| -rw-r--r-- | data-code-generator/main.py | 7 | ||||
| -rw-r--r-- | data-code-generator/packetcodegen.py | 42 |
2 files changed, 43 insertions, 6 deletions
diff --git a/data-code-generator/main.py b/data-code-generator/main.py index b79571ae..e1668369 100644 --- a/data-code-generator/main.py +++ b/data-code-generator/main.py @@ -11,7 +11,7 @@ print( f'\033[92mFinding Minecraft version...\033[m') version_manifest_data = requests.get( 'https://launchermeta.mojang.com/mc/game/version_manifest.json').json() -minecraft_version = version_manifest_data['latest']['snapshot'] +minecraft_version = version_manifest_data['latest']['release'] print( f'\033[92mUsing \033[1m{minecraft_version}..\033[m') package_url = next( @@ -22,13 +22,14 @@ client_jar_url = package_data['downloads']['client']['url'] if not SKIP_BURGER: print('\033[92mDownloading Burger...\033[m') r = os.system('git clone https://github.com/pokechu22/Burger') - os.system('git pull') + os.system('cd Burger && git pull') print('\033[92mDownloading client jar...\033[m') with open('client.jar', 'wb') as f: f.write(requests.get(client_jar_url).content) print(f'\033[92mExtracting data with Burger...\033[m') - os.system('cd Burger && python munch.py ../client.jar --output ../burger.json') + os.system( + 'cd Burger && python munch.py ../client.jar --output ../burger.json') client_mappings_url = package_data['downloads']['client_mappings']['url'] mappings = Mappings.parse(requests.get(client_mappings_url).text) diff --git a/data-code-generator/packetcodegen.py b/data-code-generator/packetcodegen.py index ccbb3845..4c59b72b 100644 --- a/data-code-generator/packetcodegen.py +++ b/data-code-generator/packetcodegen.py @@ -66,9 +66,7 @@ def burger_type_to_rust_type(burger_type): def write_packet_file(state, packet_name_snake_case, code): - path = os.path.join( - '..', f'azalea-protocol/src/packets/{state}/{packet_name_snake_case}.rs') - with open(path, 'w') as f: + with open(f'../azalea-protocol/src/packets/{state}/{packet_name_snake_case}.rs', 'w') as f: f.write(code) @@ -128,3 +126,41 @@ def generate(burger_packets, mappings: Mappings): write_packet_file(state, to_snake_case(class_name), '\n'.join(generated_packet_code)) print() + + mod_rs_dir = f'../azalea-protocol/src/packets/{state}/mod.rs' + with open(mod_rs_dir, 'r') as f: + mod_rs = f.read().splitlines() + + pub_mod_line = f'pub mod {to_snake_case(class_name)};' + if pub_mod_line not in mod_rs: + mod_rs.insert(0, pub_mod_line) + packet_mod_rs_line = f' {hex(packet["id"])}: {to_snake_case(class_name)}::{to_camel_case(class_name)},' + + in_serverbound = False + in_clientbound = False + for i, line in enumerate(mod_rs): + if line.strip() == 'Serverbound => {': + in_serverbound = True + continue + elif line.strip() == 'Clientbound => {': + in_clientbound = True + continue + elif line.strip() in ('}', '},'): + if (in_serverbound and direction == 'serverbound') or (in_clientbound and direction == 'clientbound'): + mod_rs.insert(i, packet_mod_rs_line) + break + in_serverbound = in_clientbound = False + continue + + if line.strip() == '' or line.strip().startswith('//') or (not in_serverbound and direction == 'serverbound') or (not in_clientbound and direction == 'clientbound'): + continue + + line_packet_id_hex = line.strip().split(':')[0] + assert line_packet_id_hex.startswith('0x') + line_packet_id = int(line_packet_id_hex[2:], 16) + if line_packet_id > packet['id']: + mod_rs.insert(i, packet_mod_rs_line) + break + + with open(mod_rs_dir, 'w') as f: + f.write('\n'.join(mod_rs)) |
