Mercurial > touhou
comparison pytouhou/resource/loader.py @ 97:ac2e5e1c2c3c
Refactor \o/
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Sun, 04 Sep 2011 23:50:00 +0200 |
parents | |
children | 2cad2e84a49e |
comparison
equal
deleted
inserted
replaced
96:54929d495654 | 97:ac2e5e1c2c3c |
---|---|
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 | |
7 | |
8 | |
9 from pytouhou.resource.anmwrapper import AnmWrapper | |
10 | |
11 | |
12 class ArchiveDescription(object): | |
13 _formats = {'PBG3': PBG3} | |
14 | |
15 def __init__(self, path, format_class, file_list=None): | |
16 self.path = path | |
17 self.format_class = format_class | |
18 self.file_list = file_list or [] | |
19 | |
20 | |
21 def open(self): | |
22 file = open(self.path, 'rb') | |
23 instance = self.format_class.read(file) | |
24 return instance | |
25 | |
26 | |
27 @classmethod | |
28 def get_from_path(cls, path): | |
29 with open(path, 'rb') as file: | |
30 magic = file.read(4) | |
31 file.seek(0) | |
32 format_class = cls._formats[magic] | |
33 instance = format_class.read(file) | |
34 file_list = instance.list_files() | |
35 return cls(path, format_class, file_list) | |
36 | |
37 | |
38 | |
39 class Loader(object): | |
40 def __init__(self): | |
41 self.known_files = {} | |
42 self.instanced_ecls = {} | |
43 self.instanced_anms = {} | |
44 self.instanced_stages = {} | |
45 | |
46 | |
47 def scan_archives(self, paths): | |
48 for path in paths: | |
49 archive_description = ArchiveDescription.get_from_path(path) | |
50 for name in archive_description.file_list: | |
51 self.known_files[name] = archive_description | |
52 | |
53 | |
54 def get_file_data(self, name): | |
55 with self.known_files[name].open() as archive: | |
56 content = archive.extract(name) | |
57 return content | |
58 | |
59 | |
60 def get_file(self, name): | |
61 with self.known_files[name].open() as archive: | |
62 content = archive.extract(name) | |
63 return BytesIO(content) | |
64 | |
65 | |
66 def get_anm(self, name): | |
67 if name not in self.instanced_anms: | |
68 file = self.get_file(name) | |
69 self.instanced_anms[name] = Animations.read(file) #TODO: modular | |
70 return self.instanced_anms[name] | |
71 | |
72 | |
73 def get_stage(self, name): | |
74 if name not in self.instanced_stages: | |
75 file = self.get_file(name) | |
76 self.instanced_stages[name] = Stage.read(file) #TODO: modular | |
77 return self.instanced_stages[name] | |
78 | |
79 | |
80 def get_ecl(self, name): | |
81 if name not in self.instanced_ecls: | |
82 file = self.get_file(name) | |
83 self.instanced_ecls[name] = ECL.read(file) #TODO: modular | |
84 return self.instanced_ecls[name] | |
85 | |
86 | |
87 def get_anm_wrapper(self, names): | |
88 return AnmWrapper(self.get_anm(name) for name in names) | |
89 | |
90 | |
91 def get_anm_wrapper2(self, names): | |
92 anims = [] | |
93 try: | |
94 for name in names: | |
95 anims.append(self.get_anm(name)) | |
96 except KeyError: | |
97 pass | |
98 | |
99 return AnmWrapper(anims) | |
100 |