< Summary

Class:GDX.Threading.TaskDirectorSystem
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Threading/TaskDirectorSystem.cs
Covered lines:53
Uncovered lines:0
Coverable lines:53
Total lines:158
Line coverage:100% (53 of 53)
Covered branches:0
Total branches:0
Covered methods:7
Total methods:7
Method coverage:100% (7 of 7)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
TaskDirectorSystem()0%110100%
AddToPlayerLoop()0%330100%
GetTickRate()0%110100%
RemoveFromPlayerLoop()0%2.042077.78%
SetTickRate(...)0%330100%
Initialize()0%330100%
PlayerLoopTick()0%4.24076.92%

File(s)

./Packages/com.dotbunny.gdx/GDX/Threading/TaskDirectorSystem.cs

#LineLine coverage
 1// Copyright (c) 2020-2024 dotBunny Inc.
 2// dotBunny licenses this file to you under the BSL-1.0 license.
 3// See the LICENSE file in the project root for more information.
 4
 5using System;
 6using UnityEngine;
 7using UnityEngine.LowLevel;
 8using UnityEngine.PlayerLoop;
 9
 10namespace GDX.Threading
 11{
 12    public static class TaskDirectorSystem
 13    {
 14        /// <summary>
 15        ///     Has the task director been added to the player loop for updating at runtime.
 16        /// </summary>
 17        static bool s_AddedToPlayerLoop;
 18
 19        /// <summary>
 20        ///     How often should the <see cref="TaskDirector" /> be ticked?
 21        /// </summary>
 22        /// <remarks>
 23        ///     This works by accumulation, when time between the last tick and current time exceeds
 24        ///     <see cref="s_TickRate" />, a tick will be triggered. Default configured in <see cref="Config" />.
 25        /// </remarks>
 126        static float s_TickRate = -1;
 27
 28        /// <summary>
 29        ///     An accumulation of time since the last tick.
 30        /// </summary>
 31        static float s_TimeSinceLastTick;
 32
 33        /// <summary>
 34        ///     Triggered after the <see cref="TaskDirectorSystem" /> has ticked, with the delta time.
 35        /// </summary>
 36        public static Action<float> ticked;
 37
 38        public static void AddToPlayerLoop()
 839        {
 840            if (s_AddedToPlayerLoop || !Config.TaskDirectorSystem)
 541            {
 542                return;
 43            }
 44#if !UNITY_EDITOR
 45            Debug.Log("GDX runtime task scheduler activated.");
 46#endif // !UNITY_EDITOR
 47
 348            PlayerLoopSystem systemRoot = PlayerLoop.GetCurrentPlayerLoop();
 349            systemRoot.AddSubSystemToFirstSubSystemOfType(
 50                typeof(Update.ScriptRunDelayedTasks),
 51                typeof(TaskDirectorSystem), PlayerLoopTick);
 352            PlayerLoop.SetPlayerLoop(systemRoot);
 53
 354            s_AddedToPlayerLoop = true;
 855        }
 56
 57        /// <summary>
 58        ///     Get the current tick rate used by the <see cref="TaskDirectorSystem" />.
 59        /// </summary>
 60        /// <returns>
 61        ///     A double value representing the elapsed time necessary to trigger an update to the
 62        ///     <see cref="TaskDirectorSystem" />.
 63        /// </returns>
 64        public static float GetTickRate()
 865        {
 866            return s_TickRate;
 867        }
 68
 69        public static void RemoveFromPlayerLoop()
 570        {
 571            if (!s_AddedToPlayerLoop)
 272            {
 273                return;
 74            }
 75
 376            PlayerLoopSystem systemRoot = PlayerLoop.GetCurrentPlayerLoop();
 377            systemRoot.RemoveSubSystemsOfTypeFromFirstSubSystemOfType(typeof(Update.ScriptRunDelayedTasks),
 78                typeof(TaskDirectorSystem));
 379            PlayerLoop.SetPlayerLoop(systemRoot);
 380            s_AddedToPlayerLoop = false;
 581        }
 82
 83        /// <summary>
 84        ///     Update the rate at which the <see cref="TaskDirectorSystem" /> updates the <see cref="TaskDirector" />.
 85        /// </summary>
 86        /// <remarks>
 87        ///     This will not survive domain reload, please see <see cref="Config.TaskDirectorSystemTickRate" />.
 88        /// </remarks>
 89        /// <param name="tickRate">The new tick rate.</param>
 90        public static void SetTickRate(float tickRate)
 1191        {
 1192            s_TickRate = tickRate;
 93#if UNITY_EDITOR
 1194            if (s_TickRate >= 0 && !Config.TaskDirectorSystem)
 195            {
 196                Debug.LogWarning("Tick rate set whilst TaskDirectorSystem has been configured off.");
 197            }
 98#endif // UNITY_EDITOR
 99
 100            // If for some reason the tick rate is set at runtime
 101#if !UNITY_EDITOR
 102            if (tickRate >= 0)
 103            {
 104                AddToPlayerLoop();
 105            }
 106            else
 107            {
 108                RemoveFromPlayerLoop();
 109            }
 110#endif // !UNITY_EDITOR
 11111        }
 112
 113        /// <summary>
 114        ///     Sets up some default state for the <see cref="TaskDirectorSystem" />.
 115        /// </summary>
 116        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
 117        static void Initialize()
 5118        {
 119            // Because invoke order is not deterministic, we cheat a bit and make sure that the core gets called
 120            // regardless from this system. This pattern probably needs to happen in other cases like this.
 5121            Core.InitializeOnMainThread();
 122
 5123            if (Config.TaskDirectorSystem)
 3124            {
 3125                if (s_TickRate < 0)
 1126                {
 1127                    s_TickRate = Config.TaskDirectorSystemTickRate;
 1128                }
 129
 3130                AddToPlayerLoop();
 3131            }
 5132        }
 133
 134        /// <summary>
 135        ///     The internal update call to the <see cref="TaskDirector" />.
 136        /// </summary>
 137        static void PlayerLoopTick()
 2867138        {
 139#if UNITY_EDITOR
 2867140            if (!Application.isPlaying)
 1141            {
 1142                Debug.LogWarning("Unable to tick Task Director from PlayerLoop outside of PlayMode.");
 1143                return;
 144            }
 145#endif // UNITY_EDITOR
 146
 2866147            s_TimeSinceLastTick += Time.deltaTime;
 2866148            if (s_TimeSinceLastTick < s_TickRate)
 2845149            {
 2845150                return;
 151            }
 152
 21153            TaskDirector.Tick();
 21154            ticked?.Invoke(s_TimeSinceLastTick);
 21155            s_TimeSinceLastTick = 0;
 2867156        }
 157    }
 158}

Coverage by test methods









Methods/Properties

TaskDirectorSystem()
AddToPlayerLoop()
GetTickRate()
RemoveFromPlayerLoop()
SetTickRate(System.Single)
Initialize()
PlayerLoopTick()