ReuseDialogueOptionsListFrom (#855)

## Minor features
- To avoid having to copy paste a repeated `DialogueOptionsList`, you
can instead put `ReuseDialogueOptionsListFrom` and reference another
`DialogueNode` whose options you want to repeat. Implements #854
This commit is contained in:
xen-42 2024-04-30 18:57:41 -04:00 committed by GitHub
commit 70f3e92b56
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 146 additions and 9 deletions

View File

@ -156,7 +156,7 @@ namespace NewHorizons.Builder.Props
// If they're adding dialogue we have to manually register the xml text
if (isFromAssetBundle && component is CharacterDialogueTree dialogue)
{
DialogueBuilder.AddTranslation(dialogue._xmlCharacterDialogueAsset.text, null);
DialogueBuilder.HandleUnityCreatedDialogue(dialogue);
}
FixComponent(component, go, detail.ignoreSun);

View File

@ -5,10 +5,14 @@ using NewHorizons.Utility;
using NewHorizons.Utility.OuterWilds;
using NewHorizons.Utility.OWML;
using OWML.Common;
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using UnityEngine;
using UnityEngine.XR;
namespace NewHorizons.Builder.Props
{
@ -102,7 +106,11 @@ namespace NewHorizons.Builder.Props
// We just have to merge the dialogue options
var dialogueOptions = newDialogueNode.GetChildNode("DialogueOptionsList").GetChildNodes("DialogueOption");
var existingDialogueOptionsList = existingNode.GetChildNode("DialogueOptionsList");
var firstNode = existingDialogueOptionsList.ChildNodes[0];
if (existingDialogueOptionsList == null)
{
existingDialogueOptionsList = existingDialogueDoc.CreateElement("DialogueOptionsList");
existingNode.AppendChild(existingDialogueOptionsList);
}
foreach (XmlNode node in dialogueOptions)
{
var importedNode = existingDialogueOptionsList.OwnerDocument.ImportNode(node, true);
@ -118,6 +126,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);
var newTextAsset = new TextAsset(existingDialogueDoc.OuterXml)
{
name = existingDialogue._xmlCharacterDialogueAsset.name
@ -125,13 +137,84 @@ 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(xml, characterName);
FixDialogueNextFrame(existingDialogue);
return existingDialogue;
}
private static void FixDialogueNextFrame(CharacterDialogueTree characterDialogueTree)
{
Delay.FireOnNextUpdate(() =>
{
var rawText = characterDialogueTree._xmlCharacterDialogueAsset.text;
var doc = new XmlDocument();
doc.LoadXml(rawText);
var dialogueTree = doc.DocumentElement.SelectSingleNode("//DialogueTree");
DoDialogueOptionsListReplacement(dialogueTree);
var newTextAsset = new TextAsset(doc.OuterXml)
{
name = characterDialogueTree._xmlCharacterDialogueAsset.name
};
characterDialogueTree.SetTextXml(newTextAsset);
});
}
/// <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)
{
var optionsListsByName = new Dictionary<string, XmlNode>();
var dialogueNodes = dialogueTree.GetChildNodes("DialogueNode");
foreach (XmlNode dialogueNode in dialogueNodes)
{
var optionsList = dialogueNode.GetChildNode("DialogueOptionsList");
if (optionsList != null)
{
var name = dialogueNode.GetChildNode("Name").InnerText;
optionsListsByName[name] = optionsList;
}
}
foreach (var (name, optionsList) in optionsListsByName)
{
var replacement = optionsList.GetChildNode("ReuseDialogueOptionsListFrom");
if (replacement != null)
{
var replacementName = replacement.InnerText;
if (optionsListsByName.TryGetValue(replacementName, out var replacementOptionsList))
{
if (replacementOptionsList.GetChildNode("ReuseDialogueOptionsListFrom") != null)
{
NHLogger.LogError($"Can not target a node with ReuseDialogueOptionsListFrom that also reuses options when making dialogue. Node {name} cannot reuse the list from {replacement.InnerText}");
}
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
{
NHLogger.LogError($"Could not reuse dialogue options list from node with Name {replacement.InnerText} to node with Name {name}");
}
}
}
}
private static RemoteDialogueTrigger MakeRemoteDialogueTrigger(GameObject planetGO, Sector sector, DialogueInfo info, CharacterDialogueTree dialogue)
{
var conversationTrigger = GeneralPropBuilder.MakeNew("ConversationTrigger", planetGO, sector, info.remoteTrigger, defaultPosition: info.position, defaultParentPath: info.pathToAnimController);
@ -187,13 +270,19 @@ namespace NewHorizons.Builder.Props
var dialogueTree = conversationZone.AddComponent<NHCharacterDialogueTree>();
var dialogueDoc = new XmlDocument();
dialogueDoc.LoadXml(xml);
var xmlNode = dialogueDoc.SelectSingleNode("DialogueTree");
AddTranslation(xmlNode);
xml = xmlNode.OuterXml;
var text = new TextAsset(xml)
{
// Text assets need a name to be used with VoiceMod
name = dialogueName
};
dialogueTree.SetTextXml(text);
AddTranslation(xml);
switch (info.flashlightToggle)
{
@ -214,6 +303,8 @@ namespace NewHorizons.Builder.Props
conversationZone.SetActive(true);
FixDialogueNextFrame(dialogueTree);
return dialogueTree;
}
@ -378,11 +469,17 @@ namespace NewHorizons.Builder.Props
}
}
[Obsolete("Pass in the DialogueTree XmlNode instead, this is still here because Pikpik was using it in EOTP")]
public static void AddTranslation(string xml, string characterName = null)
{
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);
var xmlNode = xmlDocument.SelectSingleNode("DialogueTree");
AddTranslation(xmlNode, characterName);
}
public static void AddTranslation(XmlNode xmlNode, string characterName = null)
{
var xmlNodeList = xmlNode.SelectNodes("DialogueNode");
// When adding dialogue to existing stuff, we have to pass in the character name
@ -417,5 +514,21 @@ namespace NewHorizons.Builder.Props
}
}
}
public static void HandleUnityCreatedDialogue(CharacterDialogueTree dialogue)
{
var text = dialogue._xmlCharacterDialogueAsset.text;
var dialogueDoc = new XmlDocument();
dialogueDoc.LoadXml(text);
var xmlNode = dialogueDoc.SelectSingleNode("DialogueTree");
AddTranslation(xmlNode, null);
var newTextAsset = new TextAsset(dialogueDoc.OuterXml)
{
name = dialogue._xmlCharacterDialogueAsset.name
};
dialogue.SetTextXml(newTextAsset);
FixDialogueNextFrame(dialogue);
}
}
}

View File

@ -14,7 +14,11 @@ internal class PlayerShipAtmosphereDetectorFix : MonoBehaviour
public void Start()
{
_fluidDetector = Locator.GetPlayerCameraDetector().GetComponent<PlayerCameraFluidDetector>();
_shipAtmosphereVolume = Locator.GetShipBody().transform.Find("Volumes/ShipAtmosphereVolume").GetComponent<SimpleFluidVolume>();
_shipAtmosphereVolume = Locator.GetShipBody()?.transform?.Find("Volumes/ShipAtmosphereVolume")?.GetComponent<SimpleFluidVolume>();
if (_shipAtmosphereVolume == null)
{
Destroy(this);
}
}
public void Update()

View File

@ -173,6 +173,26 @@ namespace NewHorizons.Handlers
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)
{
var key = string.Join(string.Empty, rawPreText) + rawText;

View File

@ -413,7 +413,7 @@ namespace NewHorizons.Utility
public static XmlNode GetChildNode(this XmlNode parentNode, string tagName)
{
return parentNode.ChildNodes.Cast<XmlNode>().First(node => node.LocalName == tagName);
return parentNode.ChildNodes.Cast<XmlNode>().FirstOrDefault(node => node.LocalName == tagName);
}
public static string TruncateWhitespaceAndToLower(this string text)

View File

@ -4,7 +4,7 @@
"author": "xen, Bwc9876, JohnCorby, MegaPiggy, Clay, Trifid, and friends",
"name": "New Horizons",
"uniqueName": "xen.NewHorizons",
"version": "1.20.1",
"version": "1.20.2",
"owmlVersion": "2.10.3",
"dependencies": [ "JohnCorby.VanillaFix", "xen.CommonCameraUtility", "dgarro.CustomShipLogModes" ],
"conflicts": [ "PacificEngine.OW_CommonResources" ],