aboutsummaryrefslogtreecommitdiff
path: root/codegen/migrate.py
blob: fa7b01a6075dfbdf9bbef8d1532b766e2f981425 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from lib.code.packet import fix_state
from lib.utils import PacketIdentifier, group_packets
import lib.code.utils
import lib.code.version
import lib.code.packet
import lib.download
import lib.extract
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)
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.extract.get_burger_data_for_version(new_version_id)
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])
    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])
    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] = []
for packet, packet_name in old_packets.items():
    if packet_name not in new_packets.values():
        removed_packets.append(packet)
        print('Removed packet:', packet, packet_name)
for (direction, state), packets in group_packets(removed_packets).items():
    lib.code.packet.remove_packet_ids(packets, direction, state)

print()

# 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():
        if old_packet_name == new_packet_name and old_packet.direction == new_packet.direction and old_packet.state == new_packet.state and old_packet.packet_id != new_packet.packet_id:
            changed_packets[old_packet] = new_packet.packet_id
            print('Changed packet id:', old_packet, '->',
                  new_packet, f'({new_packet_name})')
            break
for (direction, state), packets in group_packets(list(changed_packets.keys())).items():
    id_map: dict[int, int] = {}
    for old_packet_id in packets:
        new_packet_id = changed_packets[PacketIdentifier(
            old_packet_id, direction, state)]
        id_map[old_packet_id] = new_packet_id
    lib.code.packet.change_packet_ids(id_map, direction, state)


print()

# find added/changed packets
added_or_changed_packets: list[PacketIdentifier] = []
for packet, packet_name in new_packets.items():
    if packet_name not in old_packets.values():
        added_or_changed_packets.append(packet)
        print('Added packet:', packet, packet_name)
    if new_packets_data[packet].get('instructions') != old_packets_data[packet].get('instructions'):
        print('hmm')
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)

lib.code.version.set_protocol_version(
    new_burger_data[0]['version']['protocol'])
lib.code.version.set_version_id(new_version_id)

lib.code.utils.fmt()

print('Done!')