< 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:48
Coverable lines:48
Total lines:109
Line coverage:0% (0 of 48)
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<int, bool> isBoundingBoxEnabledForScene;
 10    private readonly IWorldState worldState;
 11    private readonly ISceneController sceneController;
 012    internal readonly Dictionary<int, WatchSceneHandler> scenesWatcher = new Dictionary<int, WatchSceneHandler>();
 013    internal readonly List<int> pendingSceneNumbers = new List<int>();
 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<int, 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(int sceneNumber)
 61    {
 062        if (!scenesWatcher.TryGetValue(sceneNumber, out WatchSceneHandler watchHandler))
 063            return;
 64
 065        watchHandler?.Dispose();
 066        scenesWatcher.Remove(sceneNumber);
 067    }
 68
 69    private void WatchScene(int sceneNumber)
 70    {
 71        // NOTE: in case scene is not loaded yet, we add it to the "pending" list
 072        if (!worldState.TryGetScene(sceneNumber, out IParcelScene scene))
 73        {
 074            if (!pendingSceneNumbers.Contains(sceneNumber))
 075                pendingSceneNumbers.Add(sceneNumber);
 76
 077            return;
 78        }
 79
 080        WatchScene(scene);
 081    }
 82
 83    private void WatchScene(IParcelScene scene)
 84    {
 085        if (scenesWatcher.TryGetValue(scene.sceneData.sceneNumber, out WatchSceneHandler watchHandler))
 086            watchHandler?.Dispose();
 87
 088        scenesWatcher[scene.sceneData.sceneNumber] = new WatchSceneHandler(scene, new SceneEntitiesTracker(updateEventHa
 089    }
 90
 91    private void IsBoundingBoxEnabledVariableOnOnRemoved(int sceneNumber, bool enabled)
 92    {
 093        KillWatchScene(sceneNumber);
 094    }
 95
 96    private void IsBoundingBoxEnabledVariableOnOnAdded(int sceneNumber, bool enabled)
 97    {
 098        if (enabled)
 099            WatchScene(sceneNumber);
 100        else
 0101            KillWatchScene(sceneNumber);
 0102    }
 103
 104    private void SceneControllerOnOnNewSceneAdded(IParcelScene scene)
 105    {
 0106        if (pendingSceneNumbers.Remove(scene.sceneData.sceneNumber))
 0107            WatchScene(scene);
 0108    }
 109}