LLib/DataManagerExtensions.cs

113 lines
3.4 KiB
C#
Raw Normal View History

2023-10-13 20:01:58 +00:00
using System;
using System.Linq;
using System.Text.RegularExpressions;
using Dalamud.Plugin.Services;
using Lumina.Excel;
using Lumina.Excel.CustomSheets;
using Lumina.Text;
using Lumina.Text.Payloads;
namespace LLib;
public static class DataManagerExtensions
{
2023-10-19 18:45:14 +00:00
public static SeString? GetSeString<T>(this IDataManager dataManager, string key)
2023-10-13 20:01:58 +00:00
where T : QuestDialogueText
{
2024-03-20 18:10:35 +00:00
ArgumentNullException.ThrowIfNull(dataManager);
2023-10-19 18:45:14 +00:00
return dataManager.GetExcelSheet<T>()?
.SingleOrDefault(x => x.Key == key)
?.Value;
}
public static string? GetString<T>(this IDataManager dataManager, string key, IPluginLog? pluginLog)
where T : QuestDialogueText
{
string? text = GetSeString<T>(dataManager, key)?.ToString();
pluginLog?.Verbose($"{typeof(T).Name}.{key} => {text}");
return text;
}
public static Regex? GetRegex<T>(this IDataManager dataManager, string key, IPluginLog? pluginLog)
where T : QuestDialogueText
{
SeString? text = GetSeString<T>(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
2023-10-21 10:32:21 +00:00
return "(.*)";
2023-10-19 18:45:14 +00:00
}));
pluginLog?.Verbose($"{typeof(T).Name}.{key} => /{regex}/");
return new Regex(regex);
2023-10-13 20:01:58 +00:00
}
public static SeString? GetSeString<T>(this IDataManager dataManager, uint rowId, Func<T, SeString?> mapper)
where T : ExcelRow
{
2024-03-20 18:10:35 +00:00
ArgumentNullException.ThrowIfNull(dataManager);
ArgumentNullException.ThrowIfNull(mapper);
2023-10-13 20:01:58 +00:00
var row = dataManager.GetExcelSheet<T>()?.GetRow(rowId);
if (row == null)
return null;
return mapper(row);
}
2024-03-20 18:10:35 +00:00
public static string? GetString<T>(this IDataManager dataManager, uint rowId, Func<T, SeString?> mapper,
IPluginLog? pluginLog = null)
2023-10-13 20:01:58 +00:00
where T : ExcelRow
{
string? text = GetSeString(dataManager, rowId, mapper)?.ToString();
pluginLog?.Verbose($"{typeof(T).Name}.{rowId} => {text}");
return text;
}
2024-03-20 18:10:35 +00:00
public static Regex? GetRegex<T>(this IDataManager dataManager, uint rowId, Func<T, SeString?> mapper,
IPluginLog? pluginLog = null)
2023-10-13 20:01:58 +00:00
where T : ExcelRow
{
SeString? text = GetSeString(dataManager, rowId, mapper);
if (text == null)
return null;
2024-08-05 13:44:45 +00:00
Regex regex = text.ToRegex();
2023-10-13 20:01:58 +00:00
pluginLog?.Verbose($"{typeof(T).Name}.{rowId} => /{regex}/");
2024-08-05 13:44:45 +00:00
return regex;
2023-10-13 20:01:58 +00:00
}
2023-10-21 10:32:21 +00:00
2024-03-20 18:10:35 +00:00
public static Regex? GetRegex<T>(this T excelRow, Func<T, SeString?> mapper, IPluginLog? pluginLog)
2023-10-21 10:32:21 +00:00
where T : ExcelRow
{
2024-03-20 18:10:35 +00:00
ArgumentNullException.ThrowIfNull(excelRow);
ArgumentNullException.ThrowIfNull(mapper);
2023-10-21 10:32:21 +00:00
SeString? text = mapper(excelRow);
if (text == null)
return null;
2024-08-05 13:44:45 +00:00
Regex regex = text.ToRegex();
pluginLog?.Verbose($"{typeof(T).Name}.regex => /{regex}/");
return regex;
}
public static Regex ToRegex(this SeString? text)
{
ArgumentNullException.ThrowIfNull(text);
return new Regex(string.Join("", text.Payloads.Select(payload =>
2023-10-21 10:32:21 +00:00
{
if (payload is TextPayload)
return Regex.Escape(payload.RawString);
else
return "(.*)";
2024-08-05 13:44:45 +00:00
})));
2023-10-21 10:32:21 +00:00
}
2023-10-13 20:01:58 +00:00
}