< Summary

Class:DCLServices.MapRendererV2.MapRenderer
Assembly:MapRendererV2
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/MapRendererV2/MapRenderer.cs
Covered lines:39
Uncovered lines:61
Coverable lines:100
Total lines:239
Line coverage:39% (39 of 100)
Covered branches:0
Total branches:0
Covered methods:9
Total methods:16
Method coverage:56.2% (9 of 16)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MapLayerStatus(...)0%110100%
MapRenderer()0%110100%
MapRenderer(...)0%110100%
Initialize()0%110100%
InitializeAsync()0%11.1711088.89%
OnFeatureFlagsChanged(...)0%110100%
RentCamera(...)0%6200%
ReleaseCamera(...)0%12300%
OnCameraZoomChanged(...)0%6200%
SetSharedLayer(...)0%56700%
EnableLayers(...)0%56700%
DisableLayers(...)0%56700%
ResetCancellationSource(...)0%6200%
Dispose()0%5.395075%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLServices/MapRendererV2/MapRenderer.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL;
 3using DCL.Helpers;
 4using DCLServices.MapRendererV2.CommonBehavior;
 5using DCLServices.MapRendererV2.ComponentsFactory;
 6using DCLServices.MapRendererV2.Culling;
 7using DCLServices.MapRendererV2.MapCameraController;
 8using DCLServices.MapRendererV2.MapLayers;
 9using MainScripts.DCL.Helpers.Utils;
 10using System;
 11using System.Collections.Generic;
 12using System.Threading;
 13using UnityEngine;
 14using UnityEngine.Pool;
 15
 16namespace DCLServices.MapRendererV2
 17{
 18    public partial class MapRenderer : IMapRenderer
 19    {
 20        private class MapLayerStatus
 21        {
 22            public readonly IMapLayerController MapLayerController;
 79223            public readonly List<IMapActivityOwner> ActivityOwners = new ();
 24
 25            public bool? SharedActive;
 26            public CancellationTokenSource CTS;
 27
 79228            public MapLayerStatus(IMapLayerController mapLayerController)
 29            {
 79230                MapLayerController = mapLayerController;
 79231            }
 32        }
 33
 134        private static readonly MapLayer[] ALL_LAYERS = EnumUtils.Values<MapLayer>();
 35
 36        private CancellationToken cancellationToken;
 37
 38        private readonly IMapRendererComponentsFactory componentsFactory;
 39
 40        private Dictionary<MapLayer, MapLayerStatus> layers;
 41        private List<IZoomScalingLayer> zoomScalingLayers;
 42
 43        private MapRendererConfiguration configurationInstance;
 44
 45        private IObjectPool<IMapCameraControllerInternal> mapCameraPool;
 46
 18547        internal IMapCullingController cullingController { get; private set; }
 48
 9749        public MapRenderer(IMapRendererComponentsFactory componentsFactory)
 50        {
 9751            this.componentsFactory = componentsFactory;
 9752        }
 53
 9754        public void Initialize() { }
 55
 56        public async UniTask InitializeAsync(CancellationToken cancellationToken)
 57        {
 9758            this.cancellationToken = cancellationToken;
 9759            layers = new Dictionary<MapLayer, MapLayerStatus>();
 9760            zoomScalingLayers = new List<IZoomScalingLayer>();
 61
 62            try
 63            {
 29164                MapRendererComponents components = await componentsFactory.Create(cancellationToken);
 8865                cullingController = components.CullingController;
 8866                mapCameraPool = components.MapCameraControllers;
 8867                configurationInstance = components.ConfigurationInstance;
 68
 105669                foreach (IZoomScalingLayer zoomScalingLayer in components.ZoomScalingLayers)
 44070                    zoomScalingLayers.Add(zoomScalingLayer);
 71
 176072                foreach (KeyValuePair<MapLayer, IMapLayerController> pair in components.Layers)
 73                {
 79274                    pair.Value.Disable(cancellationToken);
 79275                    layers[pair.Key] = new MapLayerStatus(pair.Value);
 76                }
 77
 8878                if (DataStore.i.featureFlags.flags.Get().IsInitialized)
 079                    OnFeatureFlagsChanged(null, null);
 80                else
 8881                    DataStore.i.featureFlags.flags.OnChange += OnFeatureFlagsChanged;
 8882            }
 983            catch
 84                (OperationCanceledException)
 85            {
 86                // just ignore
 987            }
 088            catch (Exception e) { Debug.LogException(e); }
 9789        }
 90
 91        private void OnFeatureFlagsChanged(FeatureFlag _, FeatureFlag __)
 92        {
 25093            bool satelliteEnabled = DataStore.i.featureFlags.flags.Get().IsFeatureEnabled("navmap-satellite-view"); // f
 25094            layers[MapLayer.SatelliteAtlas].SharedActive = satelliteEnabled;
 25095            layers[MapLayer.ParcelsAtlas].SharedActive = !satelliteEnabled;
 25096        }
 97
 98        public IMapCameraController RentCamera(in MapCameraInput cameraInput)
 99        {
 100            const int MIN_ZOOM = 5;
 101            const int MAX_ZOOM = 300;
 102
 103            // Clamp texture to the maximum size allowed, preserving aspect ratio
 0104            Vector2Int zoomValues = cameraInput.ZoomValues;
 0105            zoomValues.x = Mathf.Max(zoomValues.x, MIN_ZOOM);
 0106            zoomValues.y = Mathf.Min(zoomValues.y, MAX_ZOOM);
 107
 0108            EnableLayers(cameraInput.ActivityOwner, cameraInput.EnabledLayers);
 0109            IMapCameraControllerInternal mapCameraController = mapCameraPool.Get();
 0110            mapCameraController.Initialize(cameraInput.TextureResolution, zoomValues, cameraInput.EnabledLayers);
 0111            mapCameraController.OnReleasing += ReleaseCamera;
 112
 0113            if (DataStore.i.featureFlags.flags.Get().IsFeatureEnabled("map_focus_home_or_user"))
 0114                mapCameraController.ZoomChanged += OnCameraZoomChanged;
 115
 0116            mapCameraController.SetPositionAndZoom(cameraInput.Position, cameraInput.Zoom);
 117
 0118            return mapCameraController;
 119        }
 120
 121        private void ReleaseCamera(IMapActivityOwner owner, IMapCameraControllerInternal mapCameraController)
 122        {
 0123            mapCameraController.OnReleasing -= ReleaseCamera;
 124
 0125            if (DataStore.i.featureFlags.flags.Get().IsFeatureEnabled("map_focus_home_or_user"))
 126            {
 0127                mapCameraController.ZoomChanged -= OnCameraZoomChanged;
 128
 0129                foreach (IZoomScalingLayer layer in zoomScalingLayers)
 0130                    layer.ResetToBaseScale();
 131            }
 132
 0133            DisableLayers(owner, mapCameraController.EnabledLayers);
 0134            mapCameraPool.Release(mapCameraController);
 0135        }
 136
 137        private void OnCameraZoomChanged(float baseZoom, float newZoom)
 138        {
 0139            foreach (IZoomScalingLayer layer in zoomScalingLayers)
 0140                layer.ApplyCameraZoom(baseZoom, newZoom);
 0141        }
 142
 143        public void SetSharedLayer(MapLayer mask, bool active)
 144        {
 0145            foreach (MapLayer mapLayer in ALL_LAYERS)
 146            {
 0147                if (!EnumUtils.HasFlag(mask, mapLayer) || !layers.TryGetValue(mapLayer, out MapLayerStatus mapLayerStatu
 148                    continue;
 149
 0150                mapLayerStatus.SharedActive = active;
 151
 152                // Cancel activation/deactivation flow
 0153                ResetCancellationSource(mapLayerStatus);
 154
 0155                if (active)
 0156                    mapLayerStatus.MapLayerController.Enable(mapLayerStatus.CTS.Token).SuppressCancellationThrow().Forge
 157                else
 0158                    mapLayerStatus.MapLayerController.Disable(mapLayerStatus.CTS.Token).SuppressCancellationThrow().Forg
 159            }
 0160        }
 161
 162        private void EnableLayers(IMapActivityOwner owner, MapLayer mask)
 163        {
 0164            foreach (MapLayer mapLayer in ALL_LAYERS)
 165            {
 0166                if (!EnumUtils.HasFlag(mask, mapLayer) || !layers.TryGetValue(mapLayer, out MapLayerStatus mapLayerStatu
 167
 0168                if (owner.LayersParameters.TryGetValue(mapLayer, out IMapLayerParameter parameter))
 0169                    mapLayerStatus.MapLayerController.SetParameter(parameter);
 170
 0171                if (mapLayerStatus.ActivityOwners.Count == 0 && mapLayerStatus.SharedActive != false)
 172                {
 173                    // Cancel deactivation flow
 0174                    ResetCancellationSource(mapLayerStatus);
 0175                    mapLayerStatus.MapLayerController.Enable(mapLayerStatus.CTS.Token).SuppressCancellationThrow().Forge
 176                }
 177
 0178                mapLayerStatus.ActivityOwners.Add(owner);
 179            }
 0180        }
 181
 182        private void DisableLayers(IMapActivityOwner owner, MapLayer mask)
 183        {
 0184            foreach (MapLayer mapLayer in ALL_LAYERS)
 185            {
 0186                if (!EnumUtils.HasFlag(mask, mapLayer) || !layers.TryGetValue(mapLayer, out MapLayerStatus mapLayerStatu
 187
 0188                if (mapLayerStatus.ActivityOwners.Contains(owner))
 0189                    mapLayerStatus.ActivityOwners.Remove(owner);
 190
 0191                if (mapLayerStatus.ActivityOwners.Count == 0)
 192                {
 193                    // Cancel activation flow
 0194                    ResetCancellationSource(mapLayerStatus);
 0195                    mapLayerStatus.MapLayerController.Disable(mapLayerStatus.CTS.Token).SuppressCancellationThrow().Forg
 196                }
 197                else
 198                {
 0199                    var currentOwner = mapLayerStatus.ActivityOwners[^1];
 0200                    IReadOnlyDictionary<MapLayer, IMapLayerParameter> parametersByLayer = currentOwner.LayersParameters;
 201
 0202                    if (parametersByLayer.ContainsKey(mapLayer))
 0203                        mapLayerStatus.MapLayerController.SetParameter(parametersByLayer[mapLayer]);
 204                }
 205            }
 0206        }
 207
 208        private void ResetCancellationSource(MapLayerStatus mapLayerStatus)
 209        {
 0210            if (mapLayerStatus.CTS != null)
 211            {
 0212                mapLayerStatus.CTS.Cancel();
 0213                mapLayerStatus.CTS.Dispose();
 214            }
 215
 0216            mapLayerStatus.CTS = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
 0217        }
 218
 219        public void Dispose()
 220        {
 302221            foreach (MapLayerStatus status in layers.Values)
 222            {
 54223                if (status.CTS != null)
 224                {
 0225                    status.CTS.Cancel();
 0226                    status.CTS.Dispose();
 0227                    status.CTS = null;
 228                }
 229
 54230                status.MapLayerController.Dispose();
 231            }
 232
 97233            cullingController?.Dispose();
 234
 97235            if (configurationInstance)
 6236                Utils.SafeDestroy(configurationInstance.gameObject);
 97237        }
 238    }
 239}