< Summary

Information
Class: Lattice.ECS.Framework.SystemScheduler
Assembly: Lattice
File(s): /home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Framework/SystemScheduler.cs
Line coverage
97%
Covered lines: 86
Uncovered lines: 2
Coverable lines: 88
Total lines: 225
Line coverage: 97.7%
Branch coverage
96%
Covered branches: 29
Total branches: 30
Branch coverage: 96.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
.ctor()100%11100%
get_Count()100%11100%
get_IsInitialized()100%210%
get_Boundary()100%11100%
Add(...)100%66100%
Remove(...)83.33%6687.5%
Clear()100%22100%
Initialize(...)100%44100%
Update(...)100%44100%
Shutdown(...)100%44100%
RebuildExecutionPlan()100%44100%
ValidateContract(...)100%11100%
.ctor(...)100%11100%
get_System()100%11100%
get_Phase()100%11100%
get_RegistrationOrder()100%11100%

File(s)

/home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Framework/SystemScheduler.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using Lattice.ECS.Core;
 4using Lattice.Math;
 5
 6namespace Lattice.ECS.Framework
 7{
 8    /// <summary>
 9    /// 最小系统调度器。
 10    /// 当前正式支持的是单线程、平面、按 phase 再按注册顺序执行的系统集合;
 11    /// 不提供 enable / disable、依赖排序或 `SystemGroup` 层级模型。
 12    /// </summary>
 13    public sealed class SystemScheduler
 14    {
 115        private static readonly SystemSchedulerBoundary FlatPhasedOrderedBoundary = new(
 116            SystemSchedulerKind.FlatPhasedOrdered,
 117            SystemSchedulerCapability.OrderedExecution |
 118            SystemSchedulerCapability.ExplicitLifecycle |
 119            SystemSchedulerCapability.StaticRegistrationBeforeInitialize |
 120            SystemSchedulerCapability.PhasedExecution |
 121            SystemSchedulerCapability.AuthoringContractValidation,
 122            UnsupportedSystemSchedulerCapability.RuntimeMutation |
 123            UnsupportedSystemSchedulerCapability.EnableDisable |
 124            UnsupportedSystemSchedulerCapability.DependencyOrdering |
 125            UnsupportedSystemSchedulerCapability.HierarchicalGrouping |
 126            UnsupportedSystemSchedulerCapability.ThreadedExecution);
 27
 13628        private readonly List<SystemRegistration> _registrations = new();
 13629        private readonly List<SystemRegistration> _executionPlan = new();
 30        private bool _initialized;
 31        private int _nextRegistrationOrder;
 32
 33        /// <summary>
 34        /// 当前已注册系统数量。
 35        /// </summary>
 136        public int Count => _registrations.Count;
 37
 38        /// <summary>
 39        /// 当前是否已完成初始化。
 40        /// </summary>
 041        public bool IsInitialized => _initialized;
 42
 43        /// <summary>
 44        /// 当前系统层正式支持与不支持的调度边界。
 45        /// </summary>
 146        public SystemSchedulerBoundary Boundary => FlatPhasedOrderedBoundary;
 47
 48        /// <summary>
 49        /// 注册系统。
 50        /// 初始化后不允许再动态增删系统;若需要调整系统集合,请先 `Shutdown(frame)`。
 51        /// </summary>
 52        public void Add(ISystem system)
 53        {
 13254            ArgumentNullException.ThrowIfNull(system);
 55
 13156            if (_initialized)
 57            {
 158                throw new InvalidOperationException("Cannot add systems while scheduler is initialized. Call Shutdown(fr
 59            }
 60
 41861            for (int i = 0; i < _registrations.Count; i++)
 62            {
 8063                if (ReferenceEquals(_registrations[i].System, system))
 64                {
 165                    throw new InvalidOperationException("System is already registered.");
 66                }
 67            }
 68
 12969            ValidateContract(system);
 12870            _registrations.Add(new SystemRegistration(system, _nextRegistrationOrder++));
 12871        }
 72
 73        /// <summary>
 74        /// 移除系统。
 75        /// 初始化后不允许再动态增删系统;若需要调整系统集合,请先 `Shutdown(frame)`。
 76        /// </summary>
 77        public bool Remove(ISystem system)
 78        {
 379            ArgumentNullException.ThrowIfNull(system);
 80
 381            if (_initialized)
 82            {
 183                throw new InvalidOperationException("Cannot remove systems while scheduler is initialized. Call Shutdown
 84            }
 85
 686            for (int i = 0; i < _registrations.Count; i++)
 87            {
 388                if (ReferenceEquals(_registrations[i].System, system))
 89                {
 290                    _registrations.RemoveAt(i);
 291                    return true;
 92                }
 93            }
 94
 095            return false;
 96        }
 97
 98        /// <summary>
 99        /// 清空系统列表。
 100        /// 必须在未初始化状态下调用;若系统已初始化,请先执行 Shutdown(frame)。
 101        /// </summary>
 102        public void Clear()
 103        {
 2104            if (_initialized)
 105            {
 1106                throw new InvalidOperationException("Cannot clear systems while scheduler is initialized. Call Shutdown(
 107            }
 108
 1109            _registrations.Clear();
 1110            _executionPlan.Clear();
 1111        }
 112
 113        /// <summary>
 114        /// 初始化所有系统。
 115        /// 初始化后,当前系统集合会被冻结,直到显式执行 `Shutdown(frame)`。
 116        /// </summary>
 117        public void Initialize(Frame frame)
 118        {
 87119            ArgumentNullException.ThrowIfNull(frame);
 120
 87121            if (_initialized)
 122            {
 1123                return;
 124            }
 125
 86126            RebuildExecutionPlan();
 127
 406128            for (int i = 0; i < _executionPlan.Count; i++)
 129            {
 117130                _executionPlan[i].System.OnInit(frame);
 131            }
 132
 86133            _initialized = true;
 86134        }
 135
 136        /// <summary>
 137        /// 按 phase 再按注册顺序更新所有系统。
 138        /// </summary>
 139        public void Update(Frame frame, FP deltaTime)
 140        {
 657141            ArgumentNullException.ThrowIfNull(frame);
 142
 657143            if (!_initialized)
 144            {
 1145                throw new InvalidOperationException("SystemScheduler must be initialized before update.");
 146            }
 147
 3314148            for (int i = 0; i < _executionPlan.Count; i++)
 149            {
 1004150                ISystem system = _executionPlan[i].System;
 1004151                frame.BeginSystemAuthoringScope(system);
 152                try
 153                {
 1004154                    system.OnUpdate(frame, deltaTime);
 1001155                }
 156                finally
 157                {
 1004158                    frame.EndSystemAuthoringScope();
 1004159                }
 160            }
 653161        }
 162
 163        /// <summary>
 164        /// 关闭所有系统。
 165        /// </summary>
 166        public void Shutdown(Frame frame)
 167        {
 72168            ArgumentNullException.ThrowIfNull(frame);
 169
 72170            if (!_initialized)
 171            {
 1172                return;
 173            }
 174
 330175            for (int i = 0; i < _executionPlan.Count; i++)
 176            {
 94177                _executionPlan[i].System.OnDestroy(frame);
 178            }
 179
 71180            _initialized = false;
 71181        }
 182
 183        private void RebuildExecutionPlan()
 184        {
 86185            _executionPlan.Clear();
 186
 86187            if (_registrations.Count == 0)
 188            {
 10189                return;
 190            }
 191
 76192            _executionPlan.AddRange(_registrations);
 76193            _executionPlan.Sort(static (left, right) =>
 76194            {
 48195                int phaseComparison = left.Phase.CompareTo(right.Phase);
 48196                if (phaseComparison != 0)
 76197                {
 28198                    return phaseComparison;
 76199                }
 76200
 20201                return left.RegistrationOrder.CompareTo(right.RegistrationOrder);
 76202            });
 76203        }
 204
 205        private static void ValidateContract(ISystem system)
 206        {
 129207            system.Contract.ValidateForPhase(system.Phase, system.GetType());
 128208        }
 209
 210        private readonly struct SystemRegistration
 211        {
 212            public SystemRegistration(ISystem system, int registrationOrder)
 213            {
 128214                System = system;
 128215                RegistrationOrder = registrationOrder;
 128216            }
 217
 1394218            public ISystem System { get; }
 219
 96220            public SystemPhase Phase => System.Phase;
 221
 40222            public int RegistrationOrder { get; }
 223        }
 224    }
 225}