import struct, sys, os, zlib
# fail on missing filename
if len(sys.argv) < 2:
sys.exit("Usage: python setusegpy.py ")
# open file
try:
file = open(sys.argv[1], "r+b")
except:
sys.exit("Input file could not be read")
# read part of the header
magic, version, fileLength = struct.unpack("<3sBI", file.read(3 + 1 + 4))
# check the header so far
if magic == "FWS":
#print "Compressed: no"
compressed = False
elif magic == "CWS":
#print "Compressed: yes"
compressed = True
else:
sys.exit("Input file does not appear to be a swf")
#print "Version:", version
if version < 9:
print "Swf versions less than 9 do not support UseGPU"
#print "Uncompressed size:", fileLength
# decompress (if necessary)
if compressed:
swf = zlib.decompress(file.read())
#print "Compressed size:", file.tell()
else:
swf = file.read()
if (len(swf) + 3 + 1 + 4) != fileLength:
sys.exit("File length in header is incorrect, can't process the swf")
currentPosition = 0
# skip the RECT in header
sizeNumbits = ord(swf[currentPosition]) >> 3
buffBitsLeft = sizeNumbits * 4 - (8 - 5)
buffBytesLeft = (buffBitsLeft - 1 >> 3) + 1
currentPosition += 1 + buffBytesLeft
# skip the rest of the header
currentPosition += 4
# read first tag, should be FileAttributes
attrHeader, = struct.unpack("> 6
if tagType != 69:
sys.exit("FileAttributes tag not found, can't process the swf")
if tagLength != 4:
sys.exit("FileAttributes tag has invalid length, can't process the swf")
currentPosition += 2
# set the UseGPU bit
flags = ord(swf[currentPosition])
if flags & 0x20:
sys.exit("UseGPU flag is already set, ignoring")
flags |= 0x20 # Note: 0x40 is UseDirectBlit
swf = swf[:currentPosition] + chr(flags) + swf[currentPosition+1:]
# compress the swf string
if compressed:
swf = zlib.compress(swf, 9)
# recompile the swf
file.seek(3 + 1 + 4)
file.write(swf)
file.truncate()
# and we're done!
if compressed:
#print "New compressed size:", file.tell()
pass
print "Success!"