- Oct 18, 2021
- 4
- 12
Shadesishere here's a really really ugly script that should let you port translations. I've tested it roundtripping 1.3.1's MTL successfully, haven't yet gotten my hands on 1.2.1's base though.
Usage - python script_name.py old_base.dat old_translation.dat new_base.dat new_translation.dat
Usage - python script_name.py old_base.dat old_translation.dat new_base.dat new_translation.dat
Python:
import sys
print(sys.argv[1:])
old_base_path, old_translate_path, new_base_path, new_translate_path = sys.argv[1:]
def find_diffs(base_path, translate_path):
database = {}
with open(base_path, 'rb') as base_f:
with open(translate_path, 'rb') as translate_f:
base = base_f.read()
translate = translate_f.read()
base_i = 0
translate_i = 0
while True:
# find a difference
while base[base_i] == translate[translate_i]:
base_i += 1
translate_i += 1
if base_i >= len(base):
return database
while base[base_i - 1] != 0 and translate[translate_i - 1] != 0:
base_i -= 1
translate_i -= 1
base_str = b''
translate_str = b''
blah = base[base_i - len(base_str) - 4 : base_i- len(base_str) ]
base_size = int.from_bytes(blah, 'little')
while base[base_i] != 0:
base_str += base[base_i].to_bytes(1, 'little')
base_i += 1
base_str += b'\0'
while translate[translate_i] != 0:
translate_str += translate[translate_i].to_bytes(1, 'little')
translate_i += 1
translate_str += b'\0'
try:
# skip things that are probably sizes or other junk
a = base_str.decode("cp932").strip()
b = translate_str.decode("cp932").strip()
if len(a) <= 4 or len(b) <= 4 or a.isspace() or b.isspace():
continue
#print(base_str.decode("cp932") + " => " + translate_str.decode("cp932"))
database[base_str] = translate_str
except UnicodeDecodeError:
pass
database = find_diffs(old_base_path, old_translate_path)
def search(new_base, database):
with open(new_base, 'rb') as new_base_f:
translate = new_base_f.read()
for old, new in database.items():
found_any = False
i = 0
while True:
i = translate.find(old, i)
if i == -1:
break
if int.from_bytes(translate[i - 4 : i], 'little') != len(old):
i += 1
continue
translate = translate[0:i - 4] + len(new).to_bytes(4, 'little') + new + translate[i + len(old):]
found_any = True
i += len(new)
if not found_any:
print("Couldn't find - '" + str(old[:-1].decode('cp932')) + "' => '" + str(new[:-1].decode('cp932')) + "'")
return translate
translate = search(new_base_path, database)
print('writing')
with open(new_translate_path, 'wb') as new_translate_:
new_translate_.write(translate)