using System; using UnityEngine; using System.Threading.Tasks; using RestSharp; using Adrenak.Unex; namespace Adrenak.UniMap { public class UserPanoDownloader { /// /// Invoked everytime a request is finished. Null if the request was not successful /// public event Action OnLoaded; public event Action OnFailed; public event Action OnStarted; Texture32 m_Texture; RestRequestAsyncHandle m_Handle; bool m_Running; /// /// Downloads the panorama image using the known PanoID as a Promise /// /// The Pano ID to be downloaded /// The of the image to be downloaded /// public Task Download(string id, PanoSize size, TextureFormat format) { var promise = new TaskCompletionSource(); Download(id, size, format, result => promise.SetResult(result), exception => promise.SetException(exception) ); return promise.Task; } /// /// Downloads the panorama image using the known PanoID /// /// The PanoID to be downloaded /// The of the image to be downloaded. /// Callback containing the Texture2D of the pano image /// Callback containing the exception when the download fails public void Download(string panoID, PanoSize size, TextureFormat format, Action onResult = null, Action onException = null) { var width = PanoUtility.GetUserPanoWidth(size); string url = "https://lh5.googleusercontent.com/p/" + panoID + "=w" + width; m_Running = true; OnStarted?.Invoke(); var client = new RestClient(); var request = new RestRequest(url, Method.GET); client.ExecuteAsync(request, response => { if (!m_Running) return; Dispatcher.Enqueue(() => { if (response.IsSuccessful()) { var texture = new Texture2D(1, 1, format, true); texture.LoadImage(response.RawBytes); var result = Texture32.FromTexture2D(texture); MonoBehaviour.Destroy(texture); texture = null; onResult?.Invoke(result); OnLoaded?.Invoke(result); } else { OnFailed?.Invoke(response.GetException()); onException?.Invoke(response.GetException()); } }); }); } /// /// Not all panorama images are uploaded by Google users /// This function returns whether the panorama of the given ID can be downloaded as a User Pano as a Promise /// /// The Panorama ID to be checked /// Whether the instance can download the pano public Task IsAvailable(string id) { var source = new TaskCompletionSource(); IsAvailable(id, result => source.SetResult(result)); return source.Task; } /// /// Not all panorama images are uploaded by Google users /// This function returns whether the panorama of the given ID can be downloaded as a User Pano /// /// The Panorama ID to be checked /// Whether the pano can be downloaded public void IsAvailable(string panoID, Action result) { string url = "https://lh5.googleusercontent.com/p/" + panoID + "=w" + 1; new RestClient().ExecuteAsync(new RestRequest(url, Method.GET), response => { Dispatcher.Enqueue(() => { result?.Invoke(response.IsSuccessful()); }); }); } /// /// Destroys the internal Texture2D /// public void ClearTexture() { if (m_Texture != null) { m_Texture.Clear(); m_Texture = null; } } public void Stop() { m_Running = false; ClearTexture(); if (m_Handle != null) m_Handle.Abort(); } } }