private IEnumerator DownloadFromUrl(string url, DownloadCallback onComplete) { #if ENABLE_DEBUG_LOGS Debug.Log($"Downloading file at url: {url}\nSaving at path: {_cacheFilePath}"); #endif using UnityWebRequest request = new UnityWebRequest(url) { downloadHandler = new AssetBundlesDownloadHandler(_cacheFilePath) }; UnityWebRequestAsyncOperation operation = request.SendWebRequest(); while (!operation.isDone) { yield return(new WaitForEndOfFrame()); } bool success = request.result == UnityWebRequest.Result.Success; #if ENABLE_DEBUG_LOGS Debug.Log($"Download request result: {request.result}"); if (!success) { Debug.LogError($"Download request error: {request.error}"); } #endif yield return(onComplete.Invoke(success, _cacheFilePath)); }
IEnumerator Get(string uri, DownloadCallback callback) { using (UnityWebRequest webRequest = UnityWebRequest.Get(uri)) { // Request and wait for the desired page. yield return(webRequest.SendWebRequest()); string[] pages = uri.Split('/'); int page = pages.Length - 1; if (webRequest.isNetworkError) { Debug.Log(pages[page] + ": Error: " + webRequest.error); callback?.Invoke(webRequest.error, ""); } else { Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text); callback?.Invoke("OK", webRequest.downloadHandler.text); } } }
/// <summary> /// Attempts to start downloading a shared file. /// </summary> /// <returns>True if the download has successfully started</returns> public bool Download(DownloadCallback onSuccess = null, FailureCallback onFailure = null) { if (!_isUgc) { return(false); } if (_isDownloading) { return(false); } if (IsDownloaded) { return(false); } _isDownloading = true; remoteStorage.native.UGCDownload(_handle, 1000, (result, error) => { _isDownloading = false; if (error || result.Result != Result.OK) { onFailure?.Invoke(result.Result == 0 ? Callbacks.Result.IOFailure : (Callbacks.Result)result.Result); return; } _ownerId = result.SteamIDOwner; _sizeInBytes = result.SizeInBytes; _fileName = result.PchFileName; unsafe { _downloadedData = new byte[_sizeInBytes]; fixed(byte *bufferPtr = _downloadedData) { remoteStorage.native.UGCRead(_handle, (IntPtr)bufferPtr, _sizeInBytes, 0, UGCReadAction.ontinueReading); } } onSuccess?.Invoke(); }); return(true); }
/// <summary> /// Asynchronously download an object from an XML stream. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="url">The url to download from</param> /// <param name="postData">The http POST data to pass with the url. May be null.</param> /// <param name="callback">A callback invoked on the UI thread</param> /// <returns></returns> public static void DownloadXMLAsync <T>(string url, HttpPostData postData, DownloadCallback <T> callback) where T : class { EveClient.HttpWebService.DownloadXmlAsync(url, postData, // Callback (asyncResult, userState) => { T result = null; string errorMessage = ""; // Was there an HTTP error ?? if (asyncResult.Error != null) { errorMessage = asyncResult.Error.Message; } // No http error, let's try to deserialize else { try { // Deserialize using (XmlNodeReader reader = new XmlNodeReader(asyncResult.Result)) { XmlSerializer xs = new XmlSerializer(typeof(T)); result = (T)xs.Deserialize(reader); } } // An error occured during the deserialization catch (InvalidOperationException exc) { ExceptionHandler.LogException(exc, true); errorMessage = (exc.InnerException == null ? exc.Message : exc.InnerException.Message); } } // We got the result, let's invoke the callback on this actor Dispatcher.Invoke(() => callback.Invoke(result, errorMessage)); }, null); }