Archipelago/worlds/smz3/Rom.py

91 lines
2.8 KiB
Python
Raw Normal View History

2022-03-18 03:53:09 +00:00
import hashlib
import os
2022-03-15 12:55:57 +00:00
import Utils
from Utils import read_snes_rom
from worlds.Files import APDeltaPatch
2022-03-15 12:55:57 +00:00
SMJUHASH = '21f3e98df4780ee1c667b84e57d88675'
LTTPJPN10HASH = '03a63945398191337e896e5771f77173'
2022-03-15 12:55:57 +00:00
ROM_PLAYER_LIMIT = 256
2022-03-18 03:53:09 +00:00
class SMZ3DeltaPatch(APDeltaPatch):
hash = "3a177ba9879e3dd04fb623a219d175b2"
game = "SMZ3"
patch_file_ending = ".apsmz3"
2022-03-18 03:53:09 +00:00
@classmethod
def get_source_data(cls) -> bytes:
return get_base_rom_bytes()
2022-03-15 12:55:57 +00:00
2022-03-18 03:53:09 +00:00
def get_base_rom_bytes() -> bytes:
2022-03-15 12:55:57 +00:00
base_rom_bytes = getattr(get_base_rom_bytes, "base_rom_bytes", None)
if not base_rom_bytes:
sm_file_name = get_sm_base_rom_path()
sm_base_rom_bytes = bytes(read_snes_rom(open(sm_file_name, "rb")))
2022-03-15 12:55:57 +00:00
basemd5 = hashlib.md5()
basemd5.update(sm_base_rom_bytes)
if SMJUHASH != basemd5.hexdigest():
raise Exception('Supplied Base Rom does not match known MD5 for SM Japan+US release. '
2022-03-15 12:55:57 +00:00
'Get the correct game and version, then dump it')
lttp_file_name = get_lttp_base_rom_path()
lttp_base_rom_bytes = bytes(read_snes_rom(open(lttp_file_name, "rb")))
2022-03-15 12:55:57 +00:00
basemd5 = hashlib.md5()
basemd5.update(lttp_base_rom_bytes)
if LTTPJPN10HASH != basemd5.hexdigest():
raise Exception('Supplied Base Rom does not match known MD5 for LttP Japan(1.0) release. '
2022-03-15 12:55:57 +00:00
'Get the correct game and version, then dump it')
2022-03-18 03:53:09 +00:00
2022-03-15 12:55:57 +00:00
get_base_rom_bytes.base_rom_bytes = bytes(combine_smz3_rom(sm_base_rom_bytes, lttp_base_rom_bytes))
return get_base_rom_bytes.base_rom_bytes
2022-03-18 03:53:09 +00:00
2022-03-15 12:55:57 +00:00
def get_sm_base_rom_path(file_name: str = "") -> str:
options = Utils.get_options()
if not file_name:
file_name = options["sm_options"]["rom_file"]
if not os.path.exists(file_name):
2022-03-31 03:08:15 +00:00
file_name = Utils.user_path(file_name)
2022-03-15 12:55:57 +00:00
return file_name
2022-03-18 03:53:09 +00:00
2022-03-15 12:55:57 +00:00
def get_lttp_base_rom_path(file_name: str = "") -> str:
options = Utils.get_options()
if not file_name:
file_name = options["lttp_options"]["rom_file"]
if not os.path.exists(file_name):
2022-03-31 03:08:15 +00:00
file_name = Utils.user_path(file_name)
2022-03-15 12:55:57 +00:00
return file_name
2022-03-18 03:53:09 +00:00
def combine_smz3_rom(sm_rom: bytes, lttp_rom: bytes) -> bytearray:
2022-03-15 12:55:57 +00:00
combined = bytearray(0x600000)
# SM hi bank
pos = 0
srcpos = 0
for i in range(0x40):
combined[pos + 0x8000:pos + 0x8000 + 0x8000] = sm_rom[srcpos:srcpos + 0x8000]
srcpos += 0x8000
pos += 0x10000
# SM lo bank
pos = 0
for i in range(0x20):
combined[pos:pos + 0x8000] = sm_rom[srcpos:srcpos + 0x8000]
srcpos += 0x8000
pos += 0x10000
# Z3 hi bank
pos = 0x400000
srcpos = 0
for i in range(0x20):
combined[pos + 0x8000:pos + 0x8000 + 0x8000] = lttp_rom[srcpos:srcpos + 0x8000]
srcpos += 0x8000
pos += 0x10000
return combined