annotate pytouhou/resource/loader.py @ 779:ee09657d3789

Replace the stage parser with the Rust one
author Link Mauve <linkmauve@linkmauve.fr>
date Sat, 08 Nov 2025 19:29:33 +0100
parents 816e1f01d650
children a30ce01b9154
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
285
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
1 # -*- encoding: utf-8 -*-
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
2 ##
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
3 ## Copyright (C) 2012 Thibaut Girka <thib@sitedethib.com>
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
4 ##
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
6 ## it under the terms of the GNU General Public License as published
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
7 ## by the Free Software Foundation; version 3 only.
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
8 ##
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
9 ## This program is distributed in the hope that it will be useful,
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
12 ## GNU General Public License for more details.
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
13 ##
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
14
778
816e1f01d650 Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents: 771
diff changeset
15 from libtouhou import Loader as RustLoader
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16 from pytouhou.formats.ecl import ECL
282
dbb1a86c0235 Rename Animations to ANM0 and prepare AnmWrapper for dialogs and interface.
Thibaut Girka <thib@sitedethib.com>
parents: 263
diff changeset
17 from pytouhou.formats.anm0 import ANM0
133
2cad2e84a49e Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 97
diff changeset
18 from pytouhou.formats.msg import MSG
220
0595315d3880 Fix SHT handling; change a few things to be closer to ZUN’s mind; and first stub of PCB support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 133
diff changeset
19 from pytouhou.formats.sht import SHT
298
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
20 from pytouhou.formats.exe import SHT as EoSDSHT, InvalidExeException
321
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 298
diff changeset
21 from pytouhou.formats.music import Track
325
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 321
diff changeset
22 from pytouhou.formats.fmt import FMT
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23
298
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
24 from pytouhou.utils.helpers import get_logger
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
25
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
26 logger = get_logger(__name__)
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
27
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
28
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
29
778
816e1f01d650 Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents: 771
diff changeset
30 class Loader(RustLoader):
231
c417bb6c98bf Search for 102h.exe in the game directory instead of the current directory.
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
31 def __init__(self, game_dir=None):
504
69c73023f7a0 Make ANM garbage collectable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 430
diff changeset
32 self.instanced_anms = {} # Cache for the textures.
69c73023f7a0 Make ANM garbage collectable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 430
diff changeset
33 self.loaded_anms = [] # For the double loading warnings.
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
34
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
35
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36 def get_anm(self, name):
504
69c73023f7a0 Make ANM garbage collectable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 430
diff changeset
37 if name in self.loaded_anms:
764
d18c0bf11138 Python: Use logger.warning() as logger.warn() is deprecated
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 615
diff changeset
38 logger.warning('ANM0 %s already loaded', name)
504
69c73023f7a0 Make ANM garbage collectable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 430
diff changeset
39 file = self.get_file(name)
69c73023f7a0 Make ANM garbage collectable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 430
diff changeset
40 anm = ANM0.read(file)
69c73023f7a0 Make ANM garbage collectable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 430
diff changeset
41 self.instanced_anms[name] = anm
69c73023f7a0 Make ANM garbage collectable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 430
diff changeset
42 self.loaded_anms.append(name)
69c73023f7a0 Make ANM garbage collectable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 430
diff changeset
43 return anm
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46 def get_ecl(self, name):
413
6d7dbcb31d95 Let the gc do its work, don’t keep useless references.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 338
diff changeset
47 file = self.get_file(name)
6d7dbcb31d95 Let the gc do its work, don’t keep useless references.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 338
diff changeset
48 return ECL.read(file) #TODO: modular
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
50
133
2cad2e84a49e Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 97
diff changeset
51 def get_msg(self, name):
413
6d7dbcb31d95 Let the gc do its work, don’t keep useless references.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 338
diff changeset
52 file = self.get_file(name)
6d7dbcb31d95 Let the gc do its work, don’t keep useless references.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 338
diff changeset
53 return MSG.read(file) #TODO: modular
133
2cad2e84a49e Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 97
diff changeset
54
2cad2e84a49e Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 97
diff changeset
55
220
0595315d3880 Fix SHT handling; change a few things to be closer to ZUN’s mind; and first stub of PCB support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 133
diff changeset
56 def get_sht(self, name):
413
6d7dbcb31d95 Let the gc do its work, don’t keep useless references.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 338
diff changeset
57 file = self.get_file(name)
6d7dbcb31d95 Let the gc do its work, don’t keep useless references.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 338
diff changeset
58 return SHT.read(file) #TODO: modular
220
0595315d3880 Fix SHT handling; change a few things to be closer to ZUN’s mind; and first stub of PCB support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 133
diff changeset
59
0595315d3880 Fix SHT handling; change a few things to be closer to ZUN’s mind; and first stub of PCB support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 133
diff changeset
60
262
8fa660da5f0c Automatically search data files using different names.
Thibaut Girka <thib@sitedethib.com>
parents: 231
diff changeset
61 def get_eosd_characters(self):
231
c417bb6c98bf Search for 102h.exe in the game directory instead of the current directory.
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
62 #TODO: Move to pytouhou.games.eosd?
298
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
63 for path in self.exe_files:
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
64 try:
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
65 with open(path, 'rb') as file:
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
66 characters = EoSDSHT.read(file)
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
67 return characters
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
68 except InvalidExeException:
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
69 pass
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
70 logger.error("Required game exe not found!")
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 220
diff changeset
71
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 220
diff changeset
72
321
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 298
diff changeset
73 def get_track(self, name):
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 298
diff changeset
74 posname = name.replace('bgm/', '').replace('.mid', '.pos')
413
6d7dbcb31d95 Let the gc do its work, don’t keep useless references.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 338
diff changeset
75 file = self.get_file(posname)
6d7dbcb31d95 Let the gc do its work, don’t keep useless references.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 338
diff changeset
76 return Track.read(file) #TODO: modular
321
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 298
diff changeset
77
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 298
diff changeset
78
325
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 321
diff changeset
79 def get_fmt(self, name):
413
6d7dbcb31d95 Let the gc do its work, don’t keep useless references.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 338
diff changeset
80 file = self.get_file(name)
6d7dbcb31d95 Let the gc do its work, don’t keep useless references.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 338
diff changeset
81 return FMT.read(file) #TODO: modular
325
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 321
diff changeset
82
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 321
diff changeset
83
430
c9433188ffdb Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 429
diff changeset
84 def get_single_anm(self, name):
c9433188ffdb Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 429
diff changeset
85 """Hack for EoSD, since it doesn’t support multi-entries ANMs."""
c9433188ffdb Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 429
diff changeset
86 anm = self.get_anm(name)
c9433188ffdb Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 429
diff changeset
87 assert len(anm) == 1
c9433188ffdb Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 429
diff changeset
88 return anm[0]
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
89
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
90
430
c9433188ffdb Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 429
diff changeset
91 def get_multi_anm(self, names):
c9433188ffdb Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 429
diff changeset
92 """Hack for EoSD, since it doesn’t support multi-entries ANMs."""
c9433188ffdb Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 429
diff changeset
93 return sum((self.get_anm(name) for name in names), [])