Amazing! Pack Knytt Stories levels with Python!

  • 1 Replies
  • 1999 Views
*

Offline Mr. Monkey

  • 222
  • 1
  • jolly good fellow
    • View Profile
Amazing! Pack Knytt Stories levels with Python!
« on: February 09, 2010, 23:24:59 »
I made this thing in late August/early September but I didn't post it here for some reason?!
Anyway, with this, you can pack your levels on Linux and whatnot, and they properly unpack under vanilla KS!? AMAZING!

I was too lazy to make it easy to use I guess, so you have to be in the same directory as the level you want to compress and
Code: [Select]
/path/to/script level\ directory\ name
Also, it's probably pretty messy, and I'm just commenting it now, but that shouldn't be too much of a problem?!

Code: [Select]
#!/usr/bin/python

import os, sys

archive = open(sys.argv[1] + ".knytt.bin", "w") # GO GET EM
path = "" # I guess this is the path from the root (where you executed the script)

def tobytestring(data, length): # convert FILE SIZES into base-256 representation things that tell the unpacker how far the file goes
    bytestring = ""
    blahness = data
    for i in range(length):
        x = blahness % 256
        bytestring += chr(x)
        blahness = (blahness - x) / 256
    return bytestring

def putfile(filename): # put a file into the file
    global path
    file = open(filename, "r")
    contents = file.read()
    length = len(contents)
    archive.write("NF%s%s\0%s%s" % (path, filename, tobytestring(length, 4), contents)) # I'm guessing NF is NEW FILE and then that next bit is path, then filename, then a null terminator, then four bytes of size information, then file contents. Yep.
    file.close()

def putdir(dirname, isroot):
    global path
    os.chdir(dirname)
    if not isroot:
        oldpath = path
        path += dirname + "\\"
    for file in os.listdir("."):
        if os.path.isdir(file):
            putdir(file, False)
        else:
            putfile(file)
    os.chdir("..")
    if not isroot:
        path = oldpath

archive.write("NF%s\0BLAH" % sys.argv[1]) # I couldn't find any rhyme or reason to this size thing (the size of the level directory?! I don't even know) so I just put in BLAH and it seems to work just fine.
putdir(sys.argv[1], True)
archive.close()
o__  o

*

Offline AA

  • 510
  • 23
  • Was ITA84
    • View Profile
    • Insight on Videogames
Re: Amazing! Pack Knytt Stories levels with Python!
« Reply #1 on: February 10, 2010, 08:17:59 »
Code: [Select]
archive.write("NF%s\0BLAH" % sys.argv[1]) # I couldn't find any rhyme or reason to this size thing (the size of the level directory?! I don't even know) so I just put in BLAH and it seems to work just fine.
The four bytes following the level name just count the number of files that were in the level directory to be packed; it seems the game doesn't check it though, so you can put in anything.

The original editor also skips some files when packing a level, namely the editor default settings and the Map.bin backups. This has the side effect that the counter I mentioned before is always higher than the number of files in the packed level, because the editor counts skipped files as well.
Videogames are for everyone, by everyone