-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathpaktify.py
56 lines (45 loc) · 1.47 KB
/
paktify.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Paktify: fixes .pak file order to ensure .patch files are listed last
# requires: https://github.com/blixt/py-starbound/
import mmap
import optparse
import os
import sys
import starbound
from starbound import sbon
def main():
p = optparse.OptionParser('Usage: %prog <package path>')
options, arguments = p.parse_args()
if len(arguments) < 1:
p.error('Must specify .pak path.')
package_path = arguments[0]
with open(package_path, 'rb+') as fh:
mm = mmap.mmap(fh.fileno(), 0, access=mmap.ACCESS_WRITE)
pak = starbound.SBAsset6(mm)
pak.read_header()
ent = { } # file entries
mm.seek(pak.index_offset)
for i in range(pak.file_count):
start = mm.tell()
path = sbon.read_string(mm)
mm.read(16) # seek past offset and length
end = mm.tell()
length = end - start
mm.seek(start)
ent[path] = mm.read(length) # store full entry bytes
mm.seek(pak.index_offset) # return to start of file index
p_ent = { }
for k, v in ent.items():
if k.lower().endswith(".patch"):
p_ent[k] = v
else:
#print(k)
mm.write(v) # write entry
for k, v in p_ent.items():
#print(k)
mm.write(v)
mm.flush()
#
if __name__ == '__main__':
main()