// ------------------------------------------------------------------------ // Copyright (c) 2021-2026, Jiaqi (0x7c13) Liu. All rights reserved. // See LICENSE file in the project root for license information. // ------------------------------------------------------------------------ namespace Pal3.Game.GameSystems.Dialogue { using System; using System.Collections.Generic; public static class DialogueTextProcessor { /// /// Break long dialogue into pieces /// Basically separate a dialogue into two pieces if there are more /// than three new line chars found in the dialogue text. /// /// The input text. /// One or two sub dialogues public static IEnumerable GetSubDialoguesAsync(string text) { if (text.Contains('\n')) { int indexOfSecondNewLineChar = text.IndexOf('\n', text.IndexOf('\n') + 1); if (indexOfSecondNewLineChar != -1) { int indexOfThirdNewLineChar = text.IndexOf('\n', indexOfSecondNewLineChar + 1); if (indexOfThirdNewLineChar != -1 && indexOfThirdNewLineChar != text.Length) { string firstPart = text[..indexOfThirdNewLineChar]; string secondPart = text.Substring(indexOfThirdNewLineChar, text.Length - indexOfThirdNewLineChar); yield return firstPart; yield return text[..text.IndexOf('\n')] + secondPart; yield break; } } } yield return text; } /// /// Returns the display text with information text color used by TextMeshProUGUI. /// /// The input text. /// The hex code for the information text color. /// The formatted text with information text color. public static string GetDisplayText(string text, string informationTextColorHex) { string formattedText = text.Replace("\\n", "\n"); return ReplaceStringInTextWithPatternForEachChar(formattedText, "\\i", "\\r", $"", ""); } /// /// Returns the display text for a given selection text. Removes any numbering or /// punctuation at the beginning or end of the selection text. /// /// The selection text to process. /// The display text for the selection string. public static string GetSelectionDisplayText(string selectionText) { if (selectionText.EndsWith(";") || selectionText.EndsWith("。")) selectionText = selectionText[..^1]; if (selectionText.Contains('.')) { string numberStr = selectionText[..selectionText.IndexOf('.')]; if (int.TryParse(numberStr, out _)) { return selectionText[(selectionText.IndexOf('.') + 1)..]; } } if (selectionText.Contains('、')) { string numberStr = selectionText[..selectionText.IndexOf('、')]; if (int.TryParse(numberStr, out _)) { return selectionText[(selectionText.IndexOf('、') + 1)..]; } } // I don't think there will be more than 20 options, so let's start with 20 for (int i = 20; i >= 0; i--) { string intStr = i.ToString(); if (selectionText.StartsWith(intStr) && !string.Equals(selectionText, intStr)) { return selectionText[intStr.Length..]; } } return selectionText; } /// /// Replaces each character in a substring of the input string that is enclosed by a start pattern and an end pattern with a new pattern. /// /// The input string to process. /// The start pattern that marks the beginning of the substring to process. /// The end pattern that marks the end of the substring to process. /// The pattern to insert before each character in the substring. /// The pattern to insert after each character in the substring. /// The processed string. private static string ReplaceStringInTextWithPatternForEachChar(string text, string startPattern, string endPattern, string charStartPattern, string charEndPattern) { string newStr = string.Empty; int currentIndex = 0; int startOfInformation = text.IndexOf(startPattern, StringComparison.Ordinal); while (startOfInformation != -1) { int endOfInformation = text.IndexOf(endPattern, startOfInformation, StringComparison.Ordinal); newStr += text.Substring(currentIndex, startOfInformation - currentIndex); foreach (char ch in text.Substring( startOfInformation + startPattern.Length, endOfInformation - startOfInformation - startPattern.Length)) { newStr += $"{charStartPattern}{ch}{charEndPattern}"; } currentIndex = endOfInformation + endPattern.Length; startOfInformation = text.IndexOf( startPattern, currentIndex, StringComparison.Ordinal); } newStr += text.Substring(currentIndex, text.Length - currentIndex); return newStr; } } }