azalea/codegen/migrate.py

73 lines
2.8 KiB
Python
Raw Normal View History

2022-05-25 05:35:53 +00:00
from lib.code.packet import fix_state
2022-05-25 05:30:47 +00:00
from lib.utils import PacketIdentifier, group_packets
2022-05-25 04:05:44 +00:00
import lib.code.utils
import lib.code.version
2022-05-25 05:21:05 +00:00
import lib.code.packet
2022-05-25 04:05:44 +00:00
import lib.download
import sys
import os
old_version_id = lib.code.version.get_version_id()
old_mappings = lib.download.get_mappings_for_version(old_version_id)
old_burger_data = lib.download.get_burger_data_for_version(old_version_id)
old_packet_list = list(old_burger_data[0]['packets']['packet'].values())
new_version_id = sys.argv[1]
new_mappings = lib.download.get_mappings_for_version(new_version_id)
new_burger_data = lib.download.get_burger_data_for_version(new_version_id)
new_packet_list = list(new_burger_data[0]['packets']['packet'].values())
2022-05-25 05:21:05 +00:00
old_packets: dict[PacketIdentifier, str] = {}
new_packets: dict[PacketIdentifier, str] = {}
2022-05-25 04:05:44 +00:00
for packet in old_packet_list:
assert packet['class'].endswith('.class')
packet_name = old_mappings.get_class(packet['class'][:-6])
2022-05-25 05:21:05 +00:00
old_packets[PacketIdentifier(
2022-05-25 05:35:53 +00:00
packet['id'], packet['direction'].lower(), fix_state(packet['state']))] = packet_name
2022-05-25 04:05:44 +00:00
for packet in new_packet_list:
assert packet['class'].endswith('.class')
packet_name = new_mappings.get_class(packet['class'][:-6])
2022-05-25 05:21:05 +00:00
new_packets[PacketIdentifier(
2022-05-25 05:35:53 +00:00
packet['id'], packet['direction'].lower(), fix_state(packet['state']))] = packet_name
2022-05-25 05:21:05 +00:00
# find removed packets
removed_packets: list[PacketIdentifier] = []
2022-05-25 05:38:27 +00:00
for packet, packet_name in old_packets.items():
if packet_name not in old_packets.values():
2022-05-25 05:21:05 +00:00
removed_packets.append(packet)
2022-05-25 05:30:47 +00:00
print('Removed packet:', packet)
2022-05-25 05:21:05 +00:00
for (direction, state), packets in group_packets(removed_packets).items():
lib.code.packet.remove_packet_ids(packets, direction, state)
2022-05-25 04:05:44 +00:00
print()
2022-05-25 05:21:05 +00:00
# find packets that changed ids
changed_packets: dict[PacketIdentifier, int] = {}
for old_packet, old_packet_name in old_packets.items():
for new_packet, new_packet_name in new_packets.items():
2022-05-25 05:39:33 +00:00
if old_packet.direction == new_packet.direction and old_packet.state == new_packet.state and old_packet.packet_id != new_packet.packet_id:
2022-05-25 05:21:05 +00:00
changed_packets[old_packet] = new_packet.packet_id
2022-05-25 05:30:47 +00:00
print('Changed packet id:', old_packet, '->',
new_packet, f'({new_packet_name})')
2022-05-25 05:39:33 +00:00
break
2022-05-25 05:21:05 +00:00
for (direction, state), packets in group_packets(list(changed_packets.keys())).items():
lib.code.packet.remove_packet_ids(packets, direction, state)
2022-05-25 04:05:44 +00:00
print()
# find added packets
2022-05-25 05:21:05 +00:00
added_packets: list[PacketIdentifier] = []
2022-05-25 05:38:27 +00:00
for packet, packet_name in new_packets.items():
if packet_name not in old_packets.values():
2022-05-25 05:21:05 +00:00
added_packets.append(packet)
2022-05-25 05:30:47 +00:00
print('Added packet:', packet)
2022-05-25 05:21:05 +00:00
for packet in added_packets:
lib.code.packet.generate_packet(
2022-05-25 05:30:47 +00:00
new_burger_data[0]['packets']['packet'], new_mappings, packet.packet_id, packet.direction, packet.state)
2022-05-25 04:05:44 +00:00
lib.code.utils.fmt()
print('Done!')