92 lines
3.5 KiB
Python
92 lines
3.5 KiB
Python
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('<I', data[0:4])[0]
|
|
# size = struct.unpack('<I', data[4:8])[0]
|
|
# print(header, size)
|
|
# data[4:8] = struct.pack('<I', len(data) - header)
|
|
# size = struct.unpack('<I', data[4:8])[0]
|
|
# print(len(data) - header, size)
|
|
# return data
|
|
|
|
def main():
|
|
args = parse_arguments()
|
|
|
|
# Determine output file path
|
|
if args.out_bp_file:
|
|
out_bp_file = args.out_bp_file
|
|
else:
|
|
out_bp_file = f"{os.path.splitext(args.bp_file)[0]}_out{os.path.splitext(args.bp_file)[1]}"
|
|
|
|
# Read replacements from the txt file
|
|
replacements = read_replacements(args.txt_file)
|
|
|
|
# Replace text in the binary file
|
|
modified_data = replace_text_in_bp(args.bp_file, replacements)
|
|
|
|
# Write the modified data to the output file
|
|
with open(out_bp_file, 'wb') as f:
|
|
f.write(modified_data)
|
|
|
|
print(f'Modified binary file written to: {out_bp_file}')
|
|
|
|
if __name__ == '__main__':
|
|
main()
|