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