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