Fix translation issues when reusing dialogue options

This commit is contained in:
Nick 2024-04-29 14:26:16 -04:00
parent 8a983f18ce
commit 71dafe9046
2 changed files with 43 additions and 6 deletions

View File

@ -12,6 +12,7 @@ using System.IO;
using System.Xml; using System.Xml;
using System.Xml.Linq; using System.Xml.Linq;
using UnityEngine; using UnityEngine;
using UnityEngine.XR;
namespace NewHorizons.Builder.Props namespace NewHorizons.Builder.Props
{ {
@ -121,6 +122,10 @@ namespace NewHorizons.Builder.Props
} }
} }
// Character name is required for adding translations, something to do with how OW prefixes its dialogue
var characterName = existingDialogueTree.SelectSingleNode("NameField").InnerText;
AddTranslation(additionalDialogueDoc.GetChildNode("DialogueTree"), characterName);
DoDialogueOptionsListReplacement(existingDialogueTree); DoDialogueOptionsListReplacement(existingDialogueTree);
var newTextAsset = new TextAsset(existingDialogueDoc.OuterXml) var newTextAsset = new TextAsset(existingDialogueDoc.OuterXml)
@ -130,13 +135,13 @@ namespace NewHorizons.Builder.Props
existingDialogue.SetTextXml(newTextAsset); existingDialogue.SetTextXml(newTextAsset);
// Chracter name is required for adding translations, something to do with how OW prefixes its dialogue
var characterName = existingDialogueTree.SelectSingleNode("NameField").InnerText;
AddTranslation(additionalDialogueDoc.GetChildNode("DialogueTree"), characterName);
return existingDialogue; return existingDialogue;
} }
/// <summary>
/// Always call this after adding translations, else it won't update them properly
/// </summary>
/// <param name="dialogueTree"></param>
private static void DoDialogueOptionsListReplacement(XmlNode dialogueTree) private static void DoDialogueOptionsListReplacement(XmlNode dialogueTree)
{ {
var optionsListsByName = new Dictionary<string, XmlNode>(); var optionsListsByName = new Dictionary<string, XmlNode>();
@ -155,7 +160,8 @@ namespace NewHorizons.Builder.Props
var replacement = optionsList.GetChildNode("ReuseDialogueOptionsListFrom"); var replacement = optionsList.GetChildNode("ReuseDialogueOptionsListFrom");
if (replacement != null) if (replacement != null)
{ {
if (optionsListsByName.TryGetValue(replacement.InnerText, out var replacementOptionsList)) var replacementName = replacement.InnerText;
if (optionsListsByName.TryGetValue(replacementName, out var replacementOptionsList))
{ {
if (replacementOptionsList.GetChildNode("ReuseDialogueOptionsListFrom") != null) if (replacementOptionsList.GetChildNode("ReuseDialogueOptionsListFrom") != null)
{ {
@ -164,6 +170,17 @@ namespace NewHorizons.Builder.Props
var dialogueNode = optionsList.ParentNode; var dialogueNode = optionsList.ParentNode;
dialogueNode.RemoveChild(optionsList); dialogueNode.RemoveChild(optionsList);
dialogueNode.AppendChild(replacementOptionsList.Clone()); dialogueNode.AppendChild(replacementOptionsList.Clone());
// Have to manually fix the translations here
var characterName = dialogueTree.SelectSingleNode("NameField").InnerText;
var xmlText = replacementOptionsList.SelectNodes("DialogueOption/Text");
foreach (object option in xmlText)
{
var optionData = (XmlNode)option;
var text = optionData.InnerText.Trim();
TranslationHandler.ReuseDialogueTranslation(text, new string[] { characterName, replacementName }, new string[] { characterName, name });
}
} }
else else
{ {
@ -231,8 +248,8 @@ namespace NewHorizons.Builder.Props
var dialogueDoc = new XmlDocument(); var dialogueDoc = new XmlDocument();
dialogueDoc.LoadXml(xml); dialogueDoc.LoadXml(xml);
var xmlNode = dialogueDoc.SelectSingleNode("DialogueTree"); var xmlNode = dialogueDoc.SelectSingleNode("DialogueTree");
DoDialogueOptionsListReplacement(xmlNode);
AddTranslation(xmlNode); AddTranslation(xmlNode);
DoDialogueOptionsListReplacement(xmlNode);
xml = xmlNode.OuterXml; xml = xmlNode.OuterXml;
var text = new TextAsset(xml) var text = new TextAsset(xml)

View File

@ -173,6 +173,26 @@ namespace NewHorizons.Handlers
TextTranslation.Get().m_table.theTable[key] = value; TextTranslation.Get().m_table.theTable[key] = value;
} }
/// <summary>
/// Two dialogue nodes might share indentical text but they will have different prefixes. Still, we want to reuse that old text.
/// </summary>
/// <param name="rawText"></param>
/// <param name="oldPrefixes"></param>
/// <param name="newPrefixes"></param>
public static void ReuseDialogueTranslation(string rawText, string[] oldPrefixes, string[] newPrefixes)
{
var key = string.Join(string.Empty, newPrefixes) + rawText;
var existingKey = string.Join(string.Empty, oldPrefixes) + rawText;
if (TextTranslation.Get().m_table.theTable.TryGetValue(existingKey, out var existingTranslation))
{
TextTranslation.Get().m_table.theTable[key] = existingTranslation;
}
else
{
NHLogger.LogWarning($"Couldn't find translation key {existingKey}");
}
}
public static void AddShipLog(string rawText, params string[] rawPreText) public static void AddShipLog(string rawText, params string[] rawPreText)
{ {
var key = string.Join(string.Empty, rawPreText) + rawText; var key = string.Join(string.Empty, rawPreText) + rawText;