view 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 source

# -*- encoding: utf-8 -*-
##
## Copyright (C) 2012 Thibaut Girka <thib@sitedethib.com>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 3 only.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##

from libtouhou import Loader as RustLoader
from pytouhou.formats.std import Stage
from pytouhou.formats.ecl import ECL
from pytouhou.formats.anm0 import ANM0
from pytouhou.formats.msg import MSG
from pytouhou.formats.sht import SHT
from pytouhou.formats.exe import SHT as EoSDSHT, InvalidExeException
from pytouhou.formats.music import Track
from pytouhou.formats.fmt import FMT

from pytouhou.utils.helpers import get_logger

logger = get_logger(__name__)



class Loader(RustLoader):
    def __init__(self, game_dir=None):
        self.instanced_anms = {}  # Cache for the textures.
        self.loaded_anms = []  # For the double loading warnings.


    def get_anm(self, name):
        if name in self.loaded_anms:
            logger.warning('ANM0 %s already loaded', name)
        file = self.get_file(name)
        anm = ANM0.read(file)
        self.instanced_anms[name] = anm
        self.loaded_anms.append(name)
        return anm


    def get_stage(self, name):
        file = self.get_file(name)
        return Stage.read(file) #TODO: modular


    def get_ecl(self, name):
        file = self.get_file(name)
        return ECL.read(file) #TODO: modular


    def get_msg(self, name):
        file = self.get_file(name)
        return MSG.read(file) #TODO: modular


    def get_sht(self, name):
        file = self.get_file(name)
        return SHT.read(file) #TODO: modular


    def get_eosd_characters(self):
        #TODO: Move to pytouhou.games.eosd?
        for path in self.exe_files:
            try:
                with open(path, 'rb') as file:
                    characters = EoSDSHT.read(file)
                return characters
            except InvalidExeException:
                pass
        logger.error("Required game exe not found!")


    def get_track(self, name):
        posname = name.replace('bgm/', '').replace('.mid', '.pos')
        file = self.get_file(posname)
        return Track.read(file) #TODO: modular


    def get_fmt(self, name):
        file = self.get_file(name)
        return FMT.read(file) #TODO: modular


    def get_single_anm(self, name):
        """Hack for EoSD, since it doesn’t support multi-entries ANMs."""
        anm = self.get_anm(name)
        assert len(anm) == 1
        return anm[0]


    def get_multi_anm(self, names):
        """Hack for EoSD, since it doesn’t support multi-entries ANMs."""
        return sum((self.get_anm(name) for name in names), [])