Beispiel #1
0
        private IEnumerator DownloadManager(List <string> dlQueue)
        {
            string          currentDownload = "";
            UnityWebRequest www;

            while (true)
            {
                while (dlQueue.Count == 0)
                {
                    yield return((object)new WaitForSeconds(1f));
                }
                currentDownload = dlQueue[0];
                dlQueue.RemoveAt(0);
                if (!_jobs.ContainsKey(currentDownload))
                {
                    Debug.LogError((object)("[Downloader] Current download in queue does not have associated Job: " + currentDownload));
                }
                else
                {
                    www = UnityWebRequest.Get(_jobs[currentDownload].url);
                    www.SetRequestHeader("Authorization", GetAuthSign(www));
                    www.disposeDownloadHandlerOnDispose = true;
                    Debug.Log("Current download: " + currentDownload + " [" + _jobs[currentDownload].url + "]");
                    DataStream handler = new DataStream(_jobs[currentDownload].destination);
                    www.downloadHandler = handler;
                    www.SendWebRequest();

                    while (!www.isDone)
                    {
                        yield return(null);

                        if (!(www.result != UnityWebRequest.Result.Success) && _jobs.ContainsKey(currentDownload))
                        {
                            _jobs[currentDownload].Progress = handler.downloadedBytes;
                        }
                        else
                        {
                            break;
                        }
                    }

                    string error   = www.error;
                    bool   isError = www.result != UnityWebRequest.Result.Success;
                    long   code    = www.responseCode;

                    if (isError || code == 404L)
                    {
                        handler.FlushAndClose();
                        www.Dispose();

                        try {
                            if (_jobs.ContainsKey(currentDownload))
                            {
                                ;
                            }
                            File.Delete(_jobs[currentDownload].destination);
                        } catch (Exception ex) {
                            Debug.LogError("Error while deleting falied download file: " + ex?.ToString());
                        }

                        string responseMeaning = "";
                        if (response.ContainsKey(code.ToString()))
                        {
                            responseMeaning = response[code.ToString()];
                        }
                        _jobs[currentDownload].onEnd?.Invoke(false, "Given Error: " + error + ". Response code: " + code.ToString() + " " + responseMeaning);
                        _jobs.Remove(currentDownload);
                    }
                    else
                    {
                        handler.FlushAndClose();
                        www.disposeDownloadHandlerOnDispose = true;
                        www.Dispose();

                        if (code == 200L || _jobs[currentDownload].url.Contains("http"))
                        {
                            if (App.Data != null)
                            {
                                Debug.Log((object)("Trying to set local version of " + currentDownload));
                                // Todo:
                            }

                            _jobs[currentDownload].onEnd?.Invoke(true, null);
                            _jobs.Remove(currentDownload);
                        }
                        else
                        {
                            if (File.Exists(_jobs[currentDownload].destination))
                            {
                                File.Delete(_jobs[currentDownload].destination);
                                Debug.LogWarning((object)("Incomplete file deleted. ID: " + currentDownload));
                            }

                            string responseMeaning = "";
                            if (response.ContainsKey(code.ToString()))
                            {
                                responseMeaning = response[code.ToString()];
                            }
                            _jobs[currentDownload].onEnd?.Invoke(false, "Given Error: " + error + ". Response code: " + code.ToString() + " " + responseMeaning);
                            _jobs.Remove(currentDownload);
                        }

                        handler = null;
                    }
                }
            }
        }