diff --git a/Utils.py b/Utils.py new file mode 100644 index 0000000..e8fd73d --- /dev/null +++ b/Utils.py @@ -0,0 +1,81 @@ +import os +import subprocess +import sys +import typing +import functools + +from yaml import load, dump + +try: + from yaml import CLoader as Loader +except ImportError: + from yaml import Loader + +import xml.etree.ElementTree as ET + +def int16_as_bytes(value): + value = value & 0xFFFF + return [value & 0xFF, (value >> 8) & 0xFF] + + +def int32_as_bytes(value): + value = value & 0xFFFFFFFF + return [value & 0xFF, (value >> 8) & 0xFF, (value >> 16) & 0xFF, (value >> 24) & 0xFF] + + + +def is_bundled(): + return getattr(sys, 'frozen', False) + +def local_path(path): + if local_path.cached_path: + return os.path.join(local_path.cached_path, path) + + elif is_bundled(): + if hasattr(sys, "_MEIPASS"): + # we are running in a PyInstaller bundle + local_path.cached_path = sys._MEIPASS # pylint: disable=protected-access,no-member + else: + # cx_Freeze + local_path.cached_path = os.path.dirname(os.path.abspath(sys.argv[0])) + else: + # we are running in a normal Python environment + import __main__ + local_path.cached_path = os.path.dirname(os.path.abspath(__main__.__file__)) + + return os.path.join(local_path.cached_path, path) + +local_path.cached_path = None + + + +def make_new_base2current(old_rom='../alttp.sfc', new_rom='../working.sfc'): + from collections import OrderedDict + import json + import hashlib + with open(old_rom, 'rb') as stream: + old_rom_data = bytearray(stream.read()) + with open(new_rom, 'rb') as stream: + new_rom_data = bytearray(stream.read()) + # extend to 2 mb + old_rom_data.extend(bytearray([0x00]) * (2097152 - len(old_rom_data))) + + out_data = OrderedDict() + for idx, old in enumerate(old_rom_data): + new = new_rom_data[idx] + if old != new: + out_data[idx] = [int(new)] + for offset in reversed(list(out_data.keys())): + if offset - 1 in out_data: + out_data[offset-1].extend(out_data.pop(offset)) + with open('../base2current.json', 'wt') as outfile: + json.dump([{key: value} for key, value in out_data.items()], outfile, separators=(",", ":")) + + basemd5 = hashlib.md5() + basemd5.update(new_rom_data) + return "New Rom Hash: " + basemd5.hexdigest() + + +if __name__ == '__main__': + print(make_new_base2current()) +