diff --git a/mapper.py b/mapper.py index 82529e3..f8524fd 100644 --- a/mapper.py +++ b/mapper.py @@ -6,10 +6,63 @@ from filereader import FileReader class Mapper: def __init__(self, mapping_file): - file = open(mapping_file, "rb") + + ### NORMAL MAP LOADING ### + + self.load_data(mapping_file) + self.load_map("MAP") + + # you can use the following to load your own mapping instead of the official one + # for both cases, comment the 2 lines in the normal mode and uncomment the 2 in your mode + # and for both make sure of the following + # + # key -> the hashed path + # value -> without the .wem extension, by default the language is also removed and + # obtained back via addLang param but you can keep it in the path + # + # example : 3fe302b037275600 -> voice\\chapter4\\76\\player\\chapter4_76_player_118_f + + ### JSON MAP LOADING ### + + # make sure the json is in the format {hash: path} + # format the path to use *double backward slashes* + # + # example + # {"3fe302b037275600": "voice\\chapter4\\76\\player\\chapter4_76_player_118_f"} + + # self.load_data("maps/yourmap.json") + # self.load_map("JSON") + + ### TSV MAP LOADING ### + + # make sure it is in the format "hash \t path" + # format the path to use *single backward slashes* + # + # example + # 3fe302b037275600 voice\chapter4\76\player\chapter4_76_player_118_f + + # self.load_data("maps/yourmap.tsv") + # self.load_map("TSV") + + def load_map(self, mode): + self.mode = mode + + if self.mode == "MAP": + self.process_map() + elif self.mode == "JSON": + self.keys = json.loads(self.data) + elif self.mode == "TSV": + self.keys = { + parts[0]:parts[1] + for e in self.data.decode("utf-8").splitlines() + if (parts := e.split("\t")) and len(parts) >= 2} + + def load_data(self, file): + file = open(file, "rb") self.data = file.read() file.close() + def process_map(self): reader = FileReader(io.BytesIO(self.data), "little") # check file @@ -25,9 +78,6 @@ class Mapper: reader.ReadBytes(2) - self.process_map(reader) - - def process_map(self, reader): games = { "hk4e": "Genshin", "hkrpg": "Star Rail", @@ -113,41 +163,44 @@ class Mapper: if hasMusic: print(f": {n_music} musics") - def get_key(self, key, lang=False): - if (not key in self.keys) and (not key in self.music_keys): - return None + def get_key(self, key, addLang=False): + if self.mode == "MAP": + if (not key in self.keys) and (not key in self.music_keys): + return None - if key in self.music_keys: - return [self.music_keys[key], ""] + if key in self.music_keys: + return [self.music_keys[key], ""] - lang, offset = (self.keys[key] >> 22) & 0x03, self.keys[key] & 0x3FFFFF + lang, offset = (self.keys[key] >> 22) & 0x03, self.keys[key] & 0x3FFFFF - parts = int.from_bytes(self.files[offset:offset+1], "big") - name = [] + parts = int.from_bytes(self.files[offset:offset+1], "big") + name = [] - for i in range(parts): - word_offset = int.from_bytes(self.files[offset+1+(3*i):offset+4+(3*i)], "big") - word_parts = int.from_bytes(self.words[word_offset:word_offset+1], "big") - word = [] + for i in range(parts): + word_offset = int.from_bytes(self.files[offset+1+(3*i):offset+4+(3*i)], "big") + word_parts = int.from_bytes(self.words[word_offset:word_offset+1], "big") + word = [] - for j in range(word_parts): - string_offset = int.from_bytes(self.words[word_offset+1+(2*j):word_offset+3+(2*j)], "big") - string_size = int.from_bytes(self.strings[string_offset:string_offset+1], "big") - if string_size > 128: - string = str(int.from_bytes(self.strings[string_offset+1:string_offset+1+(string_size-128)], "big")) - else: - string = bytes([b ^ (0x97 + string_size) for b in self.strings[string_offset+1:string_offset+1+string_size]]).decode("utf-8") - word.append(string) + for j in range(word_parts): + string_offset = int.from_bytes(self.words[word_offset+1+(2*j):word_offset+3+(2*j)], "big") + string_size = int.from_bytes(self.strings[string_offset:string_offset+1], "big") + if string_size > 128: + string = str(int.from_bytes(self.strings[string_offset+1:string_offset+1+(string_size-128)], "big")) + else: + string = bytes([b ^ (0x97 + string_size) for b in self.strings[string_offset+1:string_offset+1+string_size]]).decode("utf-8") + word.append(string) - word = "_".join(word) - name.append(word) + word = "_".join(word) + name.append(word) - name = ["\\".join(name)] - - if lang: - name.append(self.languages[lang]) + name = ["\\".join(name)] + + if addLang: + name.append(self.languages[lang]) - return name + return name + elif self.mode in ["JSON", "TSV"]: + return [self.keys[key]] def reset(self): self.reader = None