Advertisement
Lavos

Shin Koihime

Aug 7th, 2014
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 50.84 KB | None | 0 0
  1. import os
  2. import sys
  3. import struct
  4. import re
  5.  
  6. opcodes = [
  7.             [0x0,'end-block',0],[0x1,'0x1',1],[0x2,'0x2',1],[0x3,'jump',1],[0x4,'call-function-local',1],[0x5,'return',0],
  8.             [0xb,'load-script',1],[0xc,'0xc',0],[0xd,'0xd',0],
  9.             [0xe,'0xe',2],[0xf,'0xf',2],[0x11,'0x11',0],[0x15,'0x15',0],[0x18,'0x18',1],
  10.             [0x1f,'0x1f',1],[0x20,'0x20-jumpcond',1],[0x21,'0x21',3],[0x22,'0x22',1],[0x23,'0x23',0],[0x28,'0x28',1],
  11.             [0x2a,'0x2a',4],[0x2b,'0x2b-jumpcond',2],[0x2c,'load-bgm',1],
  12.             [0x2d,'0x2d',4],[0x2e,'0x2e',1],[0x2f,'0x2f',0],
  13.             [0x30,'0x30',2],[0x32,'0x32',2],[0x33,'0x33',4],[0x34,'0x34',1],[0x35,'0x35',2],[0x37,'play-anim',7],
  14.             [0x38,'0x38',1],[0x3c,'move-screen',2],
  15.             [0x41,'0x41',1],[0x42,'0x42',1],[0x43,'load-textfile',1],[0x44,'0x44',1],[0x47,'0x47-load-bin',2],
  16.             [0x48,'0x48',1],[0x49,'0x49-jumpcond',1],[0x4a,'0x4a',0],[0x4b,'0x4b',4],
  17.             [0x4c,'0x4c',3],[0x4f,'string-counter',1],
  18.             [0x50,'0x50',0],[0x53,'0x53',1],[0x57,'0x57',0],[0x58,'0x58',1],[0x5a,'0x5a',1],[0x5b,'0x5b',1],
  19.             [0x5c,'0x5c',2],[0x5e,'0x5e',2],[0x5f,'0x5f',2],
  20.             [0x60,'display-image',5],[0x61,'image-transition',6],[0x62,'image-rect-loc',3],[0x63,'0x63',4],
  21.             [0x64,'create-image-rect',3],[0x65,'0x65',4],[0x66,'0x66',2],[0x67,'0x67',4],
  22.             [0x6a,'load-background',1],[0x6b,'0x6b',0],[0x6c,'image-load-loc',2],[0x6d,'load-dds-texture',3],
  23.             [0x6e,'load-png-effect',2],
  24.             [0x6f,'0x6f',0],[0x70,'narration',1],[0x71,'named-text',2],[0x73,'windowless-text',2],[0x74,'0x74',1],
  25.             [0x77,'0x77',0],[0x79,'0x79',0],
  26.             [0x7f,'0x7f',0xb],[0x81,'0x81',0],[0x82,'0x82-load-bin',3],[0x83,'0x83',5],
  27.             [0x84,'textbox-open/close',2],[0x86,'0x86',3],[0x88,'0x88',1],[0x89,'0x89',1],[0x8a,'0x8a',1],
  28.             [0x8f,'0x8f',6],[0x90,'0x90',1],[0x91,'0x91',2],[0x92,'play-voice',3],
  29.           ]
  30.  
  31. opcodes1 = [
  32.             [0x0,'end-block',0],[0x1,'0x1',1],[0x2,'0x2',1],[0x3,'jump',1],[0x4,'call-function-local',1],[0x5,'return',0],
  33.             [0xd,'0xd',0],[0xe,'0xe',2],[0x11,'0x11',0],[0x1f,'0x1f',2],[0x20,'0x20-jumpcond',1],
  34.             [0x21,'0x21',3],[0x23,'0x23',0],[0x28,'0x28',1],[0x2c,'0x2c',0],
  35.             [0x2d,'0x2d',1],[0x2e,'0x2e',1],[0x2f,'0x2f',1],[0x30,'0x30',2],[0x34,'0x34',1],[0x38,'0x38',1],
  36.             [0x3c,'move-screen',2],
  37.             [0x41,'0x41',1],[0x42,'0x42',1],[0x45,'0x45',0],[0x49,'0x49-jumpcond',1],[0x4e,'0x4e',1],
  38.             [0x57,'0x57',0],[0x58,'0x58',1],[0x5a,'0x5a',1],[0x5b,'0x5b',1],[0x5c,'0x5c',2],[0x5d,'0x5d',3],
  39.             [0x5e,'0x5e',2],[0x5f,'0x5f',2],[0x60,'display-image',5],
  40.             [0x61,'0x61',6],[0x62,'image-rect-loc',3],[0x63,'0x63',4],[0x64,'create-image-rect',3],[0x65,'0x65',4],
  41.             [0x66,'0x66',2],[0x6a,'load-background',1],[0x6b,'0x6b',0],[0x6c,'image-load-loc',2],
  42.             [0x6d,'load-dds-texture',3],[0x6e,'load-png-effect',2],[0x6f,'0x6f',0],[0x74,'0x74',1],
  43.             [0x77,'0x77',0],
  44.             [0x82,'0x82-load-bin',3],[0x84,'0x84',2],[0x86,'0x86',3],[0x88,'0x88',1],[0x8a,'0x8a',1],
  45.            ]
  46.  
  47. opcodesmessage = [
  48.                    [0x0,'0x0',0],[0x1,'0x1',1],[0x2c,'string',2],[0x2d,'0x2d',2],[0x2e,'0x2e',2],[0x2f,'0x2f',1],[0x30,'0x30',1],
  49.                  ]
  50.  
  51. def get_data(filename):
  52.     totalbytes = os.path.getsize(filename)
  53.     infile = open(filename, 'rb')
  54.     totalfiledata = infile.read(totalbytes)
  55.     return totalfiledata
  56.  
  57. def writeopcode(i,outfiledata,mainorno,funccount,opcodesmoved):
  58.     opcode = struct.unpack('<I',filedata[i:i+0x4])[0]
  59.     c = 0
  60.     if mainorno == 0:
  61.         while c != len(opcodes):
  62.             if opcodes[c][0] == opcode:
  63.                 variablecount = opcodes[c][2]
  64.                 name = opcodes[c][1]
  65.                 break;
  66.             if c+1 == len(opcodes):
  67.                 print 'Unknown script opcode: %x at loc: %x' % (opcode,i)
  68.                 sys.exit()
  69.             c += 1
  70.     else:
  71.         while c != len(opcodes1):
  72.             if opcodes1[c][0] == opcode:
  73.                 variablecount = opcodes1[c][2]
  74.                 name = opcodes1[c][1]
  75.                 break;
  76.             if c+1 == len(opcodes1):
  77.                 print 'Unknown pre-script opcode: %x at loc: %x' % (opcode,i)
  78.                 sys.exit()
  79.             c += 1
  80.  
  81.     outfiledata += ('{ %x' % opcodesmoved) + ' } ' + name + ' '
  82.     i += 0x4
  83.     endlen = i+(variablecount*0x4)
  84.     variablesmoved = 0
  85.  
  86.     if opcode == 0x0 and mainorno == 0:
  87.         outfiledata += '\n'
  88.  
  89.     elif opcode == 0x5:
  90.         outfiledata += '\n'
  91.  
  92.         if i != textstart and i != scriptstart:
  93.             #print '%x - scriptstart %x textstart %x' % (i,scriptstart,textstart)
  94.             outfiledata += '\n=== Function %d ===\n' % funccount
  95.             funccount += 1
  96.         elif i == scriptstart:
  97.             outfiledata = outfiledata[:-1]
  98.        
  99.     while i < endlen:
  100.         var = struct.unpack('<I',filedata[i:i+0x4])[0]
  101.  
  102.         if mainorno == 0:
  103.  
  104.             #-----------------------------
  105.             if opcode == 0x1:
  106.                 if var == 1:
  107.                     outfiledata += '"Wait for input" '
  108.                 else:
  109.                     outfiledata += '%x ' % var
  110.  
  111.             #-----------------------------
  112.             elif opcode == 0x3:
  113.                 if variablesmoved == 0:
  114.                     for a in range(0,len(opcodelocs)):
  115.                         if opcodelocs[a][1] == var:
  116.                             var = opcodelocs[a][0]
  117.                             break;
  118.                         if a+1 == len(opcodelocs):
  119.                             print 'Unable to find jump location %x at loc %x' % (var,i)
  120.                             sys.exit()
  121.                     outfiledata += 'label_%x ' % var
  122.                    
  123.             #-----------------------------
  124.             elif opcode == 0x4:
  125.                 if variablesmoved == 0:
  126.                     for a in range(0,len(opcodelocs)):
  127.                         if opcodelocs[a][1] == var:
  128.                             var = opcodelocs[a][0]
  129.                             break;
  130.                         if a+1 == len(opcodelocs):
  131.                             print 'Unable to find jump location %x at loc %x' % (var,i)
  132.                             sys.exit()
  133.                     outfiledata += 'label_%x ' % var
  134.  
  135.             #-----------------------------
  136.             elif opcode == 0xb:
  137.                 if variablesmoved == 0:
  138.                     string = filedata[var:filedata.find('\x00',var)]
  139.                     string = string.replace('\n','\\n')
  140.                     outfiledata += '"' + string + '" '
  141.  
  142.             #-----------------------------
  143.             elif opcode == 0x1f:
  144.                 if variablesmoved == 0:
  145.                     for a in range(0,len(opcodelocs)):
  146.                         if opcodelocs[a][1] == var:
  147.                             var = opcodelocs[a][0]
  148.                             break;
  149.                         if a+1 == len(opcodelocs):
  150.                             print 'Unable to find jump location %x at loc %x' % (var,i)
  151.                             sys.exit()
  152.                     outfiledata += 'label_%x ' % var
  153.                    
  154.             #-----------------------------
  155.             elif opcode == 0x20:
  156.                 if variablesmoved == 0:
  157.                     for a in range(0,len(opcodelocs)):
  158.                         if opcodelocs[a][1] == var:
  159.                             var = opcodelocs[a][0]
  160.                             break;
  161.                         if a+1 == len(opcodelocs):
  162.                             print 'Unable to find jump location %x at loc %x' % (var,i)
  163.                             sys.exit()
  164.                     outfiledata += 'label_%x ' % var
  165.  
  166.             #-----------------------------
  167.             elif opcode == 0x2a:
  168.                 if variablesmoved == 1:
  169.                     for a in range(0,len(opcodelocs)):
  170.                         if opcodelocs[a][1] == var:
  171.                             var = opcodelocs[a][0]
  172.                             break;
  173.                         if a+1 == len(opcodelocs):
  174.                             print 'Unable to find jump location %x at loc %x' % (var,i)
  175.                             sys.exit()
  176.                     outfiledata += 'label_%x ' % var
  177.                 else:
  178.                     outfiledata += '%x ' % var
  179.  
  180.             #-----------------------------
  181.             elif opcode == 0x2b:
  182.                 if variablesmoved == 1:
  183.                     for a in range(0,len(opcodelocs)):
  184.                         if opcodelocs[a][1] == var:
  185.                             var = opcodelocs[a][0]
  186.                             break;
  187.                         if a+1 == len(opcodelocs):
  188.                             print 'Unable to find jump location %x at loc %x' % (var,i)
  189.                             sys.exit()
  190.                     outfiledata += 'label_%x ' % var
  191.                 else:
  192.                     outfiledata += '%x ' % var
  193.            
  194.             #-----------------------------
  195.             elif opcode == 0x2c:
  196.                 if variablesmoved == 0:
  197.                     string = filedata[var:filedata.find('\x00',var)]
  198.                     string = string.replace('\n','\\n')
  199.                     outfiledata += '"' + string + '" '
  200.  
  201.             #-----------------------------
  202.             elif opcode == 0x37:
  203.                 if variablesmoved == 5 or variablesmoved == 6:
  204.                     for a in range(0,len(opcodelocs)):
  205.                         if opcodelocs[a][1] == var:
  206.                             var = opcodelocs[a][0]
  207.                             break;
  208.                         if a+1 == len(opcodelocs):
  209.                             print 'Unable to find jump location %x at loc %x' % (var,i)
  210.                             sys.exit()
  211.                     outfiledata += 'label_%x ' % var
  212.                 else:
  213.                     outfiledata += '%x ' % var
  214.  
  215.             #-----------------------------
  216.             elif opcode == 0x3c:
  217.                 if variablesmoved == 0:
  218.                     outfiledata += '"X: %x" ' % var
  219.                 elif variablesmoved == 1:
  220.                     outfiledata += '"Y: %x" ' % var
  221.  
  222.             #-----------------------------
  223.             elif opcode == 0x43:
  224.                 if variablesmoved == 0:
  225.                     string = filedata[var:filedata.find('\x00',var)]
  226.                     string = string.replace('\n','\\n')
  227.                     outfiledata += '"' + string + '" '
  228.  
  229.             #-----------------------------
  230.             elif opcode == 0x47:
  231.                 if variablesmoved == 1:
  232.                     string = filedata[var:filedata.find('\x00',var)]
  233.                     string = string.replace('\n','\\n')
  234.                     outfiledata += '"' + string + '" '
  235.                 else:
  236.                     outfiledata += '%x ' % var
  237.  
  238.             #-----------------------------
  239.             elif opcode == 0x49:
  240.                 if variablesmoved == 0:
  241.                     for a in range(0,len(opcodelocs)):
  242.                         if opcodelocs[a][1] == var:
  243.                             var = opcodelocs[a][0]
  244.                             break;
  245.                         if a+1 == len(opcodelocs):
  246.                             print 'Unable to find jump location %x at loc %x' % (var,i)
  247.                             sys.exit()
  248.                     outfiledata += 'label_%x ' % var
  249.  
  250.             #-----------------------------
  251.             elif opcode == 0x4f:
  252.                 if variablesmoved == 0:
  253.                     outfiledata += '%d ' % var
  254.  
  255.             #-----------------------------
  256.             elif opcode == 0x60:
  257.                 if variablesmoved == 1:
  258.                     outfiledata += '"R: %x" ' % var
  259.                 elif variablesmoved == 2:
  260.                     outfiledata += '"G: %x" ' % var
  261.                 elif variablesmoved == 3:
  262.                     outfiledata += '"B: %x" ' % var
  263.                 elif variablesmoved == 4:
  264.                     outfiledata += '"A: %x" ' % var
  265.                 else:
  266.                     outfiledata += '%x ' % var
  267.  
  268.             #-----------------------------
  269.             elif opcode == 0x61:
  270.                 if variablesmoved == 1:
  271.                     outfiledata += '"TransTime: %x" ' % var
  272.                 elif variablesmoved == 2:
  273.                     outfiledata += '"R: %x" ' % var
  274.                 elif variablesmoved == 3:
  275.                     outfiledata += '"G: %x" ' % var
  276.                 elif variablesmoved == 4:
  277.                     outfiledata += '"B: %x" ' % var
  278.                 elif variablesmoved == 5:
  279.                     outfiledata += '"A: %x" ' % var
  280.                 else:
  281.                     outfiledata += '%x ' % var
  282.  
  283.             #-----------------------------
  284.             elif opcode == 0x62:
  285.                 if variablesmoved == 1:
  286.                     outfiledata += '"X: %x" ' % var
  287.                 elif variablesmoved == 2:
  288.                     outfiledata += '"Y: %x" ' % var
  289.                 else:
  290.                     outfiledata += '%x ' % var
  291.  
  292.             #-----------------------------
  293.             elif opcode == 0x64:
  294.                 if variablesmoved == 1:
  295.                     outfiledata += '"X: %x" ' % var
  296.                 elif variablesmoved == 2:
  297.                     outfiledata += '"Y: %x" ' % var
  298.                 else:
  299.                     outfiledata += '%x ' % var
  300.  
  301.             #-----------------------------
  302.             elif opcode == 0x6a:
  303.                 if variablesmoved == 0:
  304.                     string = filedata[var:filedata.find('\x00',var)]
  305.                     string = string.replace('\n','\\n')
  306.                     outfiledata += '"' + string + '" '
  307.  
  308.             #-----------------------------
  309.             elif opcode == 0x6d:
  310.                 if variablesmoved == 1:
  311.                     string = filedata[var:filedata.find('\x00',var)]
  312.                     string = string.replace('\n','\\n')
  313.                     outfiledata += '"' + string + '" '
  314.                 elif variablesmoved == 2:
  315.                     string = filedata[var:filedata.find('\x00',var)]
  316.                     string = string.replace('\n','\\n')
  317.                     outfiledata += '"' + string + '" '
  318.                 else:
  319.                     outfiledata += '%x ' % var
  320.  
  321.             #-----------------------------
  322.             elif opcode == 0x6e:
  323.                 if variablesmoved == 1:
  324.                     string = filedata[var:filedata.find('\x00',var)]
  325.                     string = string.replace('\n','\\n')
  326.                     outfiledata += '"' + string + '" '
  327.                 else:
  328.                     outfiledata += '%x ' % var
  329.  
  330.             #-----------------------------
  331.             elif opcode == 0x70:
  332.                 if variablesmoved == 0:
  333.                     string = filedata[var:filedata.find('\x00',var)]
  334.                     #string = string.replace('\n','\\n')
  335.                     outfiledata += '\n////////////////////////////////////////////////////////\n' + string + '\n//////////////////////////////////////////////////////// '
  336.                    
  337.             #-----------------------------
  338.             elif opcode == 0x71:
  339.                 if variablesmoved == 0:
  340.                     string = filedata[var:filedata.find('\x00',var)]
  341.                     #string = string.replace('\n','\\n')
  342.                     outfiledata += '\n////////////////////////////////////////////////////////\n' + string
  343.                 elif variablesmoved == 1:
  344.                     string = filedata[var:filedata.find('\x00',var)]
  345.                     #string = string.replace('\n','\\n')
  346.                     outfiledata += '\n////////////////////////////////////////////////////////\n' + string + '\n/////////////// '
  347.  
  348.             #-----------------------------
  349.             elif opcode == 0x73:
  350.                 if variablesmoved == 1:
  351.                     string = filedata[var:filedata.find('\x00',var)]
  352.                     #string = string.replace('\n','\\n')
  353.                     outfiledata += '\n////////////////////////////////////////////////////////\n' + string + '\n//////////////////////////////////////////////////////// '
  354.                 else:
  355.                     outfiledata += '%x ' % var
  356.  
  357.             #-----------------------------
  358.             elif opcode == 0x7f:
  359.                 if variablesmoved == 0xa:
  360.                     for a in range(0,len(opcodelocs)):
  361.                         if opcodelocs[a][1] == var:
  362.                             var = opcodelocs[a][0]
  363.                             break;
  364.                         if a+1 == len(opcodelocs):
  365.                             print 'Unable to find jump location %x at loc %x' % (var,i)
  366.                             sys.exit()
  367.                     outfiledata += 'label_%x ' % var
  368.                 else:
  369.                     outfiledata += '%x ' % var
  370.  
  371.             #-----------------------------
  372.             elif opcode == 0x82:
  373.                 if variablesmoved == 1:
  374.                     string = filedata[var:filedata.find('\x00',var)]
  375.                     string = string.replace('\n','\\n')
  376.                     outfiledata += '"' + string + '" '
  377.                 else:
  378.                     outfiledata += '%x ' % var
  379.                    
  380.             #-----------------------------
  381.             elif opcode == 0x83:
  382.                 if variablesmoved == 1:
  383.                     string = filedata[var:filedata.find('\x00',var)]
  384.                     string = string.replace('\n','\\n')
  385.                     outfiledata += '"' + string + '" '
  386.                 else:
  387.                     outfiledata += '%x ' % var
  388.  
  389.             #-----------------------------
  390.             elif opcode == 0x84:
  391.                 if variablesmoved == 0:
  392.                     outfiledata += '%x ' % var
  393.                 elif variablesmoved == 1:
  394.                     if var == 0:
  395.                         outfiledata += 'close '
  396.                     elif var == 1:
  397.                         outfiledata += 'open '
  398.  
  399.             #-----------------------------
  400.             elif opcode == 0x92:
  401.                 if variablesmoved == 0:
  402.                     string = filedata[var:filedata.find('\x00',var)]
  403.                     string = string.replace('\n','\\n')
  404.                     outfiledata += '"' + string + '" '
  405.                 elif variablesmoved == 1:
  406.                     outfiledata += '"Vol: %x" ' % var
  407.                    
  408.                 else:
  409.                     outfiledata += '%x ' % var
  410.  
  411.             #-----------------------------
  412.             else:
  413.                 outfiledata += '%x ' % var
  414.  
  415.         else:
  416.  
  417.             #-----------------------------
  418.             if opcode == 0x3:
  419.                 if variablesmoved == 0:
  420.                     for a in range(0,len(opcodelocs)):
  421.                         if opcodelocs[a][1] == var:
  422.                             var = opcodelocs[a][0]
  423.                             break;
  424.                         if a+1 == len(opcodelocs):
  425.                             print 'Unable to find jump location %x at loc %x' % (var,i)
  426.                             sys.exit()
  427.                     outfiledata += 'label_%x ' % var
  428.                    
  429.             #-----------------------------
  430.             elif opcode == 0x4:
  431.                 if variablesmoved == 0:
  432.                     for a in range(0,len(opcodelocs)):
  433.                         if opcodelocs[a][1] == var:
  434.                             var = opcodelocs[a][0]
  435.                             break;
  436.                         if a+1 == len(opcodelocs):
  437.                             print 'Unable to find jump location %x at loc %x' % (var,i)
  438.                             sys.exit()
  439.                     outfiledata += 'label_%x ' % var
  440.  
  441.             #-----------------------------
  442.             elif opcode == 0x20:
  443.                 if variablesmoved == 0:
  444.                     for a in range(0,len(opcodelocs)):
  445.                         if opcodelocs[a][1] == var:
  446.                             var = opcodelocs[a][0]
  447.                             break;
  448.                         if a+1 == len(opcodelocs):
  449.                             print 'Unable to find jump location %x at loc %x' % (var,i)
  450.                             sys.exit()
  451.                     outfiledata += 'label_%x ' % var
  452.  
  453.             #-----------------------------
  454.             elif opcode == 0x3c:
  455.                 if variablesmoved == 0:
  456.                     outfiledata += '"X: %x" ' % var
  457.                 elif variablesmoved == 1:
  458.                     outfiledata += '"Y: %x" ' % var
  459.  
  460.             #-----------------------------
  461.             elif opcode == 0x49:
  462.                 if variablesmoved == 0:
  463.                     for a in range(0,len(opcodelocs)):
  464.                         if opcodelocs[a][1] == var:
  465.                             var = opcodelocs[a][0]
  466.                             break;
  467.                         if a+1 == len(opcodelocs):
  468.                             print 'Unable to find jump location %x at loc %x' % (var,i)
  469.                             sys.exit()
  470.                     outfiledata += 'label_%x ' % var
  471.  
  472.             #-----------------------------
  473.             elif opcode == 0x60:
  474.                 if variablesmoved == 1:
  475.                     outfiledata += '"R: %x" ' % var
  476.                 elif variablesmoved == 2:
  477.                     outfiledata += '"G: %x" ' % var
  478.                 elif variablesmoved == 3:
  479.                     outfiledata += '"B: %x" ' % var
  480.                 elif variablesmoved == 4:
  481.                     outfiledata += '"A: %x" ' % var
  482.                 else:
  483.                     outfiledata += '%x ' % var
  484.  
  485.             #-----------------------------
  486.             elif opcode == 0x62:
  487.                 if variablesmoved == 1:
  488.                     outfiledata += '"X: %x" ' % var
  489.                 elif variablesmoved == 2:
  490.                     outfiledata += '"Y: %x" ' % var
  491.                 else:
  492.                     outfiledata += '%x ' % var
  493.  
  494.             #-----------------------------
  495.             elif opcode == 0x64:
  496.                 if variablesmoved == 1:
  497.                     outfiledata += '"X: %x" ' % var
  498.                 elif variablesmoved == 2:
  499.                     outfiledata += '"Y: %x" ' % var
  500.                 else:
  501.                     outfiledata += '%x ' % var
  502.                    
  503.             #-----------------------------
  504.             elif opcode == 0x6a:
  505.                 if variablesmoved == 0:
  506.                     string = filedata[var:filedata.find('\x00',var)]
  507.                     string = string.replace('\n','\\n')
  508.                     outfiledata += '"' + string + '" '
  509.                 else:
  510.                     outfiledata += '%x ' % var
  511.  
  512.             #-----------------------------
  513.             elif opcode == 0x6d:
  514.                 if variablesmoved == 1:
  515.                     string = filedata[var:filedata.find('\x00',var)]
  516.                     string = string.replace('\n','\\n')
  517.                     outfiledata += '"' + string + '" '
  518.                 elif variablesmoved == 2:
  519.                     string = filedata[var:filedata.find('\x00',var)]
  520.                     string = string.replace('\n','\\n')
  521.                     outfiledata += '"' + string + '" '
  522.                 else:
  523.                     outfiledata += '%x ' % var
  524.  
  525.             #-----------------------------
  526.             elif opcode == 0x6e:
  527.                 if variablesmoved == 1:
  528.                     string = filedata[var:filedata.find('\x00',var)]
  529.                     string = string.replace('\n','\\n')
  530.                     outfiledata += '"' + string + '" '
  531.                 else:
  532.                     outfiledata += '%x ' % var
  533.                    
  534.             #-----------------------------
  535.             elif opcode == 0x82:
  536.                 if variablesmoved == 1:
  537.                     string = filedata[var:filedata.find('\x00',var)]
  538.                     string = string.replace('\n','\\n')
  539.                     outfiledata += '"' + string + '" '
  540.                 else:
  541.                     outfiledata += '%x ' % var
  542.                    
  543.             #-----------------------------
  544.             else:
  545.                 outfiledata += '%x ' % var
  546.  
  547.         i += 0x4
  548.         variablesmoved += 1
  549.     opcodesmoved += 1
  550.     outfiledata += '\n'
  551.  
  552.     return i,outfiledata,funccount,opcodesmoved
  553.  
  554. def extract(filename):
  555.     global filedata
  556.     filedata = get_data(filename)
  557.     global scriptstart
  558.     global textstart
  559.     opcodesmoved = 0
  560.     textstart = struct.unpack('<I',filedata[0x540:0x544])[0]
  561.     scriptstart = struct.unpack('<I',filedata[0x1b74:0x1b78])[0]
  562.  
  563.     opcodesmoved = 0
  564.     i = 0
  565.     global opcodelocs
  566.     opcodelocs = []
  567.     while i < textstart:
  568.         opcode = struct.unpack('<I',filedata[i:i+0x4])[0]
  569.         c = 0
  570.         if i >= scriptstart:
  571.             while c != len(opcodes):
  572.                 if opcodes[c][0] == opcode:
  573.                     variablecount = opcodes[c][2]
  574.                     break;
  575.                 if c+1 == len(opcodes):
  576.                     print 'Unknown script opcode: %x at loc: %x' % (opcode,i)
  577.                     sys.exit()
  578.                 c += 1
  579.         else:
  580.             while c != len(opcodes1):
  581.                 if opcodes1[c][0] == opcode:
  582.                     variablecount = opcodes1[c][2]
  583.                     break;
  584.                 if c+1 == len(opcodes1):
  585.                     print 'Unknown pre-script opcode: %x at loc: %x' % (opcode,i)
  586.                     sys.exit()
  587.                 c += 1
  588.  
  589.         opcodelocs.append([opcodesmoved,i])
  590.         i += 0x4+(variablecount*0x4)
  591.         opcodesmoved += 1
  592.    
  593.     outfiledata = '====== Pre Script ======\n# Note: Do not add opcodes to or remove opcodes from this section, the file will no longer be compilable #\n\n=== Function 0 ===\n\n'
  594.     funccount = 1
  595.     opcodesmoved = 0
  596.     i = 0
  597.    
  598.     while i < scriptstart:
  599.         i,outfiledata,funccount,opcodesmoved = writeopcode(i,outfiledata,1,funccount,opcodesmoved)
  600.  
  601.     outfiledata += '\n====== Script ======\n\n'
  602.  
  603.     while i < textstart:
  604.         i,outfiledata,funccount,opcodesmoved = writeopcode(i,outfiledata,0,funccount,opcodesmoved)
  605.  
  606.     if sys.argv[1] == '-e2':
  607.         if not os.path.isdir('Scripts'):
  608.             os.mkdir('Scripts')
  609.         outfile = open('Scripts\\' + filename.rsplit('\\')[1] + '.txt','wb')
  610.     else:
  611.         outfile = open(filename + '.txt','wb')
  612.     outfile.write(outfiledata)
  613.     outfile.close()
  614.  
  615.     filedata = None
  616.     outfiledata = None
  617.  
  618. def extractMessage(filename):
  619.     filedata = get_data(filename)
  620.     tablesend = struct.unpack('<I',filedata[:0x4])[0]
  621.     tables = []
  622.     settableend = 0
  623.     for i in range (0,tablesend,0x4):
  624.         tables.append(struct.unpack('<I',filedata[i:i+0x4])[0])
  625.  
  626.     outfiledata = ''
  627.     opcodesmoved = 0
  628.  
  629.     for i in range(0,len(tables)):
  630.         outfiledata += '=== Table %d ===\n\n' % i
  631.         tablestart = tables[i]
  632.         try:
  633.             tableend = tables[i+1]
  634.         except Exception:
  635.             tableend = settableend
  636.  
  637.         a = tablestart
  638.         while a < tableend:
  639.             opcode = struct.unpack('<I',filedata[a:a+0x4])[0]
  640.             c = 0
  641.             while c != len(opcodesmessage):
  642.                 if opcodesmessage[c][0] == opcode:
  643.                     variablecount = opcodesmessage[c][2]
  644.                     name = opcodesmessage[c][1]
  645.                     break;
  646.                 if c+1 == len(opcodesmessage):
  647.                     print 'Unknown message opcode: %x at loc: %x' % (opcode,a)
  648.                     sys.exit()
  649.                 c += 1
  650.  
  651.             outfiledata += ('{ %x } ' % opcodesmoved) + name + ' '
  652.             a += 0x4
  653.             variablesmoved = 0
  654.  
  655.             for b in range(0,variablecount):
  656.                 var = struct.unpack('<I',filedata[a:a+0x4])[0]
  657.  
  658.                 if opcode == 0x2c and variablesmoved == 1:
  659.                     string = filedata[var:filedata.find('\x00',var)]
  660.                     outfiledata += '\n////////////////////////\n' + string + '\n////////////////////////'
  661.                     if settableend == 0:
  662.                         settableend = var
  663.                 else:
  664.                     outfiledata += '%x ' % var
  665.  
  666.                 variablesmoved += 1
  667.                 a += 0x4
  668.                
  669.             outfiledata += '\n'
  670.             opcodesmoved += 1
  671.         outfiledata += '\n'
  672.            
  673.     if sys.argv[1] == '-e2':
  674.         if not os.path.isdir('Scripts'):
  675.             os.mkdir('Scripts')
  676.         outfile = open('Scripts\\' + filename.rsplit('\\')[1] + '.txt','wb')
  677.     else:
  678.         outfile = open(filename + '.txt','wb')
  679.     outfile.write(outfiledata)
  680.     outfile.close()
  681.                      
  682.  
  683. def compiled(filename):
  684.     filedata = get_data(filename).splitlines()
  685.     #olddata = get_data('Bin\\' + filename.rsplit('.',1)[0].split('\\')[1])
  686.     newfiledata = ''#olddata[:struct.unpack('<I',olddata[0x1b74:0x1b78])[0]]
  687.     newstrings = ''
  688.     stringstofix = []
  689.     opcodestouse = 1
  690.     opcodesmoved = 0
  691.     firstrun = True
  692.     firstopcode = True
  693.     locstofix = []
  694.     origlocs = []
  695.     i = 0
  696.     stringcount = 0
  697.     lastcnt = 0
  698.  
  699.     #for i in range(0,len(filedata)):
  700.     while i < len(filedata):
  701.         if filedata[i].find('======') != -1 and firstrun == True:
  702.             opcodestouse = 1
  703.             firstrun = False
  704.         elif filedata[i].find('======') != -1 and firstrun == False:
  705.             opcodestouse = 0
  706.            
  707.         if filedata[i] != '' and filedata[i].find('=') == -1 and filedata[i].find('#') == -1:
  708.             line = re.findall(r'(?:[^\s,"]|"(?:\\.|[^"])*")+', filedata[i])
  709.             isnewloc = False
  710.             isdeletedloc = False
  711.  
  712.             try:
  713.                 if line[0] != '{':
  714.                     isnewloc = True
  715.                 else:
  716.                     line.pop(0)
  717.                     if firstopcode == True:
  718.                         lastcnt = int(line[0],16)
  719.                         firstopcode = False
  720.                     else:
  721.                         if lastcnt + 1 != int(line[0],16):
  722.                             isdeletedloc = True
  723.                     origlocs.append([int(line[0],16),len(newfiledata)])
  724.                     lastcnt = int(line[0],16)
  725.                     line.pop(0)
  726.                     line.pop(0)
  727.             except Exception:
  728.                 isnewloc = True
  729.                        
  730.             c = 0
  731.             if opcodestouse == 0:
  732.                 while c != len(opcodes):
  733.                     if opcodes[c][1] == line[0]:
  734.                         variablecount = opcodes[c][2]
  735.                         opcode = opcodes[c][0]
  736.                         break;
  737.                     if c+1 == len(opcodes):
  738.                         print 'Unknown script opcode: %s on line: %x' % (line[0],i)
  739.                         sys.exit()
  740.                     c += 1
  741.             else:
  742.                 while c != len(opcodes1):
  743.                     if opcodes1[c][1] == line[0]:
  744.                         variablecount = opcodes1[c][2]
  745.                         opcode = opcodes1[c][0]
  746.                         break;
  747.                     if c+1 == len(opcodes1):
  748.                         print 'Unknown pre-script opcode: %s on line: %x' % (line[0],i)
  749.                         sys.exit()
  750.                     c += 1
  751.                
  752.             newfiledata += struct.pack('<I',opcode)
  753.             line.pop(0)
  754.  
  755.             # Handle text strings manually
  756.             if opcodestouse == 0 and (opcode == 0x70 or opcode == 0x71 or opcode == 0x73):
  757.                 if opcode == 0x70:
  758.                     i += 2
  759.                     string = ''
  760.                     while filedata[i].find('///') == -1:
  761.                         string += filedata[i] + '\n'
  762.                         i += 1
  763.                     string = string[:-1]
  764.                     if len(string) % 0x4 == 0:
  765.                         string += '\x00'
  766.                     while len(string) % 0x4 != 0:
  767.                         string += '\x00'
  768.                     stringstofix.append([len(newfiledata),string])
  769.                     newfiledata += struct.pack('<I',0x0) # fix later
  770.                     stringcount += 1
  771.  
  772.                 elif opcode == 0x71:
  773.                     i += 2
  774.                     string = ''
  775.                     while filedata[i].find('///') == -1:
  776.                         string += filedata[i] + '\n'
  777.                         i += 1
  778.                     string = string[:-1]
  779.                     if len(string) % 0x4 == 0:
  780.                         string += '\x00'
  781.                     while len(string) % 0x4 != 0:
  782.                         string += '\x00'
  783.                     stringstofix.append([len(newfiledata),string])
  784.                     newfiledata += struct.pack('<I',0x0) # fix later
  785.                    
  786.                     i += 1
  787.                     string = filedata[i]
  788.                     i += 1
  789.                     if len(string) % 0x4 == 0:
  790.                         string += '\x00'
  791.                     while len(string) % 0x4 != 0:
  792.                         string += '\x00'
  793.                     stringstofix.append([len(newfiledata),string])
  794.                     newfiledata += struct.pack('<I',0x0) # fix later
  795.                     stringcount += 1
  796.  
  797.                 elif opcode == 0x73:
  798.                     newfiledata += struct.pack('<I',int(line[0],16))
  799.                     i += 2
  800.                     string = ''
  801.                     while filedata[i].find('///') == -1:
  802.                         string += filedata[i] + '\n'
  803.                         i += 1
  804.                     string = string[:-1]
  805.                     if len(string) % 0x4 == 0:
  806.                         string += '\x00'
  807.                     while len(string) % 0x4 != 0:
  808.                         string += '\x00'
  809.                     stringstofix.append([len(newfiledata),string])
  810.                     newfiledata += struct.pack('<I',0x0) # fix later
  811.            
  812.             # Everything that isn't a text string
  813.             else:
  814.                 for a in range(0,variablecount):
  815.                     if line[a].find('"') != -1:
  816.                         line[a] = line[a][1:-1]
  817.  
  818.                     #-----------------------------
  819.                     if line[a].find('label_') != -1:
  820.                         #print 'appending %x' % len(newfiledata)
  821.                         locstofix.append([int(line[a].split('_')[1],16),len(newfiledata)])
  822.                         #newfiledata += struct.pack('<I',int(line[a].split('_')[1],16))
  823.                         newfiledata += struct.pack('<I',0x0) # fix later
  824.  
  825.                     elif opcode == 0x1 and opcodestouse == 0:
  826.                         if line[a].find('input') != -1:
  827.                             newfiledata += struct.pack('<I',0x1)
  828.                         else:
  829.                             newfiledata += struct.pack('<I',int(line[a],16))
  830.  
  831.                     #-----------------------------
  832.                     elif opcode == 0xb and a == 0 and opcodestouse == 0:
  833.                         if len(line[a]) % 0x4 == 0:
  834.                             line[a] += '\x00'
  835.                         while len(line[a]) % 0x4 != 0:
  836.                             line[a] += '\x00'
  837.                         stringstofix.append([len(newfiledata),line[a]])
  838.                         newfiledata += struct.pack('<I',0x0) # fix later
  839.  
  840.                     #-----------------------------
  841.                     elif opcode == 0x2c and a == 0 and opcodestouse == 0:
  842.                         if len(line[a]) % 0x4 == 0:
  843.                             line[a] += '\x00'
  844.                         while len(line[a]) % 0x4 != 0:
  845.                             line[a] += '\x00'
  846.                         stringstofix.append([len(newfiledata),line[a]])
  847.                         newfiledata += struct.pack('<I',0x0) # fix later
  848.  
  849.                     #-----------------------------
  850.                     elif opcode == 0x3c:
  851.                         newfiledata += struct.pack('<I',int(line[a].split(' ')[1],16))
  852.  
  853.                     #-----------------------------
  854.                     elif opcode == 0x43 and a == 0 and opcodestouse == 0:
  855.                         if len(line[a]) % 0x4 == 0:
  856.                             line[a] += '\x00'
  857.                         while len(line[a]) % 0x4 != 0:
  858.                             line[a] += '\x00'
  859.                         stringstofix.append([len(newfiledata),line[a]])
  860.                         newfiledata += struct.pack('<I',0x0) # fix later
  861.                        
  862.                     #-----------------------------
  863.                     elif opcode == 0x47 and a == 1:
  864.                         if len(line[a]) % 0x4 == 0:
  865.                             line[a] += '\x00'
  866.                         while len(line[a]) % 0x4 != 0:
  867.                             line[a] += '\x00'
  868.                         stringstofix.append([len(newfiledata),line[a]])
  869.                         newfiledata += struct.pack('<I',0x0) # fix later
  870.  
  871.                     #-----------------------------
  872.                     elif opcode == 0x4f and a == 0 and opcodestouse == 0:
  873.                         #newfiledata += struct.pack('<I',int(line[a]))
  874.                         newfiledata += struct.pack('<I',stringcount)
  875.  
  876.                     #-----------------------------
  877.                     elif opcode == 0x60:
  878.                         if a == 1 or a == 2 or a == 3 or a == 4:
  879.                             newfiledata += struct.pack('<I',int(line[a].split(' ')[1],16))
  880.                         else:
  881.                             newfiledata += struct.pack('<I',int(line[a],16))
  882.  
  883.                     #-----------------------------
  884.                     elif opcode == 0x61 and opcodestouse == 0:
  885.                         if a == 1 or a == 2 or a == 3 or a == 4 or a == 5:
  886.                             newfiledata += struct.pack('<I',int(line[a].split(' ')[1],16))
  887.                         else:
  888.                             newfiledata += struct.pack('<I',int(line[a],16))
  889.  
  890.                     #-----------------------------
  891.                     elif opcode == 0x62:
  892.                         if a == 1 or a == 2:
  893.                             newfiledata += struct.pack('<I',int(line[a].split(' ')[1],16))
  894.                         else:
  895.                             newfiledata += struct.pack('<I',int(line[a],16))
  896.  
  897.                     #-----------------------------
  898.                     elif opcode == 0x64:
  899.                         if a == 1 or a == 2:
  900.                             newfiledata += struct.pack('<I',int(line[a].split(' ')[1],16))
  901.                         else:
  902.                             newfiledata += struct.pack('<I',int(line[a],16))  
  903.  
  904.                     #-----------------------------
  905.                     elif opcode == 0x6a and a == 0:
  906.                         if len(line[a]) % 0x4 == 0:
  907.                             line[a] += '\x00'
  908.                         while len(line[a]) % 0x4 != 0:
  909.                             line[a] += '\x00'
  910.                         stringstofix.append([len(newfiledata),line[a]])
  911.                         newfiledata += struct.pack('<I',0x0) # fix later
  912.  
  913.                     #-----------------------------
  914.                     elif opcode == 0x6d and (a == 1 or a == 2):
  915.                         if len(line[a]) % 0x4 == 0:
  916.                             line[a] += '\x00'
  917.                         while len(line[a]) % 0x4 != 0:
  918.                             line[a] += '\x00'
  919.                         stringstofix.append([len(newfiledata),line[a]])
  920.                         newfiledata += struct.pack('<I',0x0) # fix later
  921.  
  922.                     #-----------------------------
  923.                     elif opcode == 0x6e and a == 1:
  924.                         if len(line[a]) % 0x4 == 0:
  925.                             line[a] += '\x00'
  926.                         while len(line[a]) % 0x4 != 0:
  927.                             line[a] += '\x00'
  928.                         stringstofix.append([len(newfiledata),line[a]])
  929.                         newfiledata += struct.pack('<I',0x0) # fix later
  930.  
  931.                     #-----------------------------
  932.                     elif opcode == 0x82 and a == 1:
  933.                         if len(line[a]) % 0x4 == 0:
  934.                             line[a] += '\x00'
  935.                         while len(line[a]) % 0x4 != 0:
  936.                             line[a] += '\x00'
  937.                         stringstofix.append([len(newfiledata),line[a]])
  938.                         newfiledata += struct.pack('<I',0x0) # fix later
  939.  
  940.                     #-----------------------------
  941.                     elif opcode == 0x83 and a == 1:
  942.                         if len(line[a]) % 0x4 == 0:
  943.                             line[a] += '\x00'
  944.                         while len(line[a]) % 0x4 != 0:
  945.                             line[a] += '\x00'
  946.                         stringstofix.append([len(newfiledata),line[a]])
  947.                         newfiledata += struct.pack('<I',0x0) # fix later
  948.  
  949.                     #-----------------------------
  950.                     elif opcode == 0x84 and a == 1 and opcodestouse == 0:
  951.                         if line[a] == 'open':
  952.                             newfiledata += struct.pack('<I',1)
  953.                         else:
  954.                             newfiledata += struct.pack('<I',0)
  955.  
  956.                     #-----------------------------
  957.                     elif opcode == 0x92:
  958.                         if a == 0:
  959.                             if len(line[a]) % 0x4 == 0:
  960.                                 line[a] += '\x00'
  961.                             while len(line[a]) % 0x4 != 0:
  962.                                 line[a] += '\x00'
  963.                             stringstofix.append([len(newfiledata),line[a]])
  964.                             newfiledata += struct.pack('<I',0x0) # fix later
  965.                         elif a == 1:
  966.                             newfiledata += struct.pack('<I',int(line[a].split(' ')[1],16))
  967.                         else:
  968.                             newfiledata += struct.pack('<I',int(line[a],16))
  969.  
  970.                     #-----------------------------
  971.                     else:
  972.                         newfiledata += struct.pack('<I',int(line[a],16))
  973.             opcodesmoved += 1
  974.         i += 1
  975.  
  976.     #print origlocs
  977.     for i in range(0,len(locstofix)):
  978.         loctojump = locstofix[i][0]
  979.         MatchFound = False
  980.         while MatchFound == False:
  981.             for a in range(0,len(origlocs)):
  982.                 if loctojump == origlocs[a][0]:
  983.                     loc = origlocs[a][1]
  984.                     MatchFound = True
  985.                     break;
  986.             loctojump += 1
  987.             if loctojump >= len(newfiledata):
  988.                 print 'Cannot find loc to jump to for %x' % locstofix[i][0]
  989.                 sys.exit()
  990.  
  991.         newfiledata = newfiledata[:locstofix[i][1]] + struct.pack('<I',loc) + newfiledata[locstofix[i][1]+0x4:]
  992.                        
  993.     for i in range(0,len(stringstofix)):
  994.         tmp1 = '\x00' + newstrings
  995.         if tmp1.find('\x00' + stringstofix[i][1]) != -1:
  996.             newpos = len(newfiledata) + tmp1.find('\x00' + stringstofix[i][1])
  997.         else:
  998.             newpos = len(newfiledata)+len(newstrings)
  999.             newstrings += stringstofix[i][1]
  1000.            
  1001.         newfiledata = newfiledata[:stringstofix[i][0]] + struct.pack('<I',newpos) + newfiledata[stringstofix[i][0]+0x4:]
  1002.  
  1003.     newfiledata += newstrings
  1004.    
  1005.     if not os.path.isdir('Compiled'):
  1006.         os.mkdir('Compiled')
  1007.        
  1008.     if sys.argv[1] == '-c2':
  1009.         outfile = open('Compiled\\' + filename.rsplit('.',1)[0].rsplit('\\',1)[1],'wb')
  1010.     else:
  1011.         outfile = open(filename.split('.')[0] + '.bin','wb')
  1012.     outfile.write(newfiledata)
  1013.     outfile.close()
  1014.    
  1015.     '''
  1016.    if sys.argv[1] == '-c2':
  1017.        filename1 = 'Bin\\' + filename.rsplit('.',1)[0].rsplit('\\')[1]
  1018.    else:
  1019.        filename1 = 'Bin\\' + filename.rsplit('.',1)[0]
  1020.  
  1021.    if newfiledata != get_data(filename1):
  1022.        print "files don't match"
  1023.        sys.exit()
  1024.    '''
  1025.  
  1026.     filedata = None
  1027.     newfiledata = None
  1028.     newstrings = None
  1029.     tmp1 = None
  1030.     origlocs = None
  1031.     stringstofix = None
  1032.  
  1033. def compileMessage(filename):
  1034.     filedata = get_data(filename).splitlines()
  1035.     newfiledata = ''
  1036.     newstrings = ''
  1037.     i = 0
  1038.     tables = []
  1039.     while i < len(filedata):
  1040.         if filedata[i].find('===') != -1:
  1041.             tables.append(i)
  1042.             newfiledata += struct.pack('<I',0x0) #fix later
  1043.         i += 1
  1044.     i = 0
  1045.  
  1046.     isnewloc = False
  1047.     firstopcode = True
  1048.     isdeletedloc = False
  1049.     origlocs = []
  1050.     lastcnt = 0
  1051.     locstofix = []
  1052.     opcodesmoved = 0
  1053.     stringstofix = []
  1054.    
  1055.     while i < len(filedata):
  1056.         for a in range(0,len(tables)):
  1057.             if i == tables[a]:
  1058.                 newfiledata = newfiledata[:(a*0x4)] + struct.pack('<I',len(newfiledata)) + newfiledata[(a*0x4)+0x4:]
  1059.  
  1060.         if filedata[i] != '' and filedata[i].find('=') == -1 and filedata[i].find('#') == -1:
  1061.             line = re.findall(r'(?:[^\s,"]|"(?:\\.|[^"])*")+', filedata[i])
  1062.             try:
  1063.                 if line[0] != '{':
  1064.                     isnewloc = True
  1065.                 else:
  1066.                     line.pop(0)
  1067.                     if firstopcode == True:
  1068.                         lastcnt = int(line[0],16)
  1069.                         firstopcode = False
  1070.                     else:
  1071.                         if lastcnt + 1 != int(line[0],16):
  1072.                             isdeletedloc = True
  1073.                     origlocs.append([int(line[0],16),len(newfiledata)])
  1074.                     lastcnt = int(line[0],16)
  1075.                     line.pop(0)
  1076.                     line.pop(0)
  1077.             except Exception:
  1078.                 isnewloc = True
  1079.  
  1080.             c = 0
  1081.             while c != len(opcodesmessage):
  1082.                 if opcodesmessage[c][1] == line[0]:
  1083.                     variablecount = opcodesmessage[c][2]
  1084.                     opcode = opcodesmessage[c][0]
  1085.                     break;
  1086.                 if c+1 == len(opcodesmessage):
  1087.                     print 'Unknown message opcode: %s on line: %x' % (line[0],i)
  1088.                     sys.exit()
  1089.                 c += 1
  1090.  
  1091.             newfiledata += struct.pack('<I',opcode)
  1092.             line.pop(0)
  1093.             variablesmoved = 0
  1094.  
  1095.             if opcode == 0x2c:
  1096.                 newfiledata += struct.pack('<I',int(line[0],16))
  1097.                 i += 2
  1098.                 string = ''
  1099.                 while filedata[i].find('///') == -1:
  1100.                     string += filedata[i] + '\n'
  1101.                     i += 1
  1102.                 string = string[:-1]
  1103.                 if len(string) % 0x4 == 0:
  1104.                     string += '\x00'
  1105.                 while len(string) % 0x4 != 0:
  1106.                     string += '\x00'
  1107.                 stringstofix.append([len(newfiledata),string])
  1108.                 newfiledata += struct.pack('<I',0x0) # fix later
  1109.                
  1110.             else:
  1111.                 for a in range(0,variablecount):
  1112.                     if line[a].find('"') != -1:
  1113.                         line[a] = line[a][1:-1]
  1114.  
  1115.                     #-----------------------------
  1116.                     if line[a].find('label_') != -1:
  1117.                         #print 'appending %x' % len(newfiledata)
  1118.                         locstofix.append([int(line[a].split('_')[1],16),len(newfiledata)])
  1119.                         #newfiledata += struct.pack('<I',int(line[a].split('_')[1],16))
  1120.                         newfiledata += struct.pack('<I',0x0) # fix later
  1121.                     else:
  1122.                         newfiledata += struct.pack('<I',int(line[a],16))
  1123.  
  1124.  
  1125.             opcodesmoved += 1
  1126.         i += 1
  1127.  
  1128.     #print origlocs
  1129.     for i in range(0,len(locstofix)):
  1130.         loctojump = locstofix[i][0]
  1131.         MatchFound = False
  1132.         while MatchFound == False:
  1133.             for a in range(0,len(origlocs)):
  1134.                 if loctojump == origlocs[a][0]:
  1135.                     loc = origlocs[a][1]
  1136.                     MatchFound = True
  1137.                     break;
  1138.             loctojump += 1
  1139.             if loctojump >= len(newfiledata):
  1140.                 print 'Cannot find loc to jump to for %x' % locstofix[i][0]
  1141.                 sys.exit()
  1142.  
  1143.         newfiledata = newfiledata[:locstofix[i][1]] + struct.pack('<I',loc) + newfiledata[locstofix[i][1]+0x4:]
  1144.                        
  1145.     for i in range(0,len(stringstofix)):
  1146.         tmp1 = '\x00' + newstrings
  1147.         if tmp1.find('\x00' + stringstofix[i][1]) != -1:
  1148.             newpos = len(newfiledata) + tmp1.find('\x00' + stringstofix[i][1])
  1149.         else:
  1150.             newpos = len(newfiledata)+len(newstrings)
  1151.             newstrings += stringstofix[i][1]
  1152.            
  1153.         newfiledata = newfiledata[:stringstofix[i][0]] + struct.pack('<I',newpos) + newfiledata[stringstofix[i][0]+0x4:]
  1154.  
  1155.     newfiledata += newstrings
  1156.  
  1157.     if not os.path.isdir('Compiled'):
  1158.         os.mkdir('Compiled')
  1159.        
  1160.     if sys.argv[1] == '-c2':
  1161.         outfile = open('Compiled\\' + filename.rsplit('.',1)[0].rsplit('\\',1)[1],'wb')
  1162.     else:
  1163.         outfile = open(filename.split('.')[0] + '.bin','wb')
  1164.     outfile.write(newfiledata)
  1165.     outfile.close()
  1166.    
  1167.     '''
  1168.    if sys.argv[1] == '-c2':
  1169.        filename1 = 'Bin\\' + filename.rsplit('.',1)[0].rsplit('\\')[1]
  1170.    else:
  1171.        filename1 = 'Bin\\' + filename.rsplit('.',1)[0]
  1172.  
  1173.    if newfiledata != get_data(filename1):
  1174.        print "files don't match"
  1175.        sys.exit()
  1176.    '''
  1177.  
  1178.     filedata = None
  1179.     newfiledata = None
  1180.     newstrings = None
  1181.     tmp1 = None
  1182.     origlocs = None
  1183.     stringstofix = None
  1184.  
  1185.        
  1186. if __name__ == '__main__':
  1187.     if sys.argv[1] == '-e1':
  1188.         print 'Extracting file %s' % sys.argv[2]
  1189.         if sys.argv[2].find('Message_') != -1:
  1190.             extractMessage(sys.argv[2])
  1191.         else:
  1192.             extract(sys.argv[2])
  1193.     elif sys.argv[1] == '-e2':
  1194.         for filename in os.listdir(sys.argv[2]):
  1195.             if filename.find('.bin') != -1 and (filename.find('Auto_') != -1 or filename.find('Nov_') != -1 or filename.find('SN00') != -1):
  1196.             #if filename.find('.bin') != -1 and (filename.find('SN00') != -1):
  1197.                 print 'Extracting file %s' % sys.argv[2] + '\\' + filename
  1198.                 extract(sys.argv[2] + '\\' + filename)
  1199.             elif filename.find('Message_') != -1:
  1200.                 print 'Extracting file %s' % sys.argv[2] + '\\' + filename
  1201.                 extractMessage(sys.argv[2] + '\\' + filename)
  1202.                
  1203.     elif sys.argv[1] == '-c1':
  1204.         print 'Compiling file %s' % sys.argv[2]
  1205.         if sys.argv[2].find('Message_') != -1:
  1206.             compileMessage(sys.argv[2])
  1207.         else:
  1208.             compiled(sys.argv[2])
  1209.     elif sys.argv[1] == '-c2':
  1210.         for filename in os.listdir(sys.argv[2]):
  1211.             if filename.find('.txt') != -1 and filename.find('Message_') != -1:
  1212.                 print 'Compiling file %s' % sys.argv[2] + '\\' + filename
  1213.                 compileMessage(sys.argv[2] + '\\' + filename)
  1214.             elif filename.find('.txt') != -1:
  1215.                 print 'Compiling file %s' % sys.argv[2] + '\\' + filename
  1216.                 compiled(sys.argv[2] + '\\' + filename)
  1217.     else:
  1218.         print 'Usage: script.py [options] [infile/infolder]'
  1219.         print 'Options:\n-e1 [infile] >extract single file'
  1220.         print '-e2 [infolder] >extract folder'
  1221.         print '-c1 [infile] >compile single file'
  1222.         print '-c2 [infolder] >compile folder'
  1223.         sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement