From 7649b0d51b35c993839b918805718f046f06ae9b Mon Sep 17 00:00:00 2001 From: Liza Carvelli Date: Thu, 19 Oct 2023 20:45:14 +0200 Subject: [PATCH] Build Regex For dialogue strings --- DataManagerExtensions.cs | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/DataManagerExtensions.cs b/DataManagerExtensions.cs index 685f691..b4e7210 100644 --- a/DataManagerExtensions.cs +++ b/DataManagerExtensions.cs @@ -11,15 +11,39 @@ namespace LLib; public static class DataManagerExtensions { - public static string GetDialogue(this IDataManager dataManager, string key, IPluginLog? pluginLog) + public static SeString? GetSeString(this IDataManager dataManager, string key) where T : QuestDialogueText { - string result = dataManager.GetExcelSheet()! - .Single(x => x.Key == key) - .Value - .ToString(); - pluginLog?.Verbose($"{typeof(T).Name}.{key} => {result}"); - return result; + return dataManager.GetExcelSheet()? + .SingleOrDefault(x => x.Key == key) + ?.Value; + } + + public static string? GetString(this IDataManager dataManager, string key, IPluginLog? pluginLog) + where T : QuestDialogueText + { + string? text = GetSeString(dataManager, key)?.ToString(); + + pluginLog?.Verbose($"{typeof(T).Name}.{key} => {text}"); + return text; + } + + public static Regex? GetRegex(this IDataManager dataManager, string key, IPluginLog? pluginLog) + where T : QuestDialogueText + { + SeString? text = GetSeString(dataManager, key); + if (text == null) + return null; + + string regex = string.Join("", text.Payloads.Select(payload => + { + if (payload is TextPayload) + return Regex.Escape(payload.RawString); + else + return ".*"; + })); + pluginLog?.Verbose($"{typeof(T).Name}.{key} => /{regex}/"); + return new Regex(regex); } public static SeString? GetSeString(this IDataManager dataManager, uint rowId, Func mapper)