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