< Summary

Class:DCL.Builder.InitialStateManager
Assembly:BuilderInWorld
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/SceneManager/InitialStateManager.cs
Covered lines:0
Uncovered lines:72
Coverable lines:72
Total lines:185
Line coverage:0% (0 of 72)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
InitialStateManager()0%2100%
GetInitialManifest(...)0%2100%
GetInitialStateManifest()0%1561200%
GetManifestByCoordinates()0%12300%
GetManifestByStatefullDefinition()0%30500%
GetProjectById()0%12300%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/BuilderInWorld/Scripts/SceneManager/InitialStateManager.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using System.Runtime.Remoting.Contexts;
 5using Cysharp.Threading.Tasks;
 6using DCL.Builder.Manifest;
 7using DCL.Configuration;
 8using DCL.Helpers;
 9using Newtonsoft.Json;
 10using UnityEngine;
 11
 12namespace DCL.Builder
 13{
 14    public interface IInitialStateManager
 15    {
 16        /// <summary>
 17        /// This call will look for the correct manifest of the land. The lookup will be the next
 18        /// 1. If the land has a published scene with a project ID, we load this project, if not we continue looking
 19        /// 2. If the scene has been published with the stateless manifest, we create a project from it, if not we conti
 20        /// 3. If the land has a linked project ( by the coords) we load the project, if not we continue looking
 21        /// 4. If there is not project associated and we cant create it from a stateless manifest, we just create an emp
 22        /// </summary>
 23        /// <param name="builderAPIController"></param>
 24        /// <param name="landCoords"></param>
 25        /// <param name="scene"></param>
 26        /// <param name="parcelSize"></param>
 27        /// <returns></returns>
 28        Promise<InitialStateResponse> GetInitialManifest(IBuilderAPIController builderAPIController, string landCoords, 
 29    }
 30
 31    public class InitialStateManager : IInitialStateManager
 32    {
 033        private InitialStateResponse response = new InitialStateResponse();
 34        private Promise<InitialStateResponse> masterManifestPromise;
 35
 36        public Promise<InitialStateResponse> GetInitialManifest(IBuilderAPIController builderAPIController, string landC
 37        {
 038            masterManifestPromise = new Promise<InitialStateResponse>();
 039            GetInitialStateManifest(builderAPIController, landCoords, scene, parcelSize);
 040            return masterManifestPromise;
 41        }
 42
 43        /// <summary>
 44        /// This call will look for the correct manifest of the land. The lookup will be the next
 45        /// 1. If the land has a published scene with a project ID, we load this project, if not we continue looking
 46        /// 2. If the scene has been published with the stateless manifest, we create a project from it, if not we conti
 47        /// 3. If the land has a linked project ( by the coords) we load the project, if not we continue looking
 48        /// 4. If there is not project associated and we cant create it from a stateless manifest, we just create an emp
 49        /// </summary>
 50        /// <param name="builderAPIController"></param>
 51        /// <param name="landCoords"></param>
 52        /// <param name="scene"></param>
 53        /// <param name="parcelSize"></param>
 54        public async void GetInitialStateManifest(IBuilderAPIController builderAPIController, string landCoords, Scene s
 55        {
 056            Manifest.Manifest manifest = null;
 057            response.hasBeenCreated = false;
 58
 59            //We check if there is a project associated with the land, if there is we load the manifest
 060            if (!string.IsNullOrEmpty(scene.projectId))
 61            {
 062                manifest = await GetProjectById(builderAPIController, scene.projectId);
 63
 64                //If there is no project associated, we continue to looking
 065                if (manifest != null)
 66                {
 067                    response.manifest = manifest;
 068                    masterManifestPromise.Resolve(response);
 069                    return;
 70                }
 71            }
 72
 73            //We try to look for coordinates manifest
 074            manifest = await GetManifestByCoordinates(builderAPIController, landCoords);
 75
 76            //If we can't find a project associated with the land coords, we continue to looking
 077            if (manifest != null)
 78            {
 079                response.manifest = manifest;
 080                masterManifestPromise.Resolve(response);
 081                return;
 82            }
 83
 84            //We look if the scene has been published with a stateful definition
 085            if (scene.HasContent(BIWSettings.BUILDER_SCENE_STATE_DEFINITION_FILE_NAME))
 86            {
 087                manifest = await GetManifestByStatefullDefinition(parcelSize, landCoords, scene);
 88
 89                //If we can't create a manifest from the stateful definition, we continue to looking
 090                if (manifest != null)
 91                {
 092                    response.manifest = manifest;
 093                    response.hasBeenCreated = true;
 094                    masterManifestPromise.Resolve(response);
 095                    return;
 96                }
 97            }
 98
 99            // If there is no builder project deployed in the land, we just create a new one
 0100            manifest = BIWUtils.CreateEmptyDefaultBuilderManifest(parcelSize, landCoords);
 0101            response.manifest = manifest;
 0102            response.hasBeenCreated = true;
 0103            masterManifestPromise.Resolve(response);
 0104        }
 105
 106        public async UniTask<Manifest.Manifest> GetManifestByCoordinates(IBuilderAPIController builderAPIController, str
 107        {
 0108            Manifest.Manifest manifest = null;
 109
 0110            var manifestPromise = builderAPIController.GetManifestByCoords(landCoords);
 0111            manifestPromise.Then(resultManifest =>
 112            {
 0113                manifest = resultManifest;
 0114            });
 0115            manifestPromise.Catch( error =>
 116            {
 117                //If the project is not found,the user never created a scene in the land
 0118                if (error != BIWSettings.PROJECT_NOT_FOUND)
 0119                    masterManifestPromise.Reject(error);
 0120            });
 121
 0122            await manifestPromise;
 0123            return manifest;
 0124        }
 125
 126        public async UniTask<Manifest.Manifest> GetManifestByStatefullDefinition(Vector2Int size, string landCoords, Sce
 127        {
 0128            Manifest.Manifest manifest = null;
 129
 0130            if (!scene.TryGetHashForContent(BIWSettings.BUILDER_SCENE_STATE_DEFINITION_FILE_NAME, out string stateManife
 0131                return null;
 132
 0133            if (!scene.TryGetHashForContent(BIWSettings.BUILDER_SCENE_ASSET_FILE_NAME, out string assetHash))
 0134                return null;
 135
 0136            var assetPromise = Environment.i.platform.serviceProviders.catalyst.GetContent(assetHash);
 0137            var statelessPromise = Environment.i.platform.serviceProviders.catalyst.GetContent(stateManifestHash);
 138
 0139            (string assetsString, string statelesString) =  await UniTask.WhenAll(assetPromise, statelessPromise);
 140
 141            try
 142            {
 0143                AssetCatalogBridge.i.AddScenesObjectToSceneCatalog(assetsString);
 0144            }
 0145            catch { }
 146
 147            try
 148            {
 0149                StatelessManifest statelessManifest = JsonConvert.DeserializeObject<StatelessManifest>(statelesString);
 0150                manifest = BIWUtils.CreateEmptyDefaultBuilderManifest(size, landCoords);
 0151                manifest.scene = ManifestTranslator.StatelessToWebBuilderScene(statelessManifest, size);
 0152                manifest.project.scene_id = manifest.scene.id;
 0153            }
 0154            catch { }
 155
 0156            return manifest;
 0157        }
 158
 159        public async UniTask<Manifest.Manifest> GetProjectById(IBuilderAPIController builderAPIController, string projec
 160        {
 0161            Manifest.Manifest manifest = null;
 162
 0163            var manifestPromise = builderAPIController.GetManifestById(projectId);
 0164            manifestPromise.Then(resultManifest =>
 165            {
 0166                manifest = resultManifest;
 0167            });
 0168            manifestPromise.Catch( error =>
 169            {
 170                //If the project is not found,the user has deleted the project, we move to the next step
 0171                if (error != BIWSettings.PROJECT_NOT_FOUND)
 0172                    masterManifestPromise.Reject(error);
 0173            });
 174
 0175            await manifestPromise;
 0176            return manifest;
 0177        }
 178    }
 179
 180    public class InitialStateResponse
 181    {
 182        public Manifest.IManifest manifest;
 183        public bool hasBeenCreated = false;
 184    }
 185}