From 355c2cff9e773157dbb202f0938bd9762beaa99e Mon Sep 17 00:00:00 2001 From: Lucent Date: Wed, 11 Oct 2023 11:51:12 +0200 Subject: [PATCH] fix CRC computation --- GUI.py | 17 ++++++++++------- Randomizer.py | 24 ++++++++++++------------ 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/GUI.py b/GUI.py index 5ad67b0..c200b01 100644 --- a/GUI.py +++ b/GUI.py @@ -20,11 +20,15 @@ tt_SectorShuffle = ' Randomly shuffle the arrangement of the Sectors. ' tt_HideItems = ' Make all items appear as ? tanks. ' generating = False -def fileHash(file): - checksum = 0 - return checksum -# WARNING: Decompyle incomplete - +def fileHash(fileName): + with open(fileName, 'rb') as fh: + hash = 0 + while True: + s = fh.read(65536) + if not s: + break + hash = zlib.crc32(s, hash) + return hash & 0xFFFFFFFF def rando_thread(window = None, values = None): global failedgen, randoerror, generating, fileName, basegame, failedgen, randoerror, generating, finishGenerating @@ -236,8 +240,7 @@ def main_window(debug): ui.popup('Please select a Metroid Fusion (U) rom.', title='No source rom selected') if basegame != None and basegame != '': checksum = fileHash(basegame) - # if checksum != 1819625372: - if checksum != 0: + if checksum != 1819625372: ui.popup('Only Metroid Fusion (U) is supported.\nCheck the CRC32 value: it should be 6C75479C', title='Bad Checksum') else: values.update({ diff --git a/Randomizer.py b/Randomizer.py index 2338551..f0bb882 100644 --- a/Randomizer.py +++ b/Randomizer.py @@ -18,12 +18,15 @@ version = '0.11.6' def ceiling(num, denom): return -(num // -denom) - -def fileHash(file): - checksum = 0 - return checksum -# WARNING: Decompyle incomplete - +def fileHash(fileName): + with open(fileName, 'rb') as fh: + hash = 0 + while True: + s = fh.read(65536) + if not s: + break + hash = zlib.crc32(s, hash) + return (hash & 0xFFFFFFFF) def enable_item(graph, item): globals()[item] = True @@ -4665,8 +4668,7 @@ def patch_game(): # FIXME: windows? os.system(os.path.join('.', 'flips', 'flips') + ' --apply ' + os.path.join('.', 'data', 'MFOR.bps') + ' "' + BaseName + '" "'+ os.path.join('.', 'seeds', '{}.gba'.format(FileName)) + '"') checksum = fileHash(os.path.join('.', 'seeds', '{}.gba'.format(FileName))) - # if checksum != 0x92561217L: - if checksum != 0: + if checksum != 2455114263: os.remove(os.path.join('.', 'seeds', '{}.gba'.format(FileName))) print('Error: Base patch file has been modified. Please go to https://metroidconstruction.com/ and re-download MFOR.') sys.exit(1) @@ -4688,16 +4690,14 @@ def start_randomizer(rom, settings): print('Error: no base game provided.') sys.exit(1) checksum = fileHash(BaseGame) - # if checksum != 1819625372: - if checksum != 0: + if checksum != 1819625372: print('Only Metroid Fusion (U) is supported. Check the CRC32 value: it should be 6C75479C') sys.exit(1) Debug = settings['Debug'] settings.pop('Debug') if Debug == False: checksum = fileHash(os.path.join('.', 'data', 'MFOR.bps')) - # if checksum != 558161692: - if checksum != 0: + if checksum != 558161692: print('Error: Base patch file has been modified. Please go to https://metroidconstruction.com/ and re-download MFOR.') sys.exit(1) totalRandoTime = time.time()