Mercurial > touhou
changeset 771:79c3f782dd41
Python: Replace the PBG3 loader with Rust’s
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Tue, 30 Aug 2022 18:41:50 +0200 |
parents | f6c287745a67 |
children | 7492d384d122 |
files | pytouhou/resource/loader.py |
diffstat | 1 files changed, 13 insertions(+), 23 deletions(-) [+] |
line wrap: on
line diff
--- 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):