Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import shutil
- import struct
- import zlib
- if os.path.isfile('shared.orig'): #Check if file exists
- pass #Do nothing
- elif os.path.isfile('shared.bin'): #Check if file exists
- shutil.copy('shared.bin','shared.orig') #Copy file
- else: #Neither file exists
- print('No shared.bin') #Print error and quit
- quit()
- new_plaintext_file = '0017e1b0.mod' #Specify input file
- compressed_new_file = '0x17e1b0.compressed.new' #Specify intermediate file
- with open(new_plaintext_file,'rb') as f: #Compress the file
- with open(compressed_new_file,'wb') as g:
- g.write(zlib.compress(f.read(),9))
- file_number = 0 #ID the 1st file
- TOC_position = 0x17da00 #Where the TOC is
- with open('shared.orig','rb') as f: #Open file
- f.seek(TOC_position + 8 + 0xC * file_number) #Move file position to header entry for this file
- file_offset = struct.unpack('<I', f.read(4))[0] #Read offset
- decompressed_size_orig = struct.unpack('<I', f.read(4))[0] #Read original decompressed size
- compressed_size_orig = struct.unpack('<I', f.read(4))[0] #Read original compressed size
- compressed_size_new = os.path.getsize(compressed_new_file) #Read new compressed size
- file_offset = TOC_position + file_offset #Compute file offset (absolute)
- if compressed_size_new > compressed_size_orig: #Check for length limits
- print('Too big.')
- quit()
- with open(compressed_new_file,'rb') as f: #Open input file and decompress
- decompressed_size_new = len(zlib.decompress(f.read())) #it to read the new decompressed size
- with open('shared.bin','wb') as dest: #Open files
- with open('shared.orig','rb') as source:
- dest.write(source.read(TOC_position + 0xC + 0xC * file_number)) #Copy up to the TOC position for the file
- dest.write(struct.pack('<I', decompressed_size_new)) #Write new compressed size
- dest.write(struct.pack('<I', compressed_size_new)) #Write new decmopressed size
- source.seek(TOC_position + 0x14 + 0xC * file_number) #Seek past original values in source file
- dest.write(source.read(file_offset - dest.tell())) #Copy up to the data position for the file
- with open(compressed_new_file,'rb') as source2: #Copy the contents of the source file
- dest.write(source2.read())
- if dest.tell() < file_offset + compressed_size_orig: #Pad with zeros
- dest.write(bytearray(file_offset + compressed_size_orig - dest.tell())) #If covers the case where the length is exactly the same
- source.seek(file_offset + compressed_size_orig) #Seek past original file data in source file
- dest.write(source.read()) #Copy the rest of the file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement