summaryrefslogtreecommitdiff
path: root/tools/convert_chars.py
blob: 261ed9239973e6dfed214ffb85521fba80487f9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#! /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()

    match_text = original_text.replace("\\x", "")

    if match_text.lower() == "1b":
        return original_text

    end = ""
    if original_text.lower().endswith("\\x1b"):
        end = original_text[-4:]
        match_text = match_text[:-2]

    try:
        text = bytes.fromhex(match_text).decode("EUC-JP")
    except UnicodeDecodeError:
        return original_text

    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()