Migrated first 6 gists.
This commit is contained in:
1
unknytt.py/README.md
Normal file
1
unknytt.py/README.md
Normal file
@@ -0,0 +1 @@
|
||||
File extractor for Knytt Stories story files updated for Python 3. Originally from here: https://nifflas.lp1.nl/index.php?topic=2685.0
|
67
unknytt.py/unknytt.py
Normal file
67
unknytt.py/unknytt.py
Normal file
@@ -0,0 +1,67 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import struct
|
||||
import os
|
||||
import sys
|
||||
|
||||
def readheader(f):
|
||||
magic = f.read(2)
|
||||
if magic == b'':
|
||||
raise EOFError
|
||||
if magic != b'NF':
|
||||
raise ValueError("Bad header")
|
||||
nextchar = f.read(1)
|
||||
name = []
|
||||
while nextchar != b'\0':
|
||||
name.append(nextchar.decode())
|
||||
nextchar = f.read(1)
|
||||
name = ''.join(name)
|
||||
sizedata = f.read(4)
|
||||
(size,) = struct.unpack('<L', sizedata)
|
||||
return name, size
|
||||
|
||||
def extractfile(f, destdir='.'):
|
||||
try:
|
||||
name, size = readheader(f)
|
||||
except EOFError:
|
||||
return False
|
||||
|
||||
outfilename = os.path.join(destdir, *name.split('\\'))
|
||||
|
||||
print("Extracting %s (%s bytes).." % (name, size))
|
||||
|
||||
outdir, dummy = os.path.split(outfilename)
|
||||
try:
|
||||
os.makedirs(outdir)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
outfile = open(outfilename, 'wb')
|
||||
bytestowrite = size
|
||||
while bytestowrite > 0:
|
||||
data = f.read(min(bytestowrite, 4096))
|
||||
if data == '':
|
||||
raise EOFError("file ended prematurely")
|
||||
outfile.write(data)
|
||||
bytestowrite -= len(data)
|
||||
|
||||
return True
|
||||
|
||||
def unknytt(f, worlddir='.'):
|
||||
try:
|
||||
name, dummy = readheader(f)
|
||||
except EOFError:
|
||||
raise ValueError("not a valid knytt stories file")
|
||||
|
||||
print("Creating %s\\" % name)
|
||||
destdir = os.path.join(worlddir, name)
|
||||
os.mkdir(destdir)
|
||||
|
||||
while extractfile(f, destdir):
|
||||
pass
|
||||
|
||||
print("Everything is ok.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
f = open(sys.argv[1], 'rb')
|
||||
unknytt(f)
|
Reference in New Issue
Block a user