Guest User

ACLR Test Patcher v2

a guest
Sep 16th, 2016
31
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import shutil
  3. import subprocess
  4. import tempfile
  5. import zlib
  6.  
  7. replace_list = [('006cc000', 0x6CC000, 25553321, 0x1FAA800,),
  8.                 ('09880800', 0x9880800, 13533, 0x09884000),
  9.                 ('0c780800', 0xC780800, 15855, 0x0C784800),
  10.                 ('093ae800', 0x93AE800, 37749, 0x093B8000)]
  11.  
  12. def unzip(f, offset, size):
  13.     saved = f.tell()
  14.     f.seek(offset)
  15.     with tempfile.NamedTemporaryFile(delete = False) as g:
  16.         filename = g.name
  17.         g.write(zlib.decompress(f.read(size)))
  18.     f.seek(saved)
  19.     return filename
  20.  
  21. #Make a copy of AC.BIN
  22. if os.path.isfile('AC.orig'):
  23.     pass
  24. elif os.path.isfile('AC.BIN'):
  25.     shutil.copy('AC.BIN', 'AC.orig')
  26. else:
  27.     print('File AC.BIN not found.')
  28.     quit()
  29.  
  30. #Extract file and decompress
  31. with open('AC.orig', 'rb') as f:
  32.     with open('AC.BIN', 'wb') as g:
  33. #Sort by sub-file address
  34.         for name, offset, size, next_offset in sorted(replace_list, key = lambda x: x[1]):
  35.             g.write(f.read(offset - g.tell()))          #Copy up to offset
  36.             tempfilename = unzip(f, offset, size)       #Unzip file
  37.             patched = '{}.BIN'.format(name)
  38. #Apply patch using xdelta
  39.             subprocess.run(['xdelta', '-d', '-s', tempfilename, '{}.xdelta'.format(name), patched])
  40.             os.remove(tempfilename)                     #Delete temporary file
  41.             with open(patched, 'rb') as h:              #Compress and write our patched file
  42.                 g.write(zlib.compress(h.read()))
  43.             os.remove(patched)                          #Delete temporary file
  44.             g.write(b'\x00' * (next_offset - g.tell())) #Write zeros up to start of next file
  45.             f.seek(next_offset)                         #Align file pointer
  46.         g.write(f.read())                               #Copy the rest of the file
RAW Paste Data