Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[3662b5]: infinity / formats / key.py Maximize Restore History

Download this file

key.py    249 lines (191 with data), 8.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# -*-python-*-
# ie_shell.py - Simple shell for Infinity Engine-based game files
# Copyright (C) 2004-2008 by Jaroslav Benkovsky, <edheldil@users.sf.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import re
import sys
from infinity import core
from infinity.format import Format, register_format
class KEY_Format (Format):
header_desc = (
{ 'key': 'signature',
'type': 'STR4',
'off': 0x0000,
'label': 'Signature' },
{ 'key': 'version',
'type': 'STR4',
'off': 0x0004,
'label': 'Version'},
{ 'key': 'num_of_bifs',
'type': 'DWORD',
'off': 0x0008,
'label': '# of BIFs'},
{ 'key': 'num_of_resrefs',
'type': 'DWORD',
'off': 0x000C,
'label': '# of resref entries'},
{ 'key': 'bif_offset',
'type': 'DWORD',
'off': 0x0010,
'label': 'First BIF offset'},
{ 'key': 'resref_offset',
'type': 'DWORD',
'off': 0x0014,
'label': 'First resref offset'},
)
bif_record_desc = (
{ 'key': 'file_len',
'type': 'DWORD',
'off': 0x0000,
'label': 'BIF file len' },
{ 'key': 'file_name',
'type': 'STROFF',
'off': 0x0004,
'label': 'BIF file name' },
{ 'key': 'file_name_offset',
'type': 'DWORD',
'off': 0x0004,
'label': 'BIF file name offset' },
{ 'key': 'file_name_len',
'type': 'WORD',
'off': 0x0008,
'label': 'BIF file name len' },
{ 'key': 'location',
'type': 'WORD',
'off': 0x000A,
'label': 'BIF location' },
)
resref_record_desc = (
{ 'key': 'resref_name',
'type': 'RESREF',
'off': 0x0000,
'label': 'resref name' },
{ 'key': 'type',
'type': 'RESTYPE',
'off': 0x0008,
'label': 'resref type' },
{ 'key': 'locator',
'type': 'DWORD',
'off': 0x000A,
'label': 'resref locator' },
{ 'key': 'locator_src_ndx',
'type': 'DWORD',
'off': 0x000A,
'bits': '31-20',
'label': 'resref locator (source index)' },
{ 'key': 'locator_tset_ndx',
'type': 'DWORD',
'off': 0x000A,
'bits': '19-14',
'label': 'resref locator (tileset index)' },
{ 'key': 'locator_ntset_ndx',
'type': 'DWORD',
'off': 0x000A,
'bits': '13-0',
'label': 'resref locator (non-tileset index)' },
)
def __init__ (self):
Format.__init__ (self)
self.expect_signature = 'KEY'
self.bif_list = []
self.bif_hash = {}
self.resref_list = []
self.resref_hash = {}
# when set to some number, read that number of resources at most
#self.options['max_read_bifs'] = None
def read (self, stream):
self.read_header (stream)
off = self.header['bif_offset']
bif_record_size = self.get_struc_size (self.bif_record_desc)
for i in range (self.header['num_of_bifs']):
obj = {}
self.read_bif_record (stream, off, obj)
self.bif_list.append (obj)
self.bif_hash[obj['file_name']] = obj
off = off + bif_record_size
off = self.header['resref_offset']
max_read_resrefs = self.header['num_of_resrefs']
if self.get_option ('format.key.max_read_resrefs'):
max_read_resrefs = min (max_read_resrefs, self.get_option ('format.key.max_read_resrefs'))
resref_record_size = self.get_struc_size (self.resref_record_desc)
tick_size = self.get_option ('format.key.tick_size')
tack_size = self.get_option ('format.key.tack_size')
for i in range (max_read_resrefs):
#if i == 1000:
# break
obj = {}
self.read_resref_record (stream, off, obj)
self.resref_list.append (obj)
obj['file_name'] = self.bif_list[obj['locator_src_ndx']]
self.resref_hash[obj['resref_name']] = obj
off = off + resref_record_size
if not (i % tick_size):
sys.stdout.write('.')
if not (i % tack_size):
sys.stdout.write('%d' %i)
sys.stdout.flush ()
print()
def write (self, stream):
# FIXME: STROFF is missing
header_size = self.get_struc_size (self.header_desc)
bif_record_size = self.get_struc_size (self.bif_record_desc)
resref_record_size = self.get_struc_size (self.resref_record_desc)
self.header['num_of_bifs'] = len (self.bif_list)
self.header['num_of_resrefs'] = len (self.resref_list)
self.header['bif_offset'] = header_size
self.header['resref_offset'] = header_size + len (self.bif_list) * bif_record_size
self.write_struc (stream, 0x0000, self.header_desc, self.header)
off2 = self.header['bif_offset']
for obj in self.bif_list:
self.write_struc (stream, off2, self.bif_record_desc, obj)
off2 += bif_record_size
off2 = self.header['resref_offset']
for obj in self.resref_list:
self.write_struc (stream, off2, self.resref_record_desc, obj)
off2 += resref_record_size
def printme (self):
self.print_header ()
i = 0
for obj in self.bif_list:
print('#%d' %i)
self.print_bif_record (obj)
i = i + 1
i = 0
for obj in self.resref_list:
print('#%d' %i)
self.print_resref_record (obj)
i = i + 1
def read_bif_record (self, stream, offset, obj):
self.read_struc (stream, offset, self.bif_record_desc, obj)
def print_bif_record (self, obj):
self.print_struc (obj, self.bif_record_desc)
def read_resref_record (self, stream, offset, obj):
self.read_struc (stream, offset, self.resref_record_desc, obj)
def print_resref_record (self, obj):
self.print_struc (obj, self.resref_record_desc)
def get_bif_by_name_re (self, name):
rx = re.compile (name)
return filter (lambda s, rx=rx: rx.search (s['file_name']), self.bif_list)
def get_resref_by_file_index (self, index):
return filter (lambda s, i=index: s['locator_src_ndx'] == i, self.resref_list)
def get_resref_by_name_re (self, name):
rx = re.compile (name)
return filter (lambda s, rx=rx: rx.search (s['resref_name']), self.resref_list)
def get_resref_by_name (self, name):
return filter (lambda s, name=name.upper (): s['resref_name'].upper () == name, self.resref_list)
def get_resref_by_type (self, type):
return filter (lambda s, type=type: s['type'] == type, self.resref_list)
register_format ('KEY', 'V1', KEY_Format)