Advertisement
Lavos

Nayuta script dump

Nov 3rd, 2014
391
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.53 KB | None | 0 0
  1. import os,sys,struct
  2.  
  3. def get_data(filename):
  4.     totalbytes = os.path.getsize(filename)
  5.     infile = open(filename, 'rb')
  6.     totalfiledata = infile.read(totalbytes)
  7.     return totalfiledata
  8.  
  9.  
  10. opcodes = [
  11.             [0x8000,'0x8000',[]],[0x8006,'0x8006',[]],[0x8008,'0x8008',[]],[0x8009,'0x8009',[]],
  12.             [0x800b,'0x800b',[]],[0x8013,'0x8013',[]],[0x801f,'0x801f',[]],[0x8020,'0x8020',[]],[0x8021,'0x8021',[]],
  13.             [0x8022,'0x8022',[]],[0x8023,'0x8023',[]],[0x8024,'0x8024',[]],[0x8026,'0x8026',[]],
  14.             [0x802a,'0x802a',[]],[0x802c,'0x802c',[]],
  15.             [0x802e,'0x802e',[]],[0x8032,'0x8032',[]],[0x803b,'0x803b',[]],
  16.             [0x8041,'0x8041',[]],[0x8042,'0x8042',[]],[0x8054,'0x8054',[]],[0x805c,'0x805c',[]],
  17.             [0x8069,'0x8069',[]],[0x806c,'0x806c',[]],
  18.             [0x8076,'0x8076',[]],[0x807b,'0x807b',[]],
  19.             [0x807d,'0x807d',[]],
  20.             [0x8082,'0x8082',[]],
  21.             [0x8098,'0x8098',[]],[0x80be,'0x80be',[]],[0x80bf,'0x80bf',[]],
  22.  
  23.             [0x82dd,'0x82dd',['d']],[0x82de,'0x82de',['f']],[0x82df,'0x82df',['d','s']],
  24.             [0x82e0,'0x82e0',['d','ukn']],
  25.           ]
  26.  
  27. def extract_script(filename):
  28.     if sys.argv[1] == '-e1':
  29.         filedata = get_data(filename)
  30.     else:
  31.         filedata = get_data(sys.argv[2] + '\\' + filename)
  32.     outdata = ''
  33.     outstrings = ''
  34.     hdrend = struct.unpack('<I',filedata[0x3c:0x40])[0]
  35.     pos = 0x18
  36.  
  37.     sections = []
  38.     while pos < hdrend:
  39.         sections.append([filedata[pos:filedata.find('\x00',pos)],struct.unpack('<I',filedata[pos+0x20:pos+0x24])[0],struct.unpack('<I',filedata[pos+0x24:pos+0x28])[0]])
  40.         pos += 0x28
  41.  
  42.     strcnt = 0
  43.     for i in range(0,len(sections)):
  44.         pos = sections[i][2]
  45.         if i > 0:
  46.             outdata += '\n'
  47.         outdata += '### ' + sections[i][0] + ' %x ###\n\n' % struct.unpack('<H',filedata[pos:pos+2])[0]
  48.         pos += 2
  49.         while pos < sections[i][2]+sections[i][1]:
  50.             opcode = struct.unpack('<H',filedata[pos:pos+2])[0]
  51.             if opcode >= 0x8200:
  52.                 c = 0
  53.                 while c != len(opcodes):
  54.                     if opcodes[c][0] == opcode:
  55.                         variables = opcodes[c][2]
  56.                         break;
  57.                     if c+1 == len(opcodes):
  58.                         print 'Unknown script opcode: %x at loc: %x in file %s' % (opcode,pos,filename)
  59.                         sys.exit()
  60.                     c += 1
  61.  
  62.                 outdata += '%x :: %s { ' % (pos,opcodes[c][1])
  63.                 pos += 0x2
  64.                
  65.                 for a in range(0,len(variables)):
  66.                     if variables[a] == 'b':
  67.                         outdata += '%x ' % struct.unpack('<B',filedata[pos:pos+1])[0]
  68.                         pos += 1
  69.                     elif variables[a] == 'w':
  70.                         outdata += '%x ' % struct.unpack('<H',filedata[pos:pos+2])[0]
  71.                         pos += 2
  72.                     elif variables[a] == 'd':
  73.                         if opcode == 0x82df:
  74.                             strlen = struct.unpack('<I',filedata[pos:pos+4])[0]
  75.                         elif opcode == 0x82e0:
  76.                             varlen = struct.unpack('<I',filedata[pos:pos+4])[0]
  77.                         else:
  78.                             outdata += '%x ' % struct.unpack('<I',filedata[pos:pos+4])[0]
  79.                         pos += 4
  80.                     elif variables[a] == 's':
  81.                         if opcode == 0x82df:
  82.                             string = filedata[pos:pos+strlen]
  83.                             #if len(string) > 0:
  84.                             #    while string[len(string)-1] == '\x00':
  85.                             #        string = string[:len(string)-1]
  86.                             outdata += '<string %d> ' % strcnt
  87.                             outstrings += ('%d: ' % strcnt) + string + '\n'
  88.                             strcnt += 1
  89.                             pos += strlen
  90.                     elif variables[a] == 'f':
  91.                         outdata += '%0.1f ' % struct.unpack('<I',filedata[pos:pos+4])[0]
  92.                         pos += 4
  93.                     elif variables[a] == 'ukn':
  94.                         outdata += '%s ' % filedata[pos:pos+varlen]
  95.                         pos += varlen
  96.                     else:
  97.                         print 'Unknown variable type'
  98.                         sys.exit()
  99.             else:
  100.                 outdata += '%x :: 0x%x { ' % (pos,opcode)
  101.                 pos += 0x2
  102.  
  103.             outdata += '}\n'
  104.  
  105.     if not os.path.exists(sys.argv[2] + '\\Scripts'):
  106.         os.mkdir(sys.argv[2] + '\\Scripts')
  107.  
  108.     if sys.argv[1] == '-e1':
  109.         outfile = open(filename.rsplit('.')[0] + '_script.txt','wb')
  110.     else:
  111.         outfile = open(sys.argv[2] + '\\Scripts\\' + filename.rsplit('.')[0] + '_script.txt','wb')
  112.     outfile.write(outdata)
  113.     outfile.close()
  114.     if sys.argv[1] == '-e1':
  115.         outfile = open(filename.rsplit('.')[0] + '_strings.txt','wb')
  116.     else:
  117.         outfile = open(sys.argv[2] + '\\Scripts\\' + filename.rsplit('.')[0] + '_strings.txt','wb')
  118.     outfile.write(outstrings)
  119.     outfile.close()
  120.  
  121. if __name__ == '__main__':
  122.     if sys.argv[1] == '-e1':
  123.         extract_script(sys.argv[2])
  124.     elif sys.argv[1] == '-e2':
  125.         for filename in os.listdir(sys.argv[2]):
  126.             if filename.find ('.bin') != -1:
  127.                 print 'Extracting %s' % filename
  128.                 extract_script(filename)
  129.     else:
  130.         print 'Argument error'
  131.         sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement