From 71dafe9046a90341572c0c96d1a92d24c2fd9270 Mon Sep 17 00:00:00 2001 From: Nick Date: Mon, 29 Apr 2024 14:26:16 -0400 Subject: [PATCH] Fix translation issues when reusing dialogue options --- NewHorizons/Builder/Props/DialogueBuilder.cs | 29 ++++++++++++++++---- NewHorizons/Handlers/TranslationHandler.cs | 20 ++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/NewHorizons/Builder/Props/DialogueBuilder.cs b/NewHorizons/Builder/Props/DialogueBuilder.cs index 779b9a37..3066eea8 100644 --- a/NewHorizons/Builder/Props/DialogueBuilder.cs +++ b/NewHorizons/Builder/Props/DialogueBuilder.cs @@ -12,6 +12,7 @@ using System.IO; using System.Xml; using System.Xml.Linq; using UnityEngine; +using UnityEngine.XR; 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); var newTextAsset = new TextAsset(existingDialogueDoc.OuterXml) @@ -130,13 +135,13 @@ namespace NewHorizons.Builder.Props 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; } + /// + /// Always call this after adding translations, else it won't update them properly + /// + /// private static void DoDialogueOptionsListReplacement(XmlNode dialogueTree) { var optionsListsByName = new Dictionary(); @@ -155,7 +160,8 @@ namespace NewHorizons.Builder.Props var replacement = optionsList.GetChildNode("ReuseDialogueOptionsListFrom"); 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) { @@ -164,6 +170,17 @@ namespace NewHorizons.Builder.Props var dialogueNode = optionsList.ParentNode; dialogueNode.RemoveChild(optionsList); 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 { @@ -231,8 +248,8 @@ namespace NewHorizons.Builder.Props var dialogueDoc = new XmlDocument(); dialogueDoc.LoadXml(xml); var xmlNode = dialogueDoc.SelectSingleNode("DialogueTree"); - DoDialogueOptionsListReplacement(xmlNode); AddTranslation(xmlNode); + DoDialogueOptionsListReplacement(xmlNode); xml = xmlNode.OuterXml; var text = new TextAsset(xml) diff --git a/NewHorizons/Handlers/TranslationHandler.cs b/NewHorizons/Handlers/TranslationHandler.cs index c2f49ebf..409266bc 100644 --- a/NewHorizons/Handlers/TranslationHandler.cs +++ b/NewHorizons/Handlers/TranslationHandler.cs @@ -173,6 +173,26 @@ namespace NewHorizons.Handlers TextTranslation.Get().m_table.theTable[key] = value; } + /// + /// Two dialogue nodes might share indentical text but they will have different prefixes. Still, we want to reuse that old text. + /// + /// + /// + /// + 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) { var key = string.Join(string.Empty, rawPreText) + rawText;