-
Notifications
You must be signed in to change notification settings - Fork 0
/
classname_mappings.py
145 lines (105 loc) · 4.19 KB
/
classname_mappings.py
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from lib.version import Version
from lib.webpack import Webpack
import os
import json
import datetime
base_path = "./webpack"
versions : dict[Version, Webpack] = {}
for x in [Version(f) for f in os.listdir(base_path) if os.path.isfile(os.path.join(base_path, f))]:
versions[x] = x.load()
with open("./ignore.json", 'r') as fp:
ignore = json.load(fp)
def find_version(id : str) -> Version:
for x in versions:
if id == x.timestamp:
return x
return None
def filter_single(key : str, val : str) -> bool:
return len(val) <= 0 or val[0].isnumeric() or " " in val
class ModuleMapping:
def __init__(self, data : dict[str, str], webpack_id : str):
self.webpack_id = webpack_id
self.raw = data
self.mappings : dict[str, dict[str, str]] = {}
self.valid = True
self.ignore_webpack_name = []
self.generate()
def generate(self):
first_version = find_version(next(iter(self.raw)))
first_webpack_module = versions[first_version].ids[self.webpack_id]
if len(first_webpack_module.mappings) >= 1000:
self.valid = False
return
ids_to_not_map = [x for x in first_webpack_module.mappings if filter_single(x, first_webpack_module.mappings[x])]
for x in ignore:
if x in first_webpack_module.mappings:
self.ignore_webpack_name.append(x)
if len(ids_to_not_map) == len(first_webpack_module.mappings):
self.valid = False
return
for x in self.raw:
version = find_version(x)
webpack_module = versions[version].ids[self.raw[x]]
for y in webpack_module.mappings:
if y in ids_to_not_map:
continue
if y not in self.mappings:
self.mappings[y] = {}
self.mappings[y][version.timestamp] = webpack_module.mappings[y]
# TODO: Slow
def match_value(self, css_class : str) -> dict[str, str]|None:
for x in self.mappings:
if css_class in self.mappings[x].values():
return self.mappings[x]
return None
def to_dict(self) -> dict:
for x in self.mappings:
self.mappings[x] = dict(sorted(self.mappings[x].items(), key= lambda x: int(x[0])))
flattened_mappings : dict[str, dict[str, str]] = {}
for _, (name, version_translations) in enumerate(self.mappings.items()):
translations : dict[str, str] = {}
keys : list[str] = []
for _, (steam_version, translation) in enumerate(version_translations.items()):
if translation not in keys:
keys.append(translation)
translations[steam_version] = translation
flattened_mappings[name] = translations
return {
"name": None,
"ids": self.raw,
"classname_mappings": flattened_mappings,
"ignore_webpack_keys": self.ignore_webpack_name
}
with open("webpack_id_mappings.json", 'r') as fp:
data = json.load(fp)
modules = [ModuleMapping(data[x], x) for x in data]
with open("./beta.orig.json", "r") as fp:
data = json.load(fp)
for i, (k, v) in enumerate(data.items()):
items = v
has_match = False
for y in items[::-1]:
if has_match:
break
for module in [z for z in modules if z.valid]:
match = module.match_value(y)
if match is None:
continue
items_to_add = [x for x in items if x not in match.values()]
i = 0
for item in items_to_add:
while str(i) in match:
i += 1
match[str(i)] = item
has_match = True
break
if not has_match:
print(f"{items} had no matches")
final = {}
versions_dict = {}
for x in [z for z in modules if z.valid]:
final[x.webpack_id] = x.to_dict()
for x in versions:
versions_dict[x.timestamp] = x.type
with open("classname_mappings.json", 'w') as fp:
json.dump({"versions": versions_dict, "module_mappings": final, "generated": datetime.datetime.now(datetime.timezone.utc).isoformat()}, fp)