Advertisement
Lavos

Durararasa

Sep 12th, 2014
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.86 KB | None | 0 0
  1. import struct,os,sys,tarfile,StringIO
  2.  
  3. opcodes = [
  4.             [0x0,'0x0',0],[0x1,'0x1',0],[0x2,'0x2',1],[0x3,'string',1],[0x4,'0x4',1],[0x5,'0x5',1],
  5.             [0x6,'0x6',0],[0x8,'jump-0x8',1],[0x9,'jump-0x9',1],
  6.             [0xd,'call-0xd',2],[0xe,'0xe',2],[0xf,'return',0],
  7.             [0x10,'0x10',0],[0x11,'0x11',0],[0x12,'0x12',1],
  8.             [0x13,'0x13',1],[0x14,'0x14',1],[0x2c,'0x2c',1],
  9.             [0x3c,'0x3c',0],[0x3d,'0x3d',0],[0x3e,'0x3e',0],[0x40,'0x40',0],[0x41,'0x41',0],[0x43,'0x43',0],[0x45,'0x45',0],
  10.  
  11.             #unchecked
  12.             [0x2e,'0x2e',1],[0x30,'0x30',0],[0x31,'0x31',0],[0x32,'0x32',0],[0x33,'0x33',0],[0x34,'0x34',0],[0x36,'0x36',0],
  13.             [0x3f,'0x3f',0],[0x44,'0x44',0],
  14.           ]
  15.  
  16. def get_data(filename):
  17.     totalbytes = os.path.getsize(filename)
  18.     infile = open(filename, 'rb')
  19.     totalfiledata = infile.read(totalbytes)
  20.     return totalfiledata
  21.  
  22. def extract(filename,typea):
  23.     filedata = get_data(filename)
  24.     newfiledata = ''
  25.    
  26.     i = 0
  27.     while i < len(filedata):
  28.         opcode = struct.unpack('<I',filedata[i:i+4])[0]
  29.         if opcode >= 0x100:
  30.             break;
  31.         c = 0
  32.         while c != len(opcodes):
  33.             if opcodes[c][0] == opcode:
  34.                 variablecount = opcodes[c][2]
  35.                 name = opcodes[c][1]
  36.                 break;
  37.             if c+1 == len(opcodes):
  38.                 print 'Unknown opcode: %x at loc: %x' % (opcode,i)
  39.                 outfile = open('out.txt','wb')
  40.                 outfile.write(newfiledata)
  41.                 outfile.close()
  42.                 sys.exit()
  43.             c += 1
  44.  
  45.         newfiledata += ('%x::' % i) + name + ' { '
  46.  
  47.         varsmoved = 0
  48.         i += 4
  49.         for a in xrange(0,variablecount):
  50.             var = struct.unpack('<I',filedata[i:i+4])[0]
  51.  
  52.             if opcode == 0x3:
  53.                 string = filedata[var*0x4:filedata.find('\x00',var*0x4)]
  54.                 newfiledata += '/////////////////////////////////////\n' + string + '\n///////////////////////////////////////////////////'
  55.             elif opcode == 0x8:
  56.                 newfiledata += '%x ' % (var*0x4)
  57.             elif opcode == 0x9:
  58.                 newfiledata += '%x ' % (var*0x4)
  59.             elif opcode == 0xd and varsmoved == 1:
  60.                 newfiledata += '%x ' % (var*0x4)
  61.             else:
  62.                 newfiledata += '%x ' % var
  63.  
  64.             varsmoved += 1
  65.             i += 4
  66.  
  67.         newfiledata += '}\n'
  68.         if opcode == 0xf:
  69.             newfiledata += '\n'
  70.     if typea == '-e2':
  71.         outfile = open('Scripts\\' + filename.rsplit('\\',2)[1] + '\\' + filename.rsplit('\\',1)[1] + '.txt','wb')
  72.     else:
  73.         outfile = open(filename + '.txt','wb')
  74.     outfile.write(newfiledata)
  75.     outfile.close()
  76.  
  77. def compiled(filename,typea):
  78.     filedata = get_data(filename).splitlines()
  79.     newfiledata = ''
  80.     newstrings = ''
  81.     stringlocs = []
  82.     fixjumplocs = {}
  83.     alllocs = {}
  84.    
  85.     i = 0
  86.     while i < len(filedata):
  87.         if filedata[i] != '':
  88.             newline = False
  89.             try:
  90.                 line = filedata[i].split('::')[1].split(' ')
  91.             except Exception:
  92.                 newline = True
  93.                 line = filedata[i].split(' ')
  94.  
  95.             c = 0
  96.             while c != len(opcodes):
  97.                 if opcodes[c][1] == line[0]:
  98.                     variablecount = opcodes[c][2]
  99.                     opcode = opcodes[c][0]
  100.                     if newline == True:
  101.                         alllocs[len(newfiledata) / 0x4] = (len(newfiledata) / 0x4)
  102.                     else:
  103.                         alllocs[int(filedata[i].split('::')[0],16) / 0x4] = (len(newfiledata) / 0x4)
  104.                     break;
  105.                 if c+1 == len(opcodes):
  106.                     print 'Unknown opcode: %s on line: %d' % (line[0],i)
  107.                     outfile = open('out.data','wb')
  108.                     outfile.write(newfiledata)
  109.                     outfile.close()
  110.                     sys.exit()
  111.                 c += 1
  112.                
  113.             newfiledata += struct.pack('<I',opcode)
  114.             line.pop(0)
  115.             line.pop(0)
  116.             line.pop(len(line)-1)
  117.  
  118.             for a in xrange(0,variablecount):
  119.                 if opcode == 0x3:
  120.                     i += 1
  121.                     line = filedata[i]
  122.                     newstring = ''
  123.                     if line.find('/////') == -1:
  124.                         while i < len(filedata):
  125.                             newstring += line
  126.                             i += 1
  127.                             line = filedata[i]
  128.                             if line.find('/////') != -1:
  129.                                 break;
  130.                             else:
  131.                                 newstring += '\n'
  132.  
  133.                     if newstring == '' or len(newstring) % 0x4 == 0:
  134.                         newstring += '\x00\x00\x00\x00'
  135.                     while len(newstring) % 0x4 != 0:
  136.                         newstring += '\x00'
  137.  
  138.                     stringlocs.append([len(newfiledata),newstring])
  139.                     newfiledata += struct.pack('<I',0x0) # fix later
  140.                    
  141.                 else:
  142.                     var = int(line[a],16)
  143.  
  144.                     if opcode == 0x8:
  145.                         #var /= 0x4
  146.                         fixjumplocs[len(newfiledata)] = var / 0x4
  147.                         newfiledata += struct.pack('<I',0) # fix later
  148.                     elif opcode == 0x9:
  149.                         #var /= 0x4
  150.                         fixjumplocs[len(newfiledata)] = var / 0x4
  151.                         newfiledata += struct.pack('<I',0) # fix later
  152.                     elif opcode == 0xd and a == 1:
  153.                         #var /= 0x4
  154.                         fixjumplocs[len(newfiledata)] = var / 0x4
  155.                         newfiledata += struct.pack('<I',0) # fix later
  156.                     else:
  157.                         newfiledata += struct.pack('<I',var)
  158.         i += 1
  159.  
  160.     #fix jump locs
  161.     for filedataloc,jumploc in fixjumplocs.iteritems():
  162.         jumploctmp = alllocs[jumploc]
  163.         newfiledata = newfiledata[:filedataloc] + struct.pack('<I',jumploctmp) + newfiledata[filedataloc+0x4:]
  164.  
  165.     #fix string locs
  166.     for i in xrange(0,len(stringlocs)):
  167.         newfiledata = newfiledata[:stringlocs[i][0]] + struct.pack('<I',len(newfiledata) / 0x4) + newfiledata[stringlocs[i][0]+0x4:]
  168.         newfiledata += stringlocs[i][1]
  169.  
  170.     if typea == '-c1':
  171.         outfile = open(filename.rsplit('.',1)[0],'wb')
  172.         outfile.write(newfiledata)
  173.         outfile.close()
  174.     else:
  175.         return newfiledata
  176.  
  177. def unpackscripts(filename):
  178.     tf = tarfile.TarFile(sys.argv[2] + '\\' + filename)
  179.     members = tf.getmembers()
  180.     if not os.path.exists('Unpacked\\' + filename[:-4]):
  181.         os.mkdir('Unpacked\\' + filename[:-4])
  182.     for member in members:
  183.         tf.extract(member, 'Unpacked\\' + filename[:-4])
  184.  
  185. if __name__ == '__main__':
  186.    
  187.     if sys.argv[1] == '-e1':
  188.         extract(sys.argv[2],'-e1')
  189.        
  190.     elif sys.argv[1] == '-e2':
  191.         if not os.path.exists('Scripts'):
  192.             os.mkdir('Scripts')
  193.         for root, dirs, files in os.walk(sys.argv[2]):
  194.             try:
  195.                 print 'Extracting %s' % os.path.join(root).rsplit('\\',1)[1]
  196.             except Exception:
  197.                 continue;
  198.             for filename in files:
  199.                 if filename.find('.ssb') != -1:
  200.                     if not os.path.exists('Scripts\\' + os.path.join(root).rsplit('\\',1)[1]):
  201.                         os.mkdir('Scripts\\' + os.path.join(root).rsplit('\\',1)[1])
  202.                     extract(os.path.join(root, filename),'-e2')
  203.                    
  204.     elif sys.argv[1] == '-c1':
  205.         compiled(sys.argv[2],'-c1')
  206.        
  207.     elif sys.argv[1] == '-c2':
  208.         if not os.path.exists('Compiled'):
  209.             os.mkdir('Compiled')
  210.         for root, dirs, files in os.walk(sys.argv[2]):
  211.             try:
  212.                 print 'Compiling %s' % os.path.join(root).rsplit('\\',1)[1]
  213.             except Exception:
  214.                 continue;
  215.             tf = tarfile.open('Compiled\\' + os.path.join(root).rsplit('\\',1)[1] + '.TAR','w')
  216.             for filename in files:
  217.                 if filename.find('.txt') != -1:
  218.                     filedata = StringIO.StringIO(compiled(os.path.join(root, filename),'-c2'))
  219.                     info = tarfile.TarInfo(name=filename)
  220.                     info.size = len(filedata.buf)
  221.                     tf.addfile(tarinfo=info,fileobj=filedata)
  222.             tf.close()
  223.            
  224.     elif sys.argv[1] == '-unpack':
  225.         if not os.path.exists('Unpacked'):
  226.             os.mkdir('Unpacked')
  227.         for filename in os.listdir(sys.argv[2]):
  228.             if filename.find('.TAR') != -1:
  229.                 print 'Extracting %s' % filename
  230.                 unpackscripts(filename)
  231.     else:
  232.         print 'Bad args'
  233.         sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement