shithub: zelda3

Download patch

ref: 6dc3c0d1085b6a58417dd2f663ad46c9bc3a8c0d
parent: 70ba5d1d963b61a309735e9f56f36f156883927a
author: Keaton Greve <keatongreve@gmail.com>
date: Tue Aug 30 18:15:23 EDT 2022

Improve flexibility of resource generation script ROM paths (#27)

changes:
1. Allows the user to copy in a ROM titled zelda3.smc, which is another common extension for SNES ROMs (file content should still be the same).
2. Allows the user to specify a location of the ROM file instead of copying the ROM to the directory where the script is located.
3. Adds and improves error messaging.

--- a/.gitignore
+++ b/.gitignore
@@ -9,6 +9,8 @@
 /tables/dungeon/*.yaml
 /tables/img/
 /tables/old/
+/tables/zelda3.sfc
+/tables/zelda3.smc
 /saves/*.sav
 /.vs/
 __pycache__
--- a/tables/extract_resources.py
+++ b/tables/extract_resources.py
@@ -7,10 +7,22 @@
 import yaml
 import extract_music
 import os
+import getopt
 
 PATH=''
 
-ROM = util.LoadedRom()
+option_list, args = getopt.getopt(sys.argv[1:], 'r:', ['rom_path='])
+rom_path = None
+for opt, arg in option_list:
+  if opt in ('-r', '--rom_path'):
+    rom_path = arg
+
+try:
+  ROM = util.LoadedRom(rom_path)
+except Exception as error:
+  print(f"Failed to load ROM data for extraction. Error: {error}")
+  sys.exit(1)
+
 
 get_byte = ROM.get_byte
 get_word = ROM.get_word
--- a/tables/util.py
+++ b/tables/util.py
@@ -1,13 +1,20 @@
 import array
 import sys
 import hashlib
+import os
 
+# Both are common SNES rom extensions. For Zelda3 (NA), they are equivalent files.
+COMMON_ROM_NAMES = ['zelda3.sfc', 'zelda3.smc']
+DEFAULT_ROM_DIRECTORY = os.path.dirname(__file__)
+ZELDA3_SHA256 = '66871d66be19ad2c34c927d6b14cd8eb6fc3181965b6e517cb361f7316009cfb'
+
 class LoadedRom:
-  def __init__(self, name = 'zelda3.sfc'):
-    self.ROM = open(name, 'rb').read()
+  def __init__(self, path = None):
+    rom_path = self.__get_rom_path(path)
+    self.ROM = open(rom_path, 'rb').read()
     hash = hashlib.sha256(self.ROM).hexdigest() 
-    if hash != '66871d66be19ad2c34c927d6b14cd8eb6fc3181965b6e517cb361f7316009cfb':
-      raise Exception('Wrong ROM version. Expecting Legend of Zelda - A Link to the Past, The (NA) (1.0).sfc')
+    if hash != ZELDA3_SHA256:
+      raise Exception(f"ROM with hash {hash} not supported. Expected {ZELDA3_SHA256}. Please verify your ROM is the NA 1.0 version.");
 
   def get_byte(self, ea):
     assert (ea & 0x8000)
@@ -37,6 +44,22 @@
       if (addr & 0x8000) == 0:
         addr += 0x8000
     return r
+
+  def __get_rom_path(self, path = None):
+    # Check default locations when no path is given by user.
+    if path is None:
+      for rom_name in COMMON_ROM_NAMES:
+        rom_path = os.path.join(DEFAULT_ROM_DIRECTORY, rom_name)
+        if os.path.isfile(rom_path):
+          return rom_path
+      raise Exception(f"Could not find any ROMs ({', '.join(COMMON_ROM_NAMES)}) at the default location {DEFAULT_ROM_DIRECTORY}.") 
+
+    rom_path = os.path.join(DEFAULT_ROM_DIRECTORY, path)
+    if os.path.isfile(rom_path):
+      return rom_path
+    raise Exception(f"No ROM found at provided path {rom_path}.")
+
+
 
 def split_list(l, n):
   return [l[i:i+n] for i in range(0, len(l), n)]