Advertisement
Guest User

ZnK Magic Insert v1.0

a guest
Aug 1st, 2014
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.30 KB | None | 0 0
  1. import os
  2. import struct
  3.  
  4. def get_data(filename):
  5.     totalbytes = os.path.getsize(filename)
  6.     infile = open(filename, 'rb')
  7.     totalfiledata = infile.read(totalbytes)
  8.     infile.close()
  9.     return totalfiledata
  10.  
  11. def replacestr(origstr,replacestr,startpos,replacelen):
  12. #Returns a string with a replaced sub-string
  13. #origstr - the original string, replacestr = the string to replace
  14. #startpos - where the replacement string should go
  15. #replacelen - how many characters of the original string to replace
  16.     return origstr[:startpos] + replacestr + origstr[startpos+replacelen:]
  17.  
  18. #Outfile data is updated with replacestr many times and eventually is written as the output
  19. #origfiledata is a copy of the original data for reference purposes
  20. origfiledata = get_data('t_magic.orig')
  21. outfiledata = get_data('t_magic.orig')
  22.  
  23. tbl1pos = 0x2   #Position of first pointer in table
  24. offset = 0      #Difference in length between original and new files
  25. previouspointer = 0 #Used to track repeats in 'Table 1'
  26. #Table 1 starts at 0x0 and ends at 0x2BC
  27.  
  28. inputindex = 0  #Tracks position within input data
  29. inputdata = []  #Stores input data
  30. infile = 'magic.data'   #Input filename
  31. #Read the input file and store it in inputdata
  32. with open(infile) as f:
  33.     for line in f:
  34.         line = line[:-1]    #Trim out the newline character
  35.         inputdata.append(line.split('\t'))  #Split the line by the tab delimiter
  36.  
  37. while tbl1pos < 0x2b0: #Stop once 0x2b0 is reached (last pointer in Table 1)
  38.     tbl1pointer = struct.unpack('H',origfiledata[tbl1pos:tbl1pos+2])[0] #Decode the table 1 pointer
  39.     if tbl1pointer != previouspointer:  #There are repeats in Table 1. We want to ignore the repeats.
  40.         previouspointer = tbl1pointer   #Resets variable to again check for repeats
  41.         #Decode the values from Table 2 (the actual pointers)
  42.         #Pointer 1 is the move name, pointer 2 is the description
  43.         tbl2pointer1 = struct.unpack('H',origfiledata[tbl1pointer-4:tbl1pointer-2])[0]
  44.         tbl2pointer2 = struct.unpack('H',origfiledata[tbl1pointer-2:tbl1pointer])[0]
  45.         if tbl2pointer1 != 0:   #There are some dummy entries in Table 2. We want to ignore those.
  46.             #Find length of original move code
  47.             origlength = origfiledata.find('\x00',tbl2pointer2) - tbl2pointer1
  48.             #Compute new pointers
  49.             tbl2pointer1new = struct.pack('H',tbl2pointer1+offset)
  50.             tbl2pointer2new = struct.pack('H',tbl2pointer1+offset+1+len(inputdata[inputindex][1]))
  51.  
  52.             #Y table codes are 2 lines long, X codes are 3 lines long
  53.             if inputdata[inputindex][0] == 'Y':
  54.                 #Compute length of new move code
  55.                 newlength = len(inputdata[inputindex][1]) + len(inputdata[inputindex+1][1]) + 1
  56.                 #Build new move code text (for insertion later)
  57.                 mystring = inputdata[inputindex][1] + '\x00' + inputdata[inputindex+1][1]
  58.                 inputindex += 2 #Slide down to the next entry in the input table
  59.            
  60.             elif inputdata[inputindex][0] == 'X':
  61.                 #Compute length of new move code
  62.                 newlength = len(inputdata[inputindex][1]) + len(inputdata[inputindex+1][1]) + len(inputdata[inputindex+2][1]) + 3
  63.                 #Build new move code text (for insertion later)
  64.                 mystring = inputdata[inputindex][1] + '\x00' + inputdata[inputindex+1][1] + '\\n' + inputdata[inputindex+2][1]
  65.                 inputindex += 3 #Slide down to the next entry in the input table
  66.  
  67.            
  68. # Update name pointer            
  69.             #For the output, rewrite table2 pointers 1 and 2 (move name and description respectively)
  70.             outfiledata = replacestr(outfiledata,tbl2pointer1new,tbl1pointer-4,2)
  71.             outfiledata = replacestr(outfiledata,tbl2pointer2new,tbl1pointer-2,2)
  72.             #Rewrite the move code
  73.             outfiledata = replacestr(outfiledata,mystring,tbl2pointer1+offset,origlength)
  74.             offset += newlength - origlength #Update offset
  75.     tbl1pos += 2    #Slide down table 1 by 2 bytes (table 1 entries are 2 bytes long)
  76.  
  77. #Write the output
  78. outfilename = 't_magic._dt'
  79. outfile = open(outfilename,'wb')
  80. outfile.write(outfiledata)
  81. outfile.close()
  82.  
  83. #Message displays data needed to adjust data.lst
  84. print 'Completed. t_magic.dt length is ' + hex(len(outfiledata))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement