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