annotate pytouhou/resource/loader.py @ 252:b5c7369abd7c

Improve data reading perfs
author Thibaut Girka <thib@sitedethib.com>
date Sun, 22 Jan 2012 15:54:51 +0100
parents c417bb6c98bf
children 8fa660da5f0c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
231
c417bb6c98bf Search for 102h.exe in the game directory instead of the current directory.
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
1 import os
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
2 from io import BytesIO
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
3
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
4 from pytouhou.formats.pbg3 import PBG3
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
5 from pytouhou.formats.std import Stage
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
6 from pytouhou.formats.ecl import ECL
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
7 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
8 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
9 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
10 from pytouhou.formats.exe import SHT as EoSDSHT
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
11
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
12
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
13 from pytouhou.resource.anmwrapper import AnmWrapper
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
14
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
15
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16 class ArchiveDescription(object):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17 _formats = {'PBG3': PBG3}
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19 def __init__(self, path, format_class, file_list=None):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20 self.path = path
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21 self.format_class = format_class
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22 self.file_list = file_list or []
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
25 def open(self):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26 file = open(self.path, 'rb')
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
27 instance = self.format_class.read(file)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
28 return instance
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
29
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
30
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
31 @classmethod
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32 def get_from_path(cls, path):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
33 with open(path, 'rb') as file:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
34 magic = file.read(4)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
35 file.seek(0)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36 format_class = cls._formats[magic]
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37 instance = format_class.read(file)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38 file_list = instance.list_files()
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39 return cls(path, format_class, file_list)
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
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43 class Loader(object):
231
c417bb6c98bf Search for 102h.exe in the game directory instead of the current directory.
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
44 def __init__(self, game_dir=None):
c417bb6c98bf Search for 102h.exe in the game directory instead of the current directory.
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
45 self.game_dir = game_dir
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46 self.known_files = {}
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
47 self.instanced_ecls = {}
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
48 self.instanced_anms = {}
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
49 self.instanced_stages = {}
133
2cad2e84a49e Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 97
diff changeset
50 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
51 self.instanced_shts = {}
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
52
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
53
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
54 def scan_archives(self, paths):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
55 for path in paths:
231
c417bb6c98bf Search for 102h.exe in the game directory instead of the current directory.
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
56 if self.game_dir and not os.path.isabs(path):
c417bb6c98bf Search for 102h.exe in the game directory instead of the current directory.
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
57 path = os.path.join(self.game_dir, path)
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
58 archive_description = ArchiveDescription.get_from_path(path)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
59 for name in archive_description.file_list:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
60 self.known_files[name] = archive_description
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
61
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
62
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
63 def get_file_data(self, name):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
64 with self.known_files[name].open() as archive:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
65 content = archive.extract(name)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66 return content
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
67
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
68
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
69 def get_file(self, name):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
70 with self.known_files[name].open() as archive:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
71 content = archive.extract(name)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
72 return BytesIO(content)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
73
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
74
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
75 def get_anm(self, name):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
76 if name not in self.instanced_anms:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
77 file = self.get_file(name)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
78 self.instanced_anms[name] = Animations.read(file) #TODO: modular
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
79 return self.instanced_anms[name]
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
80
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
81
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
82 def get_stage(self, name):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
83 if name not in self.instanced_stages:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
84 file = self.get_file(name)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
85 self.instanced_stages[name] = Stage.read(file) #TODO: modular
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
86 return self.instanced_stages[name]
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
87
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
88
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
89 def get_ecl(self, name):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
90 if name not in self.instanced_ecls:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
91 file = self.get_file(name)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
92 self.instanced_ecls[name] = ECL.read(file) #TODO: modular
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
93 return self.instanced_ecls[name]
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
94
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
95
133
2cad2e84a49e Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 97
diff changeset
96 def get_msg(self, name):
2cad2e84a49e Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 97
diff changeset
97 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
98 file = self.get_file(name)
2cad2e84a49e Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 97
diff changeset
99 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
100 return self.instanced_msgs[name]
2cad2e84a49e Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 97
diff changeset
101
2cad2e84a49e Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 97
diff changeset
102
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
103 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
104 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
105 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
106 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
107 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
108
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
109
231
c417bb6c98bf Search for 102h.exe in the game directory instead of the current directory.
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
110 def get_eosd_characters(self, path):
c417bb6c98bf Search for 102h.exe in the game directory instead of the current directory.
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
111 #TODO: Move to pytouhou.games.eosd?
c417bb6c98bf Search for 102h.exe in the game directory instead of the current directory.
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
112 if self.game_dir and not os.path.isabs(path):
c417bb6c98bf Search for 102h.exe in the game directory instead of the current directory.
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
113 path = os.path.join(self.game_dir, path)
c417bb6c98bf Search for 102h.exe in the game directory instead of the current directory.
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
114 with open(path, 'rb') as file:
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 220
diff changeset
115 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
116 return characters
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 220
diff changeset
117
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 220
diff changeset
118
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
119 def get_anm_wrapper(self, names):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
120 return AnmWrapper(self.get_anm(name) for name in names)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
121
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
122
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
123 def get_anm_wrapper2(self, names):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
124 anims = []
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
125 try:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
126 for name in names:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
127 anims.append(self.get_anm(name))
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
128 except KeyError:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
129 pass
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
130
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
131 return AnmWrapper(anims)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
132