annotate pytouhou/resource/loader.py @ 484:699c8e8c7738

Don't stick with the remote user unless the game has started, avoid skipping to prevent desynchro
author Thibaut Girka <thib@sitedethib.com>
date Thu, 29 Dec 2011 10:35:54 +0100
parents 0595315d3880
children 5afc75f71fed
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
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
9
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
10
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
11 from pytouhou.resource.anmwrapper import AnmWrapper
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
12
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
13
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
14 class ArchiveDescription(object):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
15 _formats = {'PBG3': PBG3}
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17 def __init__(self, path, format_class, file_list=None):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18 self.path = path
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19 self.format_class = format_class
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20 self.file_list = file_list or []
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23 def open(self):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24 file = open(self.path, 'rb')
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25 instance = self.format_class.read(file)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26 return instance
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
27
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
28
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
29 @classmethod
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
30 def get_from_path(cls, path):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31 with open(path, 'rb') as file:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32 magic = file.read(4)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33 file.seek(0)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
34 format_class = cls._formats[magic]
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
35 instance = format_class.read(file)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36 file_list = instance.list_files()
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37 return cls(path, format_class, file_list)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38
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 class Loader(object):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
42 def __init__(self):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43 self.known_files = {}
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44 self.instanced_ecls = {}
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45 self.instanced_anms = {}
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
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
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
50
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
51 def scan_archives(self, paths):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
52 for path in paths:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
53 archive_description = ArchiveDescription.get_from_path(path)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
54 for name in archive_description.file_list:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
55 self.known_files[name] = archive_description
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
56
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
57
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
58 def get_file_data(self, name):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
59 with self.known_files[name].open() as archive:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
60 content = archive.extract(name)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
61 return content
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
62
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
63
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
64 def get_file(self, name):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
65 with self.known_files[name].open() as archive:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66 content = archive.extract(name)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
67 return BytesIO(content)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
68
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
69
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
70 def get_anm(self, name):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
71 if name not in self.instanced_anms:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
72 file = self.get_file(name)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
73 self.instanced_anms[name] = Animations.read(file) #TODO: modular
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
74 return self.instanced_anms[name]
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
75
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
76
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
77 def get_stage(self, name):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
78 if name not in self.instanced_stages:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
79 file = self.get_file(name)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
80 self.instanced_stages[name] = Stage.read(file) #TODO: modular
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
81 return self.instanced_stages[name]
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
82
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
83
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
84 def get_ecl(self, name):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
85 if name not in self.instanced_ecls:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
86 file = self.get_file(name)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
87 self.instanced_ecls[name] = ECL.read(file) #TODO: modular
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
88 return self.instanced_ecls[name]
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
89
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
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
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
105 def get_anm_wrapper(self, names):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
106 return AnmWrapper(self.get_anm(name) for name in names)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
107
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
108
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
109 def get_anm_wrapper2(self, names):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
110 anims = []
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
111 try:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
112 for name in names:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
113 anims.append(self.get_anm(name))
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
114 except KeyError:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
115 pass
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
116
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
117 return AnmWrapper(anims)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
118