Exemple #1
0
        private void DownloadFinishEvent(object sender, EventArgs e)
        {
            bool           finish = (Boolean)sender;
            DownloadFinish status = new DownloadFinish(UploadFileResult);

            this.Invoke(status, finish);
        }
 public void AddDownloadTask(string url, DownloadFinish callback)
 {
     lock (this)
     {
         mainKey++;
         urlTask.Add(mainKey, url);
         callBackTask.Add(mainKey, callback);
     }
 }
        public void Download()
        {
            WebClient client = new WebClient();

            client.DownloadProgressChanged += Client_DownloadProgressChanged;
            new Thread(() =>
            {
                foreach (var item in song.items)// 多来源,避免单个来源出错
                {
                    try
                    {
                        client.DownloadFile(musicSources.getDownloadUrl(item), target + "\\" + item.getFileName());
                        DownloadFinish?.Invoke(this, this);
                        break;
                    }
                    catch
                    {
                    }
                }
            }).Start();
        }
Exemple #4
0
        public void Download()
        {
            WebClient client = new WebClient();

            client.DownloadProgressChanged += Clien_DownloadProgressChanged;
            new Thread(() =>
            {
                //多来源,防止单个来源出错
                foreach (var item in song1.items)
                {
                    try
                    {
                        client.DownloadFile(musicProviders.getDownloadUrl(item), target + "\\" + item.getFileName());
                        DownloadFinish?.Invoke(this, this);
                        break;
                    }
                    catch (Exception)
                    {
                    }
                }
            }).Start();
        }
        public void Download()
        {
            WebClient client = new WebClient();

            client.DownloadProgressChanged += Client_DownloadProgressChanged;
            new Thread(() =>
            {
                // 多来源,防止单个来源出错
                foreach (var item in song.items)
                {
                    try
                    {
                        client.DownloadFile(musicProviders.getDownloadUrl(item, rate), target + "\\" + item.getFileName());
                        break;
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                DownloadFinish?.Invoke(this, this);
            }).Start();
        }
Exemple #6
0
 protected virtual void OnDownloadFinish(bool isSuccess, string downloadPath, string fileId, string msg = null)
 {
     DownloadFinish?.Invoke(isSuccess, downloadPath, fileId, msg);
 }
Exemple #7
0
        private static IEnumerator UpdateModCoroutine(DependencyObject item, DownloadStart downloadStart,
                                                      DownloadProgress progress, DownloadFailed dlFail, DownloadFinish finish,
                                                      InstallFailed installFail, InstallFinish installFinish)
        { // (3.2)
            Logger.updater.Debug($"Release: {BeatSaber.ReleaseType}");

            var mod = new Ref <ApiEndpoint.Mod>(null);

            yield return(GetModInfo(item.Name, item.ResolvedVersion.ToString(), mod));

            try { mod.Verify(); }
            catch (Exception e)
            {
                Logger.updater.Error($"Error occurred while trying to get information for {item}");
                Logger.updater.Error(e);
                yield break;
            }

            var releaseName = BeatSaber.ReleaseType == BeatSaber.Release.Steam
                ? ApiEndpoint.Mod.DownloadsObject.TypeSteam : ApiEndpoint.Mod.DownloadsObject.TypeOculus;
            var platformFile = mod.Value.Downloads.First(f => f.Type == ApiEndpoint.Mod.DownloadsObject.TypeUniversal || f.Type == releaseName);

            string url = ApiEndpoint.BeatModBase + platformFile.Path;

            Logger.updater.Debug($"URL = {url}");

            const int maxTries = 3;
            int       tries    = maxTries;

            while (tries > 0)
            {
                if (tries-- != maxTries)
                {
                    Logger.updater.Debug("Re-trying download...");
                }

                using (var stream = new MemoryStream())
                    using (var request = UnityWebRequest.Get(url))
                        using (var taskTokenSource = new CancellationTokenSource())
                        {
                            var dlh = new StreamDownloadHandler(stream, (int i1, int i2, double d) => progress?.Invoke(item, i1, i2, d));
                            request.downloadHandler = dlh;

                            downloadStart?.Invoke(item);

                            Logger.updater.Debug("Sending request");
                            //Logger.updater.Debug(request?.downloadHandler?.ToString() ?? "DLH==NULL");
                            yield return(request.SendWebRequest());

                            Logger.updater.Debug("Download finished");

                            if (request.isNetworkError)
                            {
                                Logger.updater.Error("Network error while trying to update mod");
                                Logger.updater.Error(request.error);
                                dlFail?.Invoke(item, request.error);
                                taskTokenSource.Cancel();
                                continue;
                            }
                            if (request.isHttpError)
                            {
                                Logger.updater.Error("Server returned an error code while trying to update mod");
                                Logger.updater.Error(request.error);
                                dlFail?.Invoke(item, request.error);
                                taskTokenSource.Cancel();
                                continue;
                            }

                            finish?.Invoke(item);

                            stream.Seek(0, SeekOrigin.Begin); // reset to beginning

                            var downloadTask = Task.Run(() =>
                            { // use slightly more multi threaded approach than co-routines
                                // ReSharper disable once AccessToDisposedClosure
                                ExtractPluginAsync(stream, item, platformFile);
                            }, taskTokenSource.Token);

                            while (!(downloadTask.IsCompleted || downloadTask.IsCanceled || downloadTask.IsFaulted))
                            {
                                yield return(null); // pause co-routine until task is done
                            }
                            if (downloadTask.IsFaulted)
                            {
                                if (downloadTask.Exception != null && downloadTask.Exception.InnerExceptions.Any(e => e is BeatmodsInterceptException))
                                { // any exception is an intercept exception
                                    Logger.updater.Error($"BeatMods did not return expected data for {item.Name}");
                                }

                                Logger.updater.Error($"Error downloading mod {item.Name}");
                                Logger.updater.Error(downloadTask.Exception);

                                installFail?.Invoke(item, downloadTask.Exception);
                                continue;
                            }

                            break;
                        }
            }

            if (tries == 0)
            {
                Logger.updater.Warn($"Plugin download failed {maxTries} times, not re-trying");

                installFinish?.Invoke(item, true);
            }
            else
            {
                Logger.updater.Debug("Download complete");
                installFinish?.Invoke(item, false);
            }
        }
Exemple #8
0
 internal void StartDownload(IEnumerable <DependencyObject> download, DownloadStart downloadStart = null,
                             DownloadProgress downloadProgress = null, DownloadFailed downloadFail = null, DownloadFinish downloadFinish = null,
                             InstallFailed installFail         = null, InstallFinish installFinish = null)
 {
     foreach (var item in download)
     {
         StartCoroutine(UpdateModCoroutine(item, downloadStart, downloadProgress, downloadFail, downloadFinish, installFail, installFinish));
     }
 }
    public IEnumerator DownloadImages(string url, DownloadFinish callback)
    {
        bool isNet = false;

        isDowning = true;
        WWW www = null;

        if (imageDic.ContainsKey(url))
        {
            //替换本地路径
            //Debug.Log("Load from location............");
            www = new WWW(LocationPath + "/" + url);
        }
        else
        {
            isNet = true;
            //Debug.Log("Load from network............" + (FileUrl + WWW.EscapeURL(url)));
            www = new WWW(FileUrl + WWW.EscapeURL(url));
        }
        //定义www为WWW类型并且等于所下载下来的WWW中内容。
        yield return(www);

        isDowning = false;

        if (www.error != null)
        {
            Debug.LogError(www.error);
            if (callback != null)
            {
                callback(null, true, FileType.Other, "");
            }
        }
        else
        {
            if (isNet)
            {
                imageDic.Add(url, "");
            }
            SaveData(www.bytes, url);
            //if (callback != null) callback(www.bytes, false, FileType.Image);
            if (url.EndsWith(".mp4"))
            {
                if (callback != null)
                {
                    callback(www.bytes, false, FileType.Video, url);
                }
            }
            else if (url.EndsWith(".jpg") || url.EndsWith("png"))
            {
                SaveSize(url, www.bytes);
                if (callback != null)
                {
                    callback(www.bytes, false, FileType.Image, url);
                }
            }
            else
            {
                if (callback != null)
                {
                    callback(www.bytes, false, FileType.Other, url);
                }
            }
            //if (url.EndsWith(".mp4"))
            //{
            //    //sf.OnFadeOut();
            //    SaveData(www.bytes, url);
            //    //videoPlayer.url = VideoPath + "/" + url;
            //    //videoPlayer.gameObject.SetActive(true);
            //    //image.gameObject.SetActive(false);
            //    //videoPlayer.Play();
            //}
            //else if (url.EndsWith(".jpg") || url.EndsWith("png"))
            //{
            //    //sf.OnFadeOut();
            //    //videoPlayer.gameObject.SetActive(false);
            //    //image.gameObject.SetActive(true);
            //    Texture2D newTexture = www.texture;
            //    byte[] imageData = newTexture.EncodeToJPG();
            //    //if (image != null)
            //    //{
            //    //    image.sprite = Sprite.Create(newTexture, new Rect(0, 0, newTexture.width, newTexture.height), new Vector2(0.5f, 0.5f));
            //    //}
            //    SaveData(imageData, url);
            //}
            //else
            //{
            //    Debug.Log("url" + url + " 格式不支持!");
            //}
        }
    }