AsyncOperationBase.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. using System;
  2. using System.Diagnostics;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Threading.Tasks;
  6. namespace YooAsset
  7. {
  8. public abstract class AsyncOperationBase : IEnumerator, IComparable<AsyncOperationBase>
  9. {
  10. private Action<AsyncOperationBase> _callback;
  11. private string _packageName = null;
  12. private int _whileFrame = 1000;
  13. /// <summary>
  14. /// 所有子任务
  15. /// </summary>
  16. internal readonly List<AsyncOperationBase> Childs = new List<AsyncOperationBase>(10);
  17. /// <summary>
  18. /// 等待异步执行完成
  19. /// </summary>
  20. internal bool IsWaitForAsyncComplete { private set; get; } = false;
  21. /// <summary>
  22. /// 是否已经完成
  23. /// </summary>
  24. internal bool IsFinish { private set; get; } = false;
  25. /// <summary>
  26. /// 任务优先级
  27. /// </summary>
  28. public uint Priority { set; get; } = 0;
  29. /// <summary>
  30. /// 任务状态
  31. /// </summary>
  32. public EOperationStatus Status { get; protected set; } = EOperationStatus.None;
  33. /// <summary>
  34. /// 错误信息
  35. /// </summary>
  36. public string Error { get; protected set; }
  37. /// <summary>
  38. /// 处理进度
  39. /// </summary>
  40. public float Progress { get; protected set; }
  41. /// <summary>
  42. /// 所属包裹名称
  43. /// </summary>
  44. public string PackageName
  45. {
  46. get
  47. {
  48. return _packageName;
  49. }
  50. }
  51. /// <summary>
  52. /// 是否已经完成
  53. /// </summary>
  54. public bool IsDone
  55. {
  56. get
  57. {
  58. return Status == EOperationStatus.Failed || Status == EOperationStatus.Succeed;
  59. }
  60. }
  61. /// <summary>
  62. /// 完成事件
  63. /// </summary>
  64. public event Action<AsyncOperationBase> Completed
  65. {
  66. add
  67. {
  68. if (IsDone)
  69. value.Invoke(this);
  70. else
  71. _callback += value;
  72. }
  73. remove
  74. {
  75. _callback -= value;
  76. }
  77. }
  78. /// <summary>
  79. /// 异步操作任务
  80. /// </summary>
  81. public Task Task
  82. {
  83. get
  84. {
  85. if (_taskCompletionSource == null)
  86. {
  87. _taskCompletionSource = new TaskCompletionSource<object>();
  88. if (IsDone)
  89. _taskCompletionSource.SetResult(null);
  90. }
  91. return _taskCompletionSource.Task;
  92. }
  93. }
  94. internal abstract void InternalStart();
  95. internal abstract void InternalUpdate();
  96. internal virtual void InternalAbort()
  97. {
  98. }
  99. internal virtual void InternalWaitForAsyncComplete()
  100. {
  101. throw new System.NotImplementedException(this.GetType().Name);
  102. }
  103. internal virtual string InternalGetDesc()
  104. {
  105. return string.Empty;
  106. }
  107. /// <summary>
  108. /// 设置包裹名称
  109. /// </summary>
  110. internal void SetPackageName(string packageName)
  111. {
  112. _packageName = packageName;
  113. }
  114. /// <summary>
  115. /// 添加子任务
  116. /// </summary>
  117. internal void AddChildOperation(AsyncOperationBase child)
  118. {
  119. #if UNITY_EDITOR
  120. if (Childs.Contains(child))
  121. throw new Exception($"The child node {child.GetType().Name} already exists !");
  122. #endif
  123. Childs.Add(child);
  124. }
  125. /// <summary>
  126. /// 获取异步操作说明
  127. /// </summary>
  128. internal string GetOperationDesc()
  129. {
  130. return InternalGetDesc();
  131. }
  132. /// <summary>
  133. /// 开始异步操作
  134. /// </summary>
  135. internal void StartOperation()
  136. {
  137. if (Status == EOperationStatus.None)
  138. {
  139. Status = EOperationStatus.Processing;
  140. // 开始记录
  141. DebugBeginRecording();
  142. // 开始任务
  143. InternalStart();
  144. }
  145. }
  146. /// <summary>
  147. /// 更新异步操作
  148. /// </summary>
  149. internal void UpdateOperation()
  150. {
  151. if (IsDone == false)
  152. {
  153. // 更新记录
  154. DebugUpdateRecording();
  155. // 更新任务
  156. InternalUpdate();
  157. }
  158. if (IsDone && IsFinish == false)
  159. {
  160. IsFinish = true;
  161. // 进度百分百完成
  162. Progress = 1f;
  163. // 结束记录
  164. DebugEndRecording();
  165. //注意:如果完成回调内发生异常,会导致Task无限期等待
  166. _callback?.Invoke(this);
  167. if (_taskCompletionSource != null)
  168. _taskCompletionSource.TrySetResult(null);
  169. }
  170. }
  171. /// <summary>
  172. /// 终止异步任务
  173. /// </summary>
  174. internal void AbortOperation()
  175. {
  176. foreach (var child in Childs)
  177. {
  178. child.AbortOperation();
  179. }
  180. if (IsDone == false)
  181. {
  182. Status = EOperationStatus.Failed;
  183. Error = "user abort";
  184. YooLogger.Warning($"Async operaiton {this.GetType().Name} has been abort !");
  185. InternalAbort();
  186. }
  187. }
  188. /// <summary>
  189. /// 执行While循环
  190. /// </summary>
  191. protected bool ExecuteWhileDone()
  192. {
  193. if (IsDone == false)
  194. {
  195. // 执行更新逻辑
  196. InternalUpdate();
  197. // 当执行次数用完时
  198. _whileFrame--;
  199. if (_whileFrame <= 0)
  200. {
  201. Status = EOperationStatus.Failed;
  202. Error = $"Operation {this.GetType().Name} failed to wait for async complete !";
  203. YooLogger.Error(Error);
  204. }
  205. }
  206. return IsDone;
  207. }
  208. /// <summary>
  209. /// 清空完成回调
  210. /// </summary>
  211. protected void ClearCompletedCallback()
  212. {
  213. _callback = null;
  214. }
  215. /// <summary>
  216. /// 等待异步执行完毕
  217. /// </summary>
  218. public void WaitForAsyncComplete()
  219. {
  220. if (IsDone)
  221. return;
  222. //TODO 防止异步操作被挂起陷入无限死循环!
  223. // 例如:文件解压任务或者文件导入任务!
  224. if (Status == EOperationStatus.None)
  225. {
  226. StartOperation();
  227. }
  228. IsWaitForAsyncComplete = true;
  229. InternalWaitForAsyncComplete();
  230. }
  231. #region 调试信息
  232. /// <summary>
  233. /// 开始的时间
  234. /// </summary>
  235. public string BeginTime = string.Empty;
  236. /// <summary>
  237. /// 处理耗时(单位:毫秒)
  238. /// </summary>
  239. public long ProcessTime { protected set; get; }
  240. // 加载耗时统计
  241. private Stopwatch _watch = null;
  242. [Conditional("DEBUG")]
  243. private void DebugBeginRecording()
  244. {
  245. if (_watch == null)
  246. {
  247. BeginTime = SpawnTimeToString(UnityEngine.Time.realtimeSinceStartup);
  248. _watch = Stopwatch.StartNew();
  249. }
  250. }
  251. [Conditional("DEBUG")]
  252. private void DebugUpdateRecording()
  253. {
  254. if (_watch != null)
  255. {
  256. ProcessTime = _watch.ElapsedMilliseconds;
  257. }
  258. }
  259. [Conditional("DEBUG")]
  260. private void DebugEndRecording()
  261. {
  262. if (_watch != null)
  263. {
  264. ProcessTime = _watch.ElapsedMilliseconds;
  265. _watch = null;
  266. }
  267. }
  268. private string SpawnTimeToString(float spawnTime)
  269. {
  270. float h = UnityEngine.Mathf.FloorToInt(spawnTime / 3600f);
  271. float m = UnityEngine.Mathf.FloorToInt(spawnTime / 60f - h * 60f);
  272. float s = UnityEngine.Mathf.FloorToInt(spawnTime - m * 60f - h * 3600f);
  273. return h.ToString("00") + ":" + m.ToString("00") + ":" + s.ToString("00");
  274. }
  275. #endregion
  276. #region 排序接口实现
  277. public int CompareTo(AsyncOperationBase other)
  278. {
  279. return other.Priority.CompareTo(this.Priority);
  280. }
  281. #endregion
  282. #region 异步编程相关
  283. bool IEnumerator.MoveNext()
  284. {
  285. return !IsDone;
  286. }
  287. void IEnumerator.Reset()
  288. {
  289. }
  290. object IEnumerator.Current => null;
  291. private TaskCompletionSource<object> _taskCompletionSource;
  292. #endregion
  293. }
  294. }