From 8b9771b5af3cc2a80643be53f2e69ae201b2df80 Mon Sep 17 00:00:00 2001 From: Kevin Trocolli Date: Fri, 5 May 2023 00:24:47 -0400 Subject: [PATCH] mai2: implement event reader for pre-dx games --- docs/game_specific_info.md | 2 +- titles/mai2/const.py | 2 +- titles/mai2/read.py | 157 ++++++++++++++++++++++++++++++++++--- 3 files changed, 149 insertions(+), 12 deletions(-) diff --git a/docs/game_specific_info.md b/docs/game_specific_info.md index 167dc8b..a2a916a 100644 --- a/docs/game_specific_info.md +++ b/docs/game_specific_info.md @@ -145,7 +145,7 @@ python read.py --series --version --binfolder /path/to/ ``` The importer for maimai DX will import Events, Music and Tickets. -The importer for maimai Pre-DX will import Events and Music. Not all games will have patch data. Milk - Finale have file encryption, and need an AES key. That key is not provided by the developers. +The importer for maimai Pre-DX will import Events and Music. Not all games will have patch data. Milk - Finale have file encryption, and need an AES key. That key is not provided by the developers. For games that do use encryption, provide the key, as a hex string, with the `--extra` flag. Ex `--extra 00112233445566778899AABBCCDDEEFF` **Important: It is required to use the importer because some games may not function properly or even crash without Events!** diff --git a/titles/mai2/const.py b/titles/mai2/const.py index 19cb867..6d638b9 100644 --- a/titles/mai2/const.py +++ b/titles/mai2/const.py @@ -82,5 +82,5 @@ class Mai2Constants: @classmethod def game_ver_to_string(cls, ver: int): if ver >= 1000: - return cls.VERSION_STRING_OLD[ver / 1000] + return cls.VERSION_STRING_OLD[ver - 1000] return cls.VERSION_STRING[ver] diff --git a/titles/mai2/read.py b/titles/mai2/read.py index 5809464..daa908f 100644 --- a/titles/mai2/read.py +++ b/titles/mai2/read.py @@ -4,6 +4,9 @@ import os import re import xml.etree.ElementTree as ET from typing import Any, Dict, List, Optional +from Crypto.Cipher import AES +import zlib +import codecs from core.config import CoreConfig from core.data import Data @@ -34,18 +37,139 @@ class Mai2Reader(BaseReader): def read(self) -> None: data_dirs = [] - if self.bin_dir is not None: - data_dirs += self.get_data_directories(self.bin_dir) + if self.version < Mai2Constants.VER_MAIMAI: + if self.bin_dir is not None: + data_dirs += self.get_data_directories(self.bin_dir) - if self.opt_dir is not None: - data_dirs += self.get_data_directories(self.opt_dir) + if self.opt_dir is not None: + data_dirs += self.get_data_directories(self.opt_dir) - for dir in data_dirs: - self.logger.info(f"Read from {dir}") - self.get_events(f"{dir}/event") - self.disable_events(f"{dir}/information", f"{dir}/scoreRanking") - self.read_music(f"{dir}/music") - self.read_tickets(f"{dir}/ticket") + for dir in data_dirs: + self.logger.info(f"Read from {dir}") + self.get_events(f"{dir}/event") + self.disable_events(f"{dir}/information", f"{dir}/scoreRanking") + self.read_music(f"{dir}/music") + self.read_tickets(f"{dir}/ticket") + + else: + self.logger.warn("Pre-DX Readers are not yet implemented!") + if not os.path.exists(f"{self.bin_dir}/tables"): + self.logger.error(f"tables directory not found in {self.bin_dir}") + return + + if self.version >= Mai2Constants.VER_MAIMAI_MILK: + if self.extra is None: + self.logger.error("Milk - Finale requre an AES key via a hex string send as the --extra flag") + return + + key = bytes.fromhex(self.extra) + + else: + key = None + + evt_table = self.load_table_raw(f"{self.bin_dir}/tables", "mmEvent.bin", key) + txt_table = self.load_table_raw(f"{self.bin_dir}/tables", "mmtextout_jp.bin", key) + score_table = self.load_table_raw(f"{self.bin_dir}/tables", "mmScore.bin", key) + + self.read_old_events(evt_table) + + return + + def load_table_raw(self, dir: str, file: str, key: Optional[bytes]) -> Optional[List[Dict[str, str]]]: + if not os.path.exists(f"{dir}/{file}"): + self.logger.warn(f"file {file} does not exist in directory {dir}, skipping") + return + + self.logger.info(f"Load table {file} from {dir}") + if key is not None: + cipher = AES.new(key, AES.MODE_CBC) + with open(f"{dir}/{file}", "rb") as f: + f_encrypted = f.read() + f_data = cipher.decrypt(f_encrypted)[0x10:] + + else: + with open(f"{dir}/{file}", "rb") as f: + f_data = f.read()[0x10:] + + if f_data is None or not f_data: + self.logger.warn(f"file {dir} could not be read, skipping") + return + + f_data_deflate = zlib.decompress(f_data, wbits = zlib.MAX_WBITS | 16) + f_decoded = codecs.utf_16_le_decode(f_data_deflate)[0] + f_split = f_decoded.splitlines() + + has_struct_def = "struct " in f_decoded + is_struct = False + struct_def = [] + tbl_content = [] + + if has_struct_def: + for x in f_split: + if x.startswith("struct "): + is_struct = True + struct_name = x[7:-1] + continue + + if x.startswith("};"): + is_struct = False + break + + if is_struct: + try: + struct_def.append(x[x.rindex(" ") + 2: -1]) + except ValueError: + self.logger.warn(f"rindex failed on line {x}") + + if is_struct: + self.logger.warn("Struct not formatted properly") + + if not struct_def: + self.logger.warn("Struct def not found") + + name = file[:file.index(".")] + if "_" in name: + name = name[:file.index("_")] + + for x in f_split: + if not x.startswith(name.upper()): + continue + + line_match = re.match(r"(\w+)\((.*?)\)([ ]+\/{3}<[ ]+(.*))?", x) + if line_match is None: + continue + + if not line_match.group(1) == name.upper(): + self.logger.warn(f"Strange regex match for line {x} -> {line_match}") + continue + + vals = line_match.group(2) + comment = line_match.group(4) + line_dict = {} + + vals_split = vals.split(",") + for y in range(len(vals_split)): + stripped = vals_split[y].strip().lstrip("L\"").lstrip("\"").rstrip("\"") + if not stripped or stripped is None: + continue + + if has_struct_def and len(struct_def) > y: + line_dict[struct_def[y]] = stripped + + else: + line_dict[f'item_{y}'] = stripped + + if comment: + line_dict['comment'] = comment + + tbl_content.append(line_dict) + + if tbl_content: + return tbl_content + + else: + self.logger.warning("Failed load table content, skipping") + return def get_events(self, base_dir: str) -> None: self.logger.info(f"Reading events from {base_dir}...") @@ -188,3 +312,16 @@ class Mai2Reader(BaseReader): self.version, id, ticket_type, price, name ) self.logger.info(f"Added ticket {id}...") + + def read_old_events(self, events: List[Dict[str, str]]) -> None: + for event in events: + evt_id = int(event.get('イベントID', '0')) + evt_expire_time = float(event.get('オフ時強制時期', '0.0')) + is_exp = bool(int(event.get('海外許可', '0'))) + is_aou = bool(int(event.get('AOU許可', '0'))) + name = event.get('comment', f'evt_{evt_id}') + + self.data.static.put_game_event(self.version, 0, evt_id, name) + + if not (is_exp or is_aou): + self.data.static.toggle_game_event(self.version, evt_id, False) \ No newline at end of file