| | 1 | | using System; |
| | 2 | | using System.Threading; |
| | 3 | | using Cysharp.Threading.Tasks; |
| | 4 | | using DCL; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.Networking; |
| | 7 | |
|
| | 8 | | public class MapTexturePlugin : IPlugin |
| | 9 | | { |
| | 10 | | private const int RETRY_TIME = 10; |
| | 11 | | private const string MAIN_TEXTURE_URL = "https://api.decentraland.org/v1/minimap.png"; |
| | 12 | | private const string ESTATES_TEXTURE_URL = "https://api.decentraland.org/v1/estatemap.png"; |
| | 13 | |
|
| | 14 | | private CancellationTokenSource cts; |
| | 15 | |
|
| 0 | 16 | | public MapTexturePlugin() |
| | 17 | | { |
| 0 | 18 | | cts = new CancellationTokenSource(); |
| | 19 | |
|
| 0 | 20 | | DataStore.i.HUDs.mapMainTexture.Set(Resources.Load<Texture2D>("MapDefault")); |
| 0 | 21 | | DataStore.i.HUDs.mapEstatesTexture.Set(Resources.Load<Texture2D>("MapDefaultEstates")); |
| | 22 | |
|
| 0 | 23 | | DownloadTexture(MAIN_TEXTURE_URL, DataStore.i.HUDs.mapMainTexture, cts.Token); |
| 0 | 24 | | DownloadTexture(ESTATES_TEXTURE_URL, DataStore.i.HUDs.mapEstatesTexture, cts.Token); |
| 0 | 25 | | } |
| | 26 | |
|
| | 27 | | private static async UniTaskVoid DownloadTexture(string url, BaseVariable<Texture> textureVariable, CancellationToke |
| | 28 | | { |
| 0 | 29 | | while (true) |
| | 30 | | { |
| 0 | 31 | | using (UnityWebRequest www = UnityWebRequest.Get(url)) |
| | 32 | | { |
| 0 | 33 | | await www.SendWebRequest(); |
| 0 | 34 | | if (www.error != null) |
| | 35 | | { |
| 0 | 36 | | Debug.LogException(new Exception(www.error)); |
| 0 | 37 | | } |
| | 38 | | else |
| | 39 | | { |
| 0 | 40 | | Texture2D tex = new Texture2D(0, 0, TextureFormat.ETC2_RGBA8, false, true); |
| 0 | 41 | | tex.filterMode = FilterMode.Point; |
| 0 | 42 | | tex.wrapMode = TextureWrapMode.Clamp; |
| 0 | 43 | | tex.LoadImage(www.downloadHandler.data); |
| 0 | 44 | | tex.Apply(false, false); |
| 0 | 45 | | textureVariable.Set(tex); |
| 0 | 46 | | return; |
| | 47 | | } |
| 0 | 48 | | } |
| | 49 | |
|
| | 50 | | // We retry in 10 seconds |
| 0 | 51 | | await UniTask.Delay(TimeSpan.FromSeconds(RETRY_TIME), cancellationToken: ct).AttachExternalCancellation(ct); |
| | 52 | | } |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | public void Dispose() |
| | 56 | | { |
| 0 | 57 | | cts?.Cancel(); |
| 0 | 58 | | cts?.Dispose(); |
| 0 | 59 | | cts = null; |
| 0 | 60 | | } |
| | 61 | | } |