From f27d9e904ff24ee9cf897088489fe4f72f8c458a Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Fri, 10 Jul 2020 22:43:54 +0200 Subject: [PATCH] fix multithreaded import racing condition when frozen --- Main.py | 2 +- Patch.py | 4 +++- Rom.py | 16 +++++++++------- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Main.py b/Main.py index 4947d71f..32e33a97 100644 --- a/Main.py +++ b/Main.py @@ -20,6 +20,7 @@ from Fill import distribute_items_cutoff, distribute_items_staleness, distribute balance_multiworld_progression from ItemList import generate_itempool, difficulties, fill_prizes from Utils import output_path, parse_player_names, get_options, __version__ +import Patch seeddigits = 20 @@ -226,7 +227,6 @@ def main(args, seed=None): rompath = output_path(f'{outfilebase}{outfilepname}{outfilesuffix}.sfc') rom.write_to_file(rompath) if args.create_diff: - import Patch Patch.create_patch_file(rompath) return (player, team, list(rom.name)) diff --git a/Patch.py b/Patch.py index d23a3ff8..405cd3c2 100644 --- a/Patch.py +++ b/Patch.py @@ -10,7 +10,7 @@ import sys from typing import Tuple, Optional import Utils -from Rom import JAP10HASH, read_rom +from Rom import JAP10HASH def get_base_rom_path(file_name: str = "") -> str: @@ -23,6 +23,7 @@ def get_base_rom_path(file_name: str = "") -> str: def get_base_rom_bytes(file_name: str = "") -> bytes: + from Rom import read_rom base_rom_bytes = getattr(get_base_rom_bytes, "base_rom_bytes", None) if not base_rom_bytes: file_name = get_base_rom_path(file_name) @@ -160,3 +161,4 @@ if __name__ == "__main__": import traceback traceback.print_exc() input("Press enter to close.") + diff --git a/Rom.py b/Rom.py index 8d923740..a5060ef4 100644 --- a/Rom.py +++ b/Rom.py @@ -1,4 +1,6 @@ -import bisect +JAP10HASH = '03a63945398191337e896e5771f77173' +RANDOMIZERBASEHASH = 'a567da86e8bd499256da4bba2209a3fd' + import io import json import hashlib @@ -19,10 +21,9 @@ from Text import KingsReturn_texts, Sanctuary_texts, Kakariko_texts, Blacksmiths from Utils import output_path, local_path, int16_as_bytes, int32_as_bytes, snes_to_pc from Items import ItemFactory from EntranceShuffle import door_addresses +import Patch -JAP10HASH = '03a63945398191337e896e5771f77173' -RANDOMIZERBASEHASH = 'a567da86e8bd499256da4bba2209a3fd' class LocalRom(object): @@ -65,7 +66,7 @@ class LocalRom(object): return expected == buffermd5.hexdigest() def patch_base_rom(self): - from Patch import create_patch_file, create_rom_bytes + if os.path.isfile(local_path('basepatch.sfc')): with open(local_path('basepatch.sfc'), 'rb') as stream: @@ -74,11 +75,11 @@ class LocalRom(object): if self.verify(buffer): self.buffer = buffer if not os.path.exists(local_path(os.path.join('data', 'basepatch.bmbp'))): - create_patch_file(local_path('basepatch.sfc')) + Patch.create_patch_file(local_path('basepatch.sfc')) return if os.path.isfile(local_path(os.path.join('data', 'basepatch.bmbp'))): - _, target, buffer = create_rom_bytes(local_path(os.path.join('data', 'basepatch.bmbp'))) + _, target, buffer = Patch.create_rom_bytes(local_path(os.path.join('data', 'basepatch.bmbp'))) if self.verify(buffer): self.buffer = bytearray(buffer) with open(local_path('basepatch.sfc'), 'wb') as stream: @@ -105,7 +106,8 @@ class LocalRom(object): if self.verify(self.buffer): with open(local_path('basepatch.sfc'), 'wb') as stream: stream.write(self.buffer) - create_patch_file(local_path('basepatch.sfc'), destination=local_path(os.path.join('data', 'basepatch.bmbp'))) + Patch.create_patch_file(local_path('basepatch.sfc'), + destination=local_path(os.path.join('data', 'basepatch.bmbp'))) os.remove(local_path('data/base2current.json')) else: raise RuntimeError(