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 _logger; public ExcelFunctions(IDataManager dataManager, ILogger 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().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(null, name).Cast().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().GetRowOrDefault(rowId)?.Unknown0; case "Warp": return _dataManager.GetExcelSheet().GetRowOrDefault(rowId)?.Name; case "Addon": return _dataManager.GetExcelSheet().GetRowOrDefault(rowId)?.Text; case "EventPathMove": return _dataManager.GetExcelSheet().GetRowOrDefault(rowId)?.Unknown0; case "GilShop": return _dataManager.GetExcelSheet().GetRowOrDefault(rowId)?.Name; default: if (excelSheet != null) { flag = false; break; } goto case "ContentTalk"; case "ContentTalk": flag = true; break; } if (flag) { return _dataManager.GetExcelSheet().GetRowOrDefault(rowId)?.Text; } throw new ArgumentOutOfRangeException("excelSheet", "Unsupported excel sheet " + excelSheet); } }