You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
5.4 KiB

  1. using IPA.Loader;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace IPA.Utilities.Async
  9. {
  10. /// <summary>
  11. /// A class providing coroutine helpers.
  12. /// </summary>
  13. public static class Coroutines
  14. {
  15. /// <summary>
  16. /// Stalls the coroutine until <paramref name="task"/> completes, faults, or is canceled.
  17. /// </summary>
  18. /// <param name="task">the <see cref="Task"/> to wait for</param>
  19. /// <returns>a coroutine waiting for the given task</returns>
  20. public static IEnumerator WaitForTask(Task task)
  21. => WaitForTask(task, false);
  22. /// <summary>
  23. /// Stalls the coroutine until <paramref name="task"/> completes, faults, or is canceled.
  24. /// </summary>
  25. /// <param name="task">the <see cref="Task"/> to wait for</param>
  26. /// <param name="throwOnFault">whether or not to throw if the task faulted</param>
  27. /// <returns>a coroutine waiting for the given task</returns>
  28. public static IEnumerator WaitForTask(Task task, bool throwOnFault = false)
  29. {
  30. while (!task.IsCompleted && !task.IsCanceled && !task.IsFaulted)
  31. yield return null;
  32. if (throwOnFault && task.IsFaulted)
  33. throw task.Exception;
  34. }
  35. /// <summary>
  36. /// Binds a <see cref="Task"/> to a Unity coroutine, capturing exceptions as well as the coroutine call stack.
  37. /// </summary>
  38. /// <remarks>
  39. /// <para>
  40. /// This may be called off of the Unity main thread. If it is, the coroutine start will be scheduled using the default
  41. /// <see cref="UnityMainThreadTaskScheduler"/> and will be run on the main thread as required by Unity.
  42. /// </para>
  43. /// <para>
  44. /// Unity provides a handful of coroutine helpers that are not <see cref="IEnumerable"/>s. Most of these are not terribly
  45. /// helpful on their own, however <see cref="UnityEngine.WaitForSeconds"/> may be. Instead, prefer to use the typical .NET
  46. /// <see cref="Task.Wait(TimeSpan)"/> or similar overloads, or use <see cref="UnityEngine.WaitForSecondsRealtime"/>.
  47. /// </para>
  48. /// </remarks>
  49. /// <param name="coroutine">the coroutine to bind to a task</param>
  50. /// <returns>a <see cref="Task"/> that completes when <paramref name="coroutine"/> completes, and fails when it throws</returns>
  51. public static Task AsTask(IEnumerator coroutine)
  52. {
  53. if (!UnityGame.OnMainThread)
  54. return UnityMainThreadTaskScheduler.Factory.StartNew(() => AsTask(coroutine)).Unwrap();
  55. var tcs = new TaskCompletionSource<VoidStruct>(coroutine, TaskCreationOptions.RunContinuationsAsynchronously);
  56. PluginComponent.Instance.StartCoroutine(new AsTaskCoroutineExecutor(coroutine, tcs));
  57. return tcs.Task;
  58. }
  59. private struct VoidStruct { }
  60. private class ExceptionLocation : Exception
  61. {
  62. public ExceptionLocation(IEnumerable<string> locations)
  63. : base(string.Join("\n", locations.Select(s => "in " + s)))
  64. {
  65. }
  66. }
  67. private class AsTaskCoroutineExecutor : IEnumerator
  68. {
  69. private readonly TaskCompletionSource<VoidStruct> completionSource;
  70. public AsTaskCoroutineExecutor(IEnumerator coroutine, TaskCompletionSource<VoidStruct> completion)
  71. {
  72. completionSource = completion;
  73. enumerators.Push(coroutine);
  74. }
  75. private readonly Stack<IEnumerator> enumerators = new Stack<IEnumerator>(2);
  76. public object Current => enumerators.FirstOrDefault()?.Current; // effectively a TryPeek
  77. public bool MoveNext()
  78. {
  79. do
  80. {
  81. if (enumerators.Count == 0)
  82. {
  83. completionSource.SetResult(new VoidStruct());
  84. return false;
  85. }
  86. try
  87. {
  88. var top = enumerators.Peek();
  89. if (top.MoveNext())
  90. {
  91. if (top.Current is IEnumerator enumerator)
  92. {
  93. enumerators.Push(enumerator);
  94. continue;
  95. }
  96. else
  97. {
  98. return true;
  99. }
  100. }
  101. else
  102. { // this enumerator completed, so pop it and continue
  103. enumerators.Pop();
  104. continue;
  105. }
  106. }
  107. catch (Exception e)
  108. { // execution errored
  109. completionSource.SetException(new AggregateException(
  110. e, new ExceptionLocation(enumerators.Select(e => e.GetType().ToString()))
  111. ));
  112. return false;
  113. }
  114. }
  115. while (true);
  116. }
  117. public void Reset() => throw new InvalidOperationException();
  118. }
  119. }
  120. }