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:]
- #Outfile data is updated with replacestr many times and eventually is written as the output
- #origfiledata is a copy of the original data for reference purposes
- origfiledata = get_data('t_magic.orig')
- outfiledata = get_data('t_magic.orig')
- tbl1pos = 0x2 #Position of first pointer in table
- offset = 0 #Difference in length between original and new files
- previouspointer = 0 #Used to track repeats in 'Table 1'
- #Table 1 starts at 0x0 and ends at 0x2BC
- inputindex = 0 #Tracks position within input data
- inputdata = [] #Stores input data
- infile = 'magic.data' #Input filename
- #Read the input file and store it in inputdata
- with open(infile) as f:
- for line in f:
- line = line[:-1] #Trim out the newline character
- inputdata.append(line.split('\t')) #Split the line by the tab delimiter
- while tbl1pos < 0x2b0: #Stop once 0x2b0 is reached (last pointer in Table 1)
- tbl1pointer = struct.unpack('H',origfiledata[tbl1pos:tbl1pos+2])[0] #Decode the table 1 pointer
- if tbl1pointer != previouspointer: #There are repeats in Table 1. We want to ignore the repeats.
- previouspointer = tbl1pointer #Resets variable to again check for repeats
- #Decode the values from Table 2 (the actual pointers)
- #Pointer 1 is the move name, pointer 2 is the description
- tbl2pointer1 = struct.unpack('H',origfiledata[tbl1pointer-4:tbl1pointer-2])[0]
- tbl2pointer2 = struct.unpack('H',origfiledata[tbl1pointer-2:tbl1pointer])[0]
- if tbl2pointer1 != 0: #There are some dummy entries in Table 2. We want to ignore those.
- #Find length of original move code
- origlength = origfiledata.find('\x00',tbl2pointer2) - tbl2pointer1
- #Compute new pointers
- tbl2pointer1new = struct.pack('H',tbl2pointer1+offset)
- tbl2pointer2new = struct.pack('H',tbl2pointer1+offset+1+len(inputdata[inputindex][1]))
- #Y table codes are 2 lines long, X codes are 3 lines long
- if inputdata[inputindex][0] == 'Y':
- #Compute length of new move code
- newlength = len(inputdata[inputindex][1]) + len(inputdata[inputindex+1][1]) + 1
- #Build new move code text (for insertion later)
- mystring = inputdata[inputindex][1] + '\x00' + inputdata[inputindex+1][1]
- inputindex += 2 #Slide down to the next entry in the input table
- elif inputdata[inputindex][0] == 'X':
- #Compute length of new move code
- newlength = len(inputdata[inputindex][1]) + len(inputdata[inputindex+1][1]) + len(inputdata[inputindex+2][1]) + 3
- #Build new move code text (for insertion later)
- mystring = inputdata[inputindex][1] + '\x00' + inputdata[inputindex+1][1] + '\\n' + inputdata[inputindex+2][1]
- inputindex += 3 #Slide down to the next entry in the input table
- # Update name pointer
- #For the output, rewrite table2 pointers 1 and 2 (move name and description respectively)
- outfiledata = replacestr(outfiledata,tbl2pointer1new,tbl1pointer-4,2)
- outfiledata = replacestr(outfiledata,tbl2pointer2new,tbl1pointer-2,2)
- #Rewrite the move code
- outfiledata = replacestr(outfiledata,mystring,tbl2pointer1+offset,origlength)
- offset += newlength - origlength #Update offset
- tbl1pos += 2 #Slide down table 1 by 2 bytes (table 1 entries are 2 bytes long)
- #Write the output
- outfilename = 't_magic._dt'
- outfile = open(outfilename,'wb')
- outfile.write(outfiledata)
- outfile.close()
- #Message displays data needed to adjust data.lst
- print 'Completed. t_magic.dt length is ' + hex(len(outfiledata))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement