divatool/utage/names.py

120 lines
3.8 KiB
Python

import csv
import json
import io
import os
import re
from functools import partial
from glob import glob
from multiprocessing import Pool
def update_names(dir_in, old_path, languages, is_global):
names = extract_names(dir_in, is_global)
global_languages = {"enm": 1, "zh": 2, "ko": 3}
if is_global:
languages = global_languages.keys()
# we have to update japanese like the rest because CustomData exists
# and POKELABO deleted the wedding gear event stuff from the game files
for l in languages:
if l == "jpn":
out_dict = dict(zip(names, names))
elif l in global_languages.keys():
out_dict = {}
for name in names:
spl = name.split("\t")
idx = global_languages[l]
val = spl[idx] if spl[idx] != "None" else ""
out_dict[spl[0]] = val
else:
out_dict = dict.fromkeys(names, "")
langfile = os.path.join(old_path, "nametranslations_{}.json".format(l))
if os.path.isfile(langfile):
with open(langfile, "r") as lang_file:
lang_dict = json.load(lang_file)
out_dict.update(lang_dict)
with io.open(
langfile, "w", newline="\n"
) as lang_file: # you're using git right
json.dump(
out_dict, lang_file, ensure_ascii=False, indent="\t", sort_keys=True,
)
def extract_names(dir_in, is_global):
char_tsv_path = os.path.join(dir_in, "Diva", "Settings", "Character.tsv")
if not os.path.isfile(char_tsv_path):
raise FileNotFoundError("Character.tsv not found")
names, sets = read_char_tsv(char_tsv_path, is_global)
files = [
f
for f in glob(os.path.join(dir_in, "**/*.tsv"), recursive=True)
if re.search(r"/[0-9]{9}\.tsv$", f)
]
if len(files) == 0:
raise FileNotFoundError("No valid files found in directory: " + dir_in)
# TODO unsupported on global for now
if not is_global:
# tfw gil
# it's still faster on my machine so i'm keeping it
read_partial = partial(read_mission, char_names=names, char_sets=sets)
with Pool() as p:
new_names = p.map(read_partial, files)
for x in new_names:
names.update(x)
return names
def read_char_tsv(char_tsv_path, is_global):
char_names = set()
char_sets = set()
with open(char_tsv_path, "r") as char_tsv_file:
char_tsv = csv.DictReader(char_tsv_file, delimiter="\t", quotechar='"')
for row in char_tsv:
if row["CharacterName"].startswith("//"):
continue
if (
row["CharacterName"]
and row["NameText"]
and row["CharacterName"].strip()
and row["NameText"].strip()
):
if is_global:
char_names.add(
f"{row['NameText']}\t{row['CharaEnglish']}\t{row['CharaChinese']}\t{row['CharaKorean']}"
)
else:
char_names.add(row["NameText"])
char_sets.add(row["CharacterName"])
return char_names, char_sets
def read_mission(tsv_path, char_names, char_sets):
new_names = set()
with open(tsv_path, "r") as tsv_file:
tsv = csv.DictReader(tsv_file, delimiter="\t", quotechar='"')
if (
("Arg1" not in tsv.fieldnames)
or ("Text" not in tsv.fieldnames)
or ("Command" not in tsv.fieldnames)
):
return new_names
for row in tsv:
if row["Command"] and row["Command"].startswith("//"):
continue
if row["Text"] and row["Arg1"]:
if (row["Arg1"] not in char_names) and (row["Arg1"] not in char_sets):
new_names.add(row["Arg1"])
return new_names