divatool/diva/quest.py
2020-05-16 13:47:41 +02:00

120 lines
3.9 KiB
Python

import csv
import db
import io
import json
import os
from glob import glob
def update_missions(dir_in, old_path, languages, is_global, utage_in=None):
if not utage_in:
utage_in = os.path.join(dir_in, "Common/Asset/Utage")
qdb = db.QuestDB(dir_in)
quest_mst(qdb, old_path, languages)
scene_mst(qdb, old_path, dir_in, languages, utage_in)
def scene_mst(qdb, old_path, dir_in, languages, utage_in):
scenes = qdb.get_scenes()
locations = load_locations(dir_in, utage_in)
for id, scene in scenes.items():
i = 0
while True:
try:
scene["Folder"] = locations[scene["Parts"][i]]
except KeyError as exc:
i += 1
if i == len(scene["Parts"]):
raise KeyError(f"Could not find folder for scene {id}") from exc
continue
break
with io.open(
os.path.join(old_path, "XduScene.json"), "w", encoding="utf-8"
) as json_file:
json.dump(scenes, json_file, ensure_ascii=False, indent="\t", sort_keys=True)
for lang in languages:
out_dict = {}
if lang == "jpn":
for key, value in scenes.items():
out_dict[key] = {
"Name": value["Name"],
"SummaryText": value["SummaryText"],
"Credits": "POKELABO",
"Enabled": False,
}
else:
for key in scenes.keys():
out_dict[key] = {
"Name": "",
"SummaryText": "",
"Credits": "",
"Enabled": False,
}
langfile = os.path.join(old_path, f"XduSceneNames_{lang}.json")
if os.path.isfile(langfile):
with open(langfile, "r", encoding="utf-8") as lang_file:
lang_dict = json.load(lang_file)
out_dict.update(lang_dict)
with io.open(langfile, "w", encoding="utf-8") as lang_file:
json.dump(
out_dict, lang_file, ensure_ascii=False, indent="\t", sort_keys=True,
)
def quest_mst(qdb, old_path, languages):
quests = qdb.get_quests()
with io.open(
os.path.join(old_path, "XduQuest.json"), "w", encoding="utf-8"
) as json_file:
json.dump(quests, json_file, ensure_ascii=False, indent="\t", sort_keys=True)
for lang in languages:
out_dict = {}
if lang == "jpn":
for key, value in quests.items():
out_dict[key] = {"Name": value["Name"], "Enabled": False}
else:
for key, value in quests.items():
out_dict[key] = {"Name": "", "Enabled": False}
langfile = os.path.join(old_path, f"XduQuestNames_{lang}.json")
if os.path.isfile(langfile):
with open(langfile, "r", encoding="utf-8") as lang_file:
lang_dict = json.load(lang_file)
out_dict.update(lang_dict)
with io.open(langfile, "w", encoding="utf-8") as lang_file:
json.dump(
out_dict, lang_file, ensure_ascii=False, indent="\t", sort_keys=True,
)
return quests
def load_locations(dir_in, utage_in):
locations = {}
settings = glob("{}/**/Settings/Scenario.tsv".format(utage_in), recursive=True,)
if len(settings) == 0:
raise FileNotFoundError("Ya didn't decrypt the files dummy")
for scenario in settings:
with open(scenario, "r", encoding="utf-8") as tsv_file:
tsv = csv.DictReader(tsv_file, delimiter="\t", quotechar='"')
for row in tsv:
tokens = row["FileName"].split("/")
if len(tokens) < 3:
continue
if tokens[2] in locations:
raise ValueError("Conflicting paths for file {tokens[2]}")
locations[tokens[2]] = tokens[0]
return locations