Advertisement
Guest User

ZnK ItemTool v1

a guest
Jul 5th, 2014
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.96 KB | None | 0 0
  1. import struct
  2. import os
  3.  
  4. def get_data(filename):
  5.     totalbytes = os.path.getsize(filename)
  6.     infile = open(filename, 'rb')
  7.     totalfiledata = infile.read(totalbytes)
  8.     return totalfiledata
  9.  
  10. def replacestr(origstr,replacestr,startpos,replacelen):
  11. #Returns a string with a replaced sub-string
  12. #origstr - the original string, replacestr = the string to replace
  13. #startpos - where the replacement string should go
  14. #replacelen - how many characters of the original string to replace
  15.     return origstr[:startpos] + replacestr + origstr[startpos+replacelen:]
  16.  
  17. infile = 't_ittxt.data'
  18. infile1 = 't_ittxt.orig'
  19. origfiledata = get_data(infile1)
  20. outfiledata = get_data(infile1)
  21.  
  22. tblindex = 0x16
  23. offset = 0
  24. inputindex = 0
  25. lasttblpointer = 0x0
  26. inputdata = []
  27.  
  28. #Read the input file
  29. with open(infile) as f:
  30.     for line in f:
  31.         inputdata.append(line.split('\t'))
  32.  
  33. while True:
  34. #Break out of the loop if at end of pointers
  35.     if outfiledata[tblindex:tblindex+2] == '\x00\x00':
  36.         break
  37.     if inputindex == len(inputdata):
  38.         break
  39.  
  40.     origpointer = struct.unpack('<H',origfiledata[tblindex:tblindex+2])[0]+8
  41.     if origpointer != lasttblpointer:
  42.         lasttblpointer = origpointer
  43.     #Unpack pointer, add offset
  44.         tblpointer = struct.unpack('<H',outfiledata[tblindex:tblindex+2])[0] + offset
  45.     #Rewrite the pointer
  46.         outfiledata = outfiledata[:tblindex] + struct.pack('<H',tblpointer) + outfiledata[tblindex+2:]
  47.  
  48.         itemnamepointer = tblpointer + 8 #The first pointer.
  49.         itemdescriptionpointer = itemnamepointer + len(inputdata[inputindex][1])
  50.         code = struct.pack('<H',itemnamepointer) + struct.pack('<H',itemdescriptionpointer)
  51.        
  52.  
  53.         origend = origfiledata.find('\x00',origpointer)
  54.         origend = origfiledata.find('\x00',origend+1)
  55.         origlength = origend-origpointer
  56.  
  57.         if inputdata[inputindex+1][0] != "": #A 1-length record (one line long)
  58.             code += inputdata[inputindex][1][:-1] + '\x00' + " "
  59.             inputindex += 1
  60.         elif inputdata[inputindex+2][0] != "": #2-length record
  61.             code += inputdata[inputindex][1][:-1] + '\x00' + inputdata[inputindex+1][1][:-1]
  62.             inputindex += 2
  63.         else: #3-length record
  64.             code += inputdata[inputindex][1][:-1] + '\x00' + inputdata[inputindex+1][1][:-1] + inputdata[inputindex+2][1][:-1]
  65.             inputindex += 3
  66.  
  67.         newlength = len(code) - 4
  68.         oldoffset = offset
  69.         offset += newlength - origlength
  70.  
  71.         outfiledata = replacestr(outfiledata,code,tblpointer + 4,origlength + 4)
  72.     else:
  73.     #Unpack pointer, add offset
  74.         tblpointer = struct.unpack('<H',outfiledata[tblindex:tblindex+2])[0] + oldoffset
  75.     #Rewrite the pointer
  76.         outfiledata = outfiledata[:tblindex] + struct.pack('<H',tblpointer) + outfiledata[tblindex+2:]
  77.        
  78.     tblindex += 2
  79.  
  80. outfile = open('t_ittxt._dt','wb')
  81. outfile.write(outfiledata) #write the output
  82. outfile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement