OperationSystem.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. namespace YooAsset
  5. {
  6. internal class OperationSystem
  7. {
  8. private static readonly List<AsyncOperationBase> _operations = new List<AsyncOperationBase>(1000);
  9. private static readonly List<AsyncOperationBase> _newList = new List<AsyncOperationBase>(1000);
  10. // 计时器相关
  11. private static Stopwatch _watch;
  12. private static long _frameTime;
  13. /// <summary>
  14. /// 异步操作的最小时间片段
  15. /// </summary>
  16. public static long MaxTimeSlice { set; get; } = long.MaxValue;
  17. /// <summary>
  18. /// 处理器是否繁忙
  19. /// </summary>
  20. public static bool IsBusy
  21. {
  22. get
  23. {
  24. return _watch.ElapsedMilliseconds - _frameTime >= MaxTimeSlice;
  25. }
  26. }
  27. /// <summary>
  28. /// 初始化异步操作系统
  29. /// </summary>
  30. public static void Initialize()
  31. {
  32. _watch = Stopwatch.StartNew();
  33. }
  34. /// <summary>
  35. /// 更新异步操作系统
  36. /// </summary>
  37. public static void Update()
  38. {
  39. _frameTime = _watch.ElapsedMilliseconds;
  40. // 添加新增的异步操作
  41. if (_newList.Count > 0)
  42. {
  43. bool sorting = false;
  44. foreach (var operation in _newList)
  45. {
  46. if (operation.Priority > 0)
  47. {
  48. sorting = true;
  49. break;
  50. }
  51. }
  52. _operations.AddRange(_newList);
  53. _newList.Clear();
  54. // 重新排序优先级
  55. if (sorting)
  56. _operations.Sort();
  57. }
  58. // 更新进行中的异步操作
  59. for (int i = 0; i < _operations.Count; i++)
  60. {
  61. if (IsBusy)
  62. break;
  63. var operation = _operations[i];
  64. if (operation.IsFinish)
  65. continue;
  66. if (operation.IsDone == false)
  67. operation.InternalOnUpdate();
  68. if (operation.IsDone)
  69. operation.SetFinish();
  70. }
  71. // 移除已经完成的异步操作
  72. for (int i = _operations.Count - 1; i >= 0; i--)
  73. {
  74. var operation = _operations[i];
  75. if (operation.IsFinish)
  76. _operations.RemoveAt(i);
  77. }
  78. }
  79. /// <summary>
  80. /// 销毁异步操作系统
  81. /// </summary>
  82. public static void DestroyAll()
  83. {
  84. _operations.Clear();
  85. _newList.Clear();
  86. _watch = null;
  87. _frameTime = 0;
  88. MaxTimeSlice = long.MaxValue;
  89. }
  90. /// <summary>
  91. /// 销毁包裹的所有任务
  92. /// </summary>
  93. public static void ClearPackageOperation(string packageName)
  94. {
  95. // 终止临时队列里的任务
  96. foreach (var operation in _newList)
  97. {
  98. if (operation.PackageName == packageName)
  99. {
  100. operation.SetAbort();
  101. }
  102. }
  103. // 终止正在进行的任务
  104. foreach (var operation in _operations)
  105. {
  106. if (operation.PackageName == packageName)
  107. {
  108. operation.SetAbort();
  109. }
  110. }
  111. }
  112. /// <summary>
  113. /// 开始处理异步操作类
  114. /// </summary>
  115. public static void StartOperation(string packageName, AsyncOperationBase operation)
  116. {
  117. _newList.Add(operation);
  118. operation.SetPackageName(packageName);
  119. operation.SetStart();
  120. }
  121. /// <summary>
  122. /// 开始处理异步操作类
  123. /// </summary>
  124. public static void StartOperation(AsyncOperationBase operation)
  125. {
  126. _newList.Add(operation);
  127. operation.SetStart();
  128. }
  129. }
  130. }