2020-03-28 02:30:19 +00:00
|
|
|
from os import path
|
|
|
|
import sys
|
|
|
|
import struct
|
2020-03-30 23:38:51 +00:00
|
|
|
import hashlib
|
2020-03-28 02:30:19 +00:00
|
|
|
|
2020-03-31 20:41:02 +00:00
|
|
|
|
2020-04-25 00:34:00 +00:00
|
|
|
def get_str_hash(byte_array):
|
|
|
|
return str(hashlib.md5(byte_array).hexdigest())
|
|
|
|
|
|
|
|
|
|
|
|
# If the baserom exists and is correct, we don't need to change anything
|
|
|
|
if path.exists("baserom.z64"):
|
|
|
|
with open("baserom.z64", mode="rb") as file:
|
|
|
|
fileContent = bytearray(file.read())
|
|
|
|
if get_str_hash(fileContent) == "f0b7f35375f9cc8ca1b2d59d78e35405":
|
|
|
|
print("Found valid baserom - exiting early")
|
|
|
|
sys.exit(0)
|
|
|
|
|
2020-03-31 20:41:02 +00:00
|
|
|
# Determine if we have a ROM file
|
2021-11-01 18:21:05 +00:00
|
|
|
romFileExtensions = ["z64", "n64", "v64"]
|
|
|
|
|
|
|
|
def find_baserom_original():
|
|
|
|
for romFileExtLower in romFileExtensions:
|
|
|
|
for romFileExt in (romFileExtLower, romFileExtLower.upper()):
|
|
|
|
romFileNameCandidate = "baserom_original." + romFileExt
|
|
|
|
if path.exists(romFileNameCandidate):
|
|
|
|
return romFileNameCandidate
|
|
|
|
return None
|
|
|
|
|
|
|
|
romFileName = find_baserom_original()
|
|
|
|
|
|
|
|
if romFileName is None:
|
2021-01-14 20:14:33 +00:00
|
|
|
print("Error: Could not find baserom_original.z64/baserom_original.n64/baserom_original.v64.")
|
|
|
|
sys.exit(1)
|
2020-03-31 20:41:02 +00:00
|
|
|
|
|
|
|
# Read in the original ROM
|
2021-01-14 20:14:33 +00:00
|
|
|
print("File '" + romFileName + "' found.")
|
|
|
|
with open(romFileName, mode="rb") as file:
|
|
|
|
fileContent = bytearray(file.read())
|
2020-03-30 23:38:51 +00:00
|
|
|
|
2020-03-28 02:30:19 +00:00
|
|
|
# Strip the overdump
|
|
|
|
print("Stripping overdump...")
|
2021-01-14 20:14:33 +00:00
|
|
|
fileContent = fileContent[0:0x3600000]
|
|
|
|
|
|
|
|
fileContentLen = len(fileContent)
|
|
|
|
|
|
|
|
# Check if ROM needs to be byte/word swapped
|
|
|
|
# Little-endian
|
|
|
|
if fileContent[0] == 0x40:
|
|
|
|
# Word Swap ROM
|
|
|
|
print("ROM needs to be word swapped...")
|
|
|
|
words = str(int(fileContentLen/4))
|
|
|
|
little_byte_format = "<" + words + "I"
|
|
|
|
big_byte_format = ">" + words + "I"
|
|
|
|
tmp = struct.unpack_from(little_byte_format, fileContent, 0)
|
|
|
|
struct.pack_into(big_byte_format, fileContent, 0, *tmp)
|
|
|
|
|
|
|
|
print("Word swapping done.")
|
|
|
|
|
|
|
|
# Byte-swapped
|
|
|
|
elif fileContent[0] == 0x37:
|
|
|
|
# Byte Swap ROM
|
|
|
|
print("ROM needs to be byte swapped...")
|
|
|
|
halfwords = str(int(fileContentLen/2))
|
|
|
|
little_byte_format = "<" + halfwords + "H"
|
|
|
|
big_byte_format = ">" + halfwords + "H"
|
|
|
|
tmp = struct.unpack_from(little_byte_format, fileContent, 0)
|
|
|
|
struct.pack_into(big_byte_format, fileContent, 0, *tmp)
|
|
|
|
|
|
|
|
print("Byte swapping done.")
|
2020-03-28 02:30:19 +00:00
|
|
|
|
|
|
|
# Patch the header
|
|
|
|
print("Patching header...")
|
2021-01-14 20:14:33 +00:00
|
|
|
fileContent[0x3E] = 0x50
|
2020-03-28 02:30:19 +00:00
|
|
|
|
2021-11-01 18:21:05 +00:00
|
|
|
for i in range(0x35CF000, len(fileContent)):
|
|
|
|
fileContent[i] = 0xFF
|
|
|
|
|
2020-03-30 23:38:51 +00:00
|
|
|
# Check to see if the ROM is a "vanilla" Debug ROM
|
2021-01-14 20:14:33 +00:00
|
|
|
str_hash = get_str_hash(bytearray(fileContent))
|
2020-04-25 00:34:00 +00:00
|
|
|
if str_hash != "f0b7f35375f9cc8ca1b2d59d78e35405":
|
|
|
|
print("Error: Expected a hash of f0b7f35375f9cc8ca1b2d59d78e35405 but got " + str_hash + ". " +
|
|
|
|
"The baserom has probably been tampered, find a new one")
|
2021-11-01 18:21:05 +00:00
|
|
|
|
|
|
|
if str_hash == "32fe2770c0f9b1a9cd2a4d449348c1cb":
|
|
|
|
print("The provided baserom is a rom which has been edited with ZeldaEdit and is not suitable for use with decomp. " +
|
|
|
|
"Find a new one.")
|
|
|
|
|
2020-03-30 23:38:51 +00:00
|
|
|
sys.exit(1)
|
2020-03-28 02:30:19 +00:00
|
|
|
|
|
|
|
# Write out our new ROM
|
|
|
|
print("Writing new ROM 'baserom.z64'.")
|
|
|
|
with open("baserom.z64", mode="wb") as file:
|
2021-01-14 20:14:33 +00:00
|
|
|
file.write(bytes(fileContent))
|
2020-03-28 02:30:19 +00:00
|
|
|
|
2021-01-14 20:14:33 +00:00
|
|
|
print("Done!")
|