qstbak/Questionable/Questionable.Controller.Steps/TaskCreator.cs
2025-10-09 07:53:51 +10:00

99 lines
3.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using Dalamud.Plugin.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Questionable.Controller.Steps.Interactions;
using Questionable.Controller.Steps.Shared;
using Questionable.Data;
using Questionable.Model;
using Questionable.Model.Questing;
namespace Questionable.Controller.Steps;
internal sealed class TaskCreator
{
private readonly IServiceProvider _serviceProvider;
private readonly TerritoryData _territoryData;
private readonly IClientState _clientState;
private readonly IChatGui _chatGui;
private readonly ILogger<TaskCreator> _logger;
public TaskCreator(IServiceProvider serviceProvider, TerritoryData territoryData, IClientState clientState, IChatGui chatGui, ILogger<TaskCreator> logger)
{
_serviceProvider = serviceProvider;
_territoryData = territoryData;
_clientState = clientState;
_chatGui = chatGui;
_logger = logger;
}
public IReadOnlyList<ITask> CreateTasks(Quest quest, byte sequenceNumber, QuestSequence? sequence, QuestStep? step)
{
List<ITask> list2;
if (sequence == null)
{
_chatGui.PrintError($"Path for quest '{quest.Info.Name}' ({quest.Id}) does not contain sequence {sequenceNumber}, please report this.", "Questionable", 576);
int num = 1;
List<ITask> list = new List<ITask>(num);
CollectionsMarshal.SetCount(list, num);
Span<ITask> span = CollectionsMarshal.AsSpan(list);
int index = 0;
span[index] = new WaitAtEnd.WaitNextStepOrSequence();
list2 = list;
}
else if (step == null)
{
int index = 1;
List<ITask> list3 = new List<ITask>(index);
CollectionsMarshal.SetCount(list3, index);
Span<ITask> span = CollectionsMarshal.AsSpan(list3);
int num = 0;
span[num] = new WaitAtEnd.WaitNextStepOrSequence();
list2 = list3;
}
else
{
using IServiceScope serviceScope = _serviceProvider.CreateScope();
list2 = serviceScope.ServiceProvider.GetRequiredService<IEnumerable<ITaskFactory>>().SelectMany(delegate(ITaskFactory x)
{
List<ITask> list4 = x.CreateAllTasks(quest, sequence, step).ToList();
if (list4.Count > 0 && _logger.IsEnabled(LogLevel.Trace))
{
string text = x.GetType().FullName ?? x.GetType().Name;
if (text.Contains('.', StringComparison.Ordinal))
{
string text2 = text;
int num3 = text.LastIndexOf('.') + 1;
text = text2.Substring(num3, text2.Length - num3);
}
_logger.LogTrace("Factory {FactoryName} created Task {TaskNames}", text, string.Join(", ", list4.Select((ITask y) => y.ToString())));
}
return list4;
}).ToList();
SinglePlayerDuty.StartSinglePlayerDuty startSinglePlayerDuty = list2.Where((ITask y) => y is SinglePlayerDuty.StartSinglePlayerDuty).Cast<SinglePlayerDuty.StartSinglePlayerDuty>().FirstOrDefault();
if (startSinglePlayerDuty != null && _territoryData.TryGetContentFinderCondition(startSinglePlayerDuty.ContentFinderConditionId, out TerritoryData.ContentFinderConditionData contentFinderConditionData) && _clientState.TerritoryType == contentFinderConditionData.TerritoryId)
{
int num2 = list2.IndexOf(startSinglePlayerDuty);
_logger.LogWarning("Skipping {SkippedTaskCount} out of {TotalCount} tasks, questionable was started while in single player duty", num2 + 1, list2.Count);
list2.RemoveRange(0, num2 + 1);
_logger.LogInformation("Next actual task: {NextTask}, total tasks left: {RemainingTaskCount}", list2.FirstOrDefault(), list2.Count);
}
}
if (list2.Count == 0)
{
_logger.LogInformation("Nothing to execute for step?");
}
else
{
_logger.LogInformation("Tasks for {QuestId}, {Sequence}, {Step}: {Tasks}", quest.Id, sequenceNumber, (step == null) ? ((int?)null) : sequence?.Steps.IndexOf(step), string.Join(", ", list2.Select((ITask x) => x.ToString())));
}
return list2;
}
}