Mercurial > touhou
diff pytouhou/resource/loader.py @ 778:816e1f01d650
Partially replace the Loader with a Rust one
| author | Link Mauve <linkmauve@linkmauve.fr> |
|---|---|
| date | Sat, 08 Nov 2025 18:26:01 +0100 |
| parents | 79c3f782dd41 |
| children | ee09657d3789 |
line wrap: on
line diff
--- a/pytouhou/resource/loader.py +++ b/pytouhou/resource/loader.py @@ -12,13 +12,7 @@ ## GNU General Public License for more details. ## -import os -from glob import glob -from itertools import chain -from io import BytesIO - -from pytouhou.formats import WrongFormatError -from libtouhou import PBG3 +from libtouhou import Loader as RustLoader from pytouhou.formats.std import Stage from pytouhou.formats.ecl import ECL from pytouhou.formats.anm0 import ANM0 @@ -34,88 +28,12 @@ -class Directory: - def __init__(self, path): - self.path = path - - - def __enter__(self): - return self - - - def __exit__(self, type, value, traceback): - return False - - - @property - def file_list(self): - return self.list_files() - - def list_files(self): - file_list = [] - for path in os.listdir(self.path): - if os.path.isfile(os.path.join(self.path, path)): - file_list.append(path) - return file_list - - - def get_file(self, name): - return open(os.path.join(self.path, str(name)), 'rb') - - - -class ArchiveDescription: - _formats = {b'PBG3': PBG3} - - @classmethod - def get_from_path(cls, path): - if os.path.isdir(path): - instance = Directory(path) - file_list = instance.list_files() - return instance - with open(path, 'rb') as file: - magic = file.read(4) - format_class = cls._formats[magic] - return format_class.from_filename(path) - - - -class Loader: +class Loader(RustLoader): def __init__(self, game_dir=None): - self.exe_files = [] - self.game_dir = game_dir - self.known_files = {} self.instanced_anms = {} # Cache for the textures. self.loaded_anms = [] # For the double loading warnings. - def scan_archives(self, paths_lists): - for paths in paths_lists: - def _expand_paths(): - for path in paths.split(os.path.pathsep): - if self.game_dir and not os.path.isabs(path): - path = os.path.join(self.game_dir, path) - yield glob(path) - paths = list(chain(*_expand_paths())) - if not paths: - raise IOError - path = paths[0] - if os.path.splitext(path)[1] == '.exe': - self.exe_files.extend(paths) - else: - archive_description = ArchiveDescription.get_from_path(path) - for name in archive_description.file_list: - self.known_files[name] = archive_description - - - def get_file(self, 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): if name in self.loaded_anms: logger.warning('ANM0 %s already loaded', name)
