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