123 lines
3.3 KiB
C#
123 lines
3.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Immutable;
|
|
using System.Threading.Tasks;
|
|
using Dalamud.Plugin;
|
|
using Dalamud.Plugin.Ipc;
|
|
using Dalamud.Plugin.Ipc.Exceptions;
|
|
using Dalamud.Plugin.Services;
|
|
using Microsoft.Extensions.Logging;
|
|
using Questionable.Controller;
|
|
using Questionable.Data;
|
|
|
|
namespace Questionable.External;
|
|
|
|
internal sealed class AutomatonIpc : IDisposable
|
|
{
|
|
private static readonly ImmutableHashSet<string> ConflictingTweaks = new HashSet<string>
|
|
{
|
|
"DateWithDestiny", "AutoFollow", "AutoPillion", "AutoInvite", "AutoBusy", "AutoEquipXPBoosts", "FateToolKit", "AutoMerge", "AutoQueue", "EnhancedDutyStartEnd",
|
|
"EnhancedLoginLogout", "GettingTooAttached", "RetrieveMateria"
|
|
}.ToImmutableHashSet();
|
|
|
|
private const string AutoSnipeTweak = "AutoSnipeQuests";
|
|
|
|
private readonly Configuration _configuration;
|
|
|
|
private readonly QuestController _questController;
|
|
|
|
private readonly TerritoryData _territoryData;
|
|
|
|
private readonly IClientState _clientState;
|
|
|
|
private readonly ILogger<AutomatonIpc> _logger;
|
|
|
|
private readonly ICallGateSubscriber<string, bool> _isTweakEnabled;
|
|
|
|
private readonly ICallGateSubscriber<string, bool, object?> _setTweakState;
|
|
|
|
private HashSet<string>? _pausedTweaks;
|
|
|
|
public AutomatonIpc(Configuration configuration, IDalamudPluginInterface pluginInterface, QuestController questController, TerritoryData territoryData, IClientState clientState, ILogger<AutomatonIpc> logger)
|
|
{
|
|
_configuration = configuration;
|
|
_questController = questController;
|
|
_territoryData = territoryData;
|
|
_clientState = clientState;
|
|
_logger = logger;
|
|
_isTweakEnabled = pluginInterface.GetIpcSubscriber<string, bool>("Automaton.IsTweakEnabled");
|
|
_setTweakState = pluginInterface.GetIpcSubscriber<string, bool, object>("Automaton.SetTweakState");
|
|
_questController.AutomationTypeChanged += OnAutomationTypeChanged;
|
|
}
|
|
|
|
private void OnAutomationTypeChanged(object sender, QuestController.EAutomationType automationType)
|
|
{
|
|
if (automationType != QuestController.EAutomationType.Manual && !_territoryData.IsDutyInstance(_clientState.TerritoryType))
|
|
{
|
|
Task.Run((Action)DisableConflictingTweaks);
|
|
}
|
|
else
|
|
{
|
|
Task.Run((Action)RestoreConflictingTweaks);
|
|
}
|
|
}
|
|
|
|
private void DisableConflictingTweaks()
|
|
{
|
|
if (_pausedTweaks != null)
|
|
{
|
|
return;
|
|
}
|
|
_pausedTweaks = new HashSet<string>();
|
|
foreach (string conflictingTweak in ConflictingTweaks)
|
|
{
|
|
TryDisableTweak(conflictingTweak);
|
|
}
|
|
if (_configuration.General.AutoSnipe)
|
|
{
|
|
TryDisableTweak("AutoSnipeQuests");
|
|
}
|
|
}
|
|
|
|
private void TryDisableTweak(string tweak)
|
|
{
|
|
try
|
|
{
|
|
if (_isTweakEnabled.InvokeFunc(tweak))
|
|
{
|
|
_setTweakState.InvokeAction(tweak, arg2: false);
|
|
_pausedTweaks.Add(tweak);
|
|
_logger.LogInformation("Paused Automaton tweak: {Tweak}", tweak);
|
|
}
|
|
}
|
|
catch (IpcError)
|
|
{
|
|
}
|
|
}
|
|
|
|
private void RestoreConflictingTweaks()
|
|
{
|
|
if (_pausedTweaks == null)
|
|
{
|
|
return;
|
|
}
|
|
foreach (string pausedTweak in _pausedTweaks)
|
|
{
|
|
try
|
|
{
|
|
_setTweakState.InvokeAction(pausedTweak, arg2: true);
|
|
_logger.LogInformation("Restored Automaton tweak: {Tweak}", pausedTweak);
|
|
}
|
|
catch (IpcError)
|
|
{
|
|
}
|
|
}
|
|
_pausedTweaks = null;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_questController.AutomationTypeChanged -= OnAutomationTypeChanged;
|
|
RestoreConflictingTweaks();
|
|
}
|
|
}
|