view pytouhou/resource/loader.py @ 783:ec1e06402a97

Replace SDL2_mixer with the kira crate
author Link Mauve <linkmauve@linkmauve.fr>
date Fri, 21 Nov 2025 10:21:59 +0100
parents a30ce01b9154
children f56b10812b77
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.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.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_ecl(self, name):
        file = self.get_file(name)
        return ECL.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_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), [])