Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os,sys,struct
- def get_data(filename):
- totalbytes = os.path.getsize(filename)
- infile = open(filename, 'rb')
- totalfiledata = infile.read(totalbytes)
- return totalfiledata
- opcodes = [
- [0x8000,'0x8000',[]],[0x8006,'0x8006',[]],[0x8008,'0x8008',[]],[0x8009,'0x8009',[]],
- [0x800b,'0x800b',[]],[0x8013,'0x8013',[]],[0x801f,'0x801f',[]],[0x8020,'0x8020',[]],[0x8021,'0x8021',[]],
- [0x8022,'0x8022',[]],[0x8023,'0x8023',[]],[0x8024,'0x8024',[]],[0x8026,'0x8026',[]],
- [0x802a,'0x802a',[]],[0x802c,'0x802c',[]],
- [0x802e,'0x802e',[]],[0x8032,'0x8032',[]],[0x803b,'0x803b',[]],
- [0x8041,'0x8041',[]],[0x8042,'0x8042',[]],[0x8054,'0x8054',[]],[0x805c,'0x805c',[]],
- [0x8069,'0x8069',[]],[0x806c,'0x806c',[]],
- [0x8076,'0x8076',[]],[0x807b,'0x807b',[]],
- [0x807d,'0x807d',[]],
- [0x8082,'0x8082',[]],
- [0x8098,'0x8098',[]],[0x80be,'0x80be',[]],[0x80bf,'0x80bf',[]],
- [0x82dd,'0x82dd',['d']],[0x82de,'0x82de',['f']],[0x82df,'0x82df',['d','s']],
- [0x82e0,'0x82e0',['d','ukn']],
- ]
- def extract_script(filename):
- if sys.argv[1] == '-e1':
- filedata = get_data(filename)
- else:
- filedata = get_data(sys.argv[2] + '\\' + filename)
- outdata = ''
- outstrings = ''
- hdrend = struct.unpack('<I',filedata[0x3c:0x40])[0]
- pos = 0x18
- sections = []
- while pos < hdrend:
- sections.append([filedata[pos:filedata.find('\x00',pos)],struct.unpack('<I',filedata[pos+0x20:pos+0x24])[0],struct.unpack('<I',filedata[pos+0x24:pos+0x28])[0]])
- pos += 0x28
- strcnt = 0
- for i in range(0,len(sections)):
- pos = sections[i][2]
- if i > 0:
- outdata += '\n'
- outdata += '### ' + sections[i][0] + ' %x ###\n\n' % struct.unpack('<H',filedata[pos:pos+2])[0]
- pos += 2
- while pos < sections[i][2]+sections[i][1]:
- opcode = struct.unpack('<H',filedata[pos:pos+2])[0]
- if opcode >= 0x8200:
- c = 0
- while c != len(opcodes):
- if opcodes[c][0] == opcode:
- variables = opcodes[c][2]
- break;
- if c+1 == len(opcodes):
- print 'Unknown script opcode: %x at loc: %x in file %s' % (opcode,pos,filename)
- sys.exit()
- c += 1
- outdata += '%x :: %s { ' % (pos,opcodes[c][1])
- pos += 0x2
- for a in range(0,len(variables)):
- if variables[a] == 'b':
- outdata += '%x ' % struct.unpack('<B',filedata[pos:pos+1])[0]
- pos += 1
- elif variables[a] == 'w':
- outdata += '%x ' % struct.unpack('<H',filedata[pos:pos+2])[0]
- pos += 2
- elif variables[a] == 'd':
- if opcode == 0x82df:
- strlen = struct.unpack('<I',filedata[pos:pos+4])[0]
- elif opcode == 0x82e0:
- varlen = struct.unpack('<I',filedata[pos:pos+4])[0]
- else:
- outdata += '%x ' % struct.unpack('<I',filedata[pos:pos+4])[0]
- pos += 4
- elif variables[a] == 's':
- if opcode == 0x82df:
- string = filedata[pos:pos+strlen]
- #if len(string) > 0:
- # while string[len(string)-1] == '\x00':
- # string = string[:len(string)-1]
- outdata += '<string %d> ' % strcnt
- outstrings += ('%d: ' % strcnt) + string + '\n'
- strcnt += 1
- pos += strlen
- elif variables[a] == 'f':
- outdata += '%0.1f ' % struct.unpack('<I',filedata[pos:pos+4])[0]
- pos += 4
- elif variables[a] == 'ukn':
- outdata += '%s ' % filedata[pos:pos+varlen]
- pos += varlen
- else:
- print 'Unknown variable type'
- sys.exit()
- else:
- outdata += '%x :: 0x%x { ' % (pos,opcode)
- pos += 0x2
- outdata += '}\n'
- if not os.path.exists(sys.argv[2] + '\\Scripts'):
- os.mkdir(sys.argv[2] + '\\Scripts')
- if sys.argv[1] == '-e1':
- outfile = open(filename.rsplit('.')[0] + '_script.txt','wb')
- else:
- outfile = open(sys.argv[2] + '\\Scripts\\' + filename.rsplit('.')[0] + '_script.txt','wb')
- outfile.write(outdata)
- outfile.close()
- if sys.argv[1] == '-e1':
- outfile = open(filename.rsplit('.')[0] + '_strings.txt','wb')
- else:
- outfile = open(sys.argv[2] + '\\Scripts\\' + filename.rsplit('.')[0] + '_strings.txt','wb')
- outfile.write(outstrings)
- outfile.close()
- if __name__ == '__main__':
- if sys.argv[1] == '-e1':
- extract_script(sys.argv[2])
- elif sys.argv[1] == '-e2':
- for filename in os.listdir(sys.argv[2]):
- if filename.find ('.bin') != -1:
- print 'Extracting %s' % filename
- extract_script(filename)
- else:
- print 'Argument error'
- sys.exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement