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