< Summary

Class:DCL.Components.LoadableShape[LoadWrapperType,LoadWrapperModelType]
Assembly:DCL.Components.LoadableShapes
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/LoadableShapes/LoadableShape.cs
Covered lines:103
Uncovered lines:21
Coverable lines:124
Total lines:301
Line coverage:83% (103 of 124)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
LoadableShape()0%110100%
ApplyChanges(...)0%11.0211095%
AttachShape(...)0%10.378066.67%
ConfigureVisibility(...)0%550100%
ConfigureColliders(...)0%220100%
OnLoadFailed(...)0%330100%
CleanFailedWrapper(...)0%6.65060%
OnLoadCompleted(...)0%4.054085.71%
DetachShape(...)0%440100%
CallWhenReady(...)0%440100%
RaiseOnShapeUpdated(...)0%330100%
RaiseOnShapeLoaded(...)0%3.143075%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/LoadableShapes/LoadableShape.cs

#LineLine coverage
 1using System;
 2using DCL.Helpers;
 3using DCL.Models;
 4using System.Collections;
 5using UnityEngine;
 6using Object = UnityEngine.Object;
 7
 8namespace DCL.Components
 9{
 10    public class LoadableShape : BaseShape, IAssetCatalogReferenceHolder
 11    {
 12        [System.Serializable]
 13        public new class Model : BaseShape.Model
 14        {
 15            public string src;
 16            public string assetId;
 17
 18            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 19        }
 20
 21        public bool isLoaded { get; protected set; }
 22
 23        public Action<LoadableShape> OnLoaded;
 24
 25        protected Model previousModel = new Model();
 26
 27        protected LoadableShape() { model = new Model(); }
 28
 29        public override int GetClassId() { return -1; }
 30
 31        public override IEnumerator ApplyChanges(BaseModel newModel) { return null; }
 32
 33        public override bool IsVisible()
 34        {
 35            Model model = (Model) this.model;
 36            return model.visible;
 37        }
 38
 39        public override bool HasCollisions()
 40        {
 41            Model model = (Model) this.model;
 42            return model.withCollisions;
 43        }
 44
 45        public string GetAssetId()
 46        {
 47            Model model = (Model) this.model;
 48            return model.assetId;
 49        }
 50    }
 51
 52    public class LoadableShape<LoadWrapperType, LoadWrapperModelType> : LoadableShape
 53        where LoadWrapperType : LoadWrapper, new()
 54        where LoadWrapperModelType : LoadableShape.Model, new()
 55    {
 56        private bool failed = false;
 57        private event Action<BaseDisposable> OnFinishCallbacks;
 58        public System.Action<IDCLEntity> OnEntityShapeUpdated;
 59
 60        new public LoadWrapperModelType model
 61        {
 62            get
 63            {
 54364                if (base.model == null)
 065                    base.model = new LoadWrapperModelType();
 66
 54367                return base.model as LoadWrapperModelType;
 68            }
 1869            set { base.model = value; }
 70        }
 71
 72        new protected LoadWrapperModelType previousModel
 73        {
 74            get
 75            {
 42676                if (base.previousModel == null)
 077                    base.previousModel = new LoadWrapperModelType();
 78
 42679                return base.previousModel as LoadWrapperModelType;
 80            }
 17481            set { base.previousModel = value; }
 82        }
 83
 7584        public LoadableShape()
 85        {
 7586            OnDetach += DetachShape;
 7587            OnAttach += AttachShape;
 7588        }
 89
 90        public override IEnumerator ApplyChanges(BaseModel newModel)
 91        {
 8792            LoadWrapperModelType model = (LoadWrapperModelType) newModel;
 93
 8794            bool updateVisibility = true;
 8795            bool updateCollisions = true;
 8796            bool triggerAttachment = true;
 97
 8798            if (previousModel != null)
 99            {
 78100                updateVisibility = previousModel.visible != model.visible;
 78101                updateCollisions = previousModel.withCollisions != model.withCollisions || previousModel.isPointerBlocke
 78102                triggerAttachment = (!string.IsNullOrEmpty(model.src) && previousModel.src != model.src) ||
 103                                    (!string.IsNullOrEmpty(model.assetId) && previousModel.assetId != model.assetId);
 104            }
 105
 194106            foreach (var entity in attachedEntities)
 107            {
 10108                if (triggerAttachment)
 2109                    AttachShape(entity);
 110
 10111                if (updateVisibility)
 7112                    ConfigureVisibility(entity);
 113
 10114                if (updateCollisions)
 0115                    ConfigureColliders(entity);
 116
 10117                RaiseOnShapeUpdated(entity);
 118            }
 119
 87120            previousModel = model;
 87121            return null;
 122        }
 123
 124        protected virtual void AttachShape(IDCLEntity entity)
 125        {
 68126            ContentProvider provider = null;
 127
 68128            if (!string.IsNullOrEmpty(model.assetId))
 129            {
 1130                provider = AssetCatalogBridge.i.GetContentProviderForAssetIdInSceneObjectCatalog(model.assetId);
 1131                if (string.IsNullOrEmpty(model.src))
 132                {
 0133                    SceneObject sceneObject = AssetCatalogBridge.i.GetSceneObjectById(model.assetId);
 0134                    if (sceneObject == null)
 135                    {
 136#if UNITY_EDITOR
 0137                        Debug.LogWarning($"LoadableShape '{model.assetId}' not found in catalog, probably asset pack del
 138#endif
 0139                        failed = true;
 0140                        OnLoadFailed(null, new Exception($"LoadableShape '{model.assetId}' not found in catalog, probabl
 0141                        return;
 142                    }
 143
 0144                    model.src = sceneObject.model;
 145                }
 146            }
 147
 68148            if (provider == null)
 67149                provider = scene.contentProvider;
 150
 68151            if (provider.HasContentsUrl(model.src))
 152            {
 68153                isLoaded = false;
 68154                entity.EnsureMeshGameObject(componentName + " mesh");
 155
 68156                LoadWrapperType loadableShape =
 157                    Environment.i.world.state.GetOrAddLoaderForEntity<LoadWrapperType>(entity);
 158
 68159                if (loadableShape is LoadWrapper_GLTF gltfLoadWrapper)
 67160                    gltfLoadWrapper.customContentProvider = provider;
 161
 68162                entity.meshesInfo.currentShape = this;
 163
 68164                loadableShape.entity = entity;
 68165                loadableShape.useVisualFeedback = Configuration.ParcelSettings.VISUAL_LOADING_ENABLED;
 166
 68167                bool initialVisibility = model.visible;
 68168                if (!DataStore.i.debugConfig.isDebugMode.Get())
 61169                    initialVisibility &= entity.isInsideSceneBoundaries;
 68170                loadableShape.initialVisibility = initialVisibility;
 171
 68172                loadableShape.Load(model.src, OnLoadCompleted, OnLoadFailed);
 173            }
 174            else
 175            {
 176#if UNITY_EDITOR
 0177                Debug.LogWarning($"LoadableShape '{model.src}' not found in scene '{scene.sceneData.sceneNumber}' mappin
 178#endif
 0179                failed = true;
 180            }
 0181        }
 182
 183        void ConfigureVisibility(IDCLEntity entity)
 184        {
 77185            var loadable = Environment.i.world.state.GetLoaderForEntity(entity);
 186
 77187            bool initialVisibility = model.visible;
 77188            if (!DataStore.i.debugConfig.isDebugMode.Get())
 67189                initialVisibility &= entity.isInsideSceneBoundaries;
 190
 77191            if (loadable != null)
 77192                loadable.initialVisibility = initialVisibility;
 193
 77194            ConfigureVisibility(entity.meshRootGameObject, initialVisibility, entity.meshesInfo.renderers);
 195
 77196            if(!scene.componentsManagerLegacy.HasComponent(entity, CLASS_ID_COMPONENT.ANIMATOR) && entity.meshesInfo.ani
 8197                entity.meshesInfo.animation.enabled = initialVisibility;
 77198        }
 199
 200        protected virtual void ConfigureColliders(IDCLEntity entity)
 201        {
 63202            CollidersManager.i.ConfigureColliders(entity.meshRootGameObject, model.withCollisions && entity.isInsideScen
 63203        }
 204
 205        protected void OnLoadFailed(LoadWrapper loadWrapper, Exception exception)
 206        {
 3207            if (loadWrapper != null)
 3208                CleanFailedWrapper(loadWrapper);
 209
 3210            failed = true;
 3211            OnFinishCallbacks?.Invoke(this);
 3212            OnFinishCallbacks = null;
 3213        }
 214
 215        void CleanFailedWrapper(LoadWrapper loadWrapper)
 216        {
 3217            if (loadWrapper == null)
 0218                return;
 3219            if (loadWrapper.entity == null)
 0220                return;
 3221            if (loadWrapper.entity.gameObject == null)
 0222                return;
 223
 3224            GameObject go = loadWrapper.entity.gameObject;
 225
 3226            go.name += " - Failed loading";
 227
 3228            MaterialTransitionController[] transitionController =
 229                go.GetComponentsInChildren<MaterialTransitionController>(true);
 230
 6231            for (int i = 0; i < transitionController.Length; i++)
 232            {
 0233                MaterialTransitionController material = transitionController[i];
 0234                Object.Destroy(material);
 235            }
 3236        }
 237
 238        protected void OnLoadCompleted(LoadWrapper loadWrapper)
 239        {
 70240            IDCLEntity entity = loadWrapper.entity;
 241
 70242            if (entity.meshesInfo.currentShape == null)
 243            {
 0244                OnLoadFailed(loadWrapper, new Exception($"Entity {entity.entityId} current shape of mesh information is 
 0245                return;
 246            }
 247
 70248            isLoaded = true;
 70249            OnLoaded?.Invoke(this);
 250
 70251            entity.meshesInfo.meshRootGameObject = entity.meshRootGameObject;
 252
 70253            ConfigureVisibility(entity);
 70254            ConfigureColliders(entity);
 70255            RaiseOnShapeUpdated(entity);
 70256            RaiseOnShapeLoaded(entity);
 257
 70258            OnFinishCallbacks?.Invoke(this);
 70259            OnFinishCallbacks = null;
 70260        }
 261
 262        protected virtual void DetachShape(IDCLEntity entity)
 263        {
 57264            if (entity == null || entity.meshRootGameObject == null)
 3265                return;
 266
 54267            LoadWrapper loadWrapper = Environment.i.world.state.GetLoaderForEntity(entity);
 54268            loadWrapper?.Unload();
 54269            Environment.i.world.state.RemoveLoaderForEntity(entity);
 54270            entity.meshesInfo.CleanReferences();
 54271        }
 272
 273        public override void CallWhenReady(Action<ISharedComponent> callback)
 274        {
 3275            if (attachedEntities.Count == 0 || isLoaded || failed)
 276            {
 1277                callback.Invoke(this);
 278            }
 279            else
 280            {
 2281                OnFinishCallbacks += callback;
 282            }
 2283        }
 284
 285        private void RaiseOnShapeUpdated(IDCLEntity entity)
 286        {
 80287            if (!isLoaded)
 4288                return;
 289
 76290            entity.OnShapeUpdated?.Invoke(entity);
 76291        }
 292
 293        private void RaiseOnShapeLoaded(IDCLEntity entity)
 294        {
 70295            if (!isLoaded)
 0296                return;
 297
 70298            entity.OnShapeLoaded?.Invoke(entity);
 5299        }
 300    }
 301}