secure sprite table fill with threadlock

This commit is contained in:
Fabian Dill 2020-08-22 02:56:33 +02:00
parent 99c2185410
commit 8e85e2892a
2 changed files with 16 additions and 14 deletions

View File

@ -161,7 +161,7 @@ def main(args=None, callback = ERmain):
if path:
try:
settings = settings_cache[path] if settings_cache[path] else roll_settings(weights_cache[path])
if settings.sprite is not None and not os.path.isfile(settings.sprite) and not get_sprite_from_name(
if settings.sprite and not os.path.isfile(settings.sprite) and not get_sprite_from_name(
settings.sprite):
logging.warning(
f"Warning: The chosen sprite, \"{settings.sprite}\", for yaml \"{path}\", does not exist.")

12
Rom.py
View File

@ -336,21 +336,23 @@ def patch_enemizer(world, player: int, rom: LocalRom, enemizercli, random_sprite
except OSError:
pass
sprite_list_lock = threading.Lock()
_sprite_table = {}
def _populate_sprite_table():
with sprite_list_lock:
if not _sprite_table:
def load_sprite_from_file(file):
filepath = os.path.join(dir, file)
sprite = Sprite(filepath)
sprite = Sprite(file)
if sprite.valid:
_sprite_table[sprite.name.lower()] = sprite
_sprite_table[os.path.basename(filepath).split(".")[0]] = sprite # alias for filename base
_sprite_table[os.path.basename(file).split(".")[0].lower()] = sprite # alias for filename base
with concurrent.futures.ThreadPoolExecutor() as pool:
for dir in [local_path('data/sprites/alttpr'), local_path('data/sprites/custom')]:
for file in os.listdir(dir):
pool.submit(load_sprite_from_file, file)
pool.submit(load_sprite_from_file, os.path.join(dir, file))
def get_sprite_from_name(name, local_random=random):
_populate_sprite_table()