From 63cc3f948a55521ede6dbf7c1a1b9f49bce2b729 Mon Sep 17 00:00:00 2001 From: lil David <1337lilDavid@gmail.com> Date: Sun, 1 Sep 2024 13:18:50 -0500 Subject: [PATCH] Verify symbol data hash when patching --- data.py | 12 +++++++++--- rom.py | 14 +++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/data.py b/data.py index 836286a..152512b 100644 --- a/data.py +++ b/data.py @@ -1,5 +1,6 @@ from __future__ import annotations +import hashlib from io import StringIO import itertools import json @@ -89,15 +90,20 @@ def data_path(file_name: str): 6, 6, 5, 6, 5, 6, 6, 6, 6, 6, 6, 8, 8, 8, 8, 8 ] +symbols_hash = None ram_symbols = None rom_symbols = None def _get_symbols(): - global ram_symbols, rom_symbols + global ram_symbols, rom_symbols, symbols_hash - symbol_data = data_path("extracted_symbols.json").decode("utf-8") - symbols = json.loads(symbol_data) + symbol_data = data_path("extracted_symbols.json") + hasher = hashlib.md5() + hasher.update(symbol_data) + symbols_hash = hasher.hexdigest() + + symbols = json.loads(symbol_data.decode("utf-8")) ram_symbols = symbols["ewram"] | symbols["iwram"] rom_symbols = symbols["rom"] diff --git a/rom.py b/rom.py index 89fc788..2ef49aa 100644 --- a/rom.py +++ b/rom.py @@ -5,14 +5,14 @@ from pathlib import Path import struct -from typing import TYPE_CHECKING, Sequence, Tuple +from typing import TYPE_CHECKING, Sequence from BaseClasses import ItemClassification, Location import Utils -from worlds.Files import APPatchExtension, APProcedurePatch, APTokenMixin, APTokenTypes +from worlds.Files import APPatchExtension, APProcedurePatch, APTokenMixin, APTokenTypes, InvalidDataError from . import rom_data -from .data import encode_str, get_rom_address, get_symbol, get_width_of_encoded_string +from .data import encode_str, get_rom_address, get_width_of_encoded_string, symbols_hash from .items import AP_MZM_ID_BASE, ItemID, ItemType, item_data_table from .nonnative_items import get_zero_mission_sprite from .options import ChozodiaAccess, DisplayNonLocalItems, Goal @@ -27,6 +27,13 @@ class MZMPatchExtensions(APPatchExtension): game = "Metroid Zero Mission" + @staticmethod + def check_symbol_hash(caller: APProcedurePatch, rom: bytes, hash: str): + if hash != symbols_hash: + raise InvalidDataError("Memory addresses don't match. This patch was generated with a " + "different version of the apworld.") + return rom + @staticmethod def add_decompressed_graphics(caller: APProcedurePatch, rom: bytes): return rom_data.add_item_sprites(rom) @@ -53,6 +60,7 @@ class MZMProcedurePatch(APProcedurePatch, APTokenMixin): def __init__(self, *args, **kwargs): super(MZMProcedurePatch, self).__init__(*args, **kwargs) self.procedure = [ + ("check_symbol_hash", [symbols_hash]), ("apply_bsdiff4", ["basepatch.bsdiff"]), ("apply_tokens", ["token_data.bin"]), ("add_decompressed_graphics", []),