< Summary

Class:DCL.ABDetectorTracker
Assembly:ABDetectorPlugin
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ABDetectorPlugin/ABDetectorTracker.cs
Covered lines:0
Uncovered lines:70
Coverable lines:70
Total lines:175
Line coverage:0% (0 of 70)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ABDetectorTracker(...)0%2100%
Dispose()0%2100%
FindSceneForPlayer()0%6200%
LoadMaterialsIfNeeded()0%6200%
OnGlobalABDetectionChanged(...)0%6200%
OnSceneABDetectionChanged(...)0%6200%
ApplyGlobalABDetectionPainting()0%12300%
RemoveGlobalABDetectionPainting()0%6200%
ApplyABDetectionPaintingForCurrentScene()0%2100%
RemoveABDetectionPaintingForCurrentScene()0%30500%
ApplyMaterials(...)0%56700%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ABDetectorPlugin/ABDetectorTracker.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using DCL.Controllers;
 4using UnityEngine;
 5
 6namespace DCL
 7{
 8    public class ABDetectorTracker : IDisposable
 9    {
 10        private const string FROM_ASSET_BUNDLE_TAG = "FromAssetBundle";
 11        private const string FROM_RAW_GLTF_TAG = "FromRawGLTF";
 12        private const string AB_DETECTOR_MATERIALS_PREFAB_NAME = "AbDetectorMaterials";
 13
 14        private readonly DebugConfig debugConfig;
 15        private readonly DataStore_Player player;
 16
 017        private readonly Dictionary<Renderer, Material[]> rendererDict  =
 18            new Dictionary<Renderer, Material[]>();
 019        private readonly Multimap<IParcelScene, Renderer> parcelToRendererMultimap =
 20            new Multimap<IParcelScene, Renderer>();
 21
 22        private ABDetectorMaterialsHolder abDetectorMaterialsHolder;
 23        private readonly IWorldState worldState;
 24
 025        public ABDetectorTracker(DebugConfig debugConfig, DataStore_Player player, IWorldState worldState)
 26        {
 027            this.debugConfig = debugConfig;
 028            this.player = player;
 029            this.worldState = worldState;
 30
 031            debugConfig.showGlobalABDetectionLayer.OnChange += OnGlobalABDetectionChanged;
 032            debugConfig.showSceneABDetectionLayer.OnChange += OnSceneABDetectionChanged;
 033        }
 34
 35        public void Dispose()
 36        {
 037            debugConfig.showGlobalABDetectionLayer.OnChange -= OnGlobalABDetectionChanged;
 038            debugConfig.showSceneABDetectionLayer.OnChange -= OnSceneABDetectionChanged;
 039        }
 40
 41        private IParcelScene FindSceneForPlayer()
 42        {
 043            var currentPos = player.playerGridPosition.Get();
 044            if (worldState.TryGetScene(worldState.GetSceneIdByCoords(currentPos),
 45                    out IParcelScene resultScene))
 046                return resultScene;
 47
 048            return null;
 49        }
 50
 51        private void LoadMaterialsIfNeeded()
 52        {
 053            if (abDetectorMaterialsHolder == null)
 54            {
 055                abDetectorMaterialsHolder = Resources.Load<GameObject>
 56                        (AB_DETECTOR_MATERIALS_PREFAB_NAME)
 57                    .GetComponent<ABDetectorMaterialsHolder>();
 58            }
 059        }
 60
 61        private void OnGlobalABDetectionChanged(bool current, bool previous)
 62        {
 063            LoadMaterialsIfNeeded();
 64
 065            if (current)
 66            {
 067                RemoveGlobalABDetectionPainting();
 068                ApplyGlobalABDetectionPainting();
 069            }
 70            else
 71            {
 072                RemoveGlobalABDetectionPainting();
 73            }
 074        }
 75
 76        private void OnSceneABDetectionChanged(bool current, bool previous)
 77        {
 078            LoadMaterialsIfNeeded();
 79
 080            if (current)
 81            {
 082                RemoveGlobalABDetectionPainting();
 083                ApplyABDetectionPaintingForCurrentScene();
 084            }
 85            else
 86            {
 087                RemoveABDetectionPaintingForCurrentScene();
 88            }
 089        }
 90
 91        private void ApplyGlobalABDetectionPainting()
 92        {
 093            var gameObjects = GameObject.FindObjectsOfType(typeof(GameObject));
 094            foreach (var gameObject in gameObjects)
 95            {
 096                var converted = (GameObject) gameObject;
 097                if (converted.transform.parent == null)
 98                {
 099                    ApplyMaterials(converted.transform);
 100                }
 101            }
 0102        }
 103
 104        private void RemoveGlobalABDetectionPainting()
 105        {
 0106            foreach (KeyValuePair<Renderer,Material[]> keyValuePair in rendererDict)
 107            {
 0108                keyValuePair.Key.materials = keyValuePair.Value;
 109            }
 110
 0111            rendererDict.Clear();
 0112            parcelToRendererMultimap.Clear();
 0113        }
 114
 115        private void ApplyABDetectionPaintingForCurrentScene()
 116        {
 0117            var currentScene = FindSceneForPlayer();
 0118            ApplyMaterials(currentScene.GetSceneTransform(), currentScene);
 0119        }
 120
 121        private void RemoveABDetectionPaintingForCurrentScene()
 122        {
 0123            var currentScene = FindSceneForPlayer();
 0124            if (parcelToRendererMultimap.ContainsKey(currentScene))
 125            {
 0126                foreach (var renderer in parcelToRendererMultimap.GetValues(currentScene))
 127                {
 0128                    if (rendererDict.TryGetValue(renderer, out var materials))
 129                    {
 0130                        renderer.materials = materials;
 131                    }
 132
 0133                    rendererDict.Remove(renderer);
 134                }
 135
 0136                parcelToRendererMultimap.Remove(currentScene);
 137            }
 138
 0139        }
 140
 141        private void ApplyMaterials(Transform someTransform, IParcelScene optionalParcelScene = null)
 142        {
 0143            if (someTransform.childCount > 0)
 144            {
 0145                for (int i = 0; i < someTransform.childCount; i++)
 146                {
 0147                    var childTransform = someTransform.GetChild(i).transform;
 0148                    var renderers = childTransform.
 149                        GetComponents<Renderer>();
 150
 0151                    foreach (Renderer renderer in renderers)
 152                    {
 0153                        rendererDict[renderer] = renderer.materials;
 154
 0155                        if (optionalParcelScene != null)
 156                        {
 0157                            parcelToRendererMultimap.Add(optionalParcelScene, renderer);
 158                        }
 159
 0160                        if (renderer.tag.Equals(FROM_ASSET_BUNDLE_TAG))
 161                        {
 0162                            renderer.material = abDetectorMaterialsHolder.ABMaterial;
 0163                        }
 0164                        else if(renderer.tag.Equals(FROM_RAW_GLTF_TAG))
 165                        {
 0166                            renderer.material = abDetectorMaterialsHolder.GLTFMaterial;
 167                        }
 168                    }
 169
 0170                    ApplyMaterials(childTransform, optionalParcelScene);
 171                }
 172            }
 0173        }
 174    }
 175}