1
0
Fork 0
mirror of https://github.com/zeldaret/oot.git synced 2024-12-26 06:36:19 +00:00
oot/tools/convert_chars.py

62 lines
1.4 KiB
Python
Raw Normal View History

2020-03-17 04:31:30 +00:00
#! /usr/bin/python3
import os
import re
script_dir = os.path.dirname(os.path.realpath(__file__))
root_dir = script_dir + "/../"
def handle_match(match):
original_text = match.group()
2020-03-17 04:31:30 +00:00
match_text = original_text.replace("\\x", "")
if match_text.lower() == "1b":
return original_text
2020-03-17 04:31:30 +00:00
end = ""
if original_text.lower().endswith("\\x1b"):
end = original_text[-4:]
match_text = match_text[:-2]
2020-03-17 04:31:30 +00:00
try:
text = bytes.fromhex(match_text).decode("EUC-JP")
except UnicodeDecodeError:
return original_text
2020-03-17 04:31:30 +00:00
if text[2:].lower() == match_text.lower():
return original_text
return text + end
def process_file(file_path):
with open(file_path) as f:
file_text = f.read()
new_file_text = re.sub("(\\\\x[0-9a-zA-Z]{2})+", handle_match, file_text)
if new_file_text != file_text:
with open(file_path, "w") as f:
f.write(new_file_text)
return True
return False
def main():
i = 0
skip_list = ["z_file_choose.rodata.s", "z_kaleido_scope.rodata.s"]
for root, dirs, files in os.walk(root_dir):
for file in files:
if i == 100:
return
if file.endswith(".s") and file not in skip_list:
path = os.path.join(root, file)
if process_file(path):
print("Processed " + path)
i += 1
main()