Skip to content

Commit

Permalink
Verify symbol data hash when patching
Browse files Browse the repository at this point in the history
  • Loading branch information
lilDavid committed Sep 1, 2024
1 parent 9b8f65a commit 63cc3f9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
12 changes: 9 additions & 3 deletions data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import hashlib
from io import StringIO
import itertools
import json
Expand Down Expand Up @@ -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"]

Expand Down
14 changes: 11 additions & 3 deletions rom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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", []),
Expand Down

0 comments on commit 63cc3f9

Please sign in to comment.