Advertisement
Guest User

Nayuta .arb Inserter v1.1

a guest
Feb 13th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.56 KB | None | 0 0
  1. #1.1: Updates data.lst
  2. import os
  3. import struct
  4.  
  5. def get_data(filename):
  6.     totalbytes = os.path.getsize(filename)
  7.     infile = open(filename, 'rb')
  8.     totalfiledata = infile.read(totalbytes)
  9.     infile.close()
  10.     return totalfiledata
  11.  
  12. def replacestr(origstr,replacestr,startpos,replacelen):
  13. #Returns a string with a replaced sub-string
  14. #origstr - the original string, replacestr = the string to replace
  15. #startpos - where the replacement string should go
  16. #replacelen - how many characters of the original string to replace
  17.     return origstr[:startpos] + replacestr + origstr[startpos+replacelen:]
  18.  
  19. def datalstupdate(filename,newsize):
  20.     '''Updates data.lst'''
  21.     filedata = get_data('data.lst')
  22.     pos = 0x9020
  23.     filename = filename + '\x00'*(8-len(filename))
  24.     while True:
  25.         pos = filedata.find(filename,pos+1)
  26.         if pos == -1:
  27.             print '{} not found in data.lst'.format(filename)
  28.         elif filedata[pos+15] == '\x0d': #Indicates .arb file extension
  29.             newsize = struct.pack('<I',newsize)
  30.             filedata = replacestr(filedata,newsize,pos+8,4)
  31.             with open('data.lst','wb') as f:
  32.                 f.write(filedata)
  33.             break
  34.  
  35. inputdata = []
  36. with open('arbdata.csv','rb') as f:
  37.     for line in f:
  38.         line = line.translate(None,"\r\n") #Trims newline characters
  39.         line = line.split('\t')
  40.         inputdata.append((line[0],int(line[1],16),line[3]))
  41.  
  42. oldfile = ''
  43. firstpass = True
  44. for i,(filename,addr,newstring) in enumerate(inputdata):
  45.     if filename != oldfile:
  46.         if firstpass:
  47.             firstpass = False
  48.         else:
  49.             outfile = open(os.getcwd() + '\\' + oldfile + '\\' + oldfile + '.arb','wb')
  50.             outfile.write(filedata)
  51.             outfile.close()
  52.             datalstupdate(oldfile,len(filedata))
  53.         filedata = get_data(os.getcwd() + '\\' + filename + '.orig')
  54.         offset = 0
  55.     addr = addr + offset
  56.     oldsize = struct.unpack('<I',filedata[addr:addr+4])[0] + 4
  57.     if filename != oldfile:
  58.         newsize = len(newstring) + 6
  59.         newstring = struct.pack('<I',len(newstring) + 2) + ' ' + newstring + '\x00'
  60.     else:
  61.         newsize = len(newstring) + 5
  62.         newstring = struct.pack('<I',len(newstring) + 1) + newstring + '\x00'
  63.     filedata = replacestr(filedata,newstring,addr,oldsize)
  64.     offset = offset + newsize - oldsize
  65.     oldfile = filename
  66.  
  67. outfile = open(os.getcwd() + '\\' + filename + '\\' + filename + \
  68.                '.arb','wb')
  69. outfile.write(filedata)
  70. outfile.close()
  71. datalstupdate(filename,len(filedata))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement