Questionable/Questionable/External/QuestionableIpc.cs
2025-02-05 16:57:33 +01:00

63 lines
2.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Dalamud.Plugin;
using Dalamud.Plugin.Ipc;
using Questionable.Controller;
using Questionable.Model.Questing;
using Questionable.Windows.QuestComponents;
namespace Questionable.External;
internal sealed class QuestionableIpc : IDisposable
{
private const string IpcIsRunning = "Questionable.IsRunning";
private const string IpcGetCurrentQuestId = "Questionable.GetCurrentQuestId";
private const string IpcGetCurrentlyActiveEventQuests = "Questionable.GetCurrentlyActiveEventQuests";
private readonly ICallGateProvider<bool> _isRunning;
private readonly ICallGateProvider<string?> _getCurrentQuestId;
private readonly ICallGateProvider<List<string>> _getCurrentlyActiveEventQuests;
private readonly ICallGateProvider<string, bool> _startQuest;
public QuestionableIpc(
QuestController questController,
EventInfoComponent eventInfoComponent, QuestRegistry questRegistry,
IDalamudPluginInterface pluginInterface)
{
_isRunning = pluginInterface.GetIpcProvider<bool>(IpcIsRunning);
_isRunning.RegisterFunc(() =>
questController.AutomationType != QuestController.EAutomationType.Manual || questController.IsRunning);
_getCurrentQuestId = pluginInterface.GetIpcProvider<string?>(IpcGetCurrentQuestId);
_getCurrentQuestId.RegisterFunc(() => questController.CurrentQuest?.Quest.Id.ToString());
_getCurrentlyActiveEventQuests =
pluginInterface.GetIpcProvider<List<string>>(IpcGetCurrentlyActiveEventQuests);
_getCurrentlyActiveEventQuests.RegisterFunc(() =>
eventInfoComponent.GetCurrentlyActiveEventQuests().Select(q => q.ToString()).ToList());
_startQuest = pluginInterface.GetIpcProvider<string, bool>("Questionable.StartQuest");
_startQuest.RegisterFunc((string questId) => StartQuest(questController, questRegistry, questId));
}
private static bool StartQuest(QuestController qc, QuestRegistry qr, string questId)
{
if (ElementId.TryFromString(questId, out var elementId) && elementId != null && qr.TryGetQuest(elementId, out var quest))
{
qc.SetNextQuest(quest);
qc.Start("IPCQuestSelection");
return true;
}
return false;
}
public void Dispose()
{
_startQuest.UnregisterFunc();
_getCurrentlyActiveEventQuests.UnregisterFunc();
_getCurrentQuestId.UnregisterFunc();
_isRunning.UnregisterFunc();
}
}