-
-
Save polaris-/516218c68c4cd4974e10 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import struct | |
import shutil | |
import subprocess | |
import os | |
def read_byte(file): | |
return struct.unpack("<B", file.read(1))[0] | |
def read_short(file): | |
return struct.unpack("<H", file.read(2))[0] | |
def read_int(file): | |
return struct.unpack("<I", file.read(4))[0] | |
def run_command(cmd, quiet): | |
print cmd | |
if quiet: | |
with open(os.devnull, 'wb') as devnull: | |
subprocess.check_call(cmd, stdout=devnull, stderr=subprocess.STDOUT) | |
else: | |
subprocess.check_call(cmd) | |
print "" | |
def convert(filename): | |
outfilename = os.path.splitext(filename)[0] + ".ctpk" | |
width = 0 | |
height = 0 | |
data = [] | |
with open(filename, "rb") as file: | |
if file.read(0x04) != "TEX\0": | |
print "Not a valid TEX file" | |
exit(-1) | |
file.seek(0x07) | |
width = read_short(file) | |
height = read_short(file) | |
file.seek(0x14) | |
data = file.read() | |
size = struct.pack("<I", len(data)) | |
width = struct.pack("<H", width) | |
height = struct.pack("<H", height) | |
with open(outfilename, "wb") as outfile: | |
outfile.write(bytearray([0x43, 0x54, 0x50, 0x4B, 0x01, 0x00, 0x01, 0x00, 0x80, 0x00, 0x00, 0x00])) | |
outfile.write(size) | |
outfile.write(bytearray([0x58, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 0x00])) | |
outfile.write(size) | |
outfile.write(bytearray([0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00])) | |
outfile.write(width) | |
outfile.write(height) | |
outfile.write(bytearray([0x01, 0x02, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x98, 0xEF, 0xB5, 0xB9, 0x00, 0x00, 0x04, 0x00, 0x6F, 0x75, 0x74, 0x70, 0x75, 0x74, 0x2E, 0x74, 0x67, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x49, 0xA5, 0xA1, 0x38, 0x00, 0x00, 0x00, 0x00, 0x01, 0x0B, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])) | |
outfile.write(data) | |
run_command("ctpktool.exe %s" % outfilename, False) | |
convert(sys.argv[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment