< Summary

Class:GDX.Threading.TaskDirectorSystem
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Threading/TaskDirectorSystem.cs
Covered lines:49
Uncovered lines:0
Coverable lines:49
Total lines:154
Line coverage:100% (49 of 49)
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.012087.5%
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-2023 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 System.Diagnostics;
 7using GDX.Experimental;
 8using GDX.Experimental.Logging;
 9using UnityEngine;
 10using UnityEngine.LowLevel;
 11using UnityEngine.PlayerLoop;
 12
 13namespace GDX.Threading
 14{
 15    public static class TaskDirectorSystem
 16    {
 17        /// <summary>
 18        ///     Has the task director been added to the player loop for updating at runtime.
 19        /// </summary>
 20        static bool s_AddedToPlayerLoop;
 21
 22        /// <summary>
 23        ///     How often should the <see cref="TaskDirector"/> be ticked?
 24        /// </summary>
 25        /// <remarks>
 26        ///     This works by accumulation, when time between the last tick and current time exceeds
 27        ///     <see cref="s_TickRate"/>, a tick will be triggered. Default configured in <see cref="Config"/>.
 28        /// </remarks>
 129        static float s_TickRate = -1;
 30
 31        /// <summary>
 32        ///     An accumulation of time since the last tick.
 33        /// </summary>
 34        static float s_TimeSinceLastTick;
 35
 36        /// <summary>
 37        ///     Triggered after the <see cref="TaskDirectorSystem"/> has ticked, with the delta time.
 38        /// </summary>
 39        public static Action<float> ticked;
 40
 41        public static void AddToPlayerLoop()
 842        {
 1343            if (s_AddedToPlayerLoop || !Config.TaskDirectorSystem) return;
 44#if !UNITY_EDITOR
 45            ManagedLog.Info(LogCategory.GDX, "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        {
 771            if (!s_AddedToPlayerLoop) return;
 372            PlayerLoopSystem systemRoot = PlayerLoop.GetCurrentPlayerLoop();
 373            systemRoot.RemoveSubSystemsOfTypeFromFirstSubSystemOfType(typeof(Update.ScriptRunDelayedTasks),
 74                typeof(TaskDirectorSystem));
 375            PlayerLoop.SetPlayerLoop(systemRoot);
 376            s_AddedToPlayerLoop = false;
 577        }
 78
 79        /// <summary>
 80        ///     Update the rate at which the <see cref="TaskDirectorSystem"/> updates the <see cref="TaskDirector"/>.
 81        /// </summary>
 82        /// <remarks>
 83        ///     This will not survive domain reload, please see <see cref="Config.TaskDirectorSystemTickRate"/>.
 84        /// </remarks>
 85        /// <param name="tickRate">The new tick rate.</param>
 86        public static void SetTickRate(float tickRate)
 1187        {
 1188            s_TickRate = tickRate;
 89#if UNITY_EDITOR
 1190            if (s_TickRate >= 0 && !Config.TaskDirectorSystem)
 191            {
 192                ManagedLog.Warning(LogCategory.GDX, "Tick rate set whilst TaskDirectorSystem has been configured off.");
 193            }
 94#endif // UNITY_EDITOR
 95
 96            // If for some reason the tick rate is set at runtime
 97#if !UNITY_EDITOR
 98            if (tickRate >= 0)
 99            {
 100                AddToPlayerLoop();
 101            }
 102            else
 103            {
 104                RemoveFromPlayerLoop();
 105            }
 106#endif // !UNITY_EDITOR
 11107        }
 108
 109        /// <summary>
 110        ///     Sets up some default state for the <see cref="TaskDirectorSystem"/>.
 111        /// </summary>
 112        [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterAssembliesLoaded)]
 113        static void Initialize()
 5114        {
 115            // Because invoke order is not deterministic, we cheat a bit and make sure that the core gets called
 116            // regardless from this system. This pattern probably needs to happen in other cases like this.
 5117            Core.InitializeOnMainThread();
 118
 5119            if (Config.TaskDirectorSystem)
 3120            {
 3121                if (s_TickRate < 0)
 1122                {
 1123                    s_TickRate = Config.TaskDirectorSystemTickRate;
 1124                }
 125
 3126                AddToPlayerLoop();
 3127            }
 5128        }
 129
 130        /// <summary>
 131        ///     The internal update call to the <see cref="TaskDirector"/>.
 132        /// </summary>
 133        static void PlayerLoopTick()
 2814134        {
 135#if UNITY_EDITOR
 2814136            if (!Application.isPlaying)
 1137            {
 1138                ManagedLog.Warning(LogCategory.GDX, "Unable to tick Task Director from PlayerLoop outside of PlayMode.")
 1139                return;
 140            }
 141#endif // UNITY_EDITOR
 142
 2813143            s_TimeSinceLastTick += Time.deltaTime;
 2813144            if (s_TimeSinceLastTick < s_TickRate)
 2792145            {
 2792146                return;
 147            }
 148
 21149            TaskDirector.Tick();
 21150            ticked?.Invoke(s_TimeSinceLastTick);
 21151            s_TimeSinceLastTick = 0;
 2814152        }
 153    }
 154}

Coverage by test methods









Methods/Properties

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