Skip to content

Instantly share code, notes, and snippets.

@JettMonstersGoBoom
Last active August 16, 2021 20:50
Show Gist options
  • Save JettMonstersGoBoom/1292d589187e1643520eb8f678648865 to your computer and use it in GitHub Desktop.
Save JettMonstersGoBoom/1292d589187e1643520eb8f678648865 to your computer and use it in GitHub Desktop.
# load NESST session data and create binary data
# makes it easier to process and only need to hit save. not save each part or name each part
# python nesstNSStoBin.py session.nss data\output
# will create
# data\output.chr
# data\output.map
# data\output.pal
# data\output.msb
import os
import sys
#write a byte array directly
def writeByteArray(name,output):
outfile = open(basename + name,"wb")
outfile.write(output)
outfile.close()
#decode nss rle packed data
def decodeTo(input):
output = bytearray()
prev = 0
x = 0
xlen = len(input)
while(x<xlen-1):
if input[x]=='[':
x+=1
value = 0
# keep going until we hit ]
while(input[x]!=']'):
value = value<<4 | int(input[x],16)
x+=1
# unpack
while(value>1):
output.append(prev)
value-=1
x+=1
else:
value = int(input[x],16)<<4
value |= int(input[x+1],16)
prev = value
output.append(prev)
x+=2
return output
# main
if __name__ == '__main__':
if len(sys.argv)==1:
print("input.nss <basename>")
exit()
inname = sys.argv[1]
finput = open(inname, "r")
# we set the name to default incase we don't get an arg
basename = "default"
if len(sys.argv)>=2:
if sys.argv[2]!=None:
basename = sys.argv[2]
# set some default values
# shouldn't be needed
nameWidth = 0
nameHeight = 0
gridX = 64
gridY = 64
for line in finput:
args = line.split("=")
if args[0]=="VarNameW":
nameWidth = int(args[1])
elif args[0]=="VarNameH":
nameHeight = int(args[1])
elif args[0]=="VarSpriteGridX":
gridX = int(args[1])
elif args[0]=="VarSpriteGridY":
gridY = int(args[1])
elif args[0]=="CHRMain":
chrs = decodeTo(args[1])
writeByteArray(".chr",chrs)
elif args[0]=="NameTable":
nameTable = decodeTo(args[1])
elif args[0]=="AttrTable":
attrTable = decodeTo(args[1])
elif args[0]=="Palette":
pal = decodeTo(args[1])
writeByteArray(".pal",pal)
elif args[0]=="MetaSprites":
metaSpr = decodeTo(args[1])
# process meta sprites here
outfile = open(basename +".msb","wb")
outfile.write(gridX.to_bytes(1,byteorder='little'))
outfile.write(gridY.to_bytes(1,byteorder='little'))
outfile.write(metaSpr)
outfile.close()
finput.close()
# make meta-tiles when we have attr and map
# for now just save
print("Map:" + str(nameWidth) + " * " + str(nameHeight))
outfile = open(basename + ".map","wb")
outfile.write(nameTable)
outfile.write(attrTable)
outfile.write(nameWidth.to_bytes(2,byteorder='little'))
outfile.write(nameHeight.to_bytes(2,byteorder='little'))
outfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment