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
|
#!/usr/bin/env python
"""
MPAK package handling utility
Version 1.4 (Python-implementation)
Copyright (c) 2008, Mika Halttunen. <http://www.mhgames.co.nr>
This command line tool allows creation and extraction of MPAK (.mpk) packages used
in several of my games. MPAK is a simple WAD-like file format of mine, that allows storing
the game data in one single .mpk file. I originally had a very crude command line program
bit like this one (written in C++), and decided to write this Python-implementation as
an Python-programming excercise. So, it's my first Python program. :)
Version history:
v1.4: The first Python version
v1.0 -- v1.31: The original C++ implementation
"""
import getopt, sys
import os
import traceback
import struct
import binascii
import fnmatch
import shutil
from ctypes import c_uint32
def usage():
"""
Prints the program usage.
"""
print("MPAK package handling utility")
print("Version 1.4 (Python-implementation)")
print("Copyright (c) 2008, Mika Halttunen.")
print("")
print("Usage:", sys.argv[0],"[switch]","-f pakfile.mpk","[file1]","[file2]", "[...]", "[fileN]")
print("where [switch] is one of the following:")
print(" -f, --file=FILE Use package FILE")
print(" -c, --create Create a new package with files 'file1' to 'fileN'")
print(" -l, --list List the files from given package")
print(" -e, --extract Extract all files (by default) from given package. If you")
print(" want to only extract some specific files, you can name")
print(" them individually, and/or use wildcards (i.e. *.png).")
print(" You can supply path where to extract with -p.")
print(" -p, --path=PATH Extract to PATH (created if doesn't exist)")
print(" -h, --help Print this usage text")
print("")
def errorMsg(msg):
"""
Prints an error message and exits.
"""
try:
pos = traceback.extract_stack(limit=2)
if pos:
print("ERROR: In %s:%s, line %d:" % (pos[0][0], pos[0][2], pos[0][1]))
else:
print("ERROR:")
print("\t",msg)
except:
if __debug__:
traceback.print_exc()
pass
sys.exit(2)
def separator():
"""
Prints the separator line.
"""
print("-"*75)
def computeCRC(file, offset):
"""
Computes the CRC32 for the file, starting at given offset.
"""
f = open(file, "rb")
f.seek(offset)
crc = 0
# Compute a running CRC32 for the file in 16kb chunks
while True:
buffer = f.read(16384)
if not buffer: break # End of file
crc = binascii.crc32(buffer, crc)
f.close()
return crc
def createPackage(pakFile, files):
"""
Creates a new MPAK package.
This copies the given files into the new package file, writes the file table
and closes the package. MPAK doesn't support adding new files to an existing
package.
"""
print("Creating '%s'.." % pakFile)
if len(files) < 1: errorMsg("No input files specified!")
separator()
# Open the package file for writing
out = open(pakFile, "wb")
# Write the header and reserve 4+4 bytes for CRC32 and file table offset
out.write(b"MPK1")
out.write(b"."*8)
# Write each file
package = { "fileNames": [], "fileOffsets": [] }
count = 0
for file in files:
# Get the basename
filename = os.path.basename(file)
print(" <%s..." % filename, end=' ')
package["fileNames"].append(filename)
# Get the file size in bytes
stats = os.stat(file)
# Store the current offset
package["fileOffsets"].append(out.tell())
# Open the file and copy its contents
f = open(file, "rb")
shutil.copyfileobj(f, out, 16384)
f.close()
print("OK. (%.1f KB)" % (stats.st_size / 1024.0))
count = count + 1
separator()
# Grab the file table offset and write the table
ftOffset = out.tell()
# Write the number of files
out.write(struct.pack("<L", count))
# Write the file information
for i in range(count):
# File name length
length = len(package["fileNames"][i]) + 1
out.write(struct.pack("B", length))
# File name, plus one zero for the C++ implementation
out.write(package["fileNames"][i].encode('ascii'))
out.write(struct.pack("B", 0))
# File offset
out.write(struct.pack("<L", package["fileOffsets"][i]))
# Update the header to have the correct file table offset
out.seek(8)
out.write(struct.pack("<L", ftOffset))
# Compute the CRC32 and write it to the header
out.flush()
crc32 = c_uint32(0)
crc32.value = computeCRC(pakFile, 8)
out.seek(4)
out.write(struct.pack("<L", crc32.value))
print("Added %d files to %s" % (count, pakFile))
print("Package '%s' created successfully. CRC32 checksum is %s." % (pakFile, hex(crc32.value)))
out.close()
def readPackage(pakFile):
"""
Opens the given MPAK package, reads its information and stores it to a
package dictionary. Returns the dictionary.
"""
packageInfo = { "filename": pakFile }
f = open(pakFile, 'rb')
if f.read(4) != b"MPK1": errorMsg("Unsupported file format!")
# Read the CRC32 checksum and the file table header offset
buffer = f.read(8)
crc32, headerOffset = struct.unpack("<LL", buffer)
crc32 = int(crc32)
packageInfo["crc"] = crc32
# Check that the CRC32 matches
checksum = c_uint32(0)
checksum.value = computeCRC(pakFile, 8)
if checksum.value != crc32:
f.close()
errorMsg("Checksum doesn't match; perhaps a corrupted package?")
# Seek to the file table, and read the number of files
f.seek(headerOffset)
numFiles = struct.unpack("<L", f.read(4))[0]
packageInfo["numFiles"] = numFiles
# Read the file information
fileNames = []
fileOffsets = []
for i in range(numFiles):
namelen = struct.unpack("B", f.read(1))[0]
file = f.read(namelen)
offset = struct.unpack("<L", f.read(4))[0]
fileNames.append(file[:-1].decode('ascii')) # Remove the trailing null character
fileOffsets.append(offset)
# Compute the file sizes from the offsets
fileSizes = []
for i in range(numFiles-1):
fileSizes.append(fileOffsets[i+1] - fileOffsets[i])
fileSizes.append(headerOffset - fileOffsets[numFiles-1])
# Store the information
packageInfo["fileNames"] = fileNames
packageInfo["fileOffsets"] = fileOffsets
packageInfo["fileSizes"] = fileSizes
f.close()
return packageInfo
def listPackage(pakFile):
"""
Lists the contents of a MPAK package.
"""
print("Listing '%s'.." % pakFile)
package = readPackage(pakFile)
# Print the listing
numFiles = package["numFiles"]
print("'%s' (CRC32: %s) contains %d files:" % (pakFile, hex(package["crc"]), numFiles))
print("")
print(" NUM : FILE : SIZE(KB) : OFFSET")
separator()
for i in range(numFiles):
print(" %3d : %30s : %-10.1f : (at %s)" % (i+1, package["fileNames"][i], package["fileSizes"][i] / 1024.0, hex(package["fileOffsets"][i])))
separator()
print(" NUM : FILE : SIZE(KB) : OFFSET")
def extractPackage(pakFile, path, filters):
"""
Extracts files from a package to given path.
By default extracts all the files. Can be given list of wildcards (i.e. *.png) to
extract only the files that match given wildcards. Wildcards can also be file names
from the package.
The given path is created if it doesn't exist.
If the path is just a single directory name, it's assumed to exist in the current
working directory.
"""
print("Extracting files from '%s' to %s.." % (pakFile, path))
package = readPackage(pakFile)
# Try to create the path if it doesn't exist
path = os.path.abspath(path)
if not os.path.exists(path):
print("Path",path,"doesn't exist, creating it..")
try:
os.makedirs(path)
except:
errorMsg("Unable to create directory " + path + "!");
separator()
# Open the file, and extract all the individual files from it
count = 0
f = open(pakFile, "rb")
for i in range(package["numFiles"]):
# Test if the file name matches the given wildcard
if len(filters) > 0:
for filter in filters:
if fnmatch.fnmatch(package["fileNames"][i], filter):
break
else: continue
print(" >%s..." % package["fileNames"][i], end=' ')
# Seek to the correct offset
f.seek(package["fileOffsets"][i])
# Open a new file for writing, and write the file out in 16kb chunks
out = open(os.path.join(path, package["fileNames"][i]), "wb")
bytesWritten = 0
bytesTotal = package["fileSizes"][i];
while True:
# We have to watch not to write too much
bytesLeft = bytesTotal - bytesWritten
if bytesLeft > 16384: bytesLeft = 16384
buffer = f.read(bytesLeft)
out.write(buffer)
bytesWritten = bytesWritten + bytesLeft
if bytesWritten == bytesTotal:
break
out.close()
print("OK.")
count = count + 1
f.close()
separator()
print("%d (of %d) files extracted to %s." % (count, package["numFiles"], path))
def main():
"""
Main method.
"""
try:
# Get the optiosn
opts, args = getopt.getopt(sys.argv[1:], "f:clep:h", ["file=", "create", "list", "extract", "path=", "help"])
except getopt.GetoptError as err:
# Print the program usage and exit
print("ERROR: " + str(err))
usage()
sys.exit(2)
extractPath = os.getcwd()
pakFile = None
action = None
# Handle the options
for o, a in opts:
if o in ("-f", "--file"):
pakFile = a # Grab the pakfile
elif o in ("-c", "--create"):
action = "create"
elif o in ("-l", "--list"):
action = "list"
elif o in ("-e", "--extract"):
action = "extract"
elif o in ("-p", "--path"):
extractPath = a # Grab the path
elif o in ("-h", "--help"):
usage()
sys.exit()
else:
assert False, "Unhandled option"
# Check that we got a pakfile
if pakFile == None:
usage()
sys.exit(2)
if action == "create": createPackage(pakFile, args)
elif action == "list": listPackage(pakFile)
elif action == "extract": extractPackage(pakFile, extractPath, args)
else: usage()
sys.exit()
if __name__ == "__main__":
main()
|