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.

135 lines
5.5 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. #if NET3
  56. var tcs = new TaskCompletionSource<VoidStruct>(coroutine);
  57. #else
  58. var tcs = new TaskCompletionSource<VoidStruct>(coroutine, TaskCreationOptions.RunContinuationsAsynchronously);
  59. #endif
  60. PluginComponent.Instance.StartCoroutine(new AsTaskCoroutineExecutor(coroutine, tcs));
  61. return tcs.Task;
  62. }
  63. private struct VoidStruct { }
  64. private class ExceptionLocation : Exception
  65. {
  66. public ExceptionLocation(IEnumerable<string> locations)
  67. : base(string.Join("\n", locations.Select(s => "in " + s).ToArray()))
  68. {
  69. }
  70. }
  71. private class AsTaskCoroutineExecutor : IEnumerator
  72. {
  73. private readonly TaskCompletionSource<VoidStruct> completionSource;
  74. public AsTaskCoroutineExecutor(IEnumerator coroutine, TaskCompletionSource<VoidStruct> completion)
  75. {
  76. completionSource = completion;
  77. enumerators.Push(coroutine);
  78. }
  79. private readonly Stack<IEnumerator> enumerators = new Stack<IEnumerator>(2);
  80. public object Current => enumerators.FirstOrDefault()?.Current; // effectively a TryPeek
  81. public bool MoveNext()
  82. {
  83. do
  84. {
  85. if (enumerators.Count == 0)
  86. {
  87. completionSource.SetResult(new VoidStruct());
  88. return false;
  89. }
  90. try
  91. {
  92. var top = enumerators.Peek();
  93. if (top.MoveNext())
  94. {
  95. if (top.Current is IEnumerator enumerator)
  96. {
  97. enumerators.Push(enumerator);
  98. continue;
  99. }
  100. else
  101. {
  102. return true;
  103. }
  104. }
  105. else
  106. { // this enumerator completed, so pop it and continue
  107. enumerators.Pop();
  108. continue;
  109. }
  110. }
  111. catch (Exception e)
  112. { // execution errored
  113. completionSource.SetException(new AggregateException(
  114. e, new ExceptionLocation(enumerators.Select(e => e.GetType().ToString()))
  115. ));
  116. return false;
  117. }
  118. }
  119. while (true);
  120. }
  121. public void Reset() => throw new InvalidOperationException();
  122. }
  123. }
  124. }