-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvpk.py
More file actions
488 lines (375 loc) · 14 KB
/
vpk.py
File metadata and controls
488 lines (375 loc) · 14 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
import struct
from binascii import crc32
from io import FileIO
from io import open as fopen
import os
__version__ = "0.12"
__author__ = "Rossen Georgiev"
def open(vpk_path, **kwargs):
"""
Returns a VPK instance for specified path. Same argumets as VPK class.
"""
return VPK(vpk_path, **kwargs)
def new(dir_path):
"""
Returns a NewVPK instance for the specific path.
"""
return NewVPK(dir_path)
class NewVPK:
def __init__(self, path):
self.signature = 0x55aa1234
self.version = 1
self.tree_length = 0
self.header_length = 4*3
self.tree = {}
self.path = ''
self.file_count = 0
self.read_dir(path)
def __repr__(self):
return "%s('%s')" % (self.__class__.__name__, self.path)
def read_dir(self, path):
"""
Reads the given path into the tree
"""
self.tree = {}
self.file_count = 0
self.path = path
for root, _, filelist in os.walk(path):
rel = root[len(path):].lstrip('/')
# empty rel, means file is in root dir
if not rel:
rel = ' '
for filename in filelist:
filename = filename.split('.')
if len(filename) <= 1:
raise RuntimeError("Files without an extension are not supported: {0}".format(
repr(os.path.join(root, '.'.join(filename))),
))
ext = filename[-1]
filename = '.'.join(filename[:-1])
if ext not in self.tree:
self.tree[ext] = {}
if rel not in self.tree[ext]:
self.tree[ext][rel] = []
self.tree[ext][rel].append(filename)
self.file_count += 1
self.tree_length = self.calculate_tree_length()
def calculate_tree_length(self):
"""
Walks the tree and calculate the tree length
"""
tree_length = 0
for ext in self.tree:
tree_length += len(ext) + 2
for relpath in self.tree[ext]:
tree_length += len(relpath) + 2
for filename in self.tree[ext][relpath]:
tree_length += len(filename) + 1 + 18
return tree_length + 1
def save(self, vpk_output_path):
"""
Saves the VPK at the given path
"""
with fopen(vpk_output_path, 'wb') as f:
# write VPK1 header
f.write(struct.pack("3I", self.signature,
self.version,
self.tree_length))
self.header_length = f.tell()
data_offset = self.header_length + self.tree_length
# write file tree
for ext in self.tree:
f.write("{0}\x00".format(ext).encode('latin-1'))
for relpath in self.tree[ext]:
f.write("{0}\x00".format(relpath).encode('latin-1'))
for filename in self.tree[ext][relpath]:
f.write("{0}\x00".format(filename).encode('latin-1'))
# append file data
metadata_offset = f.tell()
file_offset = data_offset
real_filename = filename if not ext else "{0}.{1}".format(filename, ext)
checksum = 0
f.seek(data_offset)
with fopen(os.path.join(self.path,
'' if relpath == ' ' else relpath,
real_filename
),
'rb') as pakfile:
for chunk in iter(lambda: pakfile.read(1024), b''):
checksum = crc32(chunk, checksum)
f.write(chunk)
data_offset = f.tell()
file_length = f.tell() - file_offset
f.seek(metadata_offset)
# metadata
# crc32
# preload_length
# archive_index
# archive_offset
# file_length
# term
f.write(struct.pack("IHHIIH", checksum & 0xFFffFFff,
0,
0x7fff,
file_offset - self.tree_length - self.header_length,
file_length,
0xffff
))
# next relpath
f.write(b"\x00")
# next ext
f.write(b"\x00")
# end of file tree
f.write(b"\x00")
def save_and_open(self, path):
"""
Saves the VPK file and returns VPK instance of it
"""
self.save(path)
return VPK(path)
class VPK:
"""
Wrapper for reading Valve's VPK files
"""
def __init__(self, vpk_path, read_header_only=False):
# header
self.signature = 0
self.version = 0
self.tree_length = 0
self.header_length = 0
self.tree = {}
self.vpk_path = vpk_path
self.read_header()
if not read_header_only:
self.read_index()
def __repr__(self):
headonly = ', read_header_only=True' if len(self) == 0 else ''
return "%s('%s'%s)" % (self.__class__.__name__, self.vpk_path, headonly)
def __iter__(self):
return self.tree.__iter__()
def items(self):
def items_generator(tree):
for path in tree:
yield path, self.get_file_meta(path)
return items_generator(self.tree)
def __len__(self):
return len(self.tree)
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
pass
def _read_sz(self, f):
buf = b''
for chunk in iter(lambda: f.read(64), b''):
pos = chunk.find(b'\x00')
if pos > -1:
buf += chunk[:pos]
f.seek(f.tell() - (len(chunk) - (pos + 1)))
break
buf += chunk
return buf.decode('ascii')
def __getitem__(self, key):
"""
Returns VPKFile instance
"""
return self.get_file(key)
def get_file(self, path):
"""
Returns VPKFile instance for the given path
"""
metadata = self.get_file_meta(path)
return VPKFile(self.vpk_path, filepath=path, **metadata)
def get_file_meta(self, path):
"""
Returns metadata for given file path
"""
if path not in self.tree:
raise KeyError("Path doesn't exist")
return dict(zip(['preload',
'crc32',
'preload_length',
'archive_index',
'archive_offset',
'file_length',
], self.tree[path]))
def read_header(self):
"""
Reads VPK file header from the file
"""
with fopen(self.vpk_path, 'rb') as f:
(self.signature,
self.version,
self.tree_length
) = struct.unpack("3I", f.read(3*4))
# original format - headerless
if self.signature != 0x55aa1234:
self.signature = 0
self.version = 0
self.tree_length = 0
# v1
elif self.version == 1:
self.header_length += 4*3
# v2 with extended header
#
# according to http://forum.xentax.com/viewtopic.php?f=10&t=11208
# struct VPKDirHeader_t
# {
# int32 m_nHeaderMarker;
# int32 m_nVersion;
# int32 m_nDirectorySize;
# int32 m_nEmbeddedChunkSize;
# int32 m_nChunkHashesSize;
# int32 m_nSelfHashesSize;
# int32 m_nSignatureSize;
# }
elif self.version == 2:
(self.embed_chunk_length,
self.chunk_hashes_length,
self.self_hashes_length,
self.signature_length
) = struct.unpack("4I", f.read(4*4))
self.header_length += 4*7
else:
raise ValueError("Invalid header, or unsupported version")
def read_index(self):
"""
Reads the index and populates the directory tree
"""
self.tree = {}
with fopen(self.vpk_path, 'rb') as f:
f.seek(self.header_length)
while True:
if self.version > 0 and f.tell() > self.tree_length + self.header_length:
raise ValueError("Error parsing index (out of bounds)")
ext = self._read_sz(f)
if ext == '':
break
while True:
path = self._read_sz(f)
if path == '':
break
if path != ' ':
path += '/'
else:
path = ''
while True:
name = self._read_sz(f)
if name == '':
break
# crc32
# preload_length
# archive_index
# archive_offset
# file_length
metadata = list(struct.unpack("IHHII", f.read(16)))
if struct.unpack("H", f.read(2))[0] != 0xffff:
raise ValueError("Error while parsing index")
if metadata[2] == 0x7fff:
metadata[3] += self.header_length + self.tree_length
metadata.insert(0, f.read(metadata[1]))
self.tree["{0}{1}.{2}".format(path, name, ext)] = tuple(metadata)
class VPKFile(FileIO):
"""
Wrapper class for files with VPK
Should act like a regular file object. No garantees
"""
def __init__(self, vpk_path, **kw):
self.vpk_path = vpk_path
self.vpk_meta = kw
for k, v in kw.items():
setattr(self, k, v)
if self.vpk_meta['preload'] != b'':
self.vpk_meta['preload'] = '...'
# total file length
self.length = self.preload_length + self.file_length
# offset of entire file
self.offset = 0
if self.file_length == 0:
self.vpk_path = None
return
super(VPKFile, self).__init__(vpk_path.replace("dir.", "%03d." % self.archive_index), 'rb')
super(VPKFile, self).seek(self.archive_offset)
def save(self, path):
"""
Save the file to the specified path
"""
# remember and restore file position
pos = self.tell()
self.seek(0)
with fopen(path, 'wb') as output:
output.truncate(self.length)
for chunk in iter(lambda: self.read(1024), b''):
output.write(chunk)
self.seek(pos)
def verify(self):
"""
Returns True if the file contents match with the CRC32 attribute
note: reset
"""
# remember file pointer
pos = self.tell()
self.seek(0)
checksum = 0
for chunk in iter(lambda: self.read(1024), b''):
checksum = crc32(chunk, checksum)
# restore file pointer
self.seek(pos)
return self.crc32 == checksum & 0xffffffff
def __repr__(self):
return "%s(%s, %s)" % (
self.__class__.__name__,
repr(self.name) if self.file_length > 0 else None,
', '.join(["%s=%s" % (k, repr(v)) for k, v in self.vpk_meta.items()])
)
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.close()
def __iter__(self):
return self
def __next__(self):
return self.next()
def next(self):
line = self.readline()
if line == b'':
raise StopIteration
return line
def close(self):
super(VPKFile, self).close()
def tell(self):
return self.offset
def seek(self, offset):
if offset < 0:
raise IOError("Invalid argument")
self.offset = offset
if self.file_length > 0:
super(VPKFile, self).seek(self.archive_offset + max(offset - self.preload_length, 0))
def readlines(self):
return [line for line in self]
def readline(self, a=False):
buf = b''
for chunk in iter(lambda: self.read(256), b''):
pos = chunk.find(b'\n')
if pos > -1:
pos += 1 # include \n
buf += chunk[:pos]
self.seek(self.offset - (len(chunk) - pos))
break
buf += chunk
return buf
def read(self, length=-1):
if length == 0 or self.offset >= self.length:
return b''
data = b''
if self.offset <= self.preload_length:
data += self.preload[self.offset:self.offset+length if length > -1 else None]
self.offset += len(data)
if length > 0:
length = max(length - len(data), 0)
if self.file_length > 0 and self.offset >= self.preload_length:
left = self.file_length - (self.offset - self.preload_length)
data += super(VPKFile, self).read(left if length == -1 else min(left, length))
self.offset += left if length == -1 else min(left, length)
return data
def write(self, seq):
raise NotImplementedError("write method is not supported")