Mercurial > touhou
comparison pytouhou/resource/loader.py @ 262:8fa660da5f0c
Automatically search data files using different names.
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Mon, 23 Jan 2012 00:58:03 +0100 |
parents | c417bb6c98bf |
children | ac677dd0ffe0 |
comparison
equal
deleted
inserted
replaced
261:2876c267be00 | 262:8fa660da5f0c |
---|---|
1 import os | 1 import os |
2 from glob import glob | |
3 from itertools import chain | |
2 from io import BytesIO | 4 from io import BytesIO |
3 | 5 |
4 from pytouhou.formats.pbg3 import PBG3 | 6 from pytouhou.formats.pbg3 import PBG3 |
5 from pytouhou.formats.std import Stage | 7 from pytouhou.formats.std import Stage |
6 from pytouhou.formats.ecl import ECL | 8 from pytouhou.formats.ecl import ECL |
40 | 42 |
41 | 43 |
42 | 44 |
43 class Loader(object): | 45 class Loader(object): |
44 def __init__(self, game_dir=None): | 46 def __init__(self, game_dir=None): |
47 self.exe = None | |
45 self.game_dir = game_dir | 48 self.game_dir = game_dir |
46 self.known_files = {} | 49 self.known_files = {} |
47 self.instanced_ecls = {} | 50 self.instanced_ecls = {} |
48 self.instanced_anms = {} | 51 self.instanced_anms = {} |
49 self.instanced_stages = {} | 52 self.instanced_stages = {} |
50 self.instanced_msgs = {} | 53 self.instanced_msgs = {} |
51 self.instanced_shts = {} | 54 self.instanced_shts = {} |
52 | 55 |
53 | 56 |
54 def scan_archives(self, paths): | 57 def scan_archives(self, paths_lists): |
55 for path in paths: | 58 for paths in paths_lists: |
56 if self.game_dir and not os.path.isabs(path): | 59 def _expand_paths(): |
57 path = os.path.join(self.game_dir, path) | 60 for path in paths.split(':'): |
58 archive_description = ArchiveDescription.get_from_path(path) | 61 if self.game_dir and not os.path.isabs(path): |
59 for name in archive_description.file_list: | 62 path = os.path.join(self.game_dir, path) |
60 self.known_files[name] = archive_description | 63 yield glob(path) |
64 paths = list(chain(*_expand_paths())) | |
65 path = paths[0] | |
66 if os.path.splitext(path)[1] == '.exe': | |
67 self.exe = path | |
68 else: | |
69 archive_description = ArchiveDescription.get_from_path(path) | |
70 for name in archive_description.file_list: | |
71 self.known_files[name] = archive_description | |
61 | 72 |
62 | 73 |
63 def get_file_data(self, name): | 74 def get_file_data(self, name): |
64 with self.known_files[name].open() as archive: | 75 with self.known_files[name].open() as archive: |
65 content = archive.extract(name) | 76 content = archive.extract(name) |
105 file = self.get_file(name) | 116 file = self.get_file(name) |
106 self.instanced_shts[name] = SHT.read(file) #TODO: modular | 117 self.instanced_shts[name] = SHT.read(file) #TODO: modular |
107 return self.instanced_shts[name] | 118 return self.instanced_shts[name] |
108 | 119 |
109 | 120 |
110 def get_eosd_characters(self, path): | 121 def get_eosd_characters(self): |
111 #TODO: Move to pytouhou.games.eosd? | 122 #TODO: Move to pytouhou.games.eosd? |
123 path = self.exe | |
112 if self.game_dir and not os.path.isabs(path): | 124 if self.game_dir and not os.path.isabs(path): |
113 path = os.path.join(self.game_dir, path) | 125 path = os.path.join(self.game_dir, path) |
114 with open(path, 'rb') as file: | 126 with open(path, 'rb') as file: |
115 characters = EoSDSHT.read(file) #TODO: modular | 127 characters = EoSDSHT.read(file) #TODO: modular |
116 return characters | 128 return characters |