OpenTTD-China-Set / China-Set-Trains

The Train sector of the China Set of OpenTTD.
GNU General Public License v2.0
11 stars 6 forks source link

Change: add parent family names for trains #52

Closed WenSimEHRP closed 2 months ago

WenSimEHRP commented 2 months ago

I am not going to change the language file, so someone has to fix it. Some trains won't display the correct name. That would also need to be fixed. Should be a quick fix though.

Known bug: variant group targets don't have appropriate names. This needs to be manually fixed

@kaitokiwa2020 I would need you to fix the strings.

script used, I know it looks messy:

import glob, re ,os
from string import Template

variant_group_switch = True
csv_mode = True

t_switch_str ="""switch (FEAT_TRAINS, SELF, switch_${name}_name, getbits(extra_callback_info1, 0, 8) == 0x20? getbits(extra_callback_info1, 8, 8) : 0xFFFF) {
    0: return string(${str_name});
    return CB_RESULT_NO_TEXT;
}
"""
t_callback_str = "name: switch_${name}_name;\n"

t_switch = Template(t_switch_str)
t_template = Template(t_callback_str)

files = glob.glob('src/**/*.pnml', recursive=True)
clean_files = []
clean_files_dict = {}

for file in files:
    with open(file, 'r', encoding="utf-8") as f:
        file_string = f.read()
        graphics_str = re.findall(r"graphics\s*\{[\n\w\W]*?\}", file_string, re.DOTALL)
        variant_group_num = re.search(r"variant_group\s*:\s*(\d+)\s*;", file_string)
        if variant_group_switch and "variant_group" not in file_string:
            print(f"Variant group not found in {file}, skipping")
            continue
        elif len(graphics_str) > 1:
            print(f"More than one graphics found in {file}, skipping")
            continue
        elif len(graphics_str) == 0:
            print(f"No graphics found in {file}, skipping")
            continue
        else:
            clean_files.append(file)
            clean_files_dict[file] = variant_group_num.group(1) if variant_group_num else "0"

if csv_mode:
    if os.path.exists("train_list.csv") and input("WARNING: train_list.csv already exists, do you wish to overwrite it? (y/n)").lower() == "y":
        with open("train_list.csv", "w+", encoding="utf-8") as f:
            f.write("File,Name,Variant Group\n")
            for file in clean_files:
                f.write(f"{file},,{clean_files_dict[file]}\n")
        input("You are in CSV mode, please edit the dumped file and press enter to continue")

name_dict = {}

if csv_mode:
    with open("train_list.csv", "r", encoding="utf-8") as f:
        trains = f.readlines()
        for train in trains:
            train_name = train.split(",")[0]
            if train_name in clean_files:
                csv_str_name = train.split(",")[1].strip() if train.split(",")[1] else "NOTHING"
                name_dict[train_name] = csv_str_name
                print(f"Found {csv_str_name} for {train_name}")

for file in clean_files:
    with open(file, 'r', encoding="utf-8") as f:
        file_string = f.read()
        graphics_str = re.findall(r"graphics\s*\{[\n\w\W]*?\}", file_string, re.DOTALL)
        if csv_mode or input(f"Do you wish to process {file}? (y/n)").lower() == "y":
            str_name = ""
            filename = os.path.basename(file).split(".")[0]
            if not csv_mode:
                str_name = input("Enter the string index (enter N to cancel): ").strip()
                if str_name.lower() == "n":
                    continue
            else:
                str_name = name_dict[file] if file in name_dict else ""
            if str_name:
                new_graphics_str = graphics_str[0].split("\n")
                new_graphics_str.insert(-1, f"        {t_template.substitute(name=filename)}")
                file_string = file_string.replace(graphics_str[0], "\n".join(new_graphics_str))
                file_string = t_switch.substitute(name=filename, str_name=str_name) + file_string
                with open(file, 'w', encoding="utf-8") as f:
                    f.write(file_string)
                print(f"Processed {file}")
        else:
            continue
JohnFranklin523 commented 2 months ago

Is it okay now?

WenSimEHRP commented 2 months ago

Is it okay now?

dunno, ask @Babel-TTT and @kaitokiwa2020