| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using MainScripts.DCL.Helpers.Utils; |
| | 5 | | using System; |
| | 6 | | using System.Threading; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.Networking; |
| | 9 | | using Object = UnityEngine.Object; |
| | 10 | |
|
| | 11 | | namespace DCLServices.MapRendererV2.MapLayers.Atlas.SatelliteAtlas |
| | 12 | | { |
| | 13 | | public class SatelliteChunkController : IChunkController |
| | 14 | | { |
| | 15 | | private const string CHUNKS_API = "https://media.githubusercontent.com/media/genesis-city/genesis.city/master/ma |
| | 16 | |
|
| | 17 | | private readonly SpriteRenderer spriteRenderer; |
| | 18 | |
|
| | 19 | | private Service<IWebRequestController> webRequestController; |
| | 20 | |
|
| | 21 | | private CancellationTokenSource internalCts; |
| | 22 | | private CancellationTokenSource linkedCts; |
| | 23 | | private int webRequestAttempts; |
| | 24 | |
|
| 550 | 25 | | public SatelliteChunkController(SpriteRenderer prefab, Vector3 chunkLocalPosition, Vector2Int coordsCenter, Tran |
| | 26 | | { |
| 550 | 27 | | internalCts = new CancellationTokenSource(); |
| | 28 | |
|
| 550 | 29 | | spriteRenderer = Object.Instantiate(prefab, parent); |
| 550 | 30 | | spriteRenderer.transform.localPosition = chunkLocalPosition; |
| 550 | 31 | | spriteRenderer.sortingOrder = drawOrder; |
| | 32 | |
|
| | 33 | | #if UNITY_EDITOR |
| 550 | 34 | | spriteRenderer.gameObject.name = $"Chunk {coordsCenter.x},{coordsCenter.y}"; |
| | 35 | | #endif |
| 550 | 36 | | } |
| | 37 | |
|
| | 38 | | public void Dispose() |
| | 39 | | { |
| 0 | 40 | | internalCts?.Cancel(); |
| 0 | 41 | | linkedCts?.Dispose(); |
| 0 | 42 | | linkedCts = null; |
| | 43 | |
|
| 0 | 44 | | internalCts?.Dispose(); |
| 0 | 45 | | internalCts = null; |
| | 46 | |
|
| 0 | 47 | | if (spriteRenderer) |
| 0 | 48 | | Utils.SafeDestroy(spriteRenderer.gameObject); |
| 0 | 49 | | } |
| | 50 | |
|
| | 51 | | public async UniTask LoadImage(Vector2Int chunkId, float chunkWorldSize, CancellationToken ct) |
| | 52 | | { |
| 550 | 53 | | webRequestAttempts = 0; |
| 550 | 54 | | linkedCts = CancellationTokenSource.CreateLinkedTokenSource(internalCts.Token, ct); |
| | 55 | |
|
| 550 | 56 | | var url = $"{CHUNKS_API}{chunkId.x}%2C{chunkId.y}.jpg"; |
| | 57 | |
|
| 1650 | 58 | | UnityWebRequest webRequest = await GetTextureFromWebAsync(url, ct: linkedCts.Token); |
| | 59 | |
|
| 81 | 60 | | Texture2D texture = CreateTexture(webRequest.downloadHandler.data); |
| 81 | 61 | | texture.wrapMode = TextureWrapMode.Clamp; |
| | 62 | |
|
| 81 | 63 | | float pixelsPerUnit = texture.width / chunkWorldSize; |
| | 64 | |
|
| 81 | 65 | | spriteRenderer.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2Ext.One |
| | 66 | | 0, SpriteMeshType.FullRect, Vector4.one, false); |
| | 67 | |
|
| 81 | 68 | | return; |
| | 69 | |
|
| | 70 | | Texture2D CreateTexture(byte[] data) |
| | 71 | | { |
| 81 | 72 | | var texture2D = new Texture2D(1, 1); |
| 81 | 73 | | texture2D.LoadImage(data); |
| 81 | 74 | | return texture2D; |
| | 75 | | } |
| 81 | 76 | | } |
| | 77 | |
|
| | 78 | | private async UniTask<UnityWebRequest> GetTextureFromWebAsync(string url, int maxAttempts = 3, CancellationToken |
| | 79 | | { |
| | 80 | | UnityWebRequest webRequest; |
| | 81 | | #if UNITY_EDITOR |
| | 82 | | // fixes editor exception for downloading pictures from github: "UnityWebRequestException: Unable to complet |
| 1731 | 83 | | try { webRequest = await webRequestController.Ref.GetTextureAsync(url, cancellationToken: linkedCts.Token); |
| 0 | 84 | | catch (UnityWebRequestException e) when (e.Message.Contains("Unable to complete SSL connection")) |
| | 85 | | { |
| 0 | 86 | | if (webRequestAttempts < maxAttempts) |
| | 87 | | { |
| 0 | 88 | | webRequestAttempts++; |
| 0 | 89 | | webRequest = await GetTextureFromWebAsync(url, ct: linkedCts.Token); |
| | 90 | | } |
| | 91 | | else |
| 0 | 92 | | throw; |
| | 93 | | } |
| | 94 | | #else |
| | 95 | | webRequest = await webRequestController.Ref.GetTextureAsync(url, cancellationToken: linkedCts.Token); |
| | 96 | | #endif |
| | 97 | |
|
| 81 | 98 | | return webRequest; |
| 81 | 99 | | } |
| | 100 | | } |
| | 101 | | } |