Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions Questionable/Controller/QuestController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Collections.Generic;
using System.Numerics;
using System.Threading;
using Dalamud.Game.ClientState.Conditions;
Expand Down Expand Up @@ -108,6 +109,10 @@ public enum ECurrentQuestType
private readonly IToastGui _toastGui;
private readonly ICommandManager _commandManager;
private EAutomationType _automationType;
private static readonly TimeSpan PauseRequestLifetime = TimeSpan.FromMinutes(10);
private readonly object _pauseRequestGate = new();
private readonly Dictionary<string, DateTime> _pauseRequests = new(StringComparer.Ordinal);
private bool _pauseEffective;
private DateTime _lastAutoRefresh = DateTime.MinValue;

/// <summary>True while recovering from the player's death (waiting for the return prompt / respawn).</summary>
Expand Down Expand Up @@ -265,6 +270,49 @@ public QuestProgress? PendingQuest
public Func<bool>? IsQuestWindowOpenFunction { private get; set; } = () => true;

public bool IsRunning => !_taskQueue.AllTasksComplete;
public void SetPauseRequest(string owner, bool paused)
{
if (string.IsNullOrWhiteSpace(owner))
throw new ArgumentException("A pause-request owner is required.", nameof(owner));
lock (_pauseRequestGate)
{
if (paused)
_pauseRequests[owner] = DateTime.UtcNow.Add(PauseRequestLifetime);
else
_pauseRequests.Remove(owner);
}
}

public bool IsPauseRequestEffective(string owner)
{
lock (_pauseRequestGate)
{
PruneExpiredPauseRequests();
return !string.IsNullOrWhiteSpace(owner) && _pauseRequests.ContainsKey(owner) && _pauseEffective;
}
}

private bool HasPauseRequests()
{
lock (_pauseRequestGate)
{
PruneExpiredPauseRequests();
return _pauseRequests.Count > 0;
}
}

private void PruneExpiredPauseRequests()
{
var now = DateTime.UtcNow;
foreach (var owner in _pauseRequests.Where(request => request.Value <= now).Select(request => request.Key).ToArray())
_pauseRequests.Remove(owner);
}

private void SetPauseEffective(bool effective)
{
lock (_pauseRequestGate)
_pauseEffective = effective;
}
public TaskQueue TaskQueue => _taskQueue;

public string? CurrentTaskState
Expand Down Expand Up @@ -340,6 +388,13 @@ public void Update()
if (IsQuestingStopped)
return;

if (HasPauseRequests() && _taskQueue.CurrentTaskExecutor == null)
{
SetPauseEffective(true);
return;
}
SetPauseEffective(false);

UpdateCurrentQuest();

if (!_clientState.IsLoggedIn)
Expand Down
11 changes: 11 additions & 0 deletions Questionable/External/QuestionableIpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ internal sealed class QuestionableIpc : IDisposable
private const string IpcStartGathering = "Questionable.StartGathering";
private const string IpcStartGatheringComplex = "Questionable.StartGatheringComplex";
private const string IpcStop = "Questionable.Stop";
private const string IpcSetPauseRequest = "Questionable.SetPauseRequest";
private const string IpcIsPauseRequestEffective = "Questionable.IsPauseRequestEffective";
private const string IpcRedoLookup = "Questionable.RedoLookup";
private const string IpcRedoLookupIndex = "Questionable.RedoLookupIndex";
private readonly ICallGateProvider<string, bool> _addQuestPriority;
Expand Down Expand Up @@ -69,6 +71,8 @@ internal sealed class QuestionableIpc : IDisposable
private readonly ICallGateProvider<string, bool> _startQuest;
private readonly ICallGateProvider<string, bool> _startSingleQuest;
private readonly ICallGateProvider<string, bool> _stop;
private readonly ICallGateProvider<string, bool, object> _setPauseRequest;
private readonly ICallGateProvider<string, bool> _isPauseRequestEffective;

public QuestionableIpc(
QuestController questController,
Expand Down Expand Up @@ -148,6 +152,11 @@ public QuestionableIpc(
_stop = pluginInterface.GetIpcProvider<string, bool>(IpcStop);
_stop.RegisterFunc(Stop);

_setPauseRequest = pluginInterface.GetIpcProvider<string, bool, object>(IpcSetPauseRequest);
_setPauseRequest.RegisterAction(questController.SetPauseRequest);
_isPauseRequestEffective = pluginInterface.GetIpcProvider<string, bool>(IpcIsPauseRequestEffective);
_isPauseRequestEffective.RegisterFunc(questController.IsPauseRequestEffective);

_redoUtil = redoUtil;

_redoLookup = pluginInterface.GetIpcProvider<uint, string>(IpcRedoLookup);
Expand Down Expand Up @@ -178,6 +187,8 @@ public void Dispose()
_startGathering.UnregisterFunc();
_startGatheringComplex.UnregisterFunc();
_stop.UnregisterFunc();
_setPauseRequest.UnregisterAction();
_isPauseRequestEffective.UnregisterFunc();
_redoLookup.UnregisterFunc();
}

Expand Down