Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import sys
- import struct
- import re
- opcodes = [
- [0x0,'end-block',0],[0x1,'0x1',1],[0x2,'0x2',1],[0x3,'jump',1],[0x4,'call-function-local',1],[0x5,'return',0],
- [0xb,'load-script',1],[0xc,'0xc',0],[0xd,'0xd',0],
- [0xe,'0xe',2],[0xf,'0xf',2],[0x11,'0x11',0],[0x15,'0x15',0],[0x18,'0x18',1],
- [0x1f,'0x1f',1],[0x20,'0x20-jumpcond',1],[0x21,'0x21',3],[0x22,'0x22',1],[0x23,'0x23',0],[0x28,'0x28',1],
- [0x2a,'0x2a',4],[0x2b,'0x2b-jumpcond',2],[0x2c,'load-bgm',1],
- [0x2d,'0x2d',4],[0x2e,'0x2e',1],[0x2f,'0x2f',0],
- [0x30,'0x30',2],[0x32,'0x32',2],[0x33,'0x33',4],[0x34,'0x34',1],[0x35,'0x35',2],[0x37,'play-anim',7],
- [0x38,'0x38',1],[0x3c,'move-screen',2],
- [0x41,'0x41',1],[0x42,'0x42',1],[0x43,'load-textfile',1],[0x44,'0x44',1],[0x47,'0x47-load-bin',2],
- [0x48,'0x48',1],[0x49,'0x49-jumpcond',1],[0x4a,'0x4a',0],[0x4b,'0x4b',4],
- [0x4c,'0x4c',3],[0x4f,'string-counter',1],
- [0x50,'0x50',0],[0x53,'0x53',1],[0x57,'0x57',0],[0x58,'0x58',1],[0x5a,'0x5a',1],[0x5b,'0x5b',1],
- [0x5c,'0x5c',2],[0x5e,'0x5e',2],[0x5f,'0x5f',2],
- [0x60,'display-image',5],[0x61,'image-transition',6],[0x62,'image-rect-loc',3],[0x63,'0x63',4],
- [0x64,'create-image-rect',3],[0x65,'0x65',4],[0x66,'0x66',2],[0x67,'0x67',4],
- [0x6a,'load-background',1],[0x6b,'0x6b',0],[0x6c,'image-load-loc',2],[0x6d,'load-dds-texture',3],
- [0x6e,'load-png-effect',2],
- [0x6f,'0x6f',0],[0x70,'narration',1],[0x71,'named-text',2],[0x73,'windowless-text',2],[0x74,'0x74',1],
- [0x77,'0x77',0],[0x79,'0x79',0],
- [0x7f,'0x7f',0xb],[0x81,'0x81',0],[0x82,'0x82-load-bin',3],[0x83,'0x83',5],
- [0x84,'textbox-open/close',2],[0x86,'0x86',3],[0x88,'0x88',1],[0x89,'0x89',1],[0x8a,'0x8a',1],
- [0x8f,'0x8f',6],[0x90,'0x90',1],[0x91,'0x91',2],[0x92,'play-voice',3],
- ]
- opcodes1 = [
- [0x0,'end-block',0],[0x1,'0x1',1],[0x2,'0x2',1],[0x3,'jump',1],[0x4,'call-function-local',1],[0x5,'return',0],
- [0xd,'0xd',0],[0xe,'0xe',2],[0x11,'0x11',0],[0x1f,'0x1f',2],[0x20,'0x20-jumpcond',1],
- [0x21,'0x21',3],[0x23,'0x23',0],[0x28,'0x28',1],[0x2c,'0x2c',0],
- [0x2d,'0x2d',1],[0x2e,'0x2e',1],[0x2f,'0x2f',1],[0x30,'0x30',2],[0x34,'0x34',1],[0x38,'0x38',1],
- [0x3c,'move-screen',2],
- [0x41,'0x41',1],[0x42,'0x42',1],[0x45,'0x45',0],[0x49,'0x49-jumpcond',1],[0x4e,'0x4e',1],
- [0x57,'0x57',0],[0x58,'0x58',1],[0x5a,'0x5a',1],[0x5b,'0x5b',1],[0x5c,'0x5c',2],[0x5d,'0x5d',3],
- [0x5e,'0x5e',2],[0x5f,'0x5f',2],[0x60,'display-image',5],
- [0x61,'0x61',6],[0x62,'image-rect-loc',3],[0x63,'0x63',4],[0x64,'create-image-rect',3],[0x65,'0x65',4],
- [0x66,'0x66',2],[0x6a,'load-background',1],[0x6b,'0x6b',0],[0x6c,'image-load-loc',2],
- [0x6d,'load-dds-texture',3],[0x6e,'load-png-effect',2],[0x6f,'0x6f',0],[0x74,'0x74',1],
- [0x77,'0x77',0],
- [0x82,'0x82-load-bin',3],[0x84,'0x84',2],[0x86,'0x86',3],[0x88,'0x88',1],[0x8a,'0x8a',1],
- ]
- opcodesmessage = [
- [0x0,'0x0',0],[0x1,'0x1',1],[0x2c,'string',2],[0x2d,'0x2d',2],[0x2e,'0x2e',2],[0x2f,'0x2f',1],[0x30,'0x30',1],
- ]
- def get_data(filename):
- totalbytes = os.path.getsize(filename)
- infile = open(filename, 'rb')
- totalfiledata = infile.read(totalbytes)
- return totalfiledata
- def writeopcode(i,outfiledata,mainorno,funccount,opcodesmoved):
- opcode = struct.unpack('<I',filedata[i:i+0x4])[0]
- c = 0
- if mainorno == 0:
- while c != len(opcodes):
- if opcodes[c][0] == opcode:
- variablecount = opcodes[c][2]
- name = opcodes[c][1]
- break;
- if c+1 == len(opcodes):
- print 'Unknown script opcode: %x at loc: %x' % (opcode,i)
- sys.exit()
- c += 1
- else:
- while c != len(opcodes1):
- if opcodes1[c][0] == opcode:
- variablecount = opcodes1[c][2]
- name = opcodes1[c][1]
- break;
- if c+1 == len(opcodes1):
- print 'Unknown pre-script opcode: %x at loc: %x' % (opcode,i)
- sys.exit()
- c += 1
- outfiledata += ('{ %x' % opcodesmoved) + ' } ' + name + ' '
- i += 0x4
- endlen = i+(variablecount*0x4)
- variablesmoved = 0
- if opcode == 0x0 and mainorno == 0:
- outfiledata += '\n'
- elif opcode == 0x5:
- outfiledata += '\n'
- if i != textstart and i != scriptstart:
- #print '%x - scriptstart %x textstart %x' % (i,scriptstart,textstart)
- outfiledata += '\n=== Function %d ===\n' % funccount
- funccount += 1
- elif i == scriptstart:
- outfiledata = outfiledata[:-1]
- while i < endlen:
- var = struct.unpack('<I',filedata[i:i+0x4])[0]
- if mainorno == 0:
- #-----------------------------
- if opcode == 0x1:
- if var == 1:
- outfiledata += '"Wait for input" '
- else:
- outfiledata += '%x ' % var
- #-----------------------------
- elif opcode == 0x3:
- if variablesmoved == 0:
- for a in range(0,len(opcodelocs)):
- if opcodelocs[a][1] == var:
- var = opcodelocs[a][0]
- break;
- if a+1 == len(opcodelocs):
- print 'Unable to find jump location %x at loc %x' % (var,i)
- sys.exit()
- outfiledata += 'label_%x ' % var
- #-----------------------------
- elif opcode == 0x4:
- if variablesmoved == 0:
- for a in range(0,len(opcodelocs)):
- if opcodelocs[a][1] == var:
- var = opcodelocs[a][0]
- break;
- if a+1 == len(opcodelocs):
- print 'Unable to find jump location %x at loc %x' % (var,i)
- sys.exit()
- outfiledata += 'label_%x ' % var
- #-----------------------------
- elif opcode == 0xb:
- if variablesmoved == 0:
- string = filedata[var:filedata.find('\x00',var)]
- string = string.replace('\n','\\n')
- outfiledata += '"' + string + '" '
- #-----------------------------
- elif opcode == 0x1f:
- if variablesmoved == 0:
- for a in range(0,len(opcodelocs)):
- if opcodelocs[a][1] == var:
- var = opcodelocs[a][0]
- break;
- if a+1 == len(opcodelocs):
- print 'Unable to find jump location %x at loc %x' % (var,i)
- sys.exit()
- outfiledata += 'label_%x ' % var
- #-----------------------------
- elif opcode == 0x20:
- if variablesmoved == 0:
- for a in range(0,len(opcodelocs)):
- if opcodelocs[a][1] == var:
- var = opcodelocs[a][0]
- break;
- if a+1 == len(opcodelocs):
- print 'Unable to find jump location %x at loc %x' % (var,i)
- sys.exit()
- outfiledata += 'label_%x ' % var
- #-----------------------------
- elif opcode == 0x2a:
- if variablesmoved == 1:
- for a in range(0,len(opcodelocs)):
- if opcodelocs[a][1] == var:
- var = opcodelocs[a][0]
- break;
- if a+1 == len(opcodelocs):
- print 'Unable to find jump location %x at loc %x' % (var,i)
- sys.exit()
- outfiledata += 'label_%x ' % var
- else:
- outfiledata += '%x ' % var
- #-----------------------------
- elif opcode == 0x2b:
- if variablesmoved == 1:
- for a in range(0,len(opcodelocs)):
- if opcodelocs[a][1] == var:
- var = opcodelocs[a][0]
- break;
- if a+1 == len(opcodelocs):
- print 'Unable to find jump location %x at loc %x' % (var,i)
- sys.exit()
- outfiledata += 'label_%x ' % var
- else:
- outfiledata += '%x ' % var
- #-----------------------------
- elif opcode == 0x2c:
- if variablesmoved == 0:
- string = filedata[var:filedata.find('\x00',var)]
- string = string.replace('\n','\\n')
- outfiledata += '"' + string + '" '
- #-----------------------------
- elif opcode == 0x37:
- if variablesmoved == 5 or variablesmoved == 6:
- for a in range(0,len(opcodelocs)):
- if opcodelocs[a][1] == var:
- var = opcodelocs[a][0]
- break;
- if a+1 == len(opcodelocs):
- print 'Unable to find jump location %x at loc %x' % (var,i)
- sys.exit()
- outfiledata += 'label_%x ' % var
- else:
- outfiledata += '%x ' % var
- #-----------------------------
- elif opcode == 0x3c:
- if variablesmoved == 0:
- outfiledata += '"X: %x" ' % var
- elif variablesmoved == 1:
- outfiledata += '"Y: %x" ' % var
- #-----------------------------
- elif opcode == 0x43:
- if variablesmoved == 0:
- string = filedata[var:filedata.find('\x00',var)]
- string = string.replace('\n','\\n')
- outfiledata += '"' + string + '" '
- #-----------------------------
- elif opcode == 0x47:
- if variablesmoved == 1:
- string = filedata[var:filedata.find('\x00',var)]
- string = string.replace('\n','\\n')
- outfiledata += '"' + string + '" '
- else:
- outfiledata += '%x ' % var
- #-----------------------------
- elif opcode == 0x49:
- if variablesmoved == 0:
- for a in range(0,len(opcodelocs)):
- if opcodelocs[a][1] == var:
- var = opcodelocs[a][0]
- break;
- if a+1 == len(opcodelocs):
- print 'Unable to find jump location %x at loc %x' % (var,i)
- sys.exit()
- outfiledata += 'label_%x ' % var
- #-----------------------------
- elif opcode == 0x4f:
- if variablesmoved == 0:
- outfiledata += '%d ' % var
- #-----------------------------
- elif opcode == 0x60:
- if variablesmoved == 1:
- outfiledata += '"R: %x" ' % var
- elif variablesmoved == 2:
- outfiledata += '"G: %x" ' % var
- elif variablesmoved == 3:
- outfiledata += '"B: %x" ' % var
- elif variablesmoved == 4:
- outfiledata += '"A: %x" ' % var
- else:
- outfiledata += '%x ' % var
- #-----------------------------
- elif opcode == 0x61:
- if variablesmoved == 1:
- outfiledata += '"TransTime: %x" ' % var
- elif variablesmoved == 2:
- outfiledata += '"R: %x" ' % var
- elif variablesmoved == 3:
- outfiledata += '"G: %x" ' % var
- elif variablesmoved == 4:
- outfiledata += '"B: %x" ' % var
- elif variablesmoved == 5:
- outfiledata += '"A: %x" ' % var
- else:
- outfiledata += '%x ' % var
- #-----------------------------
- elif opcode == 0x62:
- if variablesmoved == 1:
- outfiledata += '"X: %x" ' % var
- elif variablesmoved == 2:
- outfiledata += '"Y: %x" ' % var
- else:
- outfiledata += '%x ' % var
- #-----------------------------
- elif opcode == 0x64:
- if variablesmoved == 1:
- outfiledata += '"X: %x" ' % var
- elif variablesmoved == 2:
- outfiledata += '"Y: %x" ' % var
- else:
- outfiledata += '%x ' % var
- #-----------------------------
- elif opcode == 0x6a:
- if variablesmoved == 0:
- string = filedata[var:filedata.find('\x00',var)]
- string = string.replace('\n','\\n')
- outfiledata += '"' + string + '" '
- #-----------------------------
- elif opcode == 0x6d:
- if variablesmoved == 1:
- string = filedata[var:filedata.find('\x00',var)]
- string = string.replace('\n','\\n')
- outfiledata += '"' + string + '" '
- elif variablesmoved == 2:
- string = filedata[var:filedata.find('\x00',var)]
- string = string.replace('\n','\\n')
- outfiledata += '"' + string + '" '
- else:
- outfiledata += '%x ' % var
- #-----------------------------
- elif opcode == 0x6e:
- if variablesmoved == 1:
- string = filedata[var:filedata.find('\x00',var)]
- string = string.replace('\n','\\n')
- outfiledata += '"' + string + '" '
- else:
- outfiledata += '%x ' % var
- #-----------------------------
- elif opcode == 0x70:
- if variablesmoved == 0:
- string = filedata[var:filedata.find('\x00',var)]
- #string = string.replace('\n','\\n')
- outfiledata += '\n////////////////////////////////////////////////////////\n' + string + '\n//////////////////////////////////////////////////////// '
- #-----------------------------
- elif opcode == 0x71:
- if variablesmoved == 0:
- string = filedata[var:filedata.find('\x00',var)]
- #string = string.replace('\n','\\n')
- outfiledata += '\n////////////////////////////////////////////////////////\n' + string
- elif variablesmoved == 1:
- string = filedata[var:filedata.find('\x00',var)]
- #string = string.replace('\n','\\n')
- outfiledata += '\n////////////////////////////////////////////////////////\n' + string + '\n/////////////// '
- #-----------------------------
- elif opcode == 0x73:
- if variablesmoved == 1:
- string = filedata[var:filedata.find('\x00',var)]
- #string = string.replace('\n','\\n')
- outfiledata += '\n////////////////////////////////////////////////////////\n' + string + '\n//////////////////////////////////////////////////////// '
- else:
- outfiledata += '%x ' % var
- #-----------------------------
- elif opcode == 0x7f:
- if variablesmoved == 0xa:
- for a in range(0,len(opcodelocs)):
- if opcodelocs[a][1] == var:
- var = opcodelocs[a][0]
- break;
- if a+1 == len(opcodelocs):
- print 'Unable to find jump location %x at loc %x' % (var,i)
- sys.exit()
- outfiledata += 'label_%x ' % var
- else:
- outfiledata += '%x ' % var
- #-----------------------------
- elif opcode == 0x82:
- if variablesmoved == 1:
- string = filedata[var:filedata.find('\x00',var)]
- string = string.replace('\n','\\n')
- outfiledata += '"' + string + '" '
- else:
- outfiledata += '%x ' % var
- #-----------------------------
- elif opcode == 0x83:
- if variablesmoved == 1:
- string = filedata[var:filedata.find('\x00',var)]
- string = string.replace('\n','\\n')
- outfiledata += '"' + string + '" '
- else:
- outfiledata += '%x ' % var
- #-----------------------------
- elif opcode == 0x84:
- if variablesmoved == 0:
- outfiledata += '%x ' % var
- elif variablesmoved == 1:
- if var == 0:
- outfiledata += 'close '
- elif var == 1:
- outfiledata += 'open '
- #-----------------------------
- elif opcode == 0x92:
- if variablesmoved == 0:
- string = filedata[var:filedata.find('\x00',var)]
- string = string.replace('\n','\\n')
- outfiledata += '"' + string + '" '
- elif variablesmoved == 1:
- outfiledata += '"Vol: %x" ' % var
- else:
- outfiledata += '%x ' % var
- #-----------------------------
- else:
- outfiledata += '%x ' % var
- else:
- #-----------------------------
- if opcode == 0x3:
- if variablesmoved == 0:
- for a in range(0,len(opcodelocs)):
- if opcodelocs[a][1] == var:
- var = opcodelocs[a][0]
- break;
- if a+1 == len(opcodelocs):
- print 'Unable to find jump location %x at loc %x' % (var,i)
- sys.exit()
- outfiledata += 'label_%x ' % var
- #-----------------------------
- elif opcode == 0x4:
- if variablesmoved == 0:
- for a in range(0,len(opcodelocs)):
- if opcodelocs[a][1] == var:
- var = opcodelocs[a][0]
- break;
- if a+1 == len(opcodelocs):
- print 'Unable to find jump location %x at loc %x' % (var,i)
- sys.exit()
- outfiledata += 'label_%x ' % var
- #-----------------------------
- elif opcode == 0x20:
- if variablesmoved == 0:
- for a in range(0,len(opcodelocs)):
- if opcodelocs[a][1] == var:
- var = opcodelocs[a][0]
- break;
- if a+1 == len(opcodelocs):
- print 'Unable to find jump location %x at loc %x' % (var,i)
- sys.exit()
- outfiledata += 'label_%x ' % var
- #-----------------------------
- elif opcode == 0x3c:
- if variablesmoved == 0:
- outfiledata += '"X: %x" ' % var
- elif variablesmoved == 1:
- outfiledata += '"Y: %x" ' % var
- #-----------------------------
- elif opcode == 0x49:
- if variablesmoved == 0:
- for a in range(0,len(opcodelocs)):
- if opcodelocs[a][1] == var:
- var = opcodelocs[a][0]
- break;
- if a+1 == len(opcodelocs):
- print 'Unable to find jump location %x at loc %x' % (var,i)
- sys.exit()
- outfiledata += 'label_%x ' % var
- #-----------------------------
- elif opcode == 0x60:
- if variablesmoved == 1:
- outfiledata += '"R: %x" ' % var
- elif variablesmoved == 2:
- outfiledata += '"G: %x" ' % var
- elif variablesmoved == 3:
- outfiledata += '"B: %x" ' % var
- elif variablesmoved == 4:
- outfiledata += '"A: %x" ' % var
- else:
- outfiledata += '%x ' % var
- #-----------------------------
- elif opcode == 0x62:
- if variablesmoved == 1:
- outfiledata += '"X: %x" ' % var
- elif variablesmoved == 2:
- outfiledata += '"Y: %x" ' % var
- else:
- outfiledata += '%x ' % var
- #-----------------------------
- elif opcode == 0x64:
- if variablesmoved == 1:
- outfiledata += '"X: %x" ' % var
- elif variablesmoved == 2:
- outfiledata += '"Y: %x" ' % var
- else:
- outfiledata += '%x ' % var
- #-----------------------------
- elif opcode == 0x6a:
- if variablesmoved == 0:
- string = filedata[var:filedata.find('\x00',var)]
- string = string.replace('\n','\\n')
- outfiledata += '"' + string + '" '
- else:
- outfiledata += '%x ' % var
- #-----------------------------
- elif opcode == 0x6d:
- if variablesmoved == 1:
- string = filedata[var:filedata.find('\x00',var)]
- string = string.replace('\n','\\n')
- outfiledata += '"' + string + '" '
- elif variablesmoved == 2:
- string = filedata[var:filedata.find('\x00',var)]
- string = string.replace('\n','\\n')
- outfiledata += '"' + string + '" '
- else:
- outfiledata += '%x ' % var
- #-----------------------------
- elif opcode == 0x6e:
- if variablesmoved == 1:
- string = filedata[var:filedata.find('\x00',var)]
- string = string.replace('\n','\\n')
- outfiledata += '"' + string + '" '
- else:
- outfiledata += '%x ' % var
- #-----------------------------
- elif opcode == 0x82:
- if variablesmoved == 1:
- string = filedata[var:filedata.find('\x00',var)]
- string = string.replace('\n','\\n')
- outfiledata += '"' + string + '" '
- else:
- outfiledata += '%x ' % var
- #-----------------------------
- else:
- outfiledata += '%x ' % var
- i += 0x4
- variablesmoved += 1
- opcodesmoved += 1
- outfiledata += '\n'
- return i,outfiledata,funccount,opcodesmoved
- def extract(filename):
- global filedata
- filedata = get_data(filename)
- global scriptstart
- global textstart
- opcodesmoved = 0
- textstart = struct.unpack('<I',filedata[0x540:0x544])[0]
- scriptstart = struct.unpack('<I',filedata[0x1b74:0x1b78])[0]
- opcodesmoved = 0
- i = 0
- global opcodelocs
- opcodelocs = []
- while i < textstart:
- opcode = struct.unpack('<I',filedata[i:i+0x4])[0]
- c = 0
- if i >= scriptstart:
- while c != len(opcodes):
- if opcodes[c][0] == opcode:
- variablecount = opcodes[c][2]
- break;
- if c+1 == len(opcodes):
- print 'Unknown script opcode: %x at loc: %x' % (opcode,i)
- sys.exit()
- c += 1
- else:
- while c != len(opcodes1):
- if opcodes1[c][0] == opcode:
- variablecount = opcodes1[c][2]
- break;
- if c+1 == len(opcodes1):
- print 'Unknown pre-script opcode: %x at loc: %x' % (opcode,i)
- sys.exit()
- c += 1
- opcodelocs.append([opcodesmoved,i])
- i += 0x4+(variablecount*0x4)
- opcodesmoved += 1
- 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'
- funccount = 1
- opcodesmoved = 0
- i = 0
- while i < scriptstart:
- i,outfiledata,funccount,opcodesmoved = writeopcode(i,outfiledata,1,funccount,opcodesmoved)
- outfiledata += '\n====== Script ======\n\n'
- while i < textstart:
- i,outfiledata,funccount,opcodesmoved = writeopcode(i,outfiledata,0,funccount,opcodesmoved)
- if sys.argv[1] == '-e2':
- if not os.path.isdir('Scripts'):
- os.mkdir('Scripts')
- outfile = open('Scripts\\' + filename.rsplit('\\')[1] + '.txt','wb')
- else:
- outfile = open(filename + '.txt','wb')
- outfile.write(outfiledata)
- outfile.close()
- filedata = None
- outfiledata = None
- def extractMessage(filename):
- filedata = get_data(filename)
- tablesend = struct.unpack('<I',filedata[:0x4])[0]
- tables = []
- settableend = 0
- for i in range (0,tablesend,0x4):
- tables.append(struct.unpack('<I',filedata[i:i+0x4])[0])
- outfiledata = ''
- opcodesmoved = 0
- for i in range(0,len(tables)):
- outfiledata += '=== Table %d ===\n\n' % i
- tablestart = tables[i]
- try:
- tableend = tables[i+1]
- except Exception:
- tableend = settableend
- a = tablestart
- while a < tableend:
- opcode = struct.unpack('<I',filedata[a:a+0x4])[0]
- c = 0
- while c != len(opcodesmessage):
- if opcodesmessage[c][0] == opcode:
- variablecount = opcodesmessage[c][2]
- name = opcodesmessage[c][1]
- break;
- if c+1 == len(opcodesmessage):
- print 'Unknown message opcode: %x at loc: %x' % (opcode,a)
- sys.exit()
- c += 1
- outfiledata += ('{ %x } ' % opcodesmoved) + name + ' '
- a += 0x4
- variablesmoved = 0
- for b in range(0,variablecount):
- var = struct.unpack('<I',filedata[a:a+0x4])[0]
- if opcode == 0x2c and variablesmoved == 1:
- string = filedata[var:filedata.find('\x00',var)]
- outfiledata += '\n////////////////////////\n' + string + '\n////////////////////////'
- if settableend == 0:
- settableend = var
- else:
- outfiledata += '%x ' % var
- variablesmoved += 1
- a += 0x4
- outfiledata += '\n'
- opcodesmoved += 1
- outfiledata += '\n'
- if sys.argv[1] == '-e2':
- if not os.path.isdir('Scripts'):
- os.mkdir('Scripts')
- outfile = open('Scripts\\' + filename.rsplit('\\')[1] + '.txt','wb')
- else:
- outfile = open(filename + '.txt','wb')
- outfile.write(outfiledata)
- outfile.close()
- def compiled(filename):
- filedata = get_data(filename).splitlines()
- #olddata = get_data('Bin\\' + filename.rsplit('.',1)[0].split('\\')[1])
- newfiledata = ''#olddata[:struct.unpack('<I',olddata[0x1b74:0x1b78])[0]]
- newstrings = ''
- stringstofix = []
- opcodestouse = 1
- opcodesmoved = 0
- firstrun = True
- firstopcode = True
- locstofix = []
- origlocs = []
- i = 0
- stringcount = 0
- lastcnt = 0
- #for i in range(0,len(filedata)):
- while i < len(filedata):
- if filedata[i].find('======') != -1 and firstrun == True:
- opcodestouse = 1
- firstrun = False
- elif filedata[i].find('======') != -1 and firstrun == False:
- opcodestouse = 0
- if filedata[i] != '' and filedata[i].find('=') == -1 and filedata[i].find('#') == -1:
- line = re.findall(r'(?:[^\s,"]|"(?:\\.|[^"])*")+', filedata[i])
- isnewloc = False
- isdeletedloc = False
- try:
- if line[0] != '{':
- isnewloc = True
- else:
- line.pop(0)
- if firstopcode == True:
- lastcnt = int(line[0],16)
- firstopcode = False
- else:
- if lastcnt + 1 != int(line[0],16):
- isdeletedloc = True
- origlocs.append([int(line[0],16),len(newfiledata)])
- lastcnt = int(line[0],16)
- line.pop(0)
- line.pop(0)
- except Exception:
- isnewloc = True
- c = 0
- if opcodestouse == 0:
- while c != len(opcodes):
- if opcodes[c][1] == line[0]:
- variablecount = opcodes[c][2]
- opcode = opcodes[c][0]
- break;
- if c+1 == len(opcodes):
- print 'Unknown script opcode: %s on line: %x' % (line[0],i)
- sys.exit()
- c += 1
- else:
- while c != len(opcodes1):
- if opcodes1[c][1] == line[0]:
- variablecount = opcodes1[c][2]
- opcode = opcodes1[c][0]
- break;
- if c+1 == len(opcodes1):
- print 'Unknown pre-script opcode: %s on line: %x' % (line[0],i)
- sys.exit()
- c += 1
- newfiledata += struct.pack('<I',opcode)
- line.pop(0)
- # Handle text strings manually
- if opcodestouse == 0 and (opcode == 0x70 or opcode == 0x71 or opcode == 0x73):
- if opcode == 0x70:
- i += 2
- string = ''
- while filedata[i].find('///') == -1:
- string += filedata[i] + '\n'
- i += 1
- string = string[:-1]
- if len(string) % 0x4 == 0:
- string += '\x00'
- while len(string) % 0x4 != 0:
- string += '\x00'
- stringstofix.append([len(newfiledata),string])
- newfiledata += struct.pack('<I',0x0) # fix later
- stringcount += 1
- elif opcode == 0x71:
- i += 2
- string = ''
- while filedata[i].find('///') == -1:
- string += filedata[i] + '\n'
- i += 1
- string = string[:-1]
- if len(string) % 0x4 == 0:
- string += '\x00'
- while len(string) % 0x4 != 0:
- string += '\x00'
- stringstofix.append([len(newfiledata),string])
- newfiledata += struct.pack('<I',0x0) # fix later
- i += 1
- string = filedata[i]
- i += 1
- if len(string) % 0x4 == 0:
- string += '\x00'
- while len(string) % 0x4 != 0:
- string += '\x00'
- stringstofix.append([len(newfiledata),string])
- newfiledata += struct.pack('<I',0x0) # fix later
- stringcount += 1
- elif opcode == 0x73:
- newfiledata += struct.pack('<I',int(line[0],16))
- i += 2
- string = ''
- while filedata[i].find('///') == -1:
- string += filedata[i] + '\n'
- i += 1
- string = string[:-1]
- if len(string) % 0x4 == 0:
- string += '\x00'
- while len(string) % 0x4 != 0:
- string += '\x00'
- stringstofix.append([len(newfiledata),string])
- newfiledata += struct.pack('<I',0x0) # fix later
- # Everything that isn't a text string
- else:
- for a in range(0,variablecount):
- if line[a].find('"') != -1:
- line[a] = line[a][1:-1]
- #-----------------------------
- if line[a].find('label_') != -1:
- #print 'appending %x' % len(newfiledata)
- locstofix.append([int(line[a].split('_')[1],16),len(newfiledata)])
- #newfiledata += struct.pack('<I',int(line[a].split('_')[1],16))
- newfiledata += struct.pack('<I',0x0) # fix later
- elif opcode == 0x1 and opcodestouse == 0:
- if line[a].find('input') != -1:
- newfiledata += struct.pack('<I',0x1)
- else:
- newfiledata += struct.pack('<I',int(line[a],16))
- #-----------------------------
- elif opcode == 0xb and a == 0 and opcodestouse == 0:
- if len(line[a]) % 0x4 == 0:
- line[a] += '\x00'
- while len(line[a]) % 0x4 != 0:
- line[a] += '\x00'
- stringstofix.append([len(newfiledata),line[a]])
- newfiledata += struct.pack('<I',0x0) # fix later
- #-----------------------------
- elif opcode == 0x2c and a == 0 and opcodestouse == 0:
- if len(line[a]) % 0x4 == 0:
- line[a] += '\x00'
- while len(line[a]) % 0x4 != 0:
- line[a] += '\x00'
- stringstofix.append([len(newfiledata),line[a]])
- newfiledata += struct.pack('<I',0x0) # fix later
- #-----------------------------
- elif opcode == 0x3c:
- newfiledata += struct.pack('<I',int(line[a].split(' ')[1],16))
- #-----------------------------
- elif opcode == 0x43 and a == 0 and opcodestouse == 0:
- if len(line[a]) % 0x4 == 0:
- line[a] += '\x00'
- while len(line[a]) % 0x4 != 0:
- line[a] += '\x00'
- stringstofix.append([len(newfiledata),line[a]])
- newfiledata += struct.pack('<I',0x0) # fix later
- #-----------------------------
- elif opcode == 0x47 and a == 1:
- if len(line[a]) % 0x4 == 0:
- line[a] += '\x00'
- while len(line[a]) % 0x4 != 0:
- line[a] += '\x00'
- stringstofix.append([len(newfiledata),line[a]])
- newfiledata += struct.pack('<I',0x0) # fix later
- #-----------------------------
- elif opcode == 0x4f and a == 0 and opcodestouse == 0:
- #newfiledata += struct.pack('<I',int(line[a]))
- newfiledata += struct.pack('<I',stringcount)
- #-----------------------------
- elif opcode == 0x60:
- if a == 1 or a == 2 or a == 3 or a == 4:
- newfiledata += struct.pack('<I',int(line[a].split(' ')[1],16))
- else:
- newfiledata += struct.pack('<I',int(line[a],16))
- #-----------------------------
- elif opcode == 0x61 and opcodestouse == 0:
- if a == 1 or a == 2 or a == 3 or a == 4 or a == 5:
- newfiledata += struct.pack('<I',int(line[a].split(' ')[1],16))
- else:
- newfiledata += struct.pack('<I',int(line[a],16))
- #-----------------------------
- elif opcode == 0x62:
- if a == 1 or a == 2:
- newfiledata += struct.pack('<I',int(line[a].split(' ')[1],16))
- else:
- newfiledata += struct.pack('<I',int(line[a],16))
- #-----------------------------
- elif opcode == 0x64:
- if a == 1 or a == 2:
- newfiledata += struct.pack('<I',int(line[a].split(' ')[1],16))
- else:
- newfiledata += struct.pack('<I',int(line[a],16))
- #-----------------------------
- elif opcode == 0x6a and a == 0:
- if len(line[a]) % 0x4 == 0:
- line[a] += '\x00'
- while len(line[a]) % 0x4 != 0:
- line[a] += '\x00'
- stringstofix.append([len(newfiledata),line[a]])
- newfiledata += struct.pack('<I',0x0) # fix later
- #-----------------------------
- elif opcode == 0x6d and (a == 1 or a == 2):
- if len(line[a]) % 0x4 == 0:
- line[a] += '\x00'
- while len(line[a]) % 0x4 != 0:
- line[a] += '\x00'
- stringstofix.append([len(newfiledata),line[a]])
- newfiledata += struct.pack('<I',0x0) # fix later
- #-----------------------------
- elif opcode == 0x6e and a == 1:
- if len(line[a]) % 0x4 == 0:
- line[a] += '\x00'
- while len(line[a]) % 0x4 != 0:
- line[a] += '\x00'
- stringstofix.append([len(newfiledata),line[a]])
- newfiledata += struct.pack('<I',0x0) # fix later
- #-----------------------------
- elif opcode == 0x82 and a == 1:
- if len(line[a]) % 0x4 == 0:
- line[a] += '\x00'
- while len(line[a]) % 0x4 != 0:
- line[a] += '\x00'
- stringstofix.append([len(newfiledata),line[a]])
- newfiledata += struct.pack('<I',0x0) # fix later
- #-----------------------------
- elif opcode == 0x83 and a == 1:
- if len(line[a]) % 0x4 == 0:
- line[a] += '\x00'
- while len(line[a]) % 0x4 != 0:
- line[a] += '\x00'
- stringstofix.append([len(newfiledata),line[a]])
- newfiledata += struct.pack('<I',0x0) # fix later
- #-----------------------------
- elif opcode == 0x84 and a == 1 and opcodestouse == 0:
- if line[a] == 'open':
- newfiledata += struct.pack('<I',1)
- else:
- newfiledata += struct.pack('<I',0)
- #-----------------------------
- elif opcode == 0x92:
- if a == 0:
- if len(line[a]) % 0x4 == 0:
- line[a] += '\x00'
- while len(line[a]) % 0x4 != 0:
- line[a] += '\x00'
- stringstofix.append([len(newfiledata),line[a]])
- newfiledata += struct.pack('<I',0x0) # fix later
- elif a == 1:
- newfiledata += struct.pack('<I',int(line[a].split(' ')[1],16))
- else:
- newfiledata += struct.pack('<I',int(line[a],16))
- #-----------------------------
- else:
- newfiledata += struct.pack('<I',int(line[a],16))
- opcodesmoved += 1
- i += 1
- #print origlocs
- for i in range(0,len(locstofix)):
- loctojump = locstofix[i][0]
- MatchFound = False
- while MatchFound == False:
- for a in range(0,len(origlocs)):
- if loctojump == origlocs[a][0]:
- loc = origlocs[a][1]
- MatchFound = True
- break;
- loctojump += 1
- if loctojump >= len(newfiledata):
- print 'Cannot find loc to jump to for %x' % locstofix[i][0]
- sys.exit()
- newfiledata = newfiledata[:locstofix[i][1]] + struct.pack('<I',loc) + newfiledata[locstofix[i][1]+0x4:]
- for i in range(0,len(stringstofix)):
- tmp1 = '\x00' + newstrings
- if tmp1.find('\x00' + stringstofix[i][1]) != -1:
- newpos = len(newfiledata) + tmp1.find('\x00' + stringstofix[i][1])
- else:
- newpos = len(newfiledata)+len(newstrings)
- newstrings += stringstofix[i][1]
- newfiledata = newfiledata[:stringstofix[i][0]] + struct.pack('<I',newpos) + newfiledata[stringstofix[i][0]+0x4:]
- newfiledata += newstrings
- if not os.path.isdir('Compiled'):
- os.mkdir('Compiled')
- if sys.argv[1] == '-c2':
- outfile = open('Compiled\\' + filename.rsplit('.',1)[0].rsplit('\\',1)[1],'wb')
- else:
- outfile = open(filename.split('.')[0] + '.bin','wb')
- outfile.write(newfiledata)
- outfile.close()
- '''
- if sys.argv[1] == '-c2':
- filename1 = 'Bin\\' + filename.rsplit('.',1)[0].rsplit('\\')[1]
- else:
- filename1 = 'Bin\\' + filename.rsplit('.',1)[0]
- if newfiledata != get_data(filename1):
- print "files don't match"
- sys.exit()
- '''
- filedata = None
- newfiledata = None
- newstrings = None
- tmp1 = None
- origlocs = None
- stringstofix = None
- def compileMessage(filename):
- filedata = get_data(filename).splitlines()
- newfiledata = ''
- newstrings = ''
- i = 0
- tables = []
- while i < len(filedata):
- if filedata[i].find('===') != -1:
- tables.append(i)
- newfiledata += struct.pack('<I',0x0) #fix later
- i += 1
- i = 0
- isnewloc = False
- firstopcode = True
- isdeletedloc = False
- origlocs = []
- lastcnt = 0
- locstofix = []
- opcodesmoved = 0
- stringstofix = []
- while i < len(filedata):
- for a in range(0,len(tables)):
- if i == tables[a]:
- newfiledata = newfiledata[:(a*0x4)] + struct.pack('<I',len(newfiledata)) + newfiledata[(a*0x4)+0x4:]
- if filedata[i] != '' and filedata[i].find('=') == -1 and filedata[i].find('#') == -1:
- line = re.findall(r'(?:[^\s,"]|"(?:\\.|[^"])*")+', filedata[i])
- try:
- if line[0] != '{':
- isnewloc = True
- else:
- line.pop(0)
- if firstopcode == True:
- lastcnt = int(line[0],16)
- firstopcode = False
- else:
- if lastcnt + 1 != int(line[0],16):
- isdeletedloc = True
- origlocs.append([int(line[0],16),len(newfiledata)])
- lastcnt = int(line[0],16)
- line.pop(0)
- line.pop(0)
- except Exception:
- isnewloc = True
- c = 0
- while c != len(opcodesmessage):
- if opcodesmessage[c][1] == line[0]:
- variablecount = opcodesmessage[c][2]
- opcode = opcodesmessage[c][0]
- break;
- if c+1 == len(opcodesmessage):
- print 'Unknown message opcode: %s on line: %x' % (line[0],i)
- sys.exit()
- c += 1
- newfiledata += struct.pack('<I',opcode)
- line.pop(0)
- variablesmoved = 0
- if opcode == 0x2c:
- newfiledata += struct.pack('<I',int(line[0],16))
- i += 2
- string = ''
- while filedata[i].find('///') == -1:
- string += filedata[i] + '\n'
- i += 1
- string = string[:-1]
- if len(string) % 0x4 == 0:
- string += '\x00'
- while len(string) % 0x4 != 0:
- string += '\x00'
- stringstofix.append([len(newfiledata),string])
- newfiledata += struct.pack('<I',0x0) # fix later
- else:
- for a in range(0,variablecount):
- if line[a].find('"') != -1:
- line[a] = line[a][1:-1]
- #-----------------------------
- if line[a].find('label_') != -1:
- #print 'appending %x' % len(newfiledata)
- locstofix.append([int(line[a].split('_')[1],16),len(newfiledata)])
- #newfiledata += struct.pack('<I',int(line[a].split('_')[1],16))
- newfiledata += struct.pack('<I',0x0) # fix later
- else:
- newfiledata += struct.pack('<I',int(line[a],16))
- opcodesmoved += 1
- i += 1
- #print origlocs
- for i in range(0,len(locstofix)):
- loctojump = locstofix[i][0]
- MatchFound = False
- while MatchFound == False:
- for a in range(0,len(origlocs)):
- if loctojump == origlocs[a][0]:
- loc = origlocs[a][1]
- MatchFound = True
- break;
- loctojump += 1
- if loctojump >= len(newfiledata):
- print 'Cannot find loc to jump to for %x' % locstofix[i][0]
- sys.exit()
- newfiledata = newfiledata[:locstofix[i][1]] + struct.pack('<I',loc) + newfiledata[locstofix[i][1]+0x4:]
- for i in range(0,len(stringstofix)):
- tmp1 = '\x00' + newstrings
- if tmp1.find('\x00' + stringstofix[i][1]) != -1:
- newpos = len(newfiledata) + tmp1.find('\x00' + stringstofix[i][1])
- else:
- newpos = len(newfiledata)+len(newstrings)
- newstrings += stringstofix[i][1]
- newfiledata = newfiledata[:stringstofix[i][0]] + struct.pack('<I',newpos) + newfiledata[stringstofix[i][0]+0x4:]
- newfiledata += newstrings
- if not os.path.isdir('Compiled'):
- os.mkdir('Compiled')
- if sys.argv[1] == '-c2':
- outfile = open('Compiled\\' + filename.rsplit('.',1)[0].rsplit('\\',1)[1],'wb')
- else:
- outfile = open(filename.split('.')[0] + '.bin','wb')
- outfile.write(newfiledata)
- outfile.close()
- '''
- if sys.argv[1] == '-c2':
- filename1 = 'Bin\\' + filename.rsplit('.',1)[0].rsplit('\\')[1]
- else:
- filename1 = 'Bin\\' + filename.rsplit('.',1)[0]
- if newfiledata != get_data(filename1):
- print "files don't match"
- sys.exit()
- '''
- filedata = None
- newfiledata = None
- newstrings = None
- tmp1 = None
- origlocs = None
- stringstofix = None
- if __name__ == '__main__':
- if sys.argv[1] == '-e1':
- print 'Extracting file %s' % sys.argv[2]
- if sys.argv[2].find('Message_') != -1:
- extractMessage(sys.argv[2])
- else:
- extract(sys.argv[2])
- elif sys.argv[1] == '-e2':
- for filename in os.listdir(sys.argv[2]):
- if filename.find('.bin') != -1 and (filename.find('Auto_') != -1 or filename.find('Nov_') != -1 or filename.find('SN00') != -1):
- #if filename.find('.bin') != -1 and (filename.find('SN00') != -1):
- print 'Extracting file %s' % sys.argv[2] + '\\' + filename
- extract(sys.argv[2] + '\\' + filename)
- elif filename.find('Message_') != -1:
- print 'Extracting file %s' % sys.argv[2] + '\\' + filename
- extractMessage(sys.argv[2] + '\\' + filename)
- elif sys.argv[1] == '-c1':
- print 'Compiling file %s' % sys.argv[2]
- if sys.argv[2].find('Message_') != -1:
- compileMessage(sys.argv[2])
- else:
- compiled(sys.argv[2])
- elif sys.argv[1] == '-c2':
- for filename in os.listdir(sys.argv[2]):
- if filename.find('.txt') != -1 and filename.find('Message_') != -1:
- print 'Compiling file %s' % sys.argv[2] + '\\' + filename
- compileMessage(sys.argv[2] + '\\' + filename)
- elif filename.find('.txt') != -1:
- print 'Compiling file %s' % sys.argv[2] + '\\' + filename
- compiled(sys.argv[2] + '\\' + filename)
- else:
- print 'Usage: script.py [options] [infile/infolder]'
- print 'Options:\n-e1 [infile] >extract single file'
- print '-e2 [infolder] >extract folder'
- print '-c1 [infile] >compile single file'
- print '-c2 [infolder] >compile folder'
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement