annotate pytouhou/resource/loader.py @ 382:b9aecdc6f72f

Fix the anmviewer, broken since changeset 370:74471afbac37.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 07 Sep 2012 13:04:30 +0200
parents 65453340ae95
children 6d7dbcb31d95
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
285
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
1 # -*- encoding: utf-8 -*-
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
2 ##
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
3 ## Copyright (C) 2012 Thibaut Girka <thib@sitedethib.com>
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
4 ##
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
6 ## it under the terms of the GNU General Public License as published
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
7 ## by the Free Software Foundation; version 3 only.
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
8 ##
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
9 ## This program is distributed in the hope that it will be useful,
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
12 ## GNU General Public License for more details.
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
13 ##
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
14
231
c417bb6c98bf Search for 102h.exe in the game directory instead of the current directory.
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
15 import os
262
8fa660da5f0c Automatically search data files using different names.
Thibaut Girka <thib@sitedethib.com>
parents: 231
diff changeset
16 from glob import glob
8fa660da5f0c Automatically search data files using different names.
Thibaut Girka <thib@sitedethib.com>
parents: 231
diff changeset
17 from itertools import chain
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18 from io import BytesIO
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20 from pytouhou.formats.pbg3 import PBG3
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21 from pytouhou.formats.std import Stage
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22 from pytouhou.formats.ecl import ECL
282
dbb1a86c0235 Rename Animations to ANM0 and prepare AnmWrapper for dialogs and interface.
Thibaut Girka <thib@sitedethib.com>
parents: 263
diff changeset
23 from pytouhou.formats.anm0 import ANM0
133
2cad2e84a49e Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 97
diff changeset
24 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
25 from pytouhou.formats.sht import SHT
298
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
26 from pytouhou.formats.exe import SHT as EoSDSHT, InvalidExeException
321
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 298
diff changeset
27 from pytouhou.formats.music import Track
325
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 321
diff changeset
28 from pytouhou.formats.fmt import FMT
97
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 from pytouhou.resource.anmwrapper import AnmWrapper
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32
298
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
33 from pytouhou.utils.helpers import get_logger
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
34
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
35 logger = get_logger(__name__)
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
36
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
37
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
38
263
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
39 class Directory(object):
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
40 def __init__(self, path):
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
41 self.path = path
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
42
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
43
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
44 def __enter__(self):
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
45 return self
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
46
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
47
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
48 def __exit__(self, type, value, traceback):
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
49 return False
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
50
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
51
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
52 def list_files(self):
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
53 file_list = []
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
54 for path in os.listdir(self.path):
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
55 if os.path.isfile(os.path.join(self.path, path)):
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
56 file_list.append(path)
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
57 return file_list
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
58
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
59
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
60 def extract(self, name):
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
61 with open(os.path.join(self.path, str(name)), 'rb') as file:
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
62 contents = file.read()
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
63 return contents
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
64
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
65
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
66
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
67 class ArchiveDescription(object):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
68 _formats = {'PBG3': PBG3}
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 __init__(self, path, format_class, file_list=None):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
71 self.path = path
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
72 self.format_class = format_class
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
73 self.file_list = file_list or []
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
74
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
75
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
76 def open(self):
263
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
77 if self.format_class is Directory:
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
78 return self.format_class(self.path)
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
79
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
80 file = open(self.path, 'rb')
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
81 instance = self.format_class.read(file)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
82 return instance
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 @classmethod
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
86 def get_from_path(cls, path):
263
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
87 if os.path.isdir(path):
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
88 instance = Directory(path)
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
89 file_list = instance.list_files()
ac677dd0ffe0 Add support for reading from a directory instead of a PBG3 (for debugging purposes).
Thibaut Girka <thib@sitedethib.com>
parents: 262
diff changeset
90 return cls(path, Directory, file_list)
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
91 with open(path, 'rb') as file:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
92 magic = file.read(4)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
93 file.seek(0)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
94 format_class = cls._formats[magic]
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
95 instance = format_class.read(file)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
96 file_list = instance.list_files()
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
97 return cls(path, format_class, file_list)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
98
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
99
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
100
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
101 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
102 def __init__(self, game_dir=None):
298
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
103 self.exe_files = []
231
c417bb6c98bf Search for 102h.exe in the game directory instead of the current directory.
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
104 self.game_dir = game_dir
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
105 self.known_files = {}
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
106 self.instanced_ecls = {}
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
107 self.instanced_anms = {}
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
108 self.instanced_stages = {}
133
2cad2e84a49e Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 97
diff changeset
109 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
110 self.instanced_shts = {}
321
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 298
diff changeset
111 self.instanced_tracks = {}
325
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 321
diff changeset
112 self.instanced_fmts = {}
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
113
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
114
262
8fa660da5f0c Automatically search data files using different names.
Thibaut Girka <thib@sitedethib.com>
parents: 231
diff changeset
115 def scan_archives(self, paths_lists):
8fa660da5f0c Automatically search data files using different names.
Thibaut Girka <thib@sitedethib.com>
parents: 231
diff changeset
116 for paths in paths_lists:
8fa660da5f0c Automatically search data files using different names.
Thibaut Girka <thib@sitedethib.com>
parents: 231
diff changeset
117 def _expand_paths():
297
a09ac4650e0d Fix relative path handling and os-specific path separators.
Thibaut Girka <thib@sitedethib.com>
parents: 285
diff changeset
118 for path in paths.split(os.path.pathsep):
262
8fa660da5f0c Automatically search data files using different names.
Thibaut Girka <thib@sitedethib.com>
parents: 231
diff changeset
119 if self.game_dir and not os.path.isabs(path):
8fa660da5f0c Automatically search data files using different names.
Thibaut Girka <thib@sitedethib.com>
parents: 231
diff changeset
120 path = os.path.join(self.game_dir, path)
8fa660da5f0c Automatically search data files using different names.
Thibaut Girka <thib@sitedethib.com>
parents: 231
diff changeset
121 yield glob(path)
8fa660da5f0c Automatically search data files using different names.
Thibaut Girka <thib@sitedethib.com>
parents: 231
diff changeset
122 paths = list(chain(*_expand_paths()))
338
65453340ae95 Print an error when all the needed files aren’t present.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
123 if not paths:
65453340ae95 Print an error when all the needed files aren’t present.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 325
diff changeset
124 raise IOError
262
8fa660da5f0c Automatically search data files using different names.
Thibaut Girka <thib@sitedethib.com>
parents: 231
diff changeset
125 path = paths[0]
8fa660da5f0c Automatically search data files using different names.
Thibaut Girka <thib@sitedethib.com>
parents: 231
diff changeset
126 if os.path.splitext(path)[1] == '.exe':
298
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
127 self.exe_files.extend(paths)
262
8fa660da5f0c Automatically search data files using different names.
Thibaut Girka <thib@sitedethib.com>
parents: 231
diff changeset
128 else:
8fa660da5f0c Automatically search data files using different names.
Thibaut Girka <thib@sitedethib.com>
parents: 231
diff changeset
129 archive_description = ArchiveDescription.get_from_path(path)
8fa660da5f0c Automatically search data files using different names.
Thibaut Girka <thib@sitedethib.com>
parents: 231
diff changeset
130 for name in archive_description.file_list:
8fa660da5f0c Automatically search data files using different names.
Thibaut Girka <thib@sitedethib.com>
parents: 231
diff changeset
131 self.known_files[name] = archive_description
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
132
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
133
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
134 def get_file_data(self, name):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
135 with self.known_files[name].open() as archive:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
136 content = archive.extract(name)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
137 return content
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
138
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
139
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
140 def get_file(self, name):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
141 with self.known_files[name].open() as archive:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
142 content = archive.extract(name)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
143 return BytesIO(content)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
144
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
145
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
146 def get_anm(self, name):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
147 if name not in self.instanced_anms:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
148 file = self.get_file(name)
282
dbb1a86c0235 Rename Animations to ANM0 and prepare AnmWrapper for dialogs and interface.
Thibaut Girka <thib@sitedethib.com>
parents: 263
diff changeset
149 self.instanced_anms[name] = ANM0.read(file) #TODO: modular
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
150 return self.instanced_anms[name]
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
151
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
152
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
153 def get_stage(self, name):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
154 if name not in self.instanced_stages:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
155 file = self.get_file(name)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
156 self.instanced_stages[name] = Stage.read(file) #TODO: modular
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
157 return self.instanced_stages[name]
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
158
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
159
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
160 def get_ecl(self, name):
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
161 if name not in self.instanced_ecls:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
162 file = self.get_file(name)
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
163 self.instanced_ecls[name] = ECL.read(file) #TODO: modular
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
164 return self.instanced_ecls[name]
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
165
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
166
133
2cad2e84a49e Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 97
diff changeset
167 def get_msg(self, name):
2cad2e84a49e Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 97
diff changeset
168 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
169 file = self.get_file(name)
2cad2e84a49e Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 97
diff changeset
170 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
171 return self.instanced_msgs[name]
2cad2e84a49e Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 97
diff changeset
172
2cad2e84a49e Add reading support for the MSG format.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 97
diff changeset
173
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
174 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
175 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
176 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
177 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
178 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
179
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
180
262
8fa660da5f0c Automatically search data files using different names.
Thibaut Girka <thib@sitedethib.com>
parents: 231
diff changeset
181 def get_eosd_characters(self):
231
c417bb6c98bf Search for 102h.exe in the game directory instead of the current directory.
Thibaut Girka <thib@sitedethib.com>
parents: 229
diff changeset
182 #TODO: Move to pytouhou.games.eosd?
298
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
183 for path in self.exe_files:
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
184 try:
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
185 with open(path, 'rb') as file:
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
186 characters = EoSDSHT.read(file)
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
187 return characters
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
188 except InvalidExeException:
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
189 pass
92a6fd2632f1 Improve heuristic to filter out non-game exes (like custom.exe).
Thibaut Girka <thib@sitedethib.com>
parents: 297
diff changeset
190 logger.error("Required game exe not found!")
229
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 220
diff changeset
191
5afc75f71fed Add “SHT” support to EoSD, and do a little cleanup.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 220
diff changeset
192
321
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 298
diff changeset
193 def get_track(self, name):
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 298
diff changeset
194 posname = name.replace('bgm/', '').replace('.mid', '.pos')
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 298
diff changeset
195 if name not in self.instanced_tracks:
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 298
diff changeset
196 file = self.get_file(posname)
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 298
diff changeset
197 self.instanced_tracks[name] = Track.read(file) #TODO: modular
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 298
diff changeset
198 return self.instanced_tracks[name]
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 298
diff changeset
199
61adb5453e46 Implement music playback.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 298
diff changeset
200
325
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 321
diff changeset
201 def get_fmt(self, name):
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 321
diff changeset
202 if name not in self.instanced_fmts:
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 321
diff changeset
203 file = self.get_file(name)
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 321
diff changeset
204 self.instanced_fmts[name] = FMT.read(file) #TODO: modular
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 321
diff changeset
205 return self.instanced_fmts[name]
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 321
diff changeset
206
cddfd3cb4797 Add music support for >PCB.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 321
diff changeset
207
285
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
208 def get_anm_wrapper(self, names, offsets=None):
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
209 """Create an AnmWrapper for ANM files “names”.
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
210
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
211 If one of the files “names” does not exist or is not a valid ANM file,
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
212 raises an exception.
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
213 """
282
dbb1a86c0235 Rename Animations to ANM0 and prepare AnmWrapper for dialogs and interface.
Thibaut Girka <thib@sitedethib.com>
parents: 263
diff changeset
214 return AnmWrapper((self.get_anm(name) for name in names), offsets)
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
215
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
216
285
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
217 def get_anm_wrapper2(self, names, offsets=None):
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
218 """Create an AnmWrapper for ANM files “names”.
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
219
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
220 Stop at the first non-existent or invalid ANM file if there is one,
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
221 and return an AnmWrapper for all the previous correct files.
2100276c289d Document some AnmWrapper related functions.
Thibaut Girka <thib@sitedethib.com>
parents: 282
diff changeset
222 """
282
dbb1a86c0235 Rename Animations to ANM0 and prepare AnmWrapper for dialogs and interface.
Thibaut Girka <thib@sitedethib.com>
parents: 263
diff changeset
223 anms = []
dbb1a86c0235 Rename Animations to ANM0 and prepare AnmWrapper for dialogs and interface.
Thibaut Girka <thib@sitedethib.com>
parents: 263
diff changeset
224
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
225 try:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
226 for name in names:
282
dbb1a86c0235 Rename Animations to ANM0 and prepare AnmWrapper for dialogs and interface.
Thibaut Girka <thib@sitedethib.com>
parents: 263
diff changeset
227 anms.append(self.get_anm(name))
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
228 except KeyError:
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
229 pass
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
230
282
dbb1a86c0235 Rename Animations to ANM0 and prepare AnmWrapper for dialogs and interface.
Thibaut Girka <thib@sitedethib.com>
parents: 263
diff changeset
231 return AnmWrapper(anms, offsets)
97
ac2e5e1c2c3c Refactor \o/
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
232