< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
InitialStateManager()0%110100%
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 Cysharp.Threading.Tasks;
 2using DCL.Configuration;
 3using DCL.Helpers;
 4using Newtonsoft.Json;
 5using UnityEngine;
 6
 7namespace DCL.Builder
 8{
 9    public interface IInitialStateManager
 10    {
 11        /// <summary>
 12        /// This call will look for the correct manifest of the land. The lookup will be the next
 13        /// 1. If the land has a published scene with a project ID, we load this project, if not we continue looking
 14        /// 2. If the scene has been published with the stateless manifest, we create a project from it, if not we conti
 15        /// 3. If the land has a linked project ( by the coords) we load the project, if not we continue looking
 16        /// 4. If there is not project associated and we cant create it from a stateless manifest, we just create an emp
 17        /// </summary>
 18        /// <param name="builderAPIController"></param>
 19        /// <param name="landCoords"></param>
 20        /// <param name="scene"></param>
 21        /// <param name="parcelSize"></param>
 22        /// <returns></returns>
 23        Promise<InitialStateResponse> GetInitialManifest(IBuilderAPIController builderAPIController, string landCoords, 
 24    }
 25
 26    public class InitialStateManager : IInitialStateManager
 27    {
 1428        private InitialStateResponse response = new InitialStateResponse();
 29        private Promise<InitialStateResponse> masterManifestPromise;
 30
 31        public Promise<InitialStateResponse> GetInitialManifest(IBuilderAPIController builderAPIController, string landC
 32        {
 033            masterManifestPromise = new Promise<InitialStateResponse>();
 034            GetInitialStateManifest(builderAPIController, landCoords, scene, parcelSize);
 035            return masterManifestPromise;
 36        }
 37
 38        /// <summary>
 39        /// This call will look for the correct manifest of the land. The lookup will be the next
 40        /// 1. If the land has a published scene with a project ID, we load this project, if not we continue looking
 41        /// 2. If the scene has been published with the stateless manifest, we create a project from it, if not we conti
 42        /// 3. If the land has a linked project ( by the coords) we load the project, if not we continue looking
 43        /// 4. If there is not project associated and we cant create it from a stateless manifest, we just create an emp
 44        /// </summary>
 45        /// <param name="builderAPIController"></param>
 46        /// <param name="landCoords"></param>
 47        /// <param name="scene"></param>
 48        /// <param name="parcelSize"></param>
 49        public async void GetInitialStateManifest(IBuilderAPIController builderAPIController, string landCoords, Scene s
 50        {
 051            Manifest.Manifest manifest = null;
 052            response.hasBeenCreated = false;
 53
 54            //We check if there is a project associated with the land, if there is we load the manifest
 055            if (!string.IsNullOrEmpty(scene.projectId))
 56            {
 057                manifest = await GetProjectById(builderAPIController, scene.projectId);
 58
 59                //If there is no project associated, we continue to looking
 060                if (manifest != null)
 61                {
 062                    response.manifest = manifest;
 063                    masterManifestPromise.Resolve(response);
 064                    return;
 65                }
 66            }
 67
 68            //We try to look for coordinates manifest
 069            manifest = await GetManifestByCoordinates(builderAPIController, landCoords);
 70
 71            //If we can't find a project associated with the land coords, we continue to looking
 072            if (manifest != null)
 73            {
 074                response.manifest = manifest;
 075                masterManifestPromise.Resolve(response);
 076                return;
 77            }
 78
 79            //We look if the scene has been published with a stateful definition
 080            if (scene.HasContent(BIWSettings.BUILDER_SCENE_STATE_DEFINITION_FILE_NAME))
 81            {
 082                manifest = await GetManifestByStatefullDefinition(parcelSize, landCoords, scene);
 83
 84                //If we can't create a manifest from the stateful definition, we continue to looking
 085                if (manifest != null)
 86                {
 087                    response.manifest = manifest;
 088                    response.hasBeenCreated = true;
 089                    masterManifestPromise.Resolve(response);
 090                    return;
 91                }
 92            }
 93
 94            // If there is no builder project deployed in the land, we just create a new one
 095            manifest = BIWUtils.CreateEmptyDefaultBuilderManifest(parcelSize, landCoords);
 096            response.manifest = manifest;
 097            response.hasBeenCreated = true;
 098            masterManifestPromise.Resolve(response);
 099        }
 100
 101        public async UniTask<Manifest.Manifest> GetManifestByCoordinates(IBuilderAPIController builderAPIController, str
 102        {
 0103            Manifest.Manifest manifest = null;
 104
 0105            var manifestPromise = builderAPIController.GetManifestByCoords(landCoords);
 0106            manifestPromise.Then(resultManifest =>
 107            {
 0108                manifest = resultManifest;
 0109            });
 0110            manifestPromise.Catch( error =>
 111            {
 112                //If the project is not found,the user never created a scene in the land
 0113                if (error != BIWSettings.PROJECT_NOT_FOUND)
 0114                    masterManifestPromise.Reject(error);
 0115            });
 116
 0117            await manifestPromise;
 0118            return manifest;
 0119        }
 120
 121        public async UniTask<Manifest.Manifest> GetManifestByStatefullDefinition(Vector2Int size, string landCoords, Sce
 122        {
 0123            Manifest.Manifest manifest = null;
 124
 0125            if (!scene.TryGetHashForContent(BIWSettings.BUILDER_SCENE_STATE_DEFINITION_FILE_NAME, out string stateManife
 0126                return null;
 127
 0128            if (!scene.TryGetHashForContent(BIWSettings.BUILDER_SCENE_ASSET_FILE_NAME, out string assetHash))
 0129                return null;
 130
 0131            var assetPromise = Environment.i.platform.serviceProviders.catalyst.GetContent(assetHash);
 0132            var statelessPromise = Environment.i.platform.serviceProviders.catalyst.GetContent(stateManifestHash);
 133
 0134            (string assetsString, string statelesString) =  await UniTask.WhenAll(assetPromise, statelessPromise);
 135
 136            try
 137            {
 0138                AssetCatalogBridge.i.AddScenesObjectToSceneCatalog(assetsString);
 0139            }
 0140            catch { }
 141
 142            try
 143            {
 0144                StatelessManifest statelessManifest = JsonConvert.DeserializeObject<StatelessManifest>(statelesString);
 0145                manifest = BIWUtils.CreateEmptyDefaultBuilderManifest(size, landCoords);
 0146                manifest.scene = ManifestTranslator.StatelessToWebBuilderScene(statelessManifest, size);
 0147                manifest.project.scene_id = manifest.scene.id;
 0148            }
 0149            catch { }
 150
 0151            return manifest;
 0152        }
 153
 154        public async UniTask<Manifest.Manifest> GetProjectById(IBuilderAPIController builderAPIController, string projec
 155        {
 0156            Manifest.Manifest manifest = null;
 157
 0158            var manifestPromise = builderAPIController.GetManifestById(projectId);
 0159            manifestPromise.Then(resultManifest =>
 160            {
 0161                manifest = resultManifest;
 0162            });
 0163            manifestPromise.Catch( error =>
 164            {
 165                //If the project is not found,the user has deleted the project, we move to the next step
 0166                if (error != BIWSettings.PROJECT_NOT_FOUND)
 0167                    masterManifestPromise.Reject(error);
 0168            });
 169
 0170            await manifestPromise;
 0171            return manifest;
 0172        }
 173    }
 174
 175    public class InitialStateResponse
 176    {
 177        public Manifest.IManifest manifest;
 178        public bool hasBeenCreated = false;
 179    }
 180}