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.

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