LLib/DataManagerExtensions.cs

131 lines
4.2 KiB
C#
Raw Normal View History

2023-10-13 20:01:58 +00:00
using System;
2024-11-15 21:32:13 +00:00
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
2023-10-13 20:01:58 +00:00
using System.Linq;
using System.Text.RegularExpressions;
2024-11-15 21:32:13 +00:00
using Dalamud.Game.Text.SeStringHandling.Payloads;
2023-10-13 20:01:58 +00:00
using Dalamud.Plugin.Services;
2024-11-15 21:32:13 +00:00
using Dalamud.Utility;
2023-10-13 20:01:58 +00:00
using Lumina.Excel;
2024-11-15 21:32:13 +00:00
using Lumina.Excel.Sheets;
2023-10-13 20:01:58 +00:00
using Lumina.Text;
2024-11-15 21:32:13 +00:00
using Lumina.Text.ReadOnly;
2023-10-13 20:01:58 +00:00
namespace LLib;
public static class DataManagerExtensions
{
2024-11-15 21:32:13 +00:00
/*
public static SeString? GetSeDialogueString(this IDataManager dataManager, string key)
where T : struct, IExcelRow<T>
2023-10-13 20:01:58 +00:00
{
2024-03-20 18:10:35 +00:00
ArgumentNullException.ThrowIfNull(dataManager);
2024-11-15 21:32:13 +00:00
return dataManager.GetExcelSheet<QuestDialogueText>()
2023-10-19 18:45:14 +00:00
.SingleOrDefault(x => x.Key == key)
2024-11-15 21:32:13 +00:00
.Value;
2023-10-19 18:45:14 +00:00
}
public static string? GetString<T>(this IDataManager dataManager, string key, IPluginLog? pluginLog)
2024-11-15 21:32:13 +00:00
where T : struct, IExcelRow<QuestDialogueText>
2023-10-19 18:45:14 +00:00
{
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)
2024-11-15 21:32:13 +00:00
where T : struct, IExcelRow<QuestDialogueText>
2023-10-19 18:45:14 +00:00
{
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
}
2024-11-15 21:32:13 +00:00
*/
2023-10-13 20:01:58 +00:00
2024-11-15 21:32:13 +00:00
public static ReadOnlySeString? GetSeString<T>(this IDataManager dataManager, uint rowId, Func<T, ReadOnlySeString?> mapper)
where T : struct, IExcelRow<T>
2023-10-13 20:01:58 +00:00
{
2024-03-20 18:10:35 +00:00
ArgumentNullException.ThrowIfNull(dataManager);
ArgumentNullException.ThrowIfNull(mapper);
2024-11-15 21:32:13 +00:00
var row = dataManager.GetExcelSheet<T>().GetRowOrDefault(rowId);
2023-10-13 20:01:58 +00:00
if (row == null)
return null;
2024-11-15 21:32:13 +00:00
return mapper(row.Value);
2023-10-13 20:01:58 +00:00
}
2024-11-15 21:32:13 +00:00
public static string? GetString<T>(this IDataManager dataManager, uint rowId, Func<T, ReadOnlySeString?> mapper,
2024-03-20 18:10:35 +00:00
IPluginLog? pluginLog = null)
2024-11-15 21:32:13 +00:00
where T : struct, IExcelRow<T>
2023-10-13 20:01:58 +00:00
{
string? text = GetSeString(dataManager, rowId, mapper)?.ToString();
pluginLog?.Verbose($"{typeof(T).Name}.{rowId} => {text}");
return text;
}
2024-11-15 21:32:13 +00:00
public static Regex? GetRegex<T>(this IDataManager dataManager, uint rowId, Func<T, ReadOnlySeString?> mapper,
2024-03-20 18:10:35 +00:00
IPluginLog? pluginLog = null)
2024-11-15 21:32:13 +00:00
where T : struct, IExcelRow<T>
2023-10-13 20:01:58 +00:00
{
2024-11-15 21:32:13 +00:00
ReadOnlySeString? text = GetSeString(dataManager, rowId, mapper);
2023-10-13 20:01:58 +00:00
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-11-15 21:32:13 +00:00
public static Regex? GetRegex<T>(this T excelRow, Func<T, ReadOnlySeString?> mapper, IPluginLog? pluginLog)
where T : struct, IExcelRow<T>
2023-10-21 10:32:21 +00:00
{
2024-03-20 18:10:35 +00:00
ArgumentNullException.ThrowIfNull(excelRow);
ArgumentNullException.ThrowIfNull(mapper);
2024-11-15 21:32:13 +00:00
ReadOnlySeString? text = mapper(excelRow);
2023-10-21 10:32:21 +00:00
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;
}
2024-11-15 21:32:13 +00:00
public static Regex ToRegex(this ReadOnlySeString? text)
2024-08-05 13:44:45 +00:00
{
ArgumentNullException.ThrowIfNull(text);
2024-11-15 21:32:13 +00:00
return new Regex(string.Join("", text.Value.Select(payload =>
2023-10-21 10:32:21 +00:00
{
2024-11-15 21:32:13 +00:00
if (payload.Type == ReadOnlySePayloadType.Text)
return Regex.Escape(payload.ToString());
2023-10-21 10:32:21 +00:00
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
}
2024-11-15 21:32:13 +00:00
[SuppressMessage("Performance", "CA1815")]
public readonly struct QuestDialogueText(ExcelPage page, uint offset, uint row) : IExcelRow<QuestDialogueText>
{
public uint RowId => row;
public ReadOnlySeString Key => page.ReadString(offset, offset);
public ReadOnlySeString Value => page.ReadString(offset + 4, offset);
static QuestDialogueText IExcelRow<QuestDialogueText>.Create(ExcelPage page, uint offset, uint row) =>
new(page, offset, row);
}