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

[42ca9f]: formats / tlk.py Maximize Restore History

Download this file

tlk.py    162 lines (119 with data), 4.8 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
# -*-python-*-
# ie_shell.py - Simple shell for Infinity Engine-based game files
# Copyright (C) 2004 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.
# RCS: $Id: tlk.py,v 1.1 2005/03/02 20:44:23 edheldil Exp $
import re
import string
import sys
from format import Format, register_format, TICK_SIZE, TACK_SIZE
class TLK_Format (Format):
def __init__ (self, filename):
Format.__init__ (self, filename)
self.expect_signature = 'TLK'
self.strref_list = []
t_cp1250 = '������������������������������'
t_iso8859_2 = '������������������������������'
self.xlat = string.maketrans (t_cp1250, t_iso8859_2)
self.header_desc = (
{ 'key': 'signature',
'type': 'STR4',
'off': 0x0000,
'label': 'Signature' },
{ 'key': 'version',
'type': 'STR4',
'off':0x0004,
'label': 'Version'},
{ 'key': 'unknown',
'type': 'WORD',
'off': 0x0008,
'label': '???' },
{ 'key': 'num_of_strrefs',
'type': 'DWORD',
'off': 0x000A,
'label': '# of strref entries'},
{ 'key': 'string_offset',
'type': 'DWORD',
'off': 0x000E,
'label': 'First string data offset'},
)
self.strref_record_desc = (
{ 'key': 'content_type',
'type': 'WORD',
'off': 0x0000,
'label': 'Content of this entry' },
{ 'key': 'sound_resref',
'type': 'RESREF',
'off': 0x0002,
'label': 'Sound resref' },
{ 'key': 'volume_variance',
'type': 'DWORD',
'off': 0x000A,
'label': 'Volume variance' },
{ 'key': 'pitch_variance',
'type': 'DWORD',
'off': 0x000E,
'label': 'Pitch variance' },
{ 'key': 'string_offset',
'type': 'DWORD',
'off': 0x0012,
'label': 'String data rel offset' },
{ 'key': 'string_len',
'type': 'DWORD',
'off': 0x0016,
'label': 'String data size' },
{ 'key': 'string',
'type': '_STRING',
'off': 0x0000,
'label': 'String' },
)
def decode_file (self):
self.decode_header ()
off = 0x0012
for i in range (self.header['num_of_strrefs']):
#if i == 3000:
# break
obj = {}
self.decode_strref_record (off, obj)
self.strref_list.append (obj)
off = off + 26
if not (i % TICK_SIZE):
sys.stdout.write('.')
if not (i % TACK_SIZE):
sys.stdout.write('%d' %i)
sys.stdout.flush ()
print
def print_file (self):
self.print_header ()
i = 0
for obj in self.strref_list:
print '#%d' %i
self.print_strref_record (obj)
i = i + 1
def decode_header (self):
self.header = {}
self.decode_by_desc (0x0000, self.header_desc, self.header)
def print_header (self):
self.print_by_desc (self.header, self.header_desc)
def decode_strref_record (self, offset, obj):
self.decode_by_desc (offset, self.strref_record_desc, obj)
obj['string'] = string.translate (self.decode_sized_string (self.header['string_offset'] + obj['string_offset'], obj['string_len']), self.xlat)
def print_strref_record (self, obj):
self.print_by_desc (obj, self.strref_record_desc)
def get_strref_by_str_re (self, text):
rx = re.compile (text)
return filter (lambda s, rx=rx: rx.search (s['string']), self.strref_list)
register_format ('TLK', 'V1', TLK_Format)