| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using Cysharp.Threading.Tasks; |
| | 5 | | using DCL; |
| | 6 | | using DCL.Helpers; |
| | 7 | | using Decentraland.Bff; |
| | 8 | | using UnityEngine; |
| | 9 | | using Variables.RealmsInfo; |
| | 10 | |
|
| | 11 | | public class Catalyst : ICatalyst |
| | 12 | | { |
| | 13 | | private const float DEFAULT_CACHE_TIME = 5 * 60; |
| | 14 | | private const int MAX_POINTERS_PER_REQUEST = 90; |
| | 15 | |
|
| 0 | 16 | | public string contentUrl => realmContentServerUrl; |
| 1 | 17 | | public string lambdasUrl { get; private set; } |
| | 18 | |
|
| 122 | 19 | | private string realmDomain = "https://peer.decentraland.org"; |
| 122 | 20 | | private string realmContentServerUrl = "https://peer.decentraland.org/content"; |
| | 21 | |
|
| 122 | 22 | | private readonly IDataCache<CatalystSceneEntityPayload[]> deployedScenesCache = new DataCache<CatalystSceneEntityPay |
| | 23 | |
|
| 368 | 24 | | private CurrentRealmVariable playerRealm => DataStore.i.realm.playerRealm; |
| 244 | 25 | | private BaseVariable<AboutResponse.Types.LambdasInfo> aboutLambdas => DataStore.i.realm.playerRealmAboutLambdas; |
| 244 | 26 | | private BaseVariable<AboutResponse.Types.ContentInfo> aboutContent => DataStore.i.realm.playerRealmAboutContent; |
| 121 | 27 | | private BaseVariable<AboutResponse.Types.AboutConfiguration> aboutConfiguration => DataStore.i.realm.playerRealmAbou |
| | 28 | |
|
| | 29 | |
|
| 122 | 30 | | public Catalyst() |
| | 31 | | { |
| 122 | 32 | | if (playerRealm.Get() != null) |
| | 33 | | { |
| 1 | 34 | | realmDomain = playerRealm.Get().domain; |
| 1 | 35 | | lambdasUrl = $"{realmDomain}/lambdas"; |
| 1 | 36 | | realmContentServerUrl = playerRealm.Get().contentServerUrl; |
| | 37 | | } |
| 121 | 38 | | else if (aboutConfiguration.Get() != null) |
| | 39 | | { |
| | 40 | | //TODO: This checks are going to dissapear when we inject the urls in kernel. Currently they are null, |
| | 41 | | //and we dont want to override the ones that have been set up in playerRealm |
| 0 | 42 | | if (!string.IsNullOrEmpty(aboutLambdas.Get().PublicUrl)) |
| 0 | 43 | | lambdasUrl = aboutLambdas.Get().PublicUrl; |
| | 44 | |
|
| 0 | 45 | | if (!string.IsNullOrEmpty(aboutContent.Get().PublicUrl)) |
| 0 | 46 | | realmContentServerUrl = aboutContent.Get().PublicUrl; |
| | 47 | | } |
| | 48 | |
|
| 122 | 49 | | playerRealm.OnChange += PlayerRealmOnChange; |
| 122 | 50 | | aboutContent.OnChange += PlayerRealmAboutContentOnChange; |
| 122 | 51 | | aboutLambdas.OnChange += PlayerRealmAboutLambdasOnChange; |
| 122 | 52 | | } |
| | 53 | |
|
| | 54 | | public void Dispose() |
| | 55 | | { |
| 122 | 56 | | playerRealm.OnChange -= PlayerRealmOnChange; |
| 122 | 57 | | aboutContent.OnChange -= PlayerRealmAboutContentOnChange; |
| 122 | 58 | | aboutLambdas.OnChange -= PlayerRealmAboutLambdasOnChange; |
| 122 | 59 | | deployedScenesCache.Dispose(); |
| 122 | 60 | | } |
| | 61 | |
|
| | 62 | | public async UniTask<string> GetContent(string hash) |
| | 63 | | { |
| 0 | 64 | | string callResult = ""; |
| 0 | 65 | | string url = $"{realmContentServerUrl}/contents/" + hash; |
| | 66 | |
|
| 0 | 67 | | var callPromise = Get(url); |
| 0 | 68 | | callPromise.Then(result => { callResult = result; }); |
| | 69 | |
|
| 0 | 70 | | callPromise.Catch(error => { callResult = error; }); |
| 0 | 71 | | await callPromise; |
| 0 | 72 | | return callResult; |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | public Promise<CatalystSceneEntityPayload[]> GetDeployedScenes(string[] parcels) |
| | 76 | | { |
| 0 | 77 | | return GetDeployedScenes(parcels, DEFAULT_CACHE_TIME); |
| | 78 | | } |
| | 79 | |
|
| | 80 | | public Promise<CatalystSceneEntityPayload[]> GetDeployedScenes(string[] parcels, float cacheMaxAgeSeconds) |
| | 81 | | { |
| 0 | 82 | | var promise = new Promise<CatalystSceneEntityPayload[]>(); |
| | 83 | |
|
| 0 | 84 | | string cacheKey = string.Join(";", parcels); |
| | 85 | |
|
| 0 | 86 | | if (cacheMaxAgeSeconds >= 0) |
| | 87 | | { |
| 0 | 88 | | if (deployedScenesCache.TryGet(cacheKey, out CatalystSceneEntityPayload[] cacheValue, out float lastUpdate)) |
| | 89 | | { |
| 0 | 90 | | if (Time.unscaledTime - lastUpdate <= cacheMaxAgeSeconds) |
| | 91 | | { |
| 0 | 92 | | promise.Resolve(cacheValue); |
| 0 | 93 | | return promise; |
| | 94 | | } |
| | 95 | | } |
| | 96 | | } |
| | 97 | |
|
| 0 | 98 | | GetEntities(CatalystEntitiesType.SCENE, parcels) |
| | 99 | | .Then(json => |
| | 100 | | { |
| 0 | 101 | | CatalystSceneEntityPayload[] scenes = null; |
| 0 | 102 | | bool hasException = false; |
| | 103 | |
|
| | 104 | | try |
| | 105 | | { |
| 0 | 106 | | CatalystSceneEntityPayload[] parsedValue = Utils.ParseJsonArray<CatalystSceneEntityPayload[]>(json); |
| | 107 | |
|
| | 108 | | // remove duplicated |
| 0 | 109 | | List<CatalystSceneEntityPayload> noDuplicates = new List<CatalystSceneEntityPayload>(); |
| | 110 | |
|
| 0 | 111 | | for (int i = 0; i < parsedValue.Length; i++) |
| | 112 | | { |
| 0 | 113 | | var sceneToCheck = parsedValue[i]; |
| | 114 | |
|
| 0 | 115 | | if (noDuplicates.Any(scene => scene.id == sceneToCheck.id)) |
| | 116 | | continue; |
| | 117 | |
|
| 0 | 118 | | noDuplicates.Add(sceneToCheck); |
| | 119 | | } |
| | 120 | |
|
| 0 | 121 | | scenes = noDuplicates.ToArray(); |
| 0 | 122 | | } |
| 0 | 123 | | catch (Exception e) |
| | 124 | | { |
| 0 | 125 | | promise.Reject(e.Message); |
| 0 | 126 | | hasException = true; |
| 0 | 127 | | } |
| | 128 | | finally |
| | 129 | | { |
| 0 | 130 | | if (!hasException) |
| | 131 | | { |
| 0 | 132 | | deployedScenesCache.Add(cacheKey, scenes, DEFAULT_CACHE_TIME); |
| 0 | 133 | | promise.Resolve(scenes); |
| | 134 | | } |
| 0 | 135 | | } |
| 0 | 136 | | }) |
| 0 | 137 | | .Catch(error => promise.Reject(error)); |
| | 138 | |
|
| 0 | 139 | | return promise; |
| | 140 | | } |
| | 141 | |
|
| | 142 | | public Promise<string> GetEntities(string entityType, string[] pointers) |
| | 143 | | { |
| 0 | 144 | | Promise<string> promise = new Promise<string>(); |
| | 145 | |
|
| | 146 | | string[][] pointersGroupsToFetch; |
| | 147 | |
|
| 0 | 148 | | if (pointers.Length <= MAX_POINTERS_PER_REQUEST) { pointersGroupsToFetch = new[] { pointers }; } |
| | 149 | | else |
| | 150 | | { |
| | 151 | | // split pointers array in length of MAX_POINTERS_PER_REQUEST |
| 0 | 152 | | int i = 0; |
| | 153 | |
|
| 0 | 154 | | var query = from s in pointers |
| 0 | 155 | | let num = i++ |
| 0 | 156 | | group s by num / MAX_POINTERS_PER_REQUEST |
| | 157 | | into g |
| 0 | 158 | | select g.ToArray(); |
| | 159 | |
|
| 0 | 160 | | pointersGroupsToFetch = query.ToArray(); |
| | 161 | | } |
| | 162 | |
|
| 0 | 163 | | if (pointersGroupsToFetch.Length == 0) |
| | 164 | | { |
| 0 | 165 | | promise.Reject("error: no pointers to fetch"); |
| 0 | 166 | | return promise; |
| | 167 | | } |
| | 168 | |
|
| 0 | 169 | | Promise<string>[] splittedPromises = new Promise<string>[pointersGroupsToFetch.Length]; |
| | 170 | |
|
| 0 | 171 | | for (int i = 0; i < pointersGroupsToFetch.Length; i++) |
| | 172 | | { |
| 0 | 173 | | string urlParams = ""; |
| 0 | 174 | | urlParams = pointersGroupsToFetch[i].Aggregate(urlParams, (current, pointer) => current + $"&pointer={pointe |
| 0 | 175 | | string url = $"{realmContentServerUrl}/entities/{entityType}?{urlParams}"; |
| | 176 | |
|
| 0 | 177 | | splittedPromises[i] = Get(url); |
| | 178 | |
|
| 0 | 179 | | splittedPromises[i] |
| | 180 | | .Then(value => |
| | 181 | | { |
| | 182 | | // check if all other promises have been resolved |
| 0 | 183 | | for (int j = 0; j < splittedPromises.Length; j++) |
| | 184 | | { |
| 0 | 185 | | if (splittedPromises[j] == null || splittedPromises[j].keepWaiting || !string.IsNullOrEmpty(spli |
| | 186 | | } |
| | 187 | |
|
| | 188 | | // make sure not to continue if promise was already resolved |
| 0 | 189 | | if (!promise.keepWaiting) |
| 0 | 190 | | return; |
| | 191 | |
|
| | 192 | | // build json with all promises result |
| 0 | 193 | | string json = splittedPromises[0].value.Substring(1, splittedPromises[0].value.Length - 2); |
| | 194 | |
|
| 0 | 195 | | for (int j = 1; j < splittedPromises.Length; j++) |
| | 196 | | { |
| 0 | 197 | | string jsonContent = splittedPromises[j].value.Substring(1, splittedPromises[j].value.Length - 2 |
| | 198 | |
|
| 0 | 199 | | if (!string.IsNullOrEmpty(jsonContent)) |
| 0 | 200 | | json += $",{jsonContent}"; |
| | 201 | | } |
| | 202 | |
|
| 0 | 203 | | promise.Resolve($"[{json}]"); |
| 0 | 204 | | }); |
| | 205 | |
|
| 0 | 206 | | splittedPromises[i].Catch(error => promise.Reject(error)); |
| | 207 | | } |
| | 208 | |
|
| 0 | 209 | | return promise; |
| | 210 | | } |
| | 211 | |
|
| | 212 | | public Promise<string> Get(string url) |
| | 213 | | { |
| 0 | 214 | | Promise<string> promise = new Promise<string>(); |
| | 215 | |
|
| 0 | 216 | | DCL.Environment.i.platform.webRequest.Get(url, null, request => { promise.Resolve(request.webRequest.downloadHan |
| | 217 | |
|
| 0 | 218 | | return promise; |
| | 219 | | } |
| | 220 | |
|
| | 221 | | private void PlayerRealmOnChange(CurrentRealmModel current, CurrentRealmModel previous) |
| | 222 | | { |
| 0 | 223 | | realmDomain = current.domain; |
| 0 | 224 | | lambdasUrl = $"{realmDomain}/lambdas"; |
| 0 | 225 | | realmContentServerUrl = current.contentServerUrl; |
| 0 | 226 | | } |
| | 227 | |
|
| | 228 | | private void PlayerRealmAboutLambdasOnChange(AboutResponse.Types.LambdasInfo current, AboutResponse.Types.LambdasInf |
| | 229 | | { |
| 0 | 230 | | lambdasUrl = current.PublicUrl; |
| 0 | 231 | | } |
| | 232 | |
|
| | 233 | | private void PlayerRealmAboutContentOnChange(AboutResponse.Types.ContentInfo current, AboutResponse.Types.ContentInf |
| 0 | 234 | | realmContentServerUrl = current.PublicUrl; |
| | 235 | | } |