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