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): names = extract_names(dir_in) # we have to update japanese like the rest because CustomData exists # and POKELABO deleted the wedding gear event stuff from the game files for x in languages: langfile = os.path.join(old_path, "nametranslations_{}.json".format(x)) if os.path.isfile(langfile): with open(langfile, "r") as lang_file: lang_dict = json.load(lang_file) if x == "jpn": out_dict = dict(zip(names, names)).update(lang_dict) else: out_dict = dict.fromkeys(names, "").update(lang_dict) elif x == "jpn": # bootstrap out_dict = dict(zip(names, names)) else: out_dict = dict.fromkeys(names, "") with io.open(langfile, "w", newline='\n') as lang_file: # you're using git right json.dump(lang_dict, lang_file, ensure_ascii=False, indent='\t', sort_keys=True) def extract_names(dir_in): 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) 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) # 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): 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(): 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