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