< Summary

Class:DebugShapesBoundingBoxDisplayer
Assembly:DebugPlugins_ShapesBoundingBoxDisplayer
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/DebugPlugins/ShapesBoundingBoxDisplayer/DebugShapesBoundingBoxDisplayer.cs
Covered lines:43
Uncovered lines:6
Coverable lines:49
Total lines:119
Line coverage:87.7% (43 of 49)
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    private readonly IUpdateEventHandler updateEventHandler;
 15
 016    public DebugShapesBoundingBoxDisplayer() : this(
 17        DataStore.i.debugConfig.showSceneBoundingBoxes,
 18        Environment.i.world.state,
 19        Environment.i.world.sceneController,
 020        Environment.i.platform.updateEventHandler) { }
 21
 1022    internal DebugShapesBoundingBoxDisplayer(
 23        IBaseDictionary<string, bool> isBoundingBoxEnabledVariable,
 24        IWorldState state,
 25        ISceneController sceneController,
 26        IUpdateEventHandler updateEventHandler)
 27    {
 1028        this.isBoundingBoxEnabledForScene = isBoundingBoxEnabledVariable;
 1029        this.worldState = state;
 1030        this.sceneController = sceneController;
 1031        this.updateEventHandler = updateEventHandler;
 32
 33        // NOTE: we search for scenes that might be added to the variable previous to this class instantiation
 1034        using (var iterator = isBoundingBoxEnabledVariable.Get().GetEnumerator())
 35        {
 2036            while (iterator.MoveNext() && iterator.Current.Value)
 37            {
 1038                IsBoundingBoxEnabledVariableOnOnAdded(iterator.Current.Key, iterator.Current.Value);
 39            }
 1040        }
 41
 1042        isBoundingBoxEnabledVariable.OnAdded += IsBoundingBoxEnabledVariableOnOnAdded;
 1043        isBoundingBoxEnabledVariable.OnRemoved += IsBoundingBoxEnabledVariableOnOnRemoved;
 1044        sceneController.OnNewSceneAdded += SceneControllerOnOnNewSceneAdded;
 1045    }
 46
 47    public void Dispose()
 48    {
 1049        isBoundingBoxEnabledForScene.OnAdded -= IsBoundingBoxEnabledVariableOnOnAdded;
 1050        isBoundingBoxEnabledForScene.OnRemoved -= IsBoundingBoxEnabledVariableOnOnRemoved;
 1051        sceneController.OnNewSceneAdded -= SceneControllerOnOnNewSceneAdded;
 52
 1053        var scenesId = scenesWatcher.Keys.ToArray();
 3654        for (int i = 0; i < scenesId.Length; i++)
 55        {
 856            KillWatchScene(scenesId[i]);
 57        }
 1058    }
 59
 60    private void KillWatchScene(string sceneId)
 61    {
 1062        if (!scenesWatcher.TryGetValue(sceneId, out WatchSceneHandler watchHandler))
 063            return;
 64
 1065        watchHandler?.Dispose();
 1066        scenesWatcher.Remove(sceneId);
 1067    }
 68
 69    private void WatchScene(string sceneId)
 70    {
 71        // NOTE: in case scene is not loaded yet, we add it to the "pending" list
 1072        if (!worldState.loadedScenes.TryGetValue(sceneId, out IParcelScene scene))
 73        {
 174            if (!pendingScenesId.Contains(sceneId))
 75            {
 176                pendingScenesId.Add(sceneId);
 77            }
 78
 179            return;
 80        }
 81
 982        WatchScene(scene);
 983    }
 84
 85    private void WatchScene(IParcelScene scene)
 86    {
 1087        if (scenesWatcher.TryGetValue(scene.sceneData.id, out WatchSceneHandler watchHandler))
 88        {
 089            watchHandler?.Dispose();
 90        }
 91
 1092        scenesWatcher[scene.sceneData.id] = new WatchSceneHandler(scene, new SceneEntitiesTracker(updateEventHandler));
 1093    }
 94
 95    private void IsBoundingBoxEnabledVariableOnOnRemoved(string sceneId, bool enabled)
 96    {
 097        KillWatchScene(sceneId);
 098    }
 99
 100    private void IsBoundingBoxEnabledVariableOnOnAdded(string sceneId, bool enabled)
 101    {
 12102        if (enabled)
 103        {
 10104            WatchScene(sceneId);
 10105        }
 106        else
 107        {
 2108            KillWatchScene(sceneId);
 109        }
 2110    }
 111
 112    private void SceneControllerOnOnNewSceneAdded(IParcelScene scene)
 113    {
 1114        if (pendingScenesId.Remove(scene.sceneData.id))
 115        {
 1116            WatchScene(scene);
 117        }
 1118    }
 119}