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