Questionable/Questionable/Functions/ExcelFunctions.cs

110 lines
3.8 KiB
C#
Raw Normal View History

using System;
using System.Linq;
using Dalamud.Plugin.Services;
using Dalamud.Utility;
using LLib;
2024-11-17 02:12:27 +01:00
using Lumina.Excel.Exceptions;
2024-11-16 21:53:37 +01:00
using Lumina.Excel.Sheets;
using Lumina.Text.ReadOnly;
using Microsoft.Extensions.Logging;
using Questionable.Model;
using Quest = Questionable.Model.Quest;
2024-11-16 21:53:37 +01:00
using GimmickYesNo = Lumina.Excel.Sheets.GimmickYesNo;
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;
}
2024-08-08 01:49:14 +02:00
public StringOrRegex GetDialogueText(Quest? currentQuest, string? excelSheetName, string key, bool isRegex)
{
var seString = GetRawDialogueText(currentQuest, excelSheetName, key);
if (isRegex)
return new StringOrRegex(seString.ToRegex());
else
return new StringOrRegex(seString?.ToDalamudString().ToString());
}
2024-11-16 21:53:37 +01:00
public ReadOnlySeString? GetRawDialogueText(Quest? currentQuest, string? excelSheetName, string key)
{
2024-08-08 01:49:14 +02:00
if (currentQuest != null && excelSheetName == null)
{
var questRow =
2024-11-17 19:52:04 +01:00
_dataManager.GetExcelSheet<Lumina.Excel.Sheets.Quest>().GetRowOrDefault((uint)currentQuest.Id.Value +
0x10000);
if (questRow == null)
{
_logger.LogError("Could not find quest row for {QuestId}", currentQuest.Id);
return null;
}
2024-11-17 02:12:27 +01:00
excelSheetName = $"quest/{(currentQuest.Id.Value / 100):000}/{questRow.Value.Id}";
}
2024-08-08 01:49:14 +02:00
ArgumentNullException.ThrowIfNull(excelSheetName);
2024-11-17 02:12:27 +01:00
try
{
var excelSheet = _dataManager.GetExcelSheet<QuestDialogueText>(name: excelSheetName);
return excelSheet.Cast<QuestDialogueText?>()
.FirstOrDefault(x => x!.Value.Key == key)?.Value;
}
catch (SheetNotFoundException e)
{
throw new SheetNotFoundException($"Sheet '{excelSheetName}' not found", e);
}
}
public StringOrRegex GetDialogueTextByRowId(string? excelSheet, uint rowId, bool isRegex)
{
var seString = GetRawDialogueTextByRowId(excelSheet, rowId);
if (isRegex)
return new StringOrRegex(seString.ToRegex());
else
return new StringOrRegex(seString?.ToDalamudString().ToString());
}
2024-11-16 21:53:37 +01:00
public ReadOnlySeString? GetRawDialogueTextByRowId(string? excelSheet, uint rowId)
{
if (excelSheet == "GimmickYesNo")
{
2024-11-16 21:53:37 +01:00
var questRow = _dataManager.GetExcelSheet<GimmickYesNo>().GetRowOrDefault(rowId);
return questRow?.Unknown0;
}
else if (excelSheet == "Warp")
{
2024-11-16 21:53:37 +01:00
var questRow = _dataManager.GetExcelSheet<Warp>().GetRowOrDefault(rowId);
return questRow?.Name;
}
else if (excelSheet is "Addon")
{
2024-11-16 21:53:37 +01:00
var questRow = _dataManager.GetExcelSheet<Addon>().GetRowOrDefault(rowId);
return questRow?.Text;
}
else if (excelSheet is "EventPathMove")
{
2024-11-16 21:53:37 +01:00
var questRow = _dataManager.GetExcelSheet<EventPathMove>().GetRowOrDefault(rowId);
return questRow?.Unknown0;
}
2024-09-22 14:31:14 +02:00
else if (excelSheet is "GilShop")
{
2024-11-16 21:53:37 +01:00
var questRow = _dataManager.GetExcelSheet<GilShop>().GetRowOrDefault(rowId);
2024-09-22 14:31:14 +02:00
return questRow?.Name;
}
else if (excelSheet is "ContentTalk" or null)
{
2024-11-16 21:53:37 +01:00
var questRow = _dataManager.GetExcelSheet<ContentTalk>().GetRowOrDefault(rowId);
return questRow?.Text;
}
else
throw new ArgumentOutOfRangeException(nameof(excelSheet), $"Unsupported excel sheet {excelSheet}");
}
}