import argparse import os import struct def parse_arguments(): parser = argparse.ArgumentParser(description='Replace text in a binary file based on a mapping file.') parser.add_argument('bp_file', type=str, help='The path to the binary file.') parser.add_argument('txt_file', type=str, help='The path to the text file containing replacements.') parser.add_argument('out_bp_file', type=str, nargs='?', help='The output binary file path. If not specified, will add "_out" to bp_file.') return parser.parse_args() def read_replacements(txt_file): replacements = {} with open(txt_file, 'r', encoding='utf-8') as f: while True: original_line = f.readline() if not original_line: break original_line = original_line.strip() if not original_line: continue translation_line = f.readline().rstrip('\n') if translation_line: # Skip if translation is empty # Extract the hex address and original text hex_address, original_text = original_line.split(']', 1) hex_address = hex_address.strip('[') address = int(hex_address, 16) translation_line = translation_line.split(']', 1)[1] if not translation_line: continue replacements[address] = (original_text.encode('cp932'), translation_line.encode('utf-8')) return replacements def replace_text_in_bp(bp_file, replacements): with open(bp_file, 'rb') as f: data = bytearray(f.read()) for address, (original_text, translation_text) in replacements.items(): olen = len(original_text) tlen = len(translation_text) data[address:address + olen] = translation_text + b' ' * (olen - tlen) print(address, olen, tlen, original_text.decode('cp932'), translation_text.decode(), data[address:address + olen]) return data # def replace_text_in_bp(bp_file, replacements): # with open(bp_file, 'rb') as f: # data = f.read() # offset = 0 # for address, (original_text, translation_text) in replacements.items(): # olen = len(original_text) # tlen = len(translation_text) # address += offset # data = data[:address] + translation_text + data[address+olen:] # print(address, offset, olen, tlen, original_text.decode('cp932'), translation_text.decode(), data[address:address + tlen]) # offset += tlen - olen # data = bytearray(data) # header = struct.unpack('