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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
1 from io import BytesIO
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
2
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
3 from pytouhou.formats.pbg3 import PBG3
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
4 from pytouhou.formats.std import Stage
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
5 from pytouhou.formats.ecl import ECL
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
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
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
11
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
12 from pytouhou.resource.anmwrapper import AnmWrapper
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
13
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
14
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
15 class ArchiveDescription(object):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16 _formats = {'PBG3': PBG3}
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18 def __init__(self, path, format_class, file_list=None):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19 self.path = path
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20 self.format_class = format_class
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21 self.file_list = file_list or []
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24 def open(self):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25 file = open(self.path, 'rb')
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26 instance = self.format_class.read(file)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
27 return instance
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
28
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
29
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
30 @classmethod
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31 def get_from_path(cls, path):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32 with open(path, 'rb') as file:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33 magic = file.read(4)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
34 file.seek(0)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
35 format_class = cls._formats[magic]
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36 instance = format_class.read(file)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37 file_list = instance.list_files()
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38 return cls(path, format_class, file_list)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
40
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
42 class Loader(object):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43 def __init__(self):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44 self.known_files = {}
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45 self.instanced_ecls = {}
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46 self.instanced_anms = {}
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
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
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
50
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
51
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
52 def scan_archives(self, paths):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
53 for path in paths:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
54 archive_description = ArchiveDescription.get_from_path(path)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
55 for name in archive_description.file_list:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
56 self.known_files[name] = archive_description
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
57
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
58
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
59 def get_file_data(self, name):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
60 with self.known_files[name].open() as archive:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
61 content = archive.extract(name)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
62 return content
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
63
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
64
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
65 def get_file(self, name):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66 with self.known_files[name].open() as archive:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
67 content = archive.extract(name)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
68 return BytesIO(content)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
69
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
70
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
71 def get_anm(self, name):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
72 if name not in self.instanced_anms:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
73 file = self.get_file(name)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
74 self.instanced_anms[name] = Animations.read(file) #TODO: modular
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
75 return self.instanced_anms[name]
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
76
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
77
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
78 def get_stage(self, name):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
79 if name not in self.instanced_stages:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
80 file = self.get_file(name)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
81 self.instanced_stages[name] = Stage.read(file) #TODO: modular
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
82 return self.instanced_stages[name]
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
83
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
84
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
85 def get_ecl(self, name):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
86 if name not in self.instanced_ecls:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
87 file = self.get_file(name)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
88 self.instanced_ecls[name] = ECL.read(file) #TODO: modular
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
89 return self.instanced_ecls[name]
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
90
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
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
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
112 def get_anm_wrapper(self, names):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
113 return AnmWrapper(self.get_anm(name) for name in names)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
114
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
115
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
116 def get_anm_wrapper2(self, names):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
117 anims = []
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
118 try:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
119 for name in names:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
120 anims.append(self.get_anm(name))
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
121 except KeyError:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
122 pass
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
123
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
124 return AnmWrapper(anims)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
125