| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Runtime.Remoting.Contexts; |
| | 5 | | using Cysharp.Threading.Tasks; |
| | 6 | | using DCL.Builder.Manifest; |
| | 7 | | using DCL.Configuration; |
| | 8 | | using DCL.Helpers; |
| | 9 | | using Newtonsoft.Json; |
| | 10 | | using UnityEngine; |
| | 11 | |
|
| | 12 | | namespace 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 | | { |
| 0 | 33 | | private InitialStateResponse response = new InitialStateResponse(); |
| | 34 | | private Promise<InitialStateResponse> masterManifestPromise; |
| | 35 | |
|
| | 36 | | public Promise<InitialStateResponse> GetInitialManifest(IBuilderAPIController builderAPIController, string landC |
| | 37 | | { |
| 0 | 38 | | masterManifestPromise = new Promise<InitialStateResponse>(); |
| 0 | 39 | | GetInitialStateManifest(builderAPIController, landCoords, scene, parcelSize); |
| 0 | 40 | | 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 | | { |
| 0 | 56 | | Manifest.Manifest manifest = null; |
| 0 | 57 | | response.hasBeenCreated = false; |
| | 58 | |
|
| | 59 | | //We check if there is a project associated with the land, if there is we load the manifest |
| 0 | 60 | | if (!string.IsNullOrEmpty(scene.projectId)) |
| | 61 | | { |
| 0 | 62 | | manifest = await GetProjectById(builderAPIController, scene.projectId); |
| | 63 | |
|
| | 64 | | //If there is no project associated, we continue to looking |
| 0 | 65 | | if (manifest != null) |
| | 66 | | { |
| 0 | 67 | | response.manifest = manifest; |
| 0 | 68 | | masterManifestPromise.Resolve(response); |
| 0 | 69 | | return; |
| | 70 | | } |
| | 71 | | } |
| | 72 | |
|
| | 73 | | //We try to look for coordinates manifest |
| 0 | 74 | | manifest = await GetManifestByCoordinates(builderAPIController, landCoords); |
| | 75 | |
|
| | 76 | | //If we can't find a project associated with the land coords, we continue to looking |
| 0 | 77 | | if (manifest != null) |
| | 78 | | { |
| 0 | 79 | | response.manifest = manifest; |
| 0 | 80 | | masterManifestPromise.Resolve(response); |
| 0 | 81 | | return; |
| | 82 | | } |
| | 83 | |
|
| | 84 | | //We look if the scene has been published with a stateful definition |
| 0 | 85 | | if (scene.HasContent(BIWSettings.BUILDER_SCENE_STATE_DEFINITION_FILE_NAME)) |
| | 86 | | { |
| 0 | 87 | | manifest = await GetManifestByStatefullDefinition(parcelSize, landCoords, scene); |
| | 88 | |
|
| | 89 | | //If we can't create a manifest from the stateful definition, we continue to looking |
| 0 | 90 | | if (manifest != null) |
| | 91 | | { |
| 0 | 92 | | response.manifest = manifest; |
| 0 | 93 | | response.hasBeenCreated = true; |
| 0 | 94 | | masterManifestPromise.Resolve(response); |
| 0 | 95 | | return; |
| | 96 | | } |
| | 97 | | } |
| | 98 | |
|
| | 99 | | // If there is no builder project deployed in the land, we just create a new one |
| 0 | 100 | | manifest = BIWUtils.CreateEmptyDefaultBuilderManifest(parcelSize, landCoords); |
| 0 | 101 | | response.manifest = manifest; |
| 0 | 102 | | response.hasBeenCreated = true; |
| 0 | 103 | | masterManifestPromise.Resolve(response); |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | | public async UniTask<Manifest.Manifest> GetManifestByCoordinates(IBuilderAPIController builderAPIController, str |
| | 107 | | { |
| 0 | 108 | | Manifest.Manifest manifest = null; |
| | 109 | |
|
| 0 | 110 | | var manifestPromise = builderAPIController.GetManifestByCoords(landCoords); |
| 0 | 111 | | manifestPromise.Then(resultManifest => |
| | 112 | | { |
| 0 | 113 | | manifest = resultManifest; |
| 0 | 114 | | }); |
| 0 | 115 | | manifestPromise.Catch( error => |
| | 116 | | { |
| | 117 | | //If the project is not found,the user never created a scene in the land |
| 0 | 118 | | if (error != BIWSettings.PROJECT_NOT_FOUND) |
| 0 | 119 | | masterManifestPromise.Reject(error); |
| 0 | 120 | | }); |
| | 121 | |
|
| 0 | 122 | | await manifestPromise; |
| 0 | 123 | | return manifest; |
| 0 | 124 | | } |
| | 125 | |
|
| | 126 | | public async UniTask<Manifest.Manifest> GetManifestByStatefullDefinition(Vector2Int size, string landCoords, Sce |
| | 127 | | { |
| 0 | 128 | | Manifest.Manifest manifest = null; |
| | 129 | |
|
| 0 | 130 | | if (!scene.TryGetHashForContent(BIWSettings.BUILDER_SCENE_STATE_DEFINITION_FILE_NAME, out string stateManife |
| 0 | 131 | | return null; |
| | 132 | |
|
| 0 | 133 | | if (!scene.TryGetHashForContent(BIWSettings.BUILDER_SCENE_ASSET_FILE_NAME, out string assetHash)) |
| 0 | 134 | | return null; |
| | 135 | |
|
| 0 | 136 | | var assetPromise = Environment.i.platform.serviceProviders.catalyst.GetContent(assetHash); |
| 0 | 137 | | var statelessPromise = Environment.i.platform.serviceProviders.catalyst.GetContent(stateManifestHash); |
| | 138 | |
|
| 0 | 139 | | (string assetsString, string statelesString) = await UniTask.WhenAll(assetPromise, statelessPromise); |
| | 140 | |
|
| | 141 | | try |
| | 142 | | { |
| 0 | 143 | | AssetCatalogBridge.i.AddScenesObjectToSceneCatalog(assetsString); |
| 0 | 144 | | } |
| 0 | 145 | | catch { } |
| | 146 | |
|
| | 147 | | try |
| | 148 | | { |
| 0 | 149 | | StatelessManifest statelessManifest = JsonConvert.DeserializeObject<StatelessManifest>(statelesString); |
| 0 | 150 | | manifest = BIWUtils.CreateEmptyDefaultBuilderManifest(size, landCoords); |
| 0 | 151 | | manifest.scene = ManifestTranslator.StatelessToWebBuilderScene(statelessManifest, size); |
| 0 | 152 | | manifest.project.scene_id = manifest.scene.id; |
| 0 | 153 | | } |
| 0 | 154 | | catch { } |
| | 155 | |
|
| 0 | 156 | | return manifest; |
| 0 | 157 | | } |
| | 158 | |
|
| | 159 | | public async UniTask<Manifest.Manifest> GetProjectById(IBuilderAPIController builderAPIController, string projec |
| | 160 | | { |
| 0 | 161 | | Manifest.Manifest manifest = null; |
| | 162 | |
|
| 0 | 163 | | var manifestPromise = builderAPIController.GetManifestById(projectId); |
| 0 | 164 | | manifestPromise.Then(resultManifest => |
| | 165 | | { |
| 0 | 166 | | manifest = resultManifest; |
| 0 | 167 | | }); |
| 0 | 168 | | manifestPromise.Catch( error => |
| | 169 | | { |
| | 170 | | //If the project is not found,the user has deleted the project, we move to the next step |
| 0 | 171 | | if (error != BIWSettings.PROJECT_NOT_FOUND) |
| 0 | 172 | | masterManifestPromise.Reject(error); |
| 0 | 173 | | }); |
| | 174 | |
|
| 0 | 175 | | await manifestPromise; |
| 0 | 176 | | return manifest; |
| 0 | 177 | | } |
| | 178 | | } |
| | 179 | |
|
| | 180 | | public class InitialStateResponse |
| | 181 | | { |
| | 182 | | public Manifest.IManifest manifest; |
| | 183 | | public bool hasBeenCreated = false; |
| | 184 | | } |
| | 185 | | } |