# HG changeset patch # User Emmanuel Gil Peyrot # Date 1661877710 -7200 # Node ID 79c3f782dd4109b827dc49d111406f11719c0513 # Parent f6c287745a67596376d0da873402b75dee28a844 Python: Replace the PBG3 loader with Rust’s diff --git a/pytouhou/resource/loader.py b/pytouhou/resource/loader.py --- a/pytouhou/resource/loader.py +++ b/pytouhou/resource/loader.py @@ -15,9 +15,10 @@ import os from glob import glob from itertools import chain +from io import BytesIO from pytouhou.formats import WrongFormatError -from pytouhou.formats.pbg3 import PBG3 +from libtouhou import PBG3 from pytouhou.formats.std import Stage from pytouhou.formats.ecl import ECL from pytouhou.formats.anm0 import ANM0 @@ -46,6 +47,10 @@ class Directory: return False + @property + def file_list(self): + return self.list_files() + def list_files(self): file_list = [] for path in os.listdir(self.path): @@ -62,34 +67,16 @@ class Directory: class ArchiveDescription: _formats = {b'PBG3': PBG3} - def __init__(self, path, format_class, file_list=None): - self.path = path - self.format_class = format_class - self.file_list = file_list or [] - - - def open(self): - if self.format_class is Directory: - return self.format_class(self.path) - - file = open(self.path, 'rb') - instance = self.format_class.read(file) - return instance - - @classmethod def get_from_path(cls, path): if os.path.isdir(path): instance = Directory(path) file_list = instance.list_files() - return cls(path, Directory, file_list) + return instance with open(path, 'rb') as file: magic = file.read(4) - file.seek(0) format_class = cls._formats[magic] - instance = format_class.read(file) - file_list = instance.list_files() - return cls(path, format_class, file_list) + return format_class.from_filename(path) @@ -122,8 +109,11 @@ class Loader: def get_file(self, name): - with self.known_files[name].open() as archive: - return archive.get_file(name) + archive = self.known_files[name] + file = archive.get_file(name) + if isinstance(file, bytes): + return BytesIO(file) + return file def get_anm(self, name):