From 9d71f808e3d3a10591f04cbb3f743d665fec764d Mon Sep 17 00:00:00 2001 From: Fenhl Date: Sat, 23 Mar 2024 10:01:53 +0000 Subject: [PATCH] Fix mypy errors in Location.py and Messages.py --- Location.py | 6 +++++- Messages.py | 51 ++++++++++++++++++++------------------------------- Patches.py | 11 ++++++----- 3 files changed, 31 insertions(+), 37 deletions(-) diff --git a/Location.py b/Location.py index db1532e6d..bae096483 100644 --- a/Location.py +++ b/Location.py @@ -174,7 +174,11 @@ def LocationFactory(locations: str | list[str]) -> Location | list[Location]: if location in location_table: match_location = location else: - match_location = next(filter(lambda k: k.lower() == location.lower(), location_table), None) + match_location = None + for k in location_table: + if k.lower() == location.lower(): + match_location = k + break if match_location is not None: type, scene, default, addresses, vanilla_item, filter_tags = location_table[match_location] if addresses is None: diff --git a/Messages.py b/Messages.py index 139d535f9..f131b85a0 100644 --- a/Messages.py +++ b/Messages.py @@ -559,7 +559,7 @@ # convert byte array to an integer -def bytes_to_int(data: bytes, signed: bool = False) -> int: +def bytes_to_int(data: bytes | list[int], signed: bool = False) -> int: return int.from_bytes(data, byteorder='big', signed=signed) @@ -697,7 +697,7 @@ def write(self, rom: Rom, text_start: int, offset: int) -> int: class Message: def __init__(self, raw_text: list[int] | bytearray | str, index: int, id: int, opts: int, offset: int, length: int) -> None: if isinstance(raw_text, str): - raw_text = encode_text_string(raw_text) + raw_text = bytearray(encode_text_string(raw_text)) elif not isinstance(raw_text, bytearray): raw_text = bytearray(raw_text) @@ -924,7 +924,7 @@ def update_message_by_id(messages: list[Message], id: int, text: bytearray | str if index >= 0: update_message_by_index(messages, index, text, opts) else: - add_message(messages, text, id, opts) + add_message(messages, text, id, opts or 0x00) # Gets the message by its ID. Returns None if the index does not exist @@ -1002,7 +1002,7 @@ def display(self) -> str: def write(self, rom: Rom, shop_table_address: int, index: int) -> None: entry_offset = shop_table_address + 0x20 * index - data = [] + data: list[int] = [] data += int_to_bytes(self.object, 2) data += int_to_bytes(self.model, 2) data += int_to_bytes(self.func1, 4) @@ -1138,14 +1138,14 @@ def make_player_message(text: str) -> str: # make sure to call this AFTER move_shop_item_messages() def update_item_messages(messages: list[Message], world: World) -> None: new_item_messages = ITEM_MESSAGES + KEYSANITY_MESSAGES - for id, text in new_item_messages: + for id, new_item_text in new_item_messages: if world.settings.world_count > 1: - update_message_by_id(messages, id, make_player_message(text), 0x23) + update_message_by_id(messages, id, make_player_message(new_item_text), 0x23) else: - update_message_by_id(messages, id, text, 0x23) + update_message_by_id(messages, id, new_item_text, 0x23) - for id, (text, opt) in MISC_MESSAGES: - update_message_by_id(messages, id, text, opt) + for id, (misc_text, opt) in MISC_MESSAGES: + update_message_by_id(messages, id, misc_text, opt) # run all keysanity related patching to add messages for dungeon specific items def add_item_messages(messages: list[Message], shop_items: Iterable[ShopItem], world: World) -> None: @@ -1200,7 +1200,7 @@ def read_fffc_message(rom: Rom) -> Message: # write the messages back -def repack_messages(rom: Rom, messages: list[Message], permutation: Optional[list[int]] = None, +def repack_messages(rom: Rom, messages: list[Message], permutation: Optional[Iterable[int]] = None, always_allow_skip: bool = True, speed_up_text: bool = True) -> None: rom.update_dmadata_record_by_key(ENG_TEXT_START, ENG_TEXT_START, ENG_TEXT_START + ENG_TEXT_SIZE_LIMIT) rom.update_dmadata_record_by_key(JPN_TEXT_START, JPN_TEXT_START, JPN_TEXT_START + JPN_TEXT_SIZE_LIMIT) @@ -1288,21 +1288,9 @@ def repack_messages(rom: Rom, messages: list[Message], permutation: Optional[lis # shuffles the messages in the game, making sure to keep various message types in their own group +SHOP_ITEM_MESSAGES: list[int] = [] +SCRUBS_MESSAGE_IDS: list[int] = [] def shuffle_messages(messages: list[Message], except_hints: bool = True) -> list[int]: - if not hasattr(shuffle_messages, "shop_item_messages"): - shuffle_messages.shop_item_messages = [] - if not hasattr(shuffle_messages, "scrubs_message_ids"): - shuffle_messages.scrubs_message_ids = [] - - hint_ids = ( - GOSSIP_STONE_MESSAGES + TEMPLE_HINTS_MESSAGES + - [data['id'] for data in misc_item_hint_table.values()] + - [data['id'] for data in misc_location_hint_table.values()] + - [message_id for (message_id, message) in KEYSANITY_MESSAGES] + shuffle_messages.shop_item_messages + - shuffle_messages.scrubs_message_ids + - [0x5036, 0x70F5] # Chicken count and poe count respectively - ) - permutation = [i for i, _ in enumerate(messages)] def is_exempt(m: Message) -> bool: @@ -1310,9 +1298,8 @@ def is_exempt(m: Message) -> bool: GOSSIP_STONE_MESSAGES + TEMPLE_HINTS_MESSAGES + [data['id'] for data in misc_item_hint_table.values()] + [data['id'] for data in misc_location_hint_table.values()] + - [message_id for (message_id, message) in KEYSANITY_MESSAGES] + - shuffle_messages.shop_item_messages + - shuffle_messages.scrubs_message_ids + + [message_id for message_id, _ in KEYSANITY_MESSAGES] + + SHOP_ITEM_MESSAGES + SCRUBS_MESSAGE_IDS + [0x5036, 0x70F5] # Chicken count and poe count respectively ) shuffle_exempt = [ @@ -1320,9 +1307,9 @@ def is_exempt(m: Message) -> bool: 0x208D, # "One more lap!" for Cow in House race. 0xFFFC, # Character data from JP table used on title and file select screens ] - is_hint = (except_hints and m.id in hint_ids) - is_error_message = (m.id == ERROR_MESSAGE) - is_shuffle_exempt = (m.id in shuffle_exempt) + is_hint = except_hints and m.id in hint_ids + is_error_message = m.id == ERROR_MESSAGE + is_shuffle_exempt = m.id in shuffle_exempt return is_hint or is_error_message or m.is_id_message() or is_shuffle_exempt have_goto = list(filter(lambda m: not is_exempt(m) and m.has_goto, messages)) @@ -1373,7 +1360,8 @@ def update_warp_song_text(messages: list[Message], world: World) -> None: for id, entr in msg_list.items(): if 'warp_songs_and_owls' in world.settings.misc_hints or not world.settings.warp_songs: destination = world.get_entrance(entr).connected_region - destination_name = HintArea.at(destination) + assert destination is not None + destination_name: Any = HintArea.at(destination) color = COLOR_MAP[destination_name.color] if destination_name.preposition(True) is not None: destination_name = f'to {destination_name}' @@ -1388,6 +1376,7 @@ def update_warp_song_text(messages: list[Message], world: World) -> None: for id, entr in owl_messages.items(): if 'warp_songs_and_owls' in world.settings.misc_hints: destination = world.get_entrance(entr).connected_region + assert destination is not None destination_name = HintArea.at(destination) color = COLOR_MAP[destination_name.color] if destination_name.preposition(True) is not None: diff --git a/Patches.py b/Patches.py index bcfbb3bb4..89bbf0a92 100644 --- a/Patches.py +++ b/Patches.py @@ -17,7 +17,8 @@ from ItemPool import song_list, trade_items, child_trade_items from Location import Location, DisableType from LocationList import business_scrubs -from Messages import read_messages, update_message_by_id, read_shop_items, update_warp_song_text, \ +import Messages +from Messages import SCRUBS_MESSAGE_IDS, SHOP_ITEM_MESSAGES, read_messages, update_message_by_id, read_shop_items, update_warp_song_text, \ write_shop_items, remove_unused_messages, make_player_message, \ add_item_messages, repack_messages, shuffle_messages, \ get_message_by_id, TextCode, new_messages @@ -1988,7 +1989,7 @@ def calculate_traded_flags(world): shop_items[0x000A].description_message = 0x80B5 shop_items[0x000A].purchase_message = 0x80BE - shuffle_messages.shop_item_messages = [] + Messages.SHOP_ITEM_MESSAGES = [] # kokiri shop shop_locations = [location for location in world.get_region('KF Kokiri Shop').locations if location.type == 'Shop'] # Need to filter because of the freestanding item in KF Shop @@ -2115,11 +2116,11 @@ def update_scrub_text(message: bytearray, text_replacement: list[str], default_p set_deku_salesman_data(rom) # Update scrub messages. - shuffle_messages.scrubs_message_ids = [] + Messages.SCRUBS_MESSAGE_IDS = [] for text_id, message in scrub_message_dict.items(): update_message_by_id(messages, text_id, message) if world.settings.shuffle_scrubs == 'random': - shuffle_messages.scrubs_message_ids.append(text_id) + Messages.SCRUBS_MESSAGE_IDS.append(text_id) if world.settings.shuffle_grotto_entrances: # Build the Grotto Load Table based on grotto entrance data @@ -3071,7 +3072,7 @@ def place_shop_items(rom: Rom, world: World, shop_items, messages, locations, in shop_item.description_message = 0x8100 + message_id shop_item.purchase_message = 0x8100 + message_id + 1 - shuffle_messages.shop_item_messages.extend( + Messages.SHOP_ITEM_MESSAGES.extend( [shop_item.description_message, shop_item.purchase_message]) if item_display.dungeonitem: