-
Notifications
You must be signed in to change notification settings - Fork 7
/
mapper.py
172 lines (137 loc) · 4.24 KB
/
mapper.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# reader for the .map format i've made to improve reading speed and mapping size
from filereader import FileReader
class Mapper:
def __init__(self, mapping_file):
file = open(mapping_file, "rb")
reader = FileReader(file, "little") # encoded as little
# check file
if reader.ReadBytes(4) != b"ESFM":
file.close()
raise Exception("mapping was invalid")
reader.ReadBytes(2)
map_version = reader.ReadBytes(2)
if map_version == b"\x56\x31":
print(f"Warning: you are using an old version of the mapping that is no longer supported, please use a newer one or download an older version of this tool.")
raise Exception("outdated mapping")
elif map_version == b"\x56\x32":
self.reader = reader
self.process_map()
else:
file.close()
raise Exception("invalid mapping version")
def process_map(self):
reader = self.reader
# utils
val = lambda length: vl2(reader.ReadBytes(length))
vl2 = lambda data: int.from_bytes(data, "little")
raw = lambda length: rw2(reader.ReadBytes(length))
rw2 = lambda data: data.rstrip(b"\x00").decode("utf-8")
n2p = lambda val: [e[0] for e in enumerate(list(bin(val)[2:][::-1])) if e[1] == "1"]
# get map meta
reader.ReadBytes(2)
games = {
"ys": "Genshin",
"sr": "Star Rail",
"zzz": "Zenless Zone Zero"
# more later
}
coverages = [
"english voicelines",
"chinese voicelines",
"japanese voicelines",
"korean voicelines",
"music",
"sfx"
]
header_size = val(1) # header size
block_size = 4
header_blocks = [reader.ReadBytes(block_size) for _ in range(header_size // block_size)]
infos = {
"game": games[rw2(header_blocks[0])],
"version": list(rw2(header_blocks[1])),
"coverage": int(rw2(header_blocks[2])),
# more later
}
print(f"> Loading mapping for {infos['game']} v{infos['version'][0]}.{infos['version'][1]}, this may take a few seconds...")
# read prefixes
prefixes = {}
n_prefixes = reader.ReadUInt8()
l_prefixes = reader.ReadUInt8()
for i in range(n_prefixes):
prefix = raw(l_prefixes)
marker = reader.ReadBytes(1)
prefixes[marker] = prefix
# read languages
langs_offsets = {}
n_langs = reader.ReadUInt8()
l_langs = reader.ReadUInt8()
for i in range(n_langs):
offset = reader.GetBufferPos()
langs_offsets[offset] = raw(l_langs)
self.langs_offsets = langs_offsets
# read folders
folder_offsets = {}
n_folders = reader.ReadUInt8()
for i in range(n_folders):
offset = reader.GetBufferPos()
length = reader.ReadUInt8()
prefix = reader.ReadBytes(1)
folder = raw(length)
folder = f"{prefixes[prefix]}{folder}"
folder_offsets[offset] = folder
# read files
files_offsets = {}
n_files = val(3)
for i in range(n_files):
offset = reader.GetBufferPos()
path_length = reader.ReadUInt8()
path = []
for i in range(path_length):
path.append(folder_offsets[reader.ReadUInt16()])
name_length = reader.ReadUInt8()
prefix = reader.ReadBytes(1)
if prefix != b"\x00":
prefix = prefixes[prefix]
else:
prefix = ""
name = raw(name_length)
name = f"{prefix}{name}"
path.append(name)
path = "\\".join(path)
files_offsets[offset] = path
self.files_offsets = files_offsets
# read keys
# GI 3649050
keys_data = {}
n_keys = val(3)
left = reader.GetRemainingLength()
data = bytearray(reader.ReadBytes(left))
keys_data = {rw2(data[i:i+16]): bytes(data[i+16:i+21]) for i in range(0, len(data), 21)}
self.keys_data = keys_data
# done
print(f"> Finished loading mapping")
print(f": {n_langs} supported languages")
print(f": {n_files} mapped files")
print(f": {n_keys} available keys")
print(f"")
print(f"> Mapping coverage")
coverage = n2p(infos["coverage"])
for val in coverage:
if val%2 == 0:
print(f": partial {coverages[val//2-1]}")
else:
print(f": {coverages[(val-1)//2]}")
def get_key(self, key, lang=False):
keys_data = self.keys_data
if key not in keys_data.keys():
return None
key_data = keys_data[key]
data = [self.files_offsets[int.from_bytes(key_data[2:], "little")]]
if lang:
data.append(self.langs_offsets[int.from_bytes(key_data[:1], "little")])
return data
def reset(self):
self.reader = None
self.langs_offsets.clear()
self.files_offsets.clear()
self.keys_data.clear()