Advertisement
Guest User

Untitled

a guest
Sep 17th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Really hacky code, ignore the globals
  2. import glob
  3. import shutil
  4. import os
  5. import sys
  6. import struct
  7.  
  8. data = open(sys.argv[1],"rb").read()
  9.  
  10. idx = 0
  11. buffer_size = 0
  12. images = []
  13. addrs = []
  14.  
  15. def read_header():
  16.     global idx
  17.     header = data[idx:idx+4][::-1]
  18.     section_size, unk, bytes_to_next_section = struct.unpack("<III", data[idx+4:idx+16])
  19.     idx += 16
  20.     return header, section_size, bytes_to_next_section
  21.  
  22. def read_aif():
  23.     global idx, w, h
  24.     read = True
  25.     while read:
  26.         header, section_size, bytes_to_next_section = read_header()
  27.        
  28.         read = bytes_to_next_section != 0
  29.        
  30.         if header == "aRF ":
  31.             idx += section_size - 0x10 # Skip to next section
  32.         elif header == "imgX":
  33.             size = struct.unpack("<HH", data[idx+0x18:idx+0x1c])
  34.             format = struct.unpack("<H", data[idx+0x10:idx+0x12])[0]
  35.             images.append({'size': size, 'format': format})
  36.             idx += section_size - 0x10 # Skip to next section
  37.         else:
  38.             print "Unknown section in AIF: %s @ %08x" % (header, idx)
  39.             exit(1)
  40.  
  41. def read_amf():
  42.     global idx, buffer_size
  43.     read = True
  44.     while read:
  45.         header, section_size, bytes_to_next_section = read_header()
  46.        
  47.         read = bytes_to_next_section != 0
  48.        
  49.         if header == "head":
  50.             idx += section_size - 0x10 # Skip to next section
  51.         elif header == "buff":
  52.             buffer_size = struct.unpack("<I", data[idx:idx+4])[0]
  53.             idx += section_size - 0x10 # Skip to next section
  54.         elif header == "addr":
  55.             addrs.append(struct.unpack("<I", data[idx+0x10:idx+0x14])[0])
  56.             idx += section_size - 0x10 # Skip to next section
  57.         else:
  58.             print "Unknown section in AMF: %s @ %08x" % (header, idx)
  59.             exit(1)
  60.  
  61. def read_archive(data):
  62.     global idx
  63.     read = True
  64.     while read:
  65.         header, section_size, bytes_to_next_section = read_header()
  66.        
  67.         read = bytes_to_next_section != 0
  68.        
  69.         if header == "AIF ":
  70.             read_aif()
  71.         elif header == "AMF ":
  72.             read_amf()
  73.         else:
  74.             print "Unknown section in archive: %s @ %08x" % (header, idx)
  75.             exit(1)
  76.            
  77.  
  78.            
  79. read_archive(data)
  80.  
  81.  
  82. offset = idx
  83. for i in range(0, len(images)):
  84.     w, h = images[i]['size']
  85.     format = images[i]['format']
  86.     addr = addrs[i]
  87.    
  88.     format_output = 0
  89.     format_output_2 = 0 # Swizzle
  90.     if format == 0x08:
  91.         format_output = 0x0c001000 # U8U8U8U8_RGBA
  92.         format_output_2 = 0x60000000 # Linear
  93.     elif format == 0x10:
  94.         format_output = 0x85000000 # DXT1
  95.     elif format == 0x12:
  96.         format_output = 0x86000000 # DXT3
  97.     elif format == 0x14:
  98.         format_output = 0x87000000 # DXT5
  99.     else:
  100.         shutil.copy(sys.argv[1], "unknown/" + sys.argv[1])
  101.         print "Unknown image format: %04x" % format
  102.         exit(-1)
  103.    
  104.     img = data[offset:offset+addr]
  105.     offset += addr
  106.    
  107.  
  108.     # Write gxt file
  109.     output = sys.argv[1].replace(".aif","") + ("_%04d" % (i)) + ".gxt"    
  110.    
  111.     gxt = open(output,"wb")
  112.     gxt.write(bytearray([0x47, 0x58, 0x54, 0x00, 0x03, 0x00, 0x00, 0x10, 0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00]))
  113.     gxt.write(struct.pack("<I", len(img)))
  114.     gxt.write(bytearray([0x00] * 0x0c))
  115.     gxt.write(struct.pack("<I", 0x40))
  116.     gxt.write(struct.pack("<I", len(img)))
  117.     gxt.write(struct.pack("<I", 0xffffffff))
  118.     gxt.write(bytearray([0x00] * 0x04))
  119.     gxt.write(struct.pack("<I", format_output_2))
  120.     gxt.write(struct.pack("<I", format_output))
  121.     gxt.write(struct.pack("<H", w))
  122.     gxt.write(struct.pack("<H", h))
  123.     gxt.write(struct.pack("<I", 1))
  124.     gxt.write(img)
  125.     gxt.close()
  126.  
  127.     os.system("gxtconvert.exe %s" % output)
  128.     os.remove(output)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement