cs16-client-legacy/android/makepak.py

50 lines
1.5 KiB
Python
Raw Normal View History

2016-01-30 20:35:21 +01:00
import sys
import struct
import os
#dummy class for stuffing the file headers into
class FileEntry:
pass
#arguments are source directory, then target filename e.g. "pak1.pak"
rootdir = sys.argv[1]
pakfilename = sys.argv[2]
pakfile = open(pakfilename,"wb")
#write a dummy header to start with
pakfile.write(struct.Struct("<4s2l").pack(b"PACK",0,0))
#walk the directory recursively, add the files and record the file entries
offset = 12
2016-07-22 14:17:56 +02:00
i = 0
2016-01-30 20:35:21 +01:00
fileentries = []
for root, subFolders, files in os.walk(rootdir):
for file in files:
entry = FileEntry()
impfilename = os.path.join(root,file)
entry.filename = os.path.relpath(impfilename,rootdir).replace("\\","/")
if(entry.filename.startswith(".git")):continue
2016-07-22 14:17:56 +02:00
# print "pak: "+entry.filename
2016-01-30 20:35:21 +01:00
with open(impfilename, "rb") as importfile:
pakfile.write(importfile.read())
entry.offset = offset
entry.length = importfile.tell()
offset = offset + entry.length
fileentries.append(entry)
2016-07-22 14:17:56 +02:00
i += 1
2016-01-30 20:35:21 +01:00
tablesize = 0
#after all the file data, write the list of entries
for entry in fileentries:
pakfile.write(struct.Struct("<56s").pack(entry.filename.encode("ascii")))
pakfile.write(struct.Struct("<l").pack(entry.offset))
pakfile.write(struct.Struct("<l").pack(entry.length))
tablesize = tablesize + 64
2016-08-07 20:33:17 +02:00
print "Done. " + str(i) + " files packed to " + pakfilename + "."
2016-07-22 14:17:56 +02:00
2016-01-30 20:35:21 +01:00
#return to the header and write the values correctly
pakfile.seek(0)
pakfile.write(struct.Struct("<4s2l").pack(b"PACK",offset,tablesize))