< Summary

Class:DebugShapesBoundingBoxDisplayer
Assembly:DebugPlugins_ShapesBoundingBoxDisplayer
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/DebugPlugins/ShapesBoundingBoxDisplayer/DebugShapesBoundingBoxDisplayer.cs
Covered lines:42
Uncovered lines:6
Coverable lines:48
Total lines:115
Line coverage:87.5% (42 of 48)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
DebugShapesBoundingBoxDisplayer(...)0%440100%
DebugShapesBoundingBoxDisplayer()0%2100%
Dispose()0%220100%
KillWatchScene(...)0%3.073080%
WatchScene(...)0%330100%
WatchScene(...)0%3.143075%
IsBoundingBoxEnabledVariableOnOnRemoved(...)0%2100%
IsBoundingBoxEnabledVariableOnOnAdded(...)0%220100%
SceneControllerOnOnNewSceneAdded(...)0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/DebugPlugins/ShapesBoundingBoxDisplayer/DebugShapesBoundingBoxDisplayer.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.Linq;
 3using DCL;
 4using DCL.Controllers;
 5using DCLPlugins.DebugPlugins.Commons;
 6
 7public class DebugShapesBoundingBoxDisplayer : IPlugin
 8{
 9    private readonly IBaseDictionary<string, bool> isBoundingBoxEnabledForScene;
 10    private readonly IWorldState worldState;
 11    private readonly ISceneController sceneController;
 1012    internal readonly Dictionary<string, WatchSceneHandler> scenesWatcher = new Dictionary<string, WatchSceneHandler>();
 1013    internal readonly List<string> pendingScenesId = new List<string>();
 14
 015    public DebugShapesBoundingBoxDisplayer() : this(
 16        DataStore.i.debugConfig.showSceneBoundingBoxes,
 17        Environment.i.world.state, Environment.i.world.sceneController)
 18    {
 019    }
 20
 1021    internal DebugShapesBoundingBoxDisplayer(
 22        IBaseDictionary<string, bool> isBoundingBoxEnabledVariable,
 23        IWorldState state, ISceneController sceneController)
 24    {
 1025        this.isBoundingBoxEnabledForScene = isBoundingBoxEnabledVariable;
 1026        this.worldState = state;
 1027        this.sceneController = sceneController;
 28
 29        // NOTE: we search for scenes that might be added to the variable previous to this class instantiation
 1030        using (var iterator = isBoundingBoxEnabledVariable.Get().GetEnumerator())
 31        {
 2032            while (iterator.MoveNext() && iterator.Current.Value)
 33            {
 1034                IsBoundingBoxEnabledVariableOnOnAdded(iterator.Current.Key, iterator.Current.Value);
 35            }
 1036        }
 37
 1038        isBoundingBoxEnabledVariable.OnAdded += IsBoundingBoxEnabledVariableOnOnAdded;
 1039        isBoundingBoxEnabledVariable.OnRemoved += IsBoundingBoxEnabledVariableOnOnRemoved;
 1040        sceneController.OnNewSceneAdded += SceneControllerOnOnNewSceneAdded;
 1041    }
 42
 43    public void Dispose()
 44    {
 1045        isBoundingBoxEnabledForScene.OnAdded -= IsBoundingBoxEnabledVariableOnOnAdded;
 1046        isBoundingBoxEnabledForScene.OnRemoved -= IsBoundingBoxEnabledVariableOnOnRemoved;
 1047        sceneController.OnNewSceneAdded -= SceneControllerOnOnNewSceneAdded;
 48
 1049        var scenesId = scenesWatcher.Keys.ToArray();
 3650        for (int i = 0; i < scenesId.Length; i++)
 51        {
 852            KillWatchScene(scenesId[i]);
 53        }
 1054    }
 55
 56    private void KillWatchScene(string sceneId)
 57    {
 1058        if (!scenesWatcher.TryGetValue(sceneId, out WatchSceneHandler watchHandler))
 059            return;
 60
 1061        watchHandler?.Dispose();
 1062        scenesWatcher.Remove(sceneId);
 1063    }
 64
 65    private void WatchScene(string sceneId)
 66    {
 67        // NOTE: in case scene is not loaded yet, we add it to the "pending" list
 1068        if (!worldState.loadedScenes.TryGetValue(sceneId, out IParcelScene scene))
 69        {
 170            if (!pendingScenesId.Contains(sceneId))
 71            {
 172                pendingScenesId.Add(sceneId);
 73            }
 74
 175            return;
 76        }
 77
 978        WatchScene(scene);
 979    }
 80
 81    private void WatchScene(IParcelScene scene)
 82    {
 1083        if (scenesWatcher.TryGetValue(scene.sceneData.id, out WatchSceneHandler watchHandler))
 84        {
 085            watchHandler?.Dispose();
 86        }
 87
 1088        scenesWatcher[scene.sceneData.id] = new WatchSceneHandler(scene, new SceneEntitiesTracker());
 1089    }
 90
 91    private void IsBoundingBoxEnabledVariableOnOnRemoved(string sceneId, bool enabled)
 92    {
 093        KillWatchScene(sceneId);
 094    }
 95
 96    private void IsBoundingBoxEnabledVariableOnOnAdded(string sceneId, bool enabled)
 97    {
 1298        if (enabled)
 99        {
 10100            WatchScene(sceneId);
 10101        }
 102        else
 103        {
 2104            KillWatchScene(sceneId);
 105        }
 2106    }
 107
 108    private void SceneControllerOnOnNewSceneAdded(IParcelScene scene)
 109    {
 1110        if (pendingScenesId.Remove(scene.sceneData.id))
 111        {
 1112            WatchScene(scene);
 113        }
 1114    }
 115}