Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import struct
- def get_data(filename):
- totalbytes = os.path.getsize(filename)
- infile = open(filename, 'rb')
- totalfiledata = infile.read(totalbytes)
- infile.close()
- return totalfiledata
- def replacestr(origstr,replacestr,startpos,replacelen):
- #Returns a string with a replaced sub-string
- #origstr - the original string, replacestr = the string to replace
- #startpos - where the replacement string should go
- #replacelen - how many characters of the original string to replace
- return origstr[:startpos] + replacestr + origstr[startpos+replacelen:]
- def myprogram(filename,messageoffset):
- filedata = get_data(filename + '.orig')
- unknown_data1 = filedata[:messageoffset]
- unpacked_size = struct.unpack('<I',filedata[messageoffset+4:messageoffset+8])[0]
- number_of_chunks = struct.unpack('<I',filedata[messageoffset+8:messageoffset+12])[0]
- unknown_data2 = filedata[-8:]
- filedata = get_data(filename + '.unpack3')
- pos = 0
- output = []
- print '{:8} {}'.format(filename, number_of_chunks)
- for chunk in range(number_of_chunks):
- if chunk == number_of_chunks - 1:
- output.append(unknown_data2)
- break
- output2 = ['\x00']
- bits = 8
- flag = False
- if len(filedata) - pos >= 0x7FF0:
- chunk_end = 0x7FF0 * (chunk + 1)
- else:
- chunk_end = len(filedata)
- startpos = pos
- while pos < chunk_end:
- if bits == 0:
- if chunk_end - pos < 16:
- flag = True
- s = '0'*(14 - (chunk_end - pos)) + '11' + '0'*(chunk_end - pos)
- print '{:8} {} {}'.format(filename, chunk, s)
- output2.append(struct.pack('>H',int(s,2)))
- else:
- output2.append('\x00'*2)
- bits = 16
- output2.append(filedata[pos])
- bits -= 1
- pos += 1
- if flag:
- output2.append('\x00')
- else:
- s = '0'*14 + '11'
- output2.append(struct.pack('>H',int(s,2)))
- output2.append('\x00')
- if chunk != 0:
- output.append('\x01')
- output.append(struct.pack('<H',len(''.join(output2)) + 3)) #Chunk size, 3 = size plus 00 byte
- output.append('\x00')
- output += output2
- newlen = len("".join(output)) + 8 #8 = 4 bytes for unpacked size, 4 bytes for # of chunks
- outfile = open(filename + '.tbb','wb')
- outfile.write("".join([unknown_data1, #Data before the message
- struct.pack('<I',newlen), #Message size
- struct.pack('<I',unpacked_size),
- struct.pack('<I',number_of_chunks)]))
- outfile.write(''.join(output))
- outfile.close()
- filedata = get_data('data.lst')
- basepos = 0x14150
- newsize = os.path.getsize(filename + '.tbb')
- pos = filedata.find(filename + '\x00'*(8-len(filename)),basepos)
- if pos == -1:
- print 'Error'
- quit()
- filedata = replacestr(filedata,struct.pack('<I',newsize),pos+8,4)
- outfile = open('data.lst','wb')
- outfile.write(filedata)
- outfile.close()
- myprogram('monslib',0x1F)
- myprogram('questlib',0x1F)
- myprogram('fldlist0',0x1F)
- myprogram('fldlist1',0x1F)
- myprogram('fldlist2',0x1F)
- myprogram('fldlist3',0x1F)
- myprogram('helplib',0x1F)
- myprogram('foodarea',0x1F)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement