< Summary

Class:DCL.BIWCreatorController
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/Controllers/BIWCreatorController.cs
Covered lines:143
Uncovered lines:47
Coverable lines:190
Total lines:405
Line coverage:75.2% (143 of 190)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BIWCreatorController()0%110100%
Initialize(...)0%220100%
Dispose()0%220100%
Update()0%220100%
SendAnalytics()0%2.862040%
CleanUp()0%3.053081.82%
IsAnyErrorOnEntities()0%2100%
IsInsideTheLimits(...)0%56.71808.7%
CreateErrorOnEntity(...)0%22090%
DeleteErrorOnEntity(...)0%2.022083.33%
CreateCatalogItem(...)0%110100%
CreateCatalogItem(...)0%9.099089.66%
ExistsLoadingGameObjectForEntity(...)0%110100%
CreateLoadingObject(...)0%220100%
OnShapeLoadFinish(...)0%110100%
RemoveLoadingObject(...)0%220100%
RemoveLoadingObjectInmediate(...)0%220100%
AddSmartItemComponent(...)0%2100%
AddEntityNameComponent(...)0%110100%
AddLockedComponent(...)0%220100%
AddShape(...)0%2.52050%
AddSceneMappings(...)0%660100%
CreateLastCatalogItem()0%4.074083.33%
OnCatalogItemSelected(...)0%3.333066.67%
OnCatalogItemDropped(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/BuilderMode/Controllers/BIWCreatorController.cs

#LineLine coverage
 1using DCL.Components;
 2using DCL.Models;
 3using System;
 4using System.Collections.Generic;
 5using System.Linq;
 6using DCL.Controllers;
 7using UnityEngine;
 8
 9namespace DCL
 10{
 11    public interface IBIWCreatorController : IBIWController
 12    {
 13        event Action OnCatalogItemPlaced;
 14        event Action OnInputDone;
 15        void CreateCatalogItem(CatalogItem catalogItem, bool autoSelect = true, bool isFloor = false);
 16        BIWEntity CreateCatalogItem(CatalogItem catalogItem, Vector3 startPosition, bool autoSelect = true, bool isFloor
 17        void CreateErrorOnEntity(BIWEntity entity);
 18        void RemoveLoadingObjectInmediate(string entityId);
 19        bool IsAnyErrorOnEntities();
 20        void CreateLoadingObject(BIWEntity entity);
 21        void CleanUp();
 22    }
 23
 24    public class BIWCreatorController : BIWController, IBIWCreatorController
 25    {
 26        private const float SECONDS_TO_SEND_ANALYTICS = 5f;
 27        public event Action OnInputDone;
 28        public event Action OnCatalogItemPlaced;
 29
 30        private IBIWModeController modeController;
 31
 32        private IBIWFloorHandler floorHandler;
 33        private IBIWEntityHandler entityHandler;
 34
 35        private GameObject loadingObjectPrefab;
 36        private GameObject errorPrefab;
 37
 38        private CatalogItem lastCatalogItemCreated;
 39
 3940        private readonly Dictionary<string, BIWLoadingPlaceHolder> loadingGameObjects = new Dictionary<string, BIWLoadin
 3941        private readonly Dictionary<BIWEntity, GameObject> errorGameObjects = new Dictionary<BIWEntity, GameObject>();
 42
 3943        private readonly List<KeyValuePair<CatalogItem, string>> itemsToSendAnalytics = new List<KeyValuePair<CatalogIte
 44
 45        private float lastAnalyticsSentTimestamp = 0;
 46
 47        public override void Initialize(BIWContext biwContext)
 48        {
 3949            base.Initialize(biwContext);
 50
 3951            modeController = biwContext.modeController;
 3952            floorHandler = biwContext.floorHandler;
 3953            entityHandler = biwContext.entityHandler;
 3954            loadingObjectPrefab = biwContext.projectReferencesAsset.loadingPrefab;
 3955            errorPrefab = biwContext.projectReferencesAsset.errorPrefab;
 56
 3957            if (HUDController.i.builderInWorldMainHud != null)
 58            {
 2559                HUDController.i.builderInWorldMainHud.OnCatalogItemSelected += OnCatalogItemSelected;
 2560                HUDController.i.builderInWorldMainHud.OnCatalogItemDropped += OnCatalogItemDropped;
 61            }
 3962        }
 63
 64        public override void Dispose()
 65        {
 3966            base.Dispose();
 3967            if (HUDController.i.builderInWorldMainHud != null)
 68            {
 2569                HUDController.i.builderInWorldMainHud.OnCatalogItemSelected -= OnCatalogItemSelected;
 2570                HUDController.i.builderInWorldMainHud.OnCatalogItemDropped -= OnCatalogItemDropped;
 71            }
 72
 3973            CleanUp();
 3974        }
 75
 76        public override void Update()
 77        {
 278            base.Update();
 279            if (Time.realtimeSinceStartup >= lastAnalyticsSentTimestamp)
 80            {
 281                SendAnalytics();
 282                lastAnalyticsSentTimestamp = Time.realtimeSinceStartup + SECONDS_TO_SEND_ANALYTICS;
 83            }
 284        }
 85
 86        private void SendAnalytics()
 87        {
 288            if (itemsToSendAnalytics.Count == 0)
 289                return;
 90
 091            BIWAnalytics.NewObjectPlacedChunk(itemsToSendAnalytics);
 092            itemsToSendAnalytics.Clear();
 093        }
 94
 95        public void CleanUp()
 96        {
 12897            foreach (BIWLoadingPlaceHolder placeHolder in loadingGameObjects.Values)
 98            {
 099                placeHolder.Dispose();
 100            }
 101
 166102            foreach (BIWEntity entity in errorGameObjects.Keys.ToArray())
 103            {
 19104                DeleteErrorOnEntity(entity);
 105            }
 106
 64107            loadingGameObjects.Clear();
 64108            errorGameObjects.Clear();
 64109        }
 110
 0111        public bool IsAnyErrorOnEntities() { return errorGameObjects.Count > 0; }
 112
 113        private bool IsInsideTheLimits(CatalogItem sceneObject)
 114        {
 15115            if (HUDController.i.builderInWorldMainHud == null)
 15116                return false;
 117
 0118            SceneMetricsModel limits = sceneToEdit.metricsCounter.GetLimits();
 0119            SceneMetricsModel usage = sceneToEdit.metricsCounter.GetModel();
 120
 0121            if (limits.bodies < usage.bodies + sceneObject.metrics.bodies)
 122            {
 0123                HUDController.i.builderInWorldMainHud.ShowSceneLimitsPassed();
 0124                return false;
 125            }
 126
 0127            if (limits.entities < usage.entities + sceneObject.metrics.entities)
 128            {
 0129                HUDController.i.builderInWorldMainHud.ShowSceneLimitsPassed();
 0130                return false;
 131            }
 132
 0133            if (limits.materials < usage.materials + sceneObject.metrics.materials)
 134            {
 0135                HUDController.i.builderInWorldMainHud.ShowSceneLimitsPassed();
 0136                return false;
 137            }
 138
 0139            if (limits.meshes < usage.meshes + sceneObject.metrics.meshes)
 140            {
 0141                HUDController.i.builderInWorldMainHud.ShowSceneLimitsPassed();
 0142                return false;
 143            }
 144
 0145            if (limits.textures < usage.textures + sceneObject.metrics.textures)
 146            {
 0147                HUDController.i.builderInWorldMainHud.ShowSceneLimitsPassed();
 0148                return false;
 149            }
 150
 0151            if (limits.triangles < usage.triangles + sceneObject.metrics.triangles)
 152            {
 0153                HUDController.i.builderInWorldMainHud.ShowSceneLimitsPassed();
 0154                return false;
 155            }
 156
 0157            return true;
 158        }
 159
 160        public void CreateErrorOnEntity(BIWEntity entity)
 161        {
 21162            if (errorGameObjects.ContainsKey(entity))
 0163                return;
 164
 21165            GameObject instantiatedError = UnityEngine.Object.Instantiate(errorPrefab, Vector3.zero, errorPrefab.transfo
 21166            instantiatedError.transform.SetParent(entity.rootEntity.gameObject.transform, true);
 21167            instantiatedError.transform.localPosition = Vector3.zero;
 168
 21169            var missingAsset = instantiatedError.GetComponent<MissingAsset>();
 21170            missingAsset.Configure(entity);
 171
 21172            errorGameObjects.Add(entity, instantiatedError);
 21173            entity.OnDelete += DeleteErrorOnEntity;
 21174        }
 175
 176        public void DeleteErrorOnEntity(BIWEntity entity)
 177        {
 21178            if (!errorGameObjects.ContainsKey(entity))
 0179                return;
 180
 21181            entity.OnDelete -= DeleteErrorOnEntity;
 21182            GameObject.Destroy(errorGameObjects[entity]);
 21183            errorGameObjects.Remove(entity);
 21184        }
 185
 16186        public void CreateCatalogItem(CatalogItem catalogItem, bool autoSelect = true, bool isFloor = false) { CreateCat
 187
 188        public BIWEntity CreateCatalogItem(CatalogItem catalogItem, Vector3 startPosition, bool autoSelect = true, bool 
 189        {
 15190            if (catalogItem.IsNFT() && BIWNFTController.i.IsNFTInUse(catalogItem.id))
 0191                return null;
 192
 15193            IsInsideTheLimits(catalogItem);
 194
 195            //Note (Adrian): This is a workaround until the mapping is handle by kernel
 15196            AddSceneMappings(catalogItem);
 197
 15198            Vector3 editionPosition = modeController.GetCurrentEditionPosition();
 199
 15200            BIWEntity entity = entityHandler.CreateEmptyEntity(sceneToEdit, startPosition, editionPosition, false);
 15201            entity.isFloor = isFloor;
 15202            entity.SetRotation(Vector3.zero);
 203
 15204            if (!isFloor)
 8205                CreateLoadingObject(entity);
 206
 42207            entity.rootEntity.OnShapeUpdated += (entity) => onFloorLoadedAction?.Invoke(entity);
 208
 15209            AddShape(catalogItem, entity);
 15210            AddEntityNameComponent(catalogItem, entity);
 15211            AddLockedComponent(entity);
 212
 15213            if (catalogItem.IsSmartItem())
 214            {
 0215                AddSmartItemComponent(entity);
 216            }
 217
 15218            if (catalogItem.IsVoxel())
 0219                entity.isVoxel = true;
 220
 15221            if (autoSelect)
 222            {
 8223                entityHandler.DeselectEntities();
 8224                entityHandler.Select(entity.rootEntity);
 225            }
 226
 15227            entity.rootEntity.gameObject.transform.eulerAngles = Vector3.zero;
 228
 15229            modeController.CreatedEntity(entity);
 230
 15231            lastCatalogItemCreated = catalogItem;
 232
 15233            entityHandler.EntityListChanged();
 15234            entityHandler.NotifyEntityIsCreated(entity.rootEntity);
 15235            OnInputDone?.Invoke();
 15236            OnCatalogItemPlaced?.Invoke();
 15237            return entity;
 238        }
 239
 240        #region LoadingObjects
 241
 2242        public bool ExistsLoadingGameObjectForEntity(string entityId) { return loadingGameObjects.ContainsKey(entityId);
 243
 244        public void CreateLoadingObject(BIWEntity entity)
 245        {
 10246            if (loadingGameObjects.ContainsKey(entity.rootEntity.entityId))
 1247                return;
 248
 9249            BIWLoadingPlaceHolder loadingPlaceHolder = UnityEngine.Object.Instantiate(loadingObjectPrefab, entity.rootEn
 9250            loadingGameObjects.Add(entity.rootEntity.entityId, loadingPlaceHolder);
 9251            entity.OnShapeFinishLoading += OnShapeLoadFinish;
 9252        }
 253
 254        private void OnShapeLoadFinish(BIWEntity entity)
 255        {
 8256            entity.OnShapeFinishLoading -= OnShapeLoadFinish;
 8257            RemoveLoadingObject(entity.rootEntity.entityId);
 8258        }
 259
 260        public void RemoveLoadingObject(string entityId)
 261        {
 9262            if (!loadingGameObjects.ContainsKey(entityId))
 1263                return;
 8264            BIWLoadingPlaceHolder loadingPlaceHolder = loadingGameObjects[entityId];
 8265            loadingGameObjects.Remove(entityId);
 8266            loadingPlaceHolder.DestroyAfterAnimation();
 8267        }
 268
 269        public void RemoveLoadingObjectInmediate(string entityId)
 270        {
 6271            if (!loadingGameObjects.ContainsKey(entityId))
 5272                return;
 1273            BIWLoadingPlaceHolder loadingPlaceHolder = loadingGameObjects[entityId];
 1274            loadingGameObjects.Remove(entityId);
 1275            loadingPlaceHolder.Dispose();
 1276        }
 277
 278        #endregion
 279
 280        #region Add Components
 281
 282        private void AddSmartItemComponent(BIWEntity entity)
 283        {
 284            //Note (Adrian): This will disable the smart item component until it is implemented in kernel
 285            //TODO: After the implementation in kernel of smart items, we should eliminate this return
 0286            return;
 287            SmartItemComponent.Model model = new SmartItemComponent.Model();
 288            model.values = new Dictionary<object, object>();
 289
 290            sceneToEdit.EntityComponentCreateOrUpdateWithModel(entity.rootEntity.entityId, CLASS_ID_COMPONENT.SMART_ITEM
 291
 292            //Note (Adrian): We can't wait to set the component 1 frame, so we set it
 293            if (entity.rootEntity.TryGetBaseComponent(CLASS_ID_COMPONENT.SMART_ITEM, out IEntityComponent component))
 294                ((SmartItemComponent) component).UpdateFromModel(model);
 295        }
 296
 297        private void AddEntityNameComponent(CatalogItem catalogItem, BIWEntity entity)
 298        {
 15299            DCLName name = (DCLName) sceneToEdit.SharedComponentCreate(Guid.NewGuid().ToString(), Convert.ToInt32(CLASS_
 15300            sceneToEdit.SharedComponentAttach(entity.rootEntity.entityId, name.id);
 15301            entityHandler.SetEntityName(entity, catalogItem.name, false);
 15302        }
 303
 304        private void AddLockedComponent(BIWEntity entity)
 305        {
 15306            DCLLockedOnEdit entityLocked = (DCLLockedOnEdit) sceneToEdit.SharedComponentCreate(Guid.NewGuid().ToString()
 15307            if (entity.isFloor)
 7308                entityLocked.SetIsLocked(true);
 309            else
 8310                entityLocked.SetIsLocked(false);
 311
 15312            sceneToEdit.SharedComponentAttach(entity.rootEntity.entityId, entityLocked.id);
 15313        }
 314
 315        private void AddShape(CatalogItem catalogItem, BIWEntity entity)
 316        {
 15317            if (catalogItem.IsNFT())
 318            {
 0319                NFTShape nftShape = (NFTShape) sceneToEdit.SharedComponentCreate(catalogItem.id, Convert.ToInt32(CLASS_I
 0320                nftShape.model = new NFTShape.Model();
 0321                nftShape.model.color = new Color(0.6404918f, 0.611472f, 0.8584906f);
 0322                nftShape.model.src = catalogItem.model;
 0323                nftShape.model.assetId = catalogItem.id;
 0324                sceneToEdit.SharedComponentAttach(entity.rootEntity.entityId, nftShape.id);
 325
 0326                nftShape.CallWhenReady(entity.ShapeLoadFinish);
 0327            }
 328            else
 329            {
 15330                GLTFShape gltfComponent = (GLTFShape) sceneToEdit.SharedComponentCreate(catalogItem.id, Convert.ToInt32(
 15331                gltfComponent.model = new LoadableShape.Model();
 15332                gltfComponent.model.src = catalogItem.model;
 15333                gltfComponent.model.assetId = catalogItem.id;
 15334                sceneToEdit.SharedComponentAttach(entity.rootEntity.entityId, gltfComponent.id);
 335
 15336                gltfComponent.CallWhenReady(entity.ShapeLoadFinish);
 337            }
 15338        }
 339
 340        #endregion
 341
 342        private void AddSceneMappings(CatalogItem catalogItem)
 343        {
 15344            LoadParcelScenesMessage.UnityParcelScene data = sceneToEdit.sceneData;
 15345            data.baseUrl = BIWUrlUtils.GetUrlSceneObjectContent();
 15346            if (data.contents == null)
 10347                data.contents = new List<ContentServerUtils.MappingPair>();
 88348            foreach (KeyValuePair<string, string> content in catalogItem.contents)
 349            {
 29350                ContentServerUtils.MappingPair mappingPair = new ContentServerUtils.MappingPair();
 29351                mappingPair.file = content.Key;
 29352                mappingPair.hash = content.Value;
 29353                bool found = false;
 161354                foreach (ContentServerUtils.MappingPair mappingPairToCheck in data.contents)
 355                {
 55356                    if (mappingPairToCheck.file == mappingPair.file)
 357                    {
 7358                        found = true;
 7359                        break;
 360                    }
 361                }
 362
 29363                if (!found)
 22364                    data.contents.Add(mappingPair);
 365            }
 366
 15367            DCL.Environment.i.world.sceneController.UpdateParcelScenesExecute(data);
 15368        }
 369
 370        public void CreateLastCatalogItem()
 371        {
 1372            if (lastCatalogItemCreated != null)
 373            {
 1374                if (entityHandler.IsAnyEntitySelected())
 1375                    entityHandler.DeselectEntities();
 1376                OnCatalogItemSelected(lastCatalogItemCreated);
 1377                OnInputDone?.Invoke();
 378            }
 0379        }
 380
 381        private void OnCatalogItemSelected(CatalogItem catalogItem)
 382        {
 1383            if (floorHandler.IsCatalogItemFloor(catalogItem))
 384            {
 0385                floorHandler.ChangeFloor(catalogItem);
 0386            }
 387            else
 388            {
 1389                CreateCatalogItem(catalogItem);
 390            }
 391
 1392            string catalogSection = "";
 1393            if (HUDController.i.builderInWorldMainHud != null)
 0394                catalogSection =   HUDController.i.builderInWorldMainHud.GetCatalogSectionSelected().ToString();
 395
 1396            itemsToSendAnalytics.Add(new KeyValuePair<CatalogItem, string>(catalogItem, catalogSection));
 1397        }
 398
 399        private void OnCatalogItemDropped(CatalogItem catalogItem)
 400        {
 0401            OnCatalogItemSelected(catalogItem);
 0402            entityHandler.DeselectEntities();
 0403        }
 404    }
 405}