Mercurial > touhou
annotate pytouhou/resource/loader.py @ 239:901489c21d19
Fix a few crashes in the anmrunner, disable fullscreen switch, change alt+Fx to shift+Fx
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Sun, 01 Jan 2012 21:12:50 +0100 |
parents | c417bb6c98bf |
children | 8fa660da5f0c |
rev | line source |
---|---|
231
c417bb6c98bf
Search for 102h.exe in the game directory instead of the current directory.
Thibaut Girka <thib@sitedethib.com>
parents:
229
diff
changeset
|
1 import os |
97 | 2 from io import BytesIO |
3 | |
4 from pytouhou.formats.pbg3 import PBG3 | |
5 from pytouhou.formats.std import Stage | |
6 from pytouhou.formats.ecl import ECL | |
7 from pytouhou.formats.anm0 import Animations | |
133
2cad2e84a49e
Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
97
diff
changeset
|
8 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
|
9 from pytouhou.formats.sht import SHT |
229
5afc75f71fed
Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
220
diff
changeset
|
10 from pytouhou.formats.exe import SHT as EoSDSHT |
97 | 11 |
12 | |
13 from pytouhou.resource.anmwrapper import AnmWrapper | |
14 | |
15 | |
16 class ArchiveDescription(object): | |
17 _formats = {'PBG3': PBG3} | |
18 | |
19 def __init__(self, path, format_class, file_list=None): | |
20 self.path = path | |
21 self.format_class = format_class | |
22 self.file_list = file_list or [] | |
23 | |
24 | |
25 def open(self): | |
26 file = open(self.path, 'rb') | |
27 instance = self.format_class.read(file) | |
28 return instance | |
29 | |
30 | |
31 @classmethod | |
32 def get_from_path(cls, path): | |
33 with open(path, 'rb') as file: | |
34 magic = file.read(4) | |
35 file.seek(0) | |
36 format_class = cls._formats[magic] | |
37 instance = format_class.read(file) | |
38 file_list = instance.list_files() | |
39 return cls(path, format_class, file_list) | |
40 | |
41 | |
42 | |
43 class Loader(object): | |
231
c417bb6c98bf
Search for 102h.exe in the game directory instead of the current directory.
Thibaut Girka <thib@sitedethib.com>
parents:
229
diff
changeset
|
44 def __init__(self, game_dir=None): |
c417bb6c98bf
Search for 102h.exe in the game directory instead of the current directory.
Thibaut Girka <thib@sitedethib.com>
parents:
229
diff
changeset
|
45 self.game_dir = game_dir |
97 | 46 self.known_files = {} |
47 self.instanced_ecls = {} | |
48 self.instanced_anms = {} | |
49 self.instanced_stages = {} | |
133
2cad2e84a49e
Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
97
diff
changeset
|
50 self.instanced_msgs = {} |
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
|
51 self.instanced_shts = {} |
97 | 52 |
53 | |
54 def scan_archives(self, paths): | |
55 for path in paths: | |
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 if self.game_dir and not os.path.isabs(path): |
c417bb6c98bf
Search for 102h.exe in the game directory instead of the current directory.
Thibaut Girka <thib@sitedethib.com>
parents:
229
diff
changeset
|
57 path = os.path.join(self.game_dir, path) |
97 | 58 archive_description = ArchiveDescription.get_from_path(path) |
59 for name in archive_description.file_list: | |
60 self.known_files[name] = archive_description | |
61 | |
62 | |
63 def get_file_data(self, name): | |
64 with self.known_files[name].open() as archive: | |
65 content = archive.extract(name) | |
66 return content | |
67 | |
68 | |
69 def get_file(self, name): | |
70 with self.known_files[name].open() as archive: | |
71 content = archive.extract(name) | |
72 return BytesIO(content) | |
73 | |
74 | |
75 def get_anm(self, name): | |
76 if name not in self.instanced_anms: | |
77 file = self.get_file(name) | |
78 self.instanced_anms[name] = Animations.read(file) #TODO: modular | |
79 return self.instanced_anms[name] | |
80 | |
81 | |
82 def get_stage(self, name): | |
83 if name not in self.instanced_stages: | |
84 file = self.get_file(name) | |
85 self.instanced_stages[name] = Stage.read(file) #TODO: modular | |
86 return self.instanced_stages[name] | |
87 | |
88 | |
89 def get_ecl(self, name): | |
90 if name not in self.instanced_ecls: | |
91 file = self.get_file(name) | |
92 self.instanced_ecls[name] = ECL.read(file) #TODO: modular | |
93 return self.instanced_ecls[name] | |
94 | |
95 | |
133
2cad2e84a49e
Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
97
diff
changeset
|
96 def get_msg(self, name): |
2cad2e84a49e
Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
97
diff
changeset
|
97 if name not in self.instanced_msgs: |
2cad2e84a49e
Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
97
diff
changeset
|
98 file = self.get_file(name) |
2cad2e84a49e
Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
97
diff
changeset
|
99 self.instanced_msgs[name] = MSG.read(file) #TODO: modular |
2cad2e84a49e
Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
97
diff
changeset
|
100 return self.instanced_msgs[name] |
2cad2e84a49e
Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
97
diff
changeset
|
101 |
2cad2e84a49e
Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
97
diff
changeset
|
102 |
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
|
103 def get_sht(self, name): |
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
|
104 if name not in self.instanced_shts: |
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
|
105 file = self.get_file(name) |
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
|
106 self.instanced_shts[name] = SHT.read(file) #TODO: modular |
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
|
107 return self.instanced_shts[name] |
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
|
108 |
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
|
109 |
231
c417bb6c98bf
Search for 102h.exe in the game directory instead of the current directory.
Thibaut Girka <thib@sitedethib.com>
parents:
229
diff
changeset
|
110 def get_eosd_characters(self, path): |
c417bb6c98bf
Search for 102h.exe in the game directory instead of the current directory.
Thibaut Girka <thib@sitedethib.com>
parents:
229
diff
changeset
|
111 #TODO: Move to pytouhou.games.eosd? |
c417bb6c98bf
Search for 102h.exe in the game directory instead of the current directory.
Thibaut Girka <thib@sitedethib.com>
parents:
229
diff
changeset
|
112 if self.game_dir and not os.path.isabs(path): |
c417bb6c98bf
Search for 102h.exe in the game directory instead of the current directory.
Thibaut Girka <thib@sitedethib.com>
parents:
229
diff
changeset
|
113 path = os.path.join(self.game_dir, path) |
c417bb6c98bf
Search for 102h.exe in the game directory instead of the current directory.
Thibaut Girka <thib@sitedethib.com>
parents:
229
diff
changeset
|
114 with open(path, 'rb') as file: |
229
5afc75f71fed
Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
220
diff
changeset
|
115 characters = EoSDSHT.read(file) #TODO: modular |
5afc75f71fed
Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
220
diff
changeset
|
116 return characters |
5afc75f71fed
Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
220
diff
changeset
|
117 |
5afc75f71fed
Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
220
diff
changeset
|
118 |
97 | 119 def get_anm_wrapper(self, names): |
120 return AnmWrapper(self.get_anm(name) for name in names) | |
121 | |
122 | |
123 def get_anm_wrapper2(self, names): | |
124 anims = [] | |
125 try: | |
126 for name in names: | |
127 anims.append(self.get_anm(name)) | |
128 except KeyError: | |
129 pass | |
130 | |
131 return AnmWrapper(anims) | |
132 |