< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
DebugShapesBoundingBoxDisplayer(...)0%20400%
DebugShapesBoundingBoxDisplayer()0%2100%
Dispose()0%6200%
KillWatchScene(...)0%12300%
WatchScene(...)0%12300%
WatchScene(...)0%12300%
IsBoundingBoxEnabledVariableOnOnRemoved(...)0%2100%
IsBoundingBoxEnabledVariableOnOnAdded(...)0%6200%
SceneControllerOnOnNewSceneAdded(...)0%6200%

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;
 012    internal readonly Dictionary<string, WatchSceneHandler> scenesWatcher = new Dictionary<string, WatchSceneHandler>();
 013    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
 022    internal DebugShapesBoundingBoxDisplayer(
 23        IBaseDictionary<string, bool> isBoundingBoxEnabledVariable,
 24        IWorldState state,
 25        ISceneController sceneController,
 26        IUpdateEventHandler updateEventHandler)
 27    {
 028        this.isBoundingBoxEnabledForScene = isBoundingBoxEnabledVariable;
 029        this.worldState = state;
 030        this.sceneController = sceneController;
 031        this.updateEventHandler = updateEventHandler;
 32
 33        // NOTE: we search for scenes that might be added to the variable previous to this class instantiation
 034        using (var iterator = isBoundingBoxEnabledVariable.Get().GetEnumerator())
 35        {
 036            while (iterator.MoveNext() && iterator.Current.Value)
 37            {
 038                IsBoundingBoxEnabledVariableOnOnAdded(iterator.Current.Key, iterator.Current.Value);
 39            }
 040        }
 41
 042        isBoundingBoxEnabledVariable.OnAdded += IsBoundingBoxEnabledVariableOnOnAdded;
 043        isBoundingBoxEnabledVariable.OnRemoved += IsBoundingBoxEnabledVariableOnOnRemoved;
 044        sceneController.OnNewSceneAdded += SceneControllerOnOnNewSceneAdded;
 045    }
 46
 47    public void Dispose()
 48    {
 049        isBoundingBoxEnabledForScene.OnAdded -= IsBoundingBoxEnabledVariableOnOnAdded;
 050        isBoundingBoxEnabledForScene.OnRemoved -= IsBoundingBoxEnabledVariableOnOnRemoved;
 051        sceneController.OnNewSceneAdded -= SceneControllerOnOnNewSceneAdded;
 52
 053        var scenesId = scenesWatcher.Keys.ToArray();
 054        for (int i = 0; i < scenesId.Length; i++)
 55        {
 056            KillWatchScene(scenesId[i]);
 57        }
 058    }
 59
 60    private void KillWatchScene(string sceneId)
 61    {
 062        if (!scenesWatcher.TryGetValue(sceneId, out WatchSceneHandler watchHandler))
 063            return;
 64
 065        watchHandler?.Dispose();
 066        scenesWatcher.Remove(sceneId);
 067    }
 68
 69    private void WatchScene(string sceneId)
 70    {
 71        // NOTE: in case scene is not loaded yet, we add it to the "pending" list
 072        if (!worldState.TryGetScene(sceneId, out IParcelScene scene))
 73        {
 074            if (!pendingScenesId.Contains(sceneId))
 75            {
 076                pendingScenesId.Add(sceneId);
 77            }
 78
 079            return;
 80        }
 81
 082        WatchScene(scene);
 083    }
 84
 85    private void WatchScene(IParcelScene scene)
 86    {
 087        if (scenesWatcher.TryGetValue(scene.sceneData.id, out WatchSceneHandler watchHandler))
 88        {
 089            watchHandler?.Dispose();
 90        }
 91
 092        scenesWatcher[scene.sceneData.id] = new WatchSceneHandler(scene, new SceneEntitiesTracker(updateEventHandler));
 093    }
 94
 95    private void IsBoundingBoxEnabledVariableOnOnRemoved(string sceneId, bool enabled)
 96    {
 097        KillWatchScene(sceneId);
 098    }
 99
 100    private void IsBoundingBoxEnabledVariableOnOnAdded(string sceneId, bool enabled)
 101    {
 0102        if (enabled)
 103        {
 0104            WatchScene(sceneId);
 0105        }
 106        else
 107        {
 0108            KillWatchScene(sceneId);
 109        }
 0110    }
 111
 112    private void SceneControllerOnOnNewSceneAdded(IParcelScene scene)
 113    {
 0114        if (pendingScenesId.Remove(scene.sceneData.id))
 115        {
 0116            WatchScene(scene);
 117        }
 0118    }
 119}