138 lines
3.9 KiB
C#
138 lines
3.9 KiB
C#
using System;
|
|
using Dalamud.Plugin;
|
|
using Dalamud.Plugin.Ipc;
|
|
using Dalamud.Plugin.Ipc.Exceptions;
|
|
using Microsoft.Extensions.Logging;
|
|
using Questionable.Controller.Steps;
|
|
using Questionable.Data;
|
|
using Questionable.Model.Questing;
|
|
|
|
namespace Questionable.External;
|
|
|
|
internal sealed class AutoDutyIpc
|
|
{
|
|
public enum DutyMode
|
|
{
|
|
Support = 1,
|
|
UnsyncRegular
|
|
}
|
|
|
|
private readonly Configuration _configuration;
|
|
|
|
private readonly TerritoryData _territoryData;
|
|
|
|
private readonly ILogger<AutoDutyIpc> _logger;
|
|
|
|
private readonly ICallGateSubscriber<uint, bool> _contentHasPath;
|
|
|
|
private readonly ICallGateSubscriber<string, string, object> _setConfig;
|
|
|
|
private readonly ICallGateSubscriber<uint, int, bool, object> _run;
|
|
|
|
private readonly ICallGateSubscriber<bool> _isStopped;
|
|
|
|
private readonly ICallGateSubscriber<object> _stop;
|
|
|
|
public AutoDutyIpc(IDalamudPluginInterface pluginInterface, Configuration configuration, TerritoryData territoryData, ILogger<AutoDutyIpc> logger)
|
|
{
|
|
_configuration = configuration;
|
|
_territoryData = territoryData;
|
|
_logger = logger;
|
|
_contentHasPath = pluginInterface.GetIpcSubscriber<uint, bool>("AutoDuty.ContentHasPath");
|
|
_setConfig = pluginInterface.GetIpcSubscriber<string, string, object>("AutoDuty.SetConfig");
|
|
_run = pluginInterface.GetIpcSubscriber<uint, int, bool, object>("AutoDuty.Run");
|
|
_isStopped = pluginInterface.GetIpcSubscriber<bool>("AutoDuty.IsStopped");
|
|
_stop = pluginInterface.GetIpcSubscriber<object>("AutoDuty.Stop");
|
|
}
|
|
|
|
public bool IsConfiguredToRunContent(DutyOptions? dutyOptions)
|
|
{
|
|
if (dutyOptions == null || dutyOptions.ContentFinderConditionId == 0)
|
|
{
|
|
return false;
|
|
}
|
|
if (!_configuration.Duties.RunInstancedContentWithAutoDuty)
|
|
{
|
|
return false;
|
|
}
|
|
if (_configuration.Duties.BlacklistedDutyCfcIds.Contains(dutyOptions.ContentFinderConditionId))
|
|
{
|
|
return false;
|
|
}
|
|
if (_configuration.Duties.WhitelistedDutyCfcIds.Contains(dutyOptions.ContentFinderConditionId) && _territoryData.TryGetContentFinderCondition(dutyOptions.ContentFinderConditionId, out TerritoryData.ContentFinderConditionData _))
|
|
{
|
|
return true;
|
|
}
|
|
if (dutyOptions.Enabled)
|
|
{
|
|
return HasPath(dutyOptions.ContentFinderConditionId);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public bool HasPath(uint cfcId)
|
|
{
|
|
if (!_territoryData.TryGetContentFinderCondition(cfcId, out TerritoryData.ContentFinderConditionData contentFinderConditionData))
|
|
{
|
|
return false;
|
|
}
|
|
try
|
|
{
|
|
return _contentHasPath.InvokeFunc(contentFinderConditionData.TerritoryId);
|
|
}
|
|
catch (IpcError ipcError)
|
|
{
|
|
_logger.LogWarning("Unable to query AutoDuty for path in territory {TerritoryType}: {Message}", contentFinderConditionData.TerritoryId, ipcError.Message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public void StartInstance(uint cfcId, DutyMode dutyMode)
|
|
{
|
|
if (!_territoryData.TryGetContentFinderCondition(cfcId, out TerritoryData.ContentFinderConditionData contentFinderConditionData))
|
|
{
|
|
throw new TaskException($"Unknown ContentFinderConditionId {cfcId}");
|
|
}
|
|
try
|
|
{
|
|
_setConfig.InvokeAction("Unsynced", $"{dutyMode == DutyMode.UnsyncRegular}");
|
|
ICallGateSubscriber<string, string, object> setConfig = _setConfig;
|
|
setConfig.InvokeAction("dutyModeEnum", dutyMode switch
|
|
{
|
|
DutyMode.Support => "Support",
|
|
DutyMode.UnsyncRegular => "Regular",
|
|
_ => throw new ArgumentOutOfRangeException("dutyMode", dutyMode, null),
|
|
});
|
|
_run.InvokeAction(contentFinderConditionData.TerritoryId, 1, !_configuration.Advanced.DisableAutoDutyBareMode);
|
|
}
|
|
catch (IpcError ipcError)
|
|
{
|
|
throw new TaskException("Unable to run content with AutoDuty: " + ipcError.Message, ipcError);
|
|
}
|
|
}
|
|
|
|
public bool IsStopped()
|
|
{
|
|
try
|
|
{
|
|
return _isStopped.InvokeFunc();
|
|
}
|
|
catch (IpcError)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("Calling AutoDuty.Stop");
|
|
_stop.InvokeAction();
|
|
}
|
|
catch (IpcError ipcError)
|
|
{
|
|
throw new TaskException("Unable to stop AutoDuty: " + ipcError.Message, ipcError);
|
|
}
|
|
}
|
|
}
|