From 24d0bb2b73af2b4c37706283a0e92575a9faf31f Mon Sep 17 00:00:00 2001 From: Miepee Date: Fri, 14 Feb 2025 16:43:12 +0100 Subject: [PATCH 1/6] WIP: don't duplicate item messages --- src/mars_patcher/item_patcher.py | 39 +++++++++++++++++++++++++------- src/mars_patcher/locations.py | 3 +++ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/mars_patcher/item_patcher.py b/src/mars_patcher/item_patcher.py index 87381be..c67ae32 100644 --- a/src/mars_patcher/item_patcher.py +++ b/src/mars_patcher/item_patcher.py @@ -59,6 +59,7 @@ def write_items(self) -> None: prev_area_room = (-1, -1) room_tank_count = 0 total_metroids = 0 + item_messages_to_custom_id: dict[str, int] = {} for min_loc in minor_locs: if min_loc.new_item == ItemType.INFANT_METROID: total_metroids += 1 @@ -131,10 +132,21 @@ def write_items(self) -> None: rom.write_8(item_addr + 6, min_loc.item_sprite.value) # Handle custom messages if min_loc.item_messages is not None: - self.write_custom_message( - custom_message_id, message_table_addrs, item_addr, min_loc.item_messages, False - ) - custom_message_id += 1 + # If we already encountered the message before, just write the message id. + item_message_as_tuple = min_loc.item_messages.as_str() + if item_message_as_tuple in item_messages_to_custom_id: + print(item_message_as_tuple) + rom.write_8(item_addr + 7, item_messages_to_custom_id[item_message_as_tuple]) + else: + self.write_custom_message( + custom_message_id, + message_table_addrs, + item_addr, + min_loc.item_messages, + False, + ) + item_messages_to_custom_id[item_message_as_tuple] = custom_message_id + custom_message_id += 1 # Handle major locations for maj_loc in self.settings.major_locs: @@ -146,10 +158,21 @@ def write_items(self) -> None: rom.write_8(addr, maj_loc.new_item.value) # Handle custom messages if maj_loc.item_messages is not None: - self.write_custom_message( - custom_message_id, message_table_addrs, addr, maj_loc.item_messages, True - ) - custom_message_id += 1 + # If we already encountered the message before, just write the message id. + item_message_as_tuple = maj_loc.item_messages.as_str() + if item_message_as_tuple in item_messages_to_custom_id: + print(item_message_as_tuple) + rom.write_8(addr + 1, item_messages_to_custom_id[item_message_as_tuple]) + else: + self.write_custom_message( + custom_message_id, + message_table_addrs, + addr, + maj_loc.item_messages, + True, + ) + item_messages_to_custom_id[item_message_as_tuple] = custom_message_id + custom_message_id += 1 # Write total metroid count rom.write_8(TOTAL_METROID_COUNT_ADDR, total_metroids) diff --git a/src/mars_patcher/locations.py b/src/mars_patcher/locations.py index bdace40..472e57c 100644 --- a/src/mars_patcher/locations.py +++ b/src/mars_patcher/locations.py @@ -110,6 +110,9 @@ def from_json(cls, data: Itemmessages) -> ItemMessages: centered = data.get(KEY_CENTERED, True) return cls(item_messages, centered) + def as_str(self) -> str: + return str((self.item_messages, self.centered)) + class LocationSettings: def __init__(self, major_locs: list[MajorLocation], minor_locs: list[MinorLocation]): From 178dd29145f48b918fd0975cb44550bb47baabbf Mon Sep 17 00:00:00 2001 From: Miepee Date: Fri, 14 Feb 2025 16:44:21 +0100 Subject: [PATCH 2/6] forgot to save --- src/mars_patcher/item_patcher.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/mars_patcher/item_patcher.py b/src/mars_patcher/item_patcher.py index c67ae32..d7eea0c 100644 --- a/src/mars_patcher/item_patcher.py +++ b/src/mars_patcher/item_patcher.py @@ -133,10 +133,10 @@ def write_items(self) -> None: # Handle custom messages if min_loc.item_messages is not None: # If we already encountered the message before, just write the message id. - item_message_as_tuple = min_loc.item_messages.as_str() - if item_message_as_tuple in item_messages_to_custom_id: - print(item_message_as_tuple) - rom.write_8(item_addr + 7, item_messages_to_custom_id[item_message_as_tuple]) + item_message_as_str = min_loc.item_messages.as_str() + if item_message_as_str in item_messages_to_custom_id: + print(item_message_as_str) + rom.write_8(item_addr + 7, item_messages_to_custom_id[item_message_as_str]) else: self.write_custom_message( custom_message_id, @@ -145,7 +145,7 @@ def write_items(self) -> None: min_loc.item_messages, False, ) - item_messages_to_custom_id[item_message_as_tuple] = custom_message_id + item_messages_to_custom_id[item_message_as_str] = custom_message_id custom_message_id += 1 # Handle major locations @@ -159,10 +159,10 @@ def write_items(self) -> None: # Handle custom messages if maj_loc.item_messages is not None: # If we already encountered the message before, just write the message id. - item_message_as_tuple = maj_loc.item_messages.as_str() - if item_message_as_tuple in item_messages_to_custom_id: - print(item_message_as_tuple) - rom.write_8(addr + 1, item_messages_to_custom_id[item_message_as_tuple]) + item_message_as_str = maj_loc.item_messages.as_str() + if item_message_as_str in item_messages_to_custom_id: + print(item_message_as_str) + rom.write_8(addr + 1, item_messages_to_custom_id[item_message_as_str]) else: self.write_custom_message( custom_message_id, @@ -171,7 +171,7 @@ def write_items(self) -> None: maj_loc.item_messages, True, ) - item_messages_to_custom_id[item_message_as_tuple] = custom_message_id + item_messages_to_custom_id[item_message_as_str] = custom_message_id custom_message_id += 1 # Write total metroid count From 67bcfade5d0abd29ef06aefb9d0995ee89780752 Mon Sep 17 00:00:00 2001 From: Miepee Date: Fri, 14 Feb 2025 19:39:59 +0100 Subject: [PATCH 3/6] remove prints --- src/mars_patcher/item_patcher.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/mars_patcher/item_patcher.py b/src/mars_patcher/item_patcher.py index d7eea0c..7876806 100644 --- a/src/mars_patcher/item_patcher.py +++ b/src/mars_patcher/item_patcher.py @@ -135,7 +135,6 @@ def write_items(self) -> None: # If we already encountered the message before, just write the message id. item_message_as_str = min_loc.item_messages.as_str() if item_message_as_str in item_messages_to_custom_id: - print(item_message_as_str) rom.write_8(item_addr + 7, item_messages_to_custom_id[item_message_as_str]) else: self.write_custom_message( @@ -161,7 +160,6 @@ def write_items(self) -> None: # If we already encountered the message before, just write the message id. item_message_as_str = maj_loc.item_messages.as_str() if item_message_as_str in item_messages_to_custom_id: - print(item_message_as_str) rom.write_8(addr + 1, item_messages_to_custom_id[item_message_as_str]) else: self.write_custom_message( From 90b7c817afbbd758f7dfbc649761e1fb5fb8f59a Mon Sep 17 00:00:00 2001 From: Miepee Date: Fri, 14 Feb 2025 19:48:28 +0100 Subject: [PATCH 4/6] use dataclass --- src/mars_patcher/item_patcher.py | 18 +++++++++--------- src/mars_patcher/locations.py | 12 +++++------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/mars_patcher/item_patcher.py b/src/mars_patcher/item_patcher.py index 7876806..4c257bb 100644 --- a/src/mars_patcher/item_patcher.py +++ b/src/mars_patcher/item_patcher.py @@ -59,7 +59,7 @@ def write_items(self) -> None: prev_area_room = (-1, -1) room_tank_count = 0 total_metroids = 0 - item_messages_to_custom_id: dict[str, int] = {} + item_messages_to_custom_id: dict[ItemMessages, int] = {} for min_loc in minor_locs: if min_loc.new_item == ItemType.INFANT_METROID: total_metroids += 1 @@ -133,9 +133,9 @@ def write_items(self) -> None: # Handle custom messages if min_loc.item_messages is not None: # If we already encountered the message before, just write the message id. - item_message_as_str = min_loc.item_messages.as_str() - if item_message_as_str in item_messages_to_custom_id: - rom.write_8(item_addr + 7, item_messages_to_custom_id[item_message_as_str]) + messages = min_loc.item_messages + if messages in item_messages_to_custom_id: + rom.write_8(item_addr + 7, item_messages_to_custom_id[messages]) else: self.write_custom_message( custom_message_id, @@ -144,7 +144,7 @@ def write_items(self) -> None: min_loc.item_messages, False, ) - item_messages_to_custom_id[item_message_as_str] = custom_message_id + item_messages_to_custom_id[messages] = custom_message_id custom_message_id += 1 # Handle major locations @@ -158,9 +158,9 @@ def write_items(self) -> None: # Handle custom messages if maj_loc.item_messages is not None: # If we already encountered the message before, just write the message id. - item_message_as_str = maj_loc.item_messages.as_str() - if item_message_as_str in item_messages_to_custom_id: - rom.write_8(addr + 1, item_messages_to_custom_id[item_message_as_str]) + messages = maj_loc.item_messages + if messages in item_messages_to_custom_id: + rom.write_8(addr + 1, item_messages_to_custom_id[messages]) else: self.write_custom_message( custom_message_id, @@ -169,7 +169,7 @@ def write_items(self) -> None: maj_loc.item_messages, True, ) - item_messages_to_custom_id[item_message_as_str] = custom_message_id + item_messages_to_custom_id[messages] = custom_message_id custom_message_id += 1 # Write total metroid count diff --git a/src/mars_patcher/locations.py b/src/mars_patcher/locations.py index 472e57c..ed3c0fd 100644 --- a/src/mars_patcher/locations.py +++ b/src/mars_patcher/locations.py @@ -1,6 +1,7 @@ from __future__ import annotations import json +from dataclasses import dataclass from typing import TYPE_CHECKING from mars_patcher.constants.items import ( @@ -86,7 +87,11 @@ def __init__( self.item_messages = item_messages +@dataclass(frozen=True) class ItemMessages: + item_messages: dict[Language, str] + centered: bool + LANG_ENUMS = { "JapaneseKanji": Language.JAPANESE_KANJI, "JapaneseHiragana": Language.JAPANESE_HIRAGANA, @@ -97,10 +102,6 @@ class ItemMessages: "Spanish": Language.SPANISH, } - def __init__(self, item_messages: dict[Language, str], centered: bool): - self.item_messages = item_messages - self.centered = centered - @classmethod def from_json(cls, data: Itemmessages) -> ItemMessages: item_messages: dict[Language, str] = {} @@ -110,9 +111,6 @@ def from_json(cls, data: Itemmessages) -> ItemMessages: centered = data.get(KEY_CENTERED, True) return cls(item_messages, centered) - def as_str(self) -> str: - return str((self.item_messages, self.centered)) - class LocationSettings: def __init__(self, major_locs: list[MajorLocation], minor_locs: list[MinorLocation]): From 719a7c8cc65e5a94c25e79d55185b19b167135f0 Mon Sep 17 00:00:00 2001 From: Miepee Date: Fri, 14 Feb 2025 20:47:36 +0100 Subject: [PATCH 5/6] use frozendict --- pyproject.toml | 3 ++- src/mars_patcher/locations.py | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 3af9438..5c46cac 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -21,6 +21,7 @@ classifiers = [ ] dependencies = [ "jsonschema", + "frozendict", ] [project.optional-dependencies] @@ -65,4 +66,4 @@ files = [ follow_imports = "silent" disallow_untyped_defs = true warn_return_any = true -warn_unreachable = true \ No newline at end of file +warn_unreachable = true diff --git a/src/mars_patcher/locations.py b/src/mars_patcher/locations.py index ed3c0fd..fac0a9e 100644 --- a/src/mars_patcher/locations.py +++ b/src/mars_patcher/locations.py @@ -4,6 +4,8 @@ from dataclasses import dataclass from typing import TYPE_CHECKING +from frozendict import frozendict + from mars_patcher.constants.items import ( ITEM_ENUMS, ITEM_SPRITE_ENUMS, @@ -89,7 +91,7 @@ def __init__( @dataclass(frozen=True) class ItemMessages: - item_messages: dict[Language, str] + item_messages: frozendict[Language, str] centered: bool LANG_ENUMS = { @@ -109,7 +111,7 @@ def from_json(cls, data: Itemmessages) -> ItemMessages: lang = cls.LANG_ENUMS[lang_name] item_messages[lang] = message centered = data.get(KEY_CENTERED, True) - return cls(item_messages, centered) + return cls(frozendict(item_messages), centered) class LocationSettings: From b442d2468a76f1591cd34ebd4e4b4c673ff4dd48 Mon Sep 17 00:00:00 2001 From: Miepee Date: Fri, 14 Feb 2025 21:04:02 +0100 Subject: [PATCH 6/6] use classvar --- src/mars_patcher/locations.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mars_patcher/locations.py b/src/mars_patcher/locations.py index fac0a9e..244bf27 100644 --- a/src/mars_patcher/locations.py +++ b/src/mars_patcher/locations.py @@ -2,7 +2,7 @@ import json from dataclasses import dataclass -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, ClassVar from frozendict import frozendict @@ -94,7 +94,7 @@ class ItemMessages: item_messages: frozendict[Language, str] centered: bool - LANG_ENUMS = { + LANG_ENUMS: ClassVar[dict[str, Language]] = { "JapaneseKanji": Language.JAPANESE_KANJI, "JapaneseHiragana": Language.JAPANESE_HIRAGANA, "English": Language.ENGLISH,