From 43ce929e4333ec1d2f6a6dd5ad63b2eb72dc86eb Mon Sep 17 00:00:00 2001 From: Thanatos Date: Mon, 2 Oct 2023 08:32:37 +0200 Subject: [PATCH] Add credits --- open_samus_returns_rando/files/schema.json | 8 ++ .../misc_patches/credits.py | 101 ++++++++++++++++++ .../samus_returns_patcher.py | 4 + 3 files changed, 113 insertions(+) create mode 100644 open_samus_returns_rando/misc_patches/credits.py diff --git a/open_samus_returns_rando/files/schema.json b/open_samus_returns_rando/files/schema.json index 19ed095..ca2769c 100644 --- a/open_samus_returns_rando/files/schema.json +++ b/open_samus_returns_rando/files/schema.json @@ -230,6 +230,14 @@ "additionalProperties": false }, "default": [] + }, + "spoiler_log": { + "type": "object", + "additionalProperties": { + "type": "string" + }, + "description": "A dict mapping item names to a string describing their location.", + "default": {} } }, "required": [ diff --git a/open_samus_returns_rando/misc_patches/credits.py b/open_samus_returns_rando/misc_patches/credits.py new file mode 100644 index 0000000..d46593e --- /dev/null +++ b/open_samus_returns_rando/misc_patches/credits.py @@ -0,0 +1,101 @@ + +import re + +from mercury_engine_data_structures.formats import Txt + +from open_samus_returns_rando.patcher_editor import PatcherEditor + +_PROJECT_MEMBERS = { + "Project Leads": [ + "Dyceron", + "Thanatos", + ], + "Game Patching": [ + "Henrique 'Darkszero' Gemignani", + "duncathan_salt", + "MeriKatt", + ], + "Logic Database": [ + "Miepee", + ], + " ": [ + "With contributions from many others.", + ] +} + +def _create_randomizer_credits(spoiler_log: dict[str, str]): + rando_credits: list[tuple[str, str]] = [ + ("Randomizer Credits", "CREDIT_TITLE"), + ] + + for group, group_members in _PROJECT_MEMBERS.items(): + rando_credits.append((group, "CREDIT_SUBTITLE")) + for member in group_members: + rando_credits.append((member, "CREDIT_")) + + if spoiler_log: + rando_credits.append((" ", "CREDIT_SUBTITLE")) + rando_credits.append(("Major Item Locations", "CREDIT_TITLE")) + for item, loc in spoiler_log.items(): + rando_credits.append((item, "CREDIT_SUBTITLE")) + rando_credits.append((loc, "CREDIT_")) + + rando_credits.append((" ", "CREDIT_SUBTITLE")) + + amount_title = 0 + amount_subtitle = 0 + amount_credit = 0 + + def new_sort(prefix: str, item) -> tuple[str, str]: + nonlocal amount_title + nonlocal amount_subtitle + nonlocal amount_credit + if prefix.startswith("CREDIT_TITLE"): + amount_title = amount_title + 1 + return (f"{prefix}{amount_title}", item) + elif prefix.startswith("CREDIT_SUBTITLE"): + amount_subtitle = amount_subtitle + 1 + return (f"{prefix}{amount_subtitle}", item) + else: + amount_credit = amount_credit + 1 + return (f"{prefix}{amount_credit}", item) + + rando_credits = [ + new_sort(prefix, item) + for item, prefix in rando_credits + ] + return rando_credits, amount_title, amount_subtitle, amount_credit + + +def patch_credits(editor: PatcherEditor, spoiler_log: dict[str, str]): + english_files = [ + "system/localization/mse_english.txt", + ] + + rando_credits, amount_title, amount_subtitle, amount_credit = _create_randomizer_credits(spoiler_log) + + for file in english_files: + text = editor.get_file(file, Txt) + ordered_credits = list(text.strings.items()) + + # suffix needs to be sorted per "type" + for index, credit_tuple in enumerate(ordered_credits): + title = re.findall(r'CREDIT_TITLE(\d+)', credit_tuple[0]) + if len(title) > 0: + new_suffix = int(title[0]) + amount_title + ordered_credits[index] = (f"CREDIT_TITLE{new_suffix}", credit_tuple[1]) + continue + + title = re.findall(r'CREDIT_SUBTITLE(\d+)', credit_tuple[0]) + if len(title) > 0: + new_suffix = int(title[0]) + amount_subtitle + ordered_credits[index] = (f"CREDIT_SUBTITLE{new_suffix}", credit_tuple[1]) + continue + + title = re.findall(r'CREDIT_(\d+)', credit_tuple[0]) + if len(title) > 0: + new_suffix = int(title[0]) + amount_credit + ordered_credits[index] = (f"CREDIT_{new_suffix}", credit_tuple[1]) + continue + + text.strings = dict(rando_credits + ordered_credits) diff --git a/open_samus_returns_rando/samus_returns_patcher.py b/open_samus_returns_rando/samus_returns_patcher.py index c0118a9..64693d7 100644 --- a/open_samus_returns_rando/samus_returns_patcher.py +++ b/open_samus_returns_rando/samus_returns_patcher.py @@ -8,6 +8,7 @@ from open_samus_returns_rando.lua_editor import LuaEditor from open_samus_returns_rando.misc_patches import lua_util +from open_samus_returns_rando.misc_patches.credits import patch_credits from open_samus_returns_rando.misc_patches.exefs import DSPatch from open_samus_returns_rando.patcher_editor import PatcherEditor from open_samus_returns_rando.pickup import patch_pickups @@ -146,6 +147,9 @@ def patch_extracted(input_path: Path, output_path: Path, configuration: dict): # Specific game patches game_patches.apply_game_patches(editor, configuration.get("game_patches", {})) + # Patch credits + patch_credits(editor, configuration["spoiler_log"]) + out_romfs = output_path.joinpath("romfs") out_exefs = output_path.joinpath("exefs") shutil.rmtree(out_romfs, ignore_errors=True)