qstbak/Questionable/Questionable.Functions/ExcelFunctions.cs
2025-10-09 07:47:19 +10:00

103 lines
3.3 KiB
C#

using System;
using System.Linq;
using Dalamud.Plugin.Services;
using Dalamud.Utility;
using LLib;
using Lumina.Excel.Exceptions;
using Lumina.Excel.Sheets;
using Lumina.Text.ReadOnly;
using Microsoft.Extensions.Logging;
using Questionable.Model;
namespace Questionable.Functions;
internal sealed class ExcelFunctions
{
private readonly IDataManager _dataManager;
private readonly ILogger<ExcelFunctions> _logger;
public ExcelFunctions(IDataManager dataManager, ILogger<ExcelFunctions> logger)
{
_dataManager = dataManager;
_logger = logger;
}
public StringOrRegex GetDialogueText(Questionable.Model.Quest? currentQuest, string? excelSheetName, string key, bool isRegex)
{
ReadOnlySeString? rawDialogueText = GetRawDialogueText(currentQuest, excelSheetName, key);
if (isRegex)
{
return new StringOrRegex(rawDialogueText.ToRegex());
}
return new StringOrRegex(rawDialogueText?.WithCertainMacroCodeReplacements());
}
public ReadOnlySeString? GetRawDialogueText(Questionable.Model.Quest? currentQuest, string? excelSheetName, string key)
{
if (currentQuest != null && excelSheetName == null)
{
Lumina.Excel.Sheets.Quest? rowOrDefault = _dataManager.GetExcelSheet<Lumina.Excel.Sheets.Quest>().GetRowOrDefault((uint)(currentQuest.Id.Value + 65536));
if (!rowOrDefault.HasValue)
{
_logger.LogError("Could not find quest row for {QuestId}", currentQuest.Id);
return null;
}
excelSheetName = $"quest/{currentQuest.Id.Value / 100:000}/{rowOrDefault.Value.Id}";
}
ArgumentNullException.ThrowIfNull(excelSheetName, "excelSheetName");
try
{
IDataManager dataManager = _dataManager;
string name = excelSheetName;
return dataManager.GetExcelSheet<QuestDialogueText>(null, name).Cast<QuestDialogueText?>().FirstOrDefault((QuestDialogueText? x) => x.Value.Key == key)?.Value;
}
catch (SheetNotFoundException innerException)
{
throw new SheetNotFoundException("Sheet '" + excelSheetName + "' not found", innerException);
}
}
public StringOrRegex GetDialogueTextByRowId(string? excelSheet, uint rowId, bool isRegex)
{
ReadOnlySeString? rawDialogueTextByRowId = GetRawDialogueTextByRowId(excelSheet, rowId);
if (isRegex)
{
return new StringOrRegex(rawDialogueTextByRowId.ToRegex());
}
return new StringOrRegex(rawDialogueTextByRowId?.ToDalamudString().ToString());
}
public ReadOnlySeString? GetRawDialogueTextByRowId(string? excelSheet, uint rowId)
{
bool flag;
switch (excelSheet)
{
case "GimmickYesNo":
return _dataManager.GetExcelSheet<GimmickYesNo>().GetRowOrDefault(rowId)?.Unknown0;
case "Warp":
return _dataManager.GetExcelSheet<Warp>().GetRowOrDefault(rowId)?.Name;
case "Addon":
return _dataManager.GetExcelSheet<Addon>().GetRowOrDefault(rowId)?.Text;
case "EventPathMove":
return _dataManager.GetExcelSheet<EventPathMove>().GetRowOrDefault(rowId)?.Unknown0;
case "GilShop":
return _dataManager.GetExcelSheet<GilShop>().GetRowOrDefault(rowId)?.Name;
default:
if (excelSheet != null)
{
flag = false;
break;
}
goto case "ContentTalk";
case "ContentTalk":
flag = true;
break;
}
if (flag)
{
return _dataManager.GetExcelSheet<ContentTalk>().GetRowOrDefault(rowId)?.Text;
}
throw new ArgumentOutOfRangeException("excelSheet", "Unsupported excel sheet " + excelSheet);
}
}