aboutsummaryrefslogtreecommitdiff
path: root/codegen
diff options
context:
space:
mode:
Diffstat (limited to 'codegen')
-rw-r--r--codegen/lib/code/packet.py32
-rw-r--r--codegen/lib/code/utils.py3
-rw-r--r--codegen/lib/code/version.py6
-rw-r--r--codegen/lib/download.py31
-rw-r--r--codegen/lib/extract.py14
-rw-r--r--codegen/migrate.py43
6 files changed, 109 insertions, 20 deletions
diff --git a/codegen/lib/code/packet.py b/codegen/lib/code/packet.py
index de8254e7..ffa7841c 100644
--- a/codegen/lib/code/packet.py
+++ b/codegen/lib/code/packet.py
@@ -110,7 +110,8 @@ def set_packets(packet_ids: list[int], packet_class_names: list[str], direction:
packet_ids, packet_class_names = [list(x) for x in zip(
*sorted(zip(packet_ids, packet_class_names), key=lambda pair: pair[0]))] # type: ignore
- mod_rs_dir = f'../azalea-protocol/src/packets/{state}/mod.rs'
+ mod_rs_dir = get_dir_location(
+ f'../azalea-protocol/src/packets/{state}/mod.rs')
with open(mod_rs_dir, 'r') as f:
mod_rs = f.read().splitlines()
new_mod_rs = []
@@ -140,6 +141,7 @@ def set_packets(packet_ids: list[int], packet_class_names: list[str], direction:
new_mod_rs.append(
make_packet_mod_rs_line(packet_id, packet_class_name)
)
+ required_modules.append(packet_class_name)
else:
ignore_lines = False
continue
@@ -164,7 +166,8 @@ def set_packets(packet_ids: list[int], packet_class_names: list[str], direction:
def get_packets(direction: str, state: str):
- mod_rs_dir = f'../azalea-protocol/src/packets/{state}/mod.rs'
+ mod_rs_dir = get_dir_location(
+ f'../azalea-protocol/src/packets/{state}/mod.rs')
with open(mod_rs_dir, 'r') as f:
mod_rs = f.read().splitlines()
@@ -268,3 +271,28 @@ def remove_packet_ids(removing_packet_ids: list[int], direction: str, state: str
new_packet_class_names.append(packet_class_name)
set_packets(new_packet_ids, new_packet_class_names, direction, state)
+
+
+def are_packet_instructions_identical(old_packet, new_packet):
+ old_packet = old_packet or []
+ new_packet = new_packet or []
+
+ if len(old_packet) != len(new_packet):
+ return False
+
+ for old_field, new_field in zip(old_packet, new_packet):
+ if old_field['operation'] != new_field['operation']:
+ return False
+ if new_field['operation'] == 'write':
+ if burger_type_to_rust_type(old_field.get('type')) != burger_type_to_rust_type(new_field.get('type')):
+ return False
+ else:
+ # comparing is too complicated here since it's possible the type has variables
+ # so we just don't
+ pass
+
+ if 'instructions' in old_field and 'instructions' in new_field:
+ if not are_packet_instructions_identical(old_field['instructions'], new_field['instructions']):
+ return False
+
+ return True
diff --git a/codegen/lib/code/utils.py b/codegen/lib/code/utils.py
index ecfff4fb..0c22d7ba 100644
--- a/codegen/lib/code/utils.py
+++ b/codegen/lib/code/utils.py
@@ -62,8 +62,7 @@ def burger_type_to_rust_type(burger_type):
burger_type[:-2])
field_type_rs = f'Vec<{field_type_rs}>'
else:
- print('Unknown field type:', burger_type)
- exit()
+ raise Exception(f'Unknown field type: {burger_type}')
return field_type_rs, is_var, uses
diff --git a/codegen/lib/code/version.py b/codegen/lib/code/version.py
index 511d30d1..13d9472d 100644
--- a/codegen/lib/code/version.py
+++ b/codegen/lib/code/version.py
@@ -36,7 +36,7 @@ def set_version_id(version_id: str) -> None:
def get_protocol_version() -> str:
# azalea-protocol/src/packets/mod.rs
# pub const PROTOCOL_VERSION: u32 = 758;
- with open('../azalea-protocol/src/packets/mod.rs', 'r') as f:
+ with open(get_dir_location('../azalea-protocol/src/packets/mod.rs'), 'r') as f:
mod_rs = f.read().splitlines()
for line in mod_rs:
if line.strip().startswith('pub const PROTOCOL_VERSION'):
@@ -46,7 +46,7 @@ def get_protocol_version() -> str:
def set_protocol_version(protocol_version: str) -> None:
- with open('../azalea-protocol/src/packets/mod.rs', 'r') as f:
+ with open(get_dir_location('../azalea-protocol/src/packets/mod.rs'), 'r') as f:
mod_rs = f.read().splitlines()
for i, line in enumerate(mod_rs):
if line.strip().startswith('pub const PROTOCOL_VERSION'):
@@ -56,5 +56,5 @@ def set_protocol_version(protocol_version: str) -> None:
raise Exception(
'Could not find protocol version in azalea-protocol/src/packets/mod.rs')
- with open('../azalea-protocol/src/packets/mod.rs', 'w') as f:
+ with open(get_dir_location('../azalea-protocol/src/packets/mod.rs'), 'w') as f:
f.write('\n'.join(mod_rs))
diff --git a/codegen/lib/download.py b/codegen/lib/download.py
index e9712f8d..d9e2e63f 100644
--- a/codegen/lib/download.py
+++ b/codegen/lib/download.py
@@ -1,4 +1,5 @@
from lib.utils import get_dir_location
+import xml.etree.ElementTree as ET
from .mappings import Mappings
import requests
import json
@@ -118,11 +119,39 @@ def get_yarn_data(version_id: str):
return version
+def get_fabric_api_versions():
+ # https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api/maven-metadata.xml
+ if not os.path.exists(get_dir_location('downloads/fabric_api_versions.json')):
+ print('\033[92mDownloading Fabric API versions...\033[m')
+ fabric_api_versions_xml_text = requests.get(
+ 'https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api/maven-metadata.xml').text
+ # parse xml
+ fabric_api_versions_data_xml = ET.fromstring(
+ fabric_api_versions_xml_text)
+ fabric_api_versions = []
+
+ versioning_el = fabric_api_versions_data_xml.find('versioning')
+ assert versioning_el
+ versions_el = versioning_el.find('versions')
+ assert versions_el
+
+ for version_el in versions_el.findall('version'):
+ fabric_api_versions.append(version_el.text)
+
+ with open(get_dir_location('downloads/fabric_api_versions.json'), 'w') as f:
+ f.write(json.dumps(fabric_api_versions))
+ else:
+ with open(get_dir_location('downloads/fabric_api_versions.json'), 'r') as f:
+ fabric_api_versions = json.loads(f.read())
+ return fabric_api_versions
+
+
def clear_version_cache():
print('\033[92mClearing version cache...\033[m')
files = [
'version_manifest.json',
- 'yarn_versions.json'
+ 'yarn_versions.json',
+ 'fabric_api_versions.json'
]
for file in files:
if os.path.exists(get_dir_location(f'downloads/{file}')):
diff --git a/codegen/lib/extract.py b/codegen/lib/extract.py
index 893b8d5c..4c2d2399 100644
--- a/codegen/lib/extract.py
+++ b/codegen/lib/extract.py
@@ -1,6 +1,6 @@
# Extracting data from the Minecraft jars
-from lib.download import get_server_jar, get_burger, get_client_jar, get_generator_mod, get_yarn_data
+from lib.download import get_server_jar, get_burger, get_client_jar, get_generator_mod, get_yarn_data, get_fabric_api_versions
from lib.utils import get_dir_location
import json
import os
@@ -61,7 +61,9 @@ def get_generator_mod_data(version_id: str, category: str):
# looks like 1.19+build.1
yarn_version = yarn_data['version']
- # the mod has the minecraft version hard-coded by default, so we just change the gradle.properties
+ fabric_api_version = get_fabric_api_versions()[-1]
+
+ # the mod has the minecraft version hard-coded by default, so we just change the gradle.properties and fabric.mod.json
with open(get_dir_location(f'{generator_mod_dir}/gradle.properties'), 'r') as f:
lines = f.readlines()
with open(get_dir_location(f'{generator_mod_dir}/gradle.properties'), 'w') as f:
@@ -70,7 +72,15 @@ def get_generator_mod_data(version_id: str, category: str):
line = f'minecraft_version={version_id}\n'
if line.startswith('yarn_mappings='):
line = f'yarn_mappings={yarn_version}\n'
+ if line.startswith('fabric_version='):
+ line = f'fabric_version={fabric_api_version}\n'
f.write(line)
+ # edit the fabric.mod.json to support this version
+ with open(get_dir_location(f'{generator_mod_dir}/src/main/resources/fabric.mod.json'), 'r') as f:
+ fabric_mod_json = json.load(f)
+ fabric_mod_json['depends']['minecraft'] = '*'
+ with open(get_dir_location(f'{generator_mod_dir}/src/main/resources/fabric.mod.json'), 'w') as f:
+ json.dump(fabric_mod_json, f, indent=2)
os.system(
f'cd {generator_mod_dir} && gradlew runServer'
diff --git a/codegen/migrate.py b/codegen/migrate.py
index e5b17770..2dacc208 100644
--- a/codegen/migrate.py
+++ b/codegen/migrate.py
@@ -9,6 +9,14 @@ import sys
lib.download.clear_version_cache()
+if len(sys.argv) == 1:
+ print('\033[91mYou must provide a version to migrate to.\033[m')
+ version_manifest = lib.download.get_version_manifest()
+ newest_version = version_manifest['latest']['snapshot']
+ print(f'Hint: newest version is \033[1m{newest_version}\033[m')
+ exit()
+
+
old_version_id = lib.code.version.get_version_id()
old_mappings = lib.download.get_mappings_for_version(old_version_id)
old_burger_data = lib.extract.get_burger_data_for_version(old_version_id)
@@ -21,18 +29,24 @@ new_packet_list = list(new_burger_data[0]['packets']['packet'].values())
old_packets: dict[PacketIdentifier, str] = {}
+old_packets_data: dict[PacketIdentifier, dict] = {}
new_packets: dict[PacketIdentifier, str] = {}
+new_packets_data: dict[PacketIdentifier, dict] = {}
for packet in old_packet_list:
assert packet['class'].endswith('.class')
packet_name = old_mappings.get_class(packet['class'][:-6])
- old_packets[PacketIdentifier(
- packet['id'], packet['direction'].lower(), fix_state(packet['state']))] = packet_name
+ packet_ident = PacketIdentifier(
+ packet['id'], packet['direction'].lower(), fix_state(packet['state']))
+ old_packets[packet_ident] = packet_name
+ old_packets_data[packet_ident] = packet
for packet in new_packet_list:
assert packet['class'].endswith('.class')
packet_name = new_mappings.get_class(packet['class'][:-6])
- new_packets[PacketIdentifier(
- packet['id'], packet['direction'].lower(), fix_state(packet['state']))] = packet_name
+ packet_ident = PacketIdentifier(
+ packet['id'], packet['direction'].lower(), fix_state(packet['state']))
+ new_packets[packet_ident] = packet_name
+ new_packets_data[packet_ident] = packet
# find removed packets
removed_packets: list[PacketIdentifier] = []
@@ -65,13 +79,22 @@ for (direction, state), packets in group_packets(list(changed_packets.keys())).i
print()
-# find added packets
-added_packets: list[PacketIdentifier] = []
-for packet, packet_name in new_packets.items():
+# find added/changed packets
+added_or_changed_packets: list[PacketIdentifier] = []
+for new_packet, packet_name in new_packets.items():
+ old_packet = None
+ for old_packet_tmp, old_packet_name in old_packets.items():
+ if old_packet_name == packet_name:
+ old_packet = old_packet_tmp
+ break
+
if packet_name not in old_packets.values():
- added_packets.append(packet)
- print('Added packet:', packet, packet_name)
-for packet in added_packets:
+ added_or_changed_packets.append(new_packet)
+ print('Added packet:', new_packet, packet_name)
+ elif old_packet and not lib.code.packet.are_packet_instructions_identical(new_packets_data[new_packet].get('instructions'), old_packets_data[old_packet].get('instructions')):
+ added_or_changed_packets.append(new_packet)
+ print('Changed packet:', new_packet, packet_name)
+for packet in added_or_changed_packets:
lib.code.packet.generate_packet(
new_burger_data[0]['packets']['packet'], new_mappings, packet.packet_id, packet.direction, packet.state)