< Summary

Class:DCL.WorldState
Assembly:DCL.Runtime
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/WorldState.cs
Covered lines:92
Uncovered lines:16
Coverable lines:108
Total lines:251
Line coverage:85.1% (92 of 108)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
WorldState()0%110100%
GetCurrentSceneId()0%110100%
GetLoadedScenes()0%110100%
GetGlobalScenes()0%2100%
GetScenesSortedByDistance()0%110100%
GetScene(...)0%6200%
GetScene(...)0%2.152066.67%
ContainsScene(...)0%330100%
TryGetScene(...)0%330100%
TryGetScene[T](...)0%3.033085.71%
GetAllLoadedScenesCoords()0%330100%
GetLoaderForEntity(...)0%2.262060%
GetOrAddLoaderForEntity[T](...)0%220100%
RemoveLoaderForEntity(...)0%330100%
GetSceneIdByCoords(...)0%6200%
SortScenesByDistance(...)0%550100%
ForceCurrentScene(...)0%110100%
AddScene(...)0%4.074083.33%
RemoveScene(...)0%330100%
AddGlobalScene(...)0%2.092071.43%
Dispose()0%110100%
Initialize()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/WorldState.cs

#LineLine coverage
 1using DCL.Controllers;
 2using System.Collections.Generic;
 3using DCL.Configuration;
 4using DCL.Models;
 5using UnityEngine;
 6
 7namespace DCL
 8{
 9    public class WorldState : IWorldState
 10    {
 77511        private Dictionary<string, IParcelScene> loadedScenes { get; } = new Dictionary<string, IParcelScene>();
 77512        private Dictionary<Vector2Int, string> loadedScenesByCoordinate { get; } = new Dictionary<Vector2Int, string>();
 77513        private List<IParcelScene> scenesSortedByDistance { get; } = new List<IParcelScene>();
 77514        private List<string> globalSceneIds { get; } = new List<string>();
 77515        private Vector2Int sortAuxiliaryVector = new Vector2Int(EnvironmentSettings.MORDOR_SCALAR, EnvironmentSettings.M
 77516        private readonly List<IParcelScene> globalScenes = new List<IParcelScene>();
 17        private string currentSceneId;
 18
 344619        public string GetCurrentSceneId() => currentSceneId;
 20
 69521        public IEnumerable<KeyValuePair<string, IParcelScene>> GetLoadedScenes() => loadedScenes;
 22
 023        public List<IParcelScene> GetGlobalScenes() => globalScenes;
 24
 142325        public List<IParcelScene> GetScenesSortedByDistance() => scenesSortedByDistance;
 26
 27        public IParcelScene GetScene(Vector2Int coords)
 28        {
 029            var id = GetSceneIdByCoords(coords);
 30
 031            if (!ContainsScene(id))
 032                return null;
 33
 034            return loadedScenes[id];
 35        }
 36
 37        public IParcelScene GetScene(string id)
 38        {
 34339            if (!ContainsScene(id))
 040                return null;
 41
 34342            return loadedScenes[id];
 43        }
 44
 45        public bool ContainsScene(string id)
 46        {
 223947            if (string.IsNullOrEmpty(id) || !loadedScenes.ContainsKey(id))
 52748                return false;
 49
 171250            return true;
 51        }
 52
 53        public bool TryGetScene(string id, out IParcelScene scene)
 54        {
 52655            scene = null;
 56
 52657            if (string.IsNullOrEmpty(id) || !loadedScenes.ContainsKey(id))
 1258                return false;
 59
 51460            scene = loadedScenes[id];
 51461            return true;
 62        }
 63
 64        public bool TryGetScene<T>(string id, out T scene)
 65            where T : class, IParcelScene
 66        {
 41967            scene = default(T);
 41968            bool result = TryGetScene(id, out IParcelScene baseScene);
 69
 41970            if (result)
 41971                scene = baseScene as T;
 72
 41973            if (scene == null)
 074                result = false;
 75
 41976            return result;
 77        }
 78
 79        public HashSet<Vector2Int> GetAllLoadedScenesCoords()
 80        {
 80381            HashSet<Vector2Int> allLoadedParcelCoords = new HashSet<Vector2Int>();
 82
 83            // Create fast (hashset) collection of loaded parcels coords
 186884            foreach (var element in loadedScenes)
 85            {
 13186                ParcelScene scene = element.Value as ParcelScene;
 87
 13188                if (!scene.sceneLifecycleHandler.isReady)
 89                    continue;
 90
 591                allLoadedParcelCoords.UnionWith(scene.parcels);
 92            }
 93
 80394            return allLoadedParcelCoords;
 95        }
 96
 77597        private readonly Dictionary<GameObject, LoadWrapper>
 98            attachedLoaders = new Dictionary<GameObject, LoadWrapper>();
 99
 100        public LoadWrapper GetLoaderForEntity(IDCLEntity entity)
 101        {
 408102            if (entity.meshRootGameObject == null)
 103            {
 0104                Debug.LogWarning("NULL meshRootGameObject at GetLoaderForEntity()");
 0105                return null;
 106            }
 107
 408108            attachedLoaders.TryGetValue(entity.meshRootGameObject, out LoadWrapper result);
 408109            return result;
 110        }
 111
 112        public T GetOrAddLoaderForEntity<T>(IDCLEntity entity)
 113            where T : LoadWrapper, new()
 114        {
 104115            if (!attachedLoaders.TryGetValue(entity.meshRootGameObject, out LoadWrapper result))
 116            {
 102117                result = new T();
 102118                attachedLoaders.Add(entity.meshRootGameObject, result);
 119            }
 120
 104121            return result as T;
 122        }
 123
 124        public void RemoveLoaderForEntity(IDCLEntity entity)
 125        {
 81126            if (entity == null || entity.meshRootGameObject == null)
 7127                return;
 128
 74129            attachedLoaders.Remove(entity.meshRootGameObject);
 74130        }
 131
 132        public string GetSceneIdByCoords(Vector2Int coords)
 133        {
 0134            if (loadedScenesByCoordinate.ContainsKey(coords))
 0135                return loadedScenesByCoordinate[coords];
 136
 0137            return null;
 138        }
 139
 140        public void SortScenesByDistance(Vector2Int position)
 141        {
 680142            currentSceneId = null;
 680143            scenesSortedByDistance.Sort((sceneA, sceneB) =>
 144            {
 60145                sortAuxiliaryVector = sceneA.sceneData.basePosition - position;
 60146                int dist1 = sortAuxiliaryVector.sqrMagnitude;
 147
 60148                sortAuxiliaryVector = sceneB.sceneData.basePosition - position;
 60149                int dist2 = sortAuxiliaryVector.sqrMagnitude;
 150
 60151                return dist1 - dist2;
 152            });
 153
 680154            using var iterator = scenesSortedByDistance.GetEnumerator();
 155
 694156            while (iterator.MoveNext())
 157            {
 31158                IParcelScene scene = iterator.Current;
 159
 31160                if (scene == null)
 161                    continue;
 162
 31163                bool characterIsInsideScene = WorldStateUtils.IsCharacterInsideScene(scene);
 31164                bool isGlobalScene = globalSceneIds.Contains(scene.sceneData.id);
 165
 31166                if (isGlobalScene || !characterIsInsideScene)
 167                    continue;
 168
 17169                currentSceneId = scene.sceneData.id;
 170
 17171                break;
 172            }
 173
 1343174        }
 175        public void ForceCurrentScene(string id)
 176        {
 513177            currentSceneId = id;
 513178        }
 179
 180        public void AddScene(string id, IParcelScene newScene)
 181        {
 531182            if (loadedScenes.ContainsKey(id))
 183            {
 0184                Debug.LogWarning($"This scene already exists! {id}");
 0185                return;
 186            }
 187
 531188            loadedScenes.Add(id, newScene);
 189
 2116190            foreach (Vector2Int parcelPosition in newScene.GetParcels())
 191            {
 527192                loadedScenesByCoordinate[parcelPosition] = id;
 193            }
 194
 531195            scenesSortedByDistance.Add(newScene);
 196
 531197            if (currentSceneId == null)
 198            {
 501199                currentSceneId = id;
 200            }
 531201        }
 202
 203        public void RemoveScene(string id)
 204        {
 419205            IParcelScene loadedScene = loadedScenes[id];
 206
 1682207            foreach (Vector2Int sceneParcel in loadedScene.GetParcels())
 208            {
 422209                loadedScenesByCoordinate.Remove(sceneParcel);
 210            }
 211
 419212            scenesSortedByDistance.Remove(loadedScene);
 213
 419214            loadedScenes.Remove(id);
 419215            globalSceneIds.Remove(id);
 216
 419217            if (globalScenes.Contains(loadedScene))
 218            {
 5219                globalScenes.Remove(loadedScene);
 220            }
 419221        }
 222
 223        public void AddGlobalScene(string sceneId, IParcelScene newScene)
 224        {
 5225            if (globalSceneIds.Contains(sceneId))
 226            {
 0227                Debug.LogWarning($"This GLOBAL scene already exists! {sceneId}");
 0228                return;
 229            }
 230
 5231            globalSceneIds.Add(sceneId);
 5232            globalScenes.Add(newScene);
 233
 5234            AddScene(sceneId, newScene);
 5235        }
 236
 237        public void Dispose()
 238        {
 768239            loadedScenes.Clear();
 768240            loadedScenesByCoordinate.Clear();
 768241            scenesSortedByDistance.Clear();
 768242            globalScenes.Clear();
 768243            globalScenes.Clear();
 768244            currentSceneId = null;
 768245        }
 246
 247        public void Initialize()
 248        {
 768249        }
 250    }
 251}