Mercurial > touhou
annotate 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 |
| 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 | 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 |
|
325
cddfd3cb4797
Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
321
diff
changeset
|
21 from pytouhou.formats.fmt import FMT |
| 97 | 22 |
|
298
92a6fd2632f1
Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents:
297
diff
changeset
|
23 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
|
24 |
|
92a6fd2632f1
Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents:
297
diff
changeset
|
25 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
|
26 |
|
92a6fd2632f1
Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents:
297
diff
changeset
|
27 |
| 97 | 28 |
|
778
816e1f01d650
Partially replace the Loader with a Rust one
Link Mauve <linkmauve@linkmauve.fr>
parents:
771
diff
changeset
|
29 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
|
30 def __init__(self, game_dir=None): |
|
504
69c73023f7a0
Make ANM garbage collectable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
430
diff
changeset
|
31 self.instanced_anms = {} # Cache for the textures. |
|
69c73023f7a0
Make ANM garbage collectable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
430
diff
changeset
|
32 self.loaded_anms = [] # For the double loading warnings. |
| 97 | 33 |
| 34 | |
| 35 def get_anm(self, name): | |
|
504
69c73023f7a0
Make ANM garbage collectable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
430
diff
changeset
|
36 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
|
37 logger.warning('ANM0 %s already loaded', name) |
|
504
69c73023f7a0
Make ANM garbage collectable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
430
diff
changeset
|
38 file = self.get_file(name) |
|
69c73023f7a0
Make ANM garbage collectable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
430
diff
changeset
|
39 anm = ANM0.read(file) |
|
69c73023f7a0
Make ANM garbage collectable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
430
diff
changeset
|
40 self.instanced_anms[name] = anm |
|
69c73023f7a0
Make ANM garbage collectable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
430
diff
changeset
|
41 self.loaded_anms.append(name) |
|
69c73023f7a0
Make ANM garbage collectable.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
430
diff
changeset
|
42 return anm |
| 97 | 43 |
| 44 | |
| 45 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
|
46 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
|
47 return ECL.read(file) #TODO: modular |
| 97 | 48 |
| 49 | |
|
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
|
50 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
|
51 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
|
52 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
|
53 |
|
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
|
54 |
|
262
8fa660da5f0c
Automatically search data files using different names.
Thibaut Girka <thib@sitedethib.com>
parents:
231
diff
changeset
|
55 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
|
56 #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
|
57 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
|
58 try: |
|
92a6fd2632f1
Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents:
297
diff
changeset
|
59 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
|
60 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
|
61 return characters |
|
92a6fd2632f1
Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents:
297
diff
changeset
|
62 except InvalidExeException: |
|
92a6fd2632f1
Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents:
297
diff
changeset
|
63 pass |
|
92a6fd2632f1
Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents:
297
diff
changeset
|
64 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
|
65 |
|
5afc75f71fed
Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
220
diff
changeset
|
66 |
|
325
cddfd3cb4797
Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
321
diff
changeset
|
67 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
|
68 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
|
69 return FMT.read(file) #TODO: modular |
|
325
cddfd3cb4797
Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
321
diff
changeset
|
70 |
|
cddfd3cb4797
Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
321
diff
changeset
|
71 |
|
430
c9433188ffdb
Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
429
diff
changeset
|
72 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
|
73 """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
|
74 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
|
75 assert len(anm) == 1 |
|
c9433188ffdb
Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
429
diff
changeset
|
76 return anm[0] |
| 97 | 77 |
| 78 | |
|
430
c9433188ffdb
Remove AnmWrapper, since ANMs are lists of entries now.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
429
diff
changeset
|
79 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
|
80 """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
|
81 return sum((self.get_anm(name) for name in names), []) |
