Merge remote-tracking branch 'origin/master' into net8-rollup

This commit is contained in:
github-actions[bot] 2024-03-19 03:11:08 +00:00
commit 990f04715f
4 changed files with 314 additions and 26 deletions

View file

@ -1,11 +1,29 @@
using System.Threading;
using System.Threading.Tasks;
using Dalamud.Interface.Internal.Windows.Data.Widgets;
namespace Dalamud.Plugin.Services;
/// <summary>
/// This class represents the Framework of the native game client and grants access to various subsystems.
/// </summary>
/// <remarks>
/// <para><b>Choosing between <c>RunOnFrameworkThread</c> and <c>RunOnFrameworkThreadAwaitable</c></b></para>
/// <ul>
/// <li>If you do need to do use <c>await</c> and have your task keep executing on the main thread after waiting is
/// done, use <c>RunOnFrameworkThreadAwaitable</c>.</li>
/// <li>If you need to call <see cref="Task.Wait()"/> or <see cref="Task{TResult}.Result"/>, use
/// <c>RunOnFrameworkThread</c>.</li>
/// </ul>
/// <para>The game is likely to completely lock up if you call above synchronous function and getter, because starting
/// a new task by default runs on <see cref="TaskScheduler.Current"/>, which would make the task run on the framework
/// thread if invoked via <c>RunOnFrameworkThreadAwaitable</c>. This includes <c>Task.Factory.StartNew</c> and
/// <c>Task.ContinueWith</c>. Use <c>Task.Run</c> if you need to start a new task from the callback specified to
/// <c>RunOnFrameworkThreadAwaitable</c>, as it will force your task to be run in the default thread pool.</para>
/// <para>See <see cref="TaskSchedulerWidget"/> to see the difference in behaviors, and how would a misuse of these
/// functions result in a deadlock.</para>
/// </remarks>
public interface IFramework
{
/// <summary>
@ -29,11 +47,6 @@ public interface IFramework
/// </summary>
public DateTime LastUpdateUTC { get; }
/// <summary>
/// Gets a <see cref="TaskFactory"/> that runs tasks during Framework Update event.
/// </summary>
public TaskFactory FrameworkThreadTaskFactory { get; }
/// <summary>
/// Gets the delta between the last Framework Update and the currently executing one.
/// </summary>
@ -49,20 +62,97 @@ public interface IFramework
/// </summary>
public bool IsFrameworkUnloading { get; }
/// <summary>Gets a <see cref="TaskFactory"/> that runs tasks during Framework Update event.</summary>
/// <returns>The task factory.</returns>
public TaskFactory GetTaskFactory();
/// <summary>
/// Returns a task that completes after the given number of ticks.
/// </summary>
/// <param name="numTicks">Number of ticks to delay.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A new <see cref="Task"/> that gets resolved after specified number of ticks happen.</returns>
/// <remarks>The continuation will run on the framework thread by default.</remarks>
public Task DelayTicks(long numTicks, CancellationToken cancellationToken = default);
/// <summary>
/// Run given function right away if this function has been called from game's Framework.Update thread, or otherwise run on next Framework.Update call.
/// </summary>
/// <param name="action">Function to call.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task representing the pending or already completed function.</returns>
/// <remarks>
/// <para>Starting new tasks and waiting on them <b>synchronously</b> from this callback will completely lock up
/// the game. Use <c>await</c> if you need to wait on something from an <c>async</c> callback.</para>
/// <para>See the remarks on <see cref="IFramework"/> if you need to choose which one to use, between
/// <c>RunOnFrameworkThreadAwaitable</c> and <c>RunOnFrameworkThread</c>. Note that <c>RunOnTick</c> is a fancy
/// version of <c>RunOnFrameworkThread</c>.</para>
/// </remarks>
public Task RunOnFrameworkThreadAwaitable(Action action, CancellationToken cancellationToken = default);
/// <summary>
/// Run given function right away if this function has been called from game's Framework.Update thread, or otherwise run on next Framework.Update call.
/// </summary>
/// <typeparam name="T">Return type.</typeparam>
/// <param name="action">Function to call.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task representing the pending or already completed function.</returns>
/// <remarks>
/// <para>Starting new tasks and waiting on them <b>synchronously</b> from this callback will completely lock up
/// the game. Use <c>await</c> if you need to wait on something from an <c>async</c> callback.</para>
/// <para>See the remarks on <see cref="IFramework"/> if you need to choose which one to use, between
/// <c>RunOnFrameworkThreadAwaitable</c> and <c>RunOnFrameworkThread</c>. Note that <c>RunOnTick</c> is a fancy
/// version of <c>RunOnFrameworkThread</c>.</para>
/// </remarks>
public Task<T> RunOnFrameworkThreadAwaitable<T>(Func<T> action, CancellationToken cancellationToken = default);
/// <summary>
/// Run given function right away if this function has been called from game's Framework.Update thread, or otherwise run on next Framework.Update call.
/// </summary>
/// <param name="action">Function to call.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task representing the pending or already completed function.</returns>
/// <remarks>
/// <para>Starting new tasks and waiting on them <b>synchronously</b> from this callback will completely lock up
/// the game. Use <c>await</c> if you need to wait on something from an <c>async</c> callback.</para>
/// <para>See the remarks on <see cref="IFramework"/> if you need to choose which one to use, between
/// <c>RunOnFrameworkThreadAwaitable</c> and <c>RunOnFrameworkThread</c>. Note that <c>RunOnTick</c> is a fancy
/// version of <c>RunOnFrameworkThread</c>.</para>
/// </remarks>
public Task RunOnFrameworkThreadAwaitable(Func<Task> action, CancellationToken cancellationToken = default);
/// <summary>
/// Run given function right away if this function has been called from game's Framework.Update thread, or otherwise run on next Framework.Update call.
/// </summary>
/// <typeparam name="T">Return type.</typeparam>
/// <param name="action">Function to call.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task representing the pending or already completed function.</returns>
/// <remarks>
/// <para>Starting new tasks and waiting on them <b>synchronously</b> from this callback will completely lock up
/// the game. Use <c>await</c> if you need to wait on something from an <c>async</c> callback.</para>
/// <para>See the remarks on <see cref="IFramework"/> if you need to choose which one to use, between
/// <c>RunOnFrameworkThreadAwaitable</c> and <c>RunOnFrameworkThread</c>. Note that <c>RunOnTick</c> is a fancy
/// version of <c>RunOnFrameworkThread</c>.</para>
/// </remarks>
public Task<T> RunOnFrameworkThreadAwaitable<T>(Func<Task<T>> action, CancellationToken cancellationToken = default);
/// <summary>
/// Run given function right away if this function has been called from game's Framework.Update thread, or otherwise run on next Framework.Update call.
/// </summary>
/// <typeparam name="T">Return type.</typeparam>
/// <param name="func">Function to call.</param>
/// <returns>Task representing the pending or already completed function.</returns>
/// <remarks>
/// <para><c>await</c>, <c>Task.Factory.StartNew</c> or alike will continue off the framework thread.</para>
/// <para>Awaiting on the returned <see cref="Task"/> from <c>RunOnFrameworkThread</c>,
/// <c>RunOnFrameworkThreadAwaitable</c>, or <c>RunOnTick</c> right away inside the callback specified to this
/// function has a chance of locking up the game. Do not do <c>await framework.RunOnFrameworkThread(...);</c>
/// directly or indirectly from the delegate passed to this function.</para>
/// <para>See the remarks on <see cref="IFramework"/> if you need to choose which one to use, between
/// <c>RunOnFrameworkThreadAwaitable</c> and <c>RunOnFrameworkThread</c>. Note that <c>RunOnTick</c> is a fancy
/// version of <c>RunOnFrameworkThread</c>.</para>
/// </remarks>
public Task<T> RunOnFrameworkThread<T>(Func<T> func);
/// <summary>
@ -70,6 +160,16 @@ public interface IFramework
/// </summary>
/// <param name="action">Function to call.</param>
/// <returns>Task representing the pending or already completed function.</returns>
/// <remarks>
/// <para><c>await</c>, <c>Task.Factory.StartNew</c> or alike will continue off the framework thread.</para>
/// <para>Awaiting on the returned <see cref="Task"/> from <c>RunOnFrameworkThread</c>,
/// <c>RunOnFrameworkThreadAwaitable</c>, or <c>RunOnTick</c> right away inside the callback specified to this
/// function has a chance of locking up the game. Do not do <c>await framework.RunOnFrameworkThread(...);</c>
/// directly or indirectly from the delegate passed to this function.</para>
/// <para>See the remarks on <see cref="IFramework"/> if you need to choose which one to use, between
/// <c>RunOnFrameworkThreadAwaitable</c> and <c>RunOnFrameworkThread</c>. Note that <c>RunOnTick</c> is a fancy
/// version of <c>RunOnFrameworkThread</c>.</para>
/// </remarks>
public Task RunOnFrameworkThread(Action action);
/// <summary>
@ -78,6 +178,16 @@ public interface IFramework
/// <typeparam name="T">Return type.</typeparam>
/// <param name="func">Function to call.</param>
/// <returns>Task representing the pending or already completed function.</returns>
/// <remarks>
/// <para><c>await</c>, <c>Task.Factory.StartNew</c> or alike will continue off the framework thread.</para>
/// <para>Awaiting on the returned <see cref="Task"/> from <c>RunOnFrameworkThread</c>,
/// <c>RunOnFrameworkThreadAwaitable</c>, or <c>RunOnTick</c> right away inside the callback specified to this
/// function has a chance of locking up the game. Do not do <c>await framework.RunOnFrameworkThread(...);</c>
/// directly or indirectly from the delegate passed to this function.</para>
/// <para>See the remarks on <see cref="IFramework"/> if you need to choose which one to use, between
/// <c>RunOnFrameworkThreadAwaitable</c> and <c>RunOnFrameworkThread</c>. Note that <c>RunOnTick</c> is a fancy
/// version of <c>RunOnFrameworkThread</c>.</para>
/// </remarks>
[Obsolete($"Use {nameof(RunOnTick)} instead.")]
public Task<T> RunOnFrameworkThread<T>(Func<Task<T>> func);
@ -86,6 +196,16 @@ public interface IFramework
/// </summary>
/// <param name="func">Function to call.</param>
/// <returns>Task representing the pending or already completed function.</returns>
/// <remarks>
/// <para><c>await</c>, <c>Task.Factory.StartNew</c> or alike will continue off the framework thread.</para>
/// <para>Awaiting on the returned <see cref="Task"/> from <c>RunOnFrameworkThread</c>,
/// <c>RunOnFrameworkThreadAwaitable</c>, or <c>RunOnTick</c> right away inside the callback specified to this
/// function has a chance of locking up the game. Do not do <c>await framework.RunOnFrameworkThread(...);</c>
/// directly or indirectly from the delegate passed to this function.</para>
/// <para>See the remarks on <see cref="IFramework"/> if you need to choose which one to use, between
/// <c>RunOnFrameworkThreadAwaitable</c> and <c>RunOnFrameworkThread</c>. Note that <c>RunOnTick</c> is a fancy
/// version of <c>RunOnFrameworkThread</c>.</para>
/// </remarks>
[Obsolete($"Use {nameof(RunOnTick)} instead.")]
public Task RunOnFrameworkThread(Func<Task> func);
@ -98,6 +218,16 @@ public interface IFramework
/// <param name="delayTicks">Count given number of Framework.Tick calls before calling this function. This takes precedence over delay parameter.</param>
/// <param name="cancellationToken">Cancellation token which will prevent the execution of this function if wait conditions are not met.</param>
/// <returns>Task representing the pending function.</returns>
/// <remarks>
/// <para><c>await</c>, <c>Task.Factory.StartNew</c> or alike will continue off the framework thread.</para>
/// <para>Awaiting on the returned <see cref="Task"/> from <c>RunOnFrameworkThread</c>,
/// <c>RunOnFrameworkThreadAwaitable</c>, or <c>RunOnTick</c> right away inside the callback specified to this
/// function has a chance of locking up the game. Do not do <c>await framework.RunOnFrameworkThread(...);</c>
/// directly or indirectly from the delegate passed to this function.</para>
/// <para>See the remarks on <see cref="IFramework"/> if you need to choose which one to use, between
/// <c>RunOnFrameworkThreadAwaitable</c> and <c>RunOnFrameworkThread</c>. Note that <c>RunOnTick</c> is a fancy
/// version of <c>RunOnFrameworkThread</c>.</para>
/// </remarks>
public Task<T> RunOnTick<T>(Func<T> func, TimeSpan delay = default, int delayTicks = default, CancellationToken cancellationToken = default);
/// <summary>
@ -108,6 +238,16 @@ public interface IFramework
/// <param name="delayTicks">Count given number of Framework.Tick calls before calling this function. This takes precedence over delay parameter.</param>
/// <param name="cancellationToken">Cancellation token which will prevent the execution of this function if wait conditions are not met.</param>
/// <returns>Task representing the pending function.</returns>
/// <remarks>
/// <para><c>await</c>, <c>Task.Factory.StartNew</c> or alike will continue off the framework thread.</para>
/// <para>Awaiting on the returned <see cref="Task"/> from <c>RunOnFrameworkThread</c>,
/// <c>RunOnFrameworkThreadAwaitable</c>, or <c>RunOnTick</c> right away inside the callback specified to this
/// function has a chance of locking up the game. Do not do <c>await framework.RunOnFrameworkThread(...);</c>
/// directly or indirectly from the delegate passed to this function.</para>
/// <para>See the remarks on <see cref="IFramework"/> if you need to choose which one to use, between
/// <c>RunOnFrameworkThreadAwaitable</c> and <c>RunOnFrameworkThread</c>. Note that <c>RunOnTick</c> is a fancy
/// version of <c>RunOnFrameworkThread</c>.</para>
/// </remarks>
public Task RunOnTick(Action action, TimeSpan delay = default, int delayTicks = default, CancellationToken cancellationToken = default);
/// <summary>
@ -119,6 +259,16 @@ public interface IFramework
/// <param name="delayTicks">Count given number of Framework.Tick calls before calling this function. This takes precedence over delay parameter.</param>
/// <param name="cancellationToken">Cancellation token which will prevent the execution of this function if wait conditions are not met.</param>
/// <returns>Task representing the pending function.</returns>
/// <remarks>
/// <para><c>await</c>, <c>Task.Factory.StartNew</c> or alike will continue off the framework thread.</para>
/// <para>Awaiting on the returned <see cref="Task"/> from <c>RunOnFrameworkThread</c>,
/// <c>RunOnFrameworkThreadAwaitable</c>, or <c>RunOnTick</c> right away inside the callback specified to this
/// function has a chance of locking up the game. Do not do <c>await framework.RunOnFrameworkThread(...);</c>
/// directly or indirectly from the delegate passed to this function.</para>
/// <para>See the remarks on <see cref="IFramework"/> if you need to choose which one to use, between
/// <c>RunOnFrameworkThreadAwaitable</c> and <c>RunOnFrameworkThread</c>. Note that <c>RunOnTick</c> is a fancy
/// version of <c>RunOnFrameworkThread</c>.</para>
/// </remarks>
public Task<T> RunOnTick<T>(Func<Task<T>> func, TimeSpan delay = default, int delayTicks = default, CancellationToken cancellationToken = default);
/// <summary>
@ -129,5 +279,15 @@ public interface IFramework
/// <param name="delayTicks">Count given number of Framework.Tick calls before calling this function. This takes precedence over delay parameter.</param>
/// <param name="cancellationToken">Cancellation token which will prevent the execution of this function if wait conditions are not met.</param>
/// <returns>Task representing the pending function.</returns>
/// <remarks>
/// <para><c>await</c>, <c>Task.Factory.StartNew</c> or alike will continue off the framework thread.</para>
/// <para>Awaiting on the returned <see cref="Task"/> from <c>RunOnFrameworkThread</c>,
/// <c>RunOnFrameworkThreadAwaitable</c>, or <c>RunOnTick</c> right away inside the callback specified to this
/// function has a chance of locking up the game. Do not do <c>await framework.RunOnFrameworkThread(...);</c>
/// directly or indirectly from the delegate passed to this function.</para>
/// <para>See the remarks on <see cref="IFramework"/> if you need to choose which one to use, between
/// <c>RunOnFrameworkThreadAwaitable</c> and <c>RunOnFrameworkThread</c>. Note that <c>RunOnTick</c> is a fancy
/// version of <c>RunOnFrameworkThread</c>.</para>
/// </remarks>
public Task RunOnTick(Func<Task> func, TimeSpan delay = default, int delayTicks = default, CancellationToken cancellationToken = default);
}