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