Advertisement
Guest User

QB script test

a guest
Oct 24th, 2015
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.10 KB | None | 0 0
  1. import os
  2. import shutil
  3. import struct
  4. import zlib
  5.  
  6. if os.path.isfile('shared.orig'):           #Check if file exists
  7.     pass                                    #Do nothing
  8. elif os.path.isfile('shared.bin'):          #Check if file exists
  9.     shutil.copy('shared.bin','shared.orig') #Copy file
  10. else:                                       #Neither file exists
  11.     print('No shared.bin')                  #Print error and quit
  12.     quit()
  13.  
  14. new_plaintext_file = '0017e1b0.mod'                             #Specify input file
  15. compressed_new_file = '0x17e1b0.compressed.new'                 #Specify intermediate file
  16. with open(new_plaintext_file,'rb') as f:                        #Compress the file
  17.     with open(compressed_new_file,'wb') as g:
  18.         g.write(zlib.compress(f.read(),9))    
  19.  
  20. file_number = 0                                                 #ID the 1st file
  21. TOC_position = 0x17da00                                         #Where the TOC is
  22. with open('shared.orig','rb') as f:                             #Open file
  23.     f.seek(TOC_position + 8 + 0xC * file_number)                #Move file position to header entry for this file
  24.     file_offset = struct.unpack('<I', f.read(4))[0]             #Read offset
  25.     decompressed_size_orig = struct.unpack('<I', f.read(4))[0]  #Read original decompressed size
  26.     compressed_size_orig = struct.unpack('<I', f.read(4))[0]    #Read original compressed size
  27. compressed_size_new = os.path.getsize(compressed_new_file)      #Read new compressed size
  28. file_offset = TOC_position + file_offset                        #Compute file offset (absolute)
  29. if compressed_size_new > compressed_size_orig:                  #Check for length limits
  30.     print('Too big.')
  31.     quit()
  32.  
  33. with open(compressed_new_file,'rb') as f:                       #Open input file and decompress
  34.     decompressed_size_new = len(zlib.decompress(f.read()))      #it to read the new decompressed size
  35.  
  36.  
  37. with open('shared.bin','wb') as dest:                           #Open files
  38.     with open('shared.orig','rb') as source:
  39.         dest.write(source.read(TOC_position + 0xC + 0xC * file_number))     #Copy up to the TOC position for the file
  40.         dest.write(struct.pack('<I', decompressed_size_new))    #Write new compressed size
  41.         dest.write(struct.pack('<I', compressed_size_new))      #Write new decmopressed size
  42.         source.seek(TOC_position + 0x14 + 0xC * file_number)    #Seek past original values in source file
  43.         dest.write(source.read(file_offset - dest.tell()))      #Copy up to the data position for the file
  44.         with open(compressed_new_file,'rb') as source2:         #Copy the contents of the source file
  45.             dest.write(source2.read())
  46.         if dest.tell() < file_offset + compressed_size_orig:    #Pad with zeros
  47.             dest.write(bytearray(file_offset + compressed_size_orig - dest.tell()))     #If covers the case where the length is exactly the same
  48.         source.seek(file_offset + compressed_size_orig)         #Seek past original file data in source file
  49.         dest.write(source.read())                               #Copy the rest of the file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement