Ejemplo n.º 1
0
        public async Task <ObjectResult <object> > DownloadPackage(int packageId, string token)
        {
            Uri url = new Uri(ApiUrl + $"download?token={token}&package_id={packageId}");

            OnDownloadProgressChanged?.Invoke(0);

            WebClient webClient = new WebClient();

            webClient.DownloadProgressChanged += (sender, e) =>
            {
                OnDownloadProgressChanged?.Invoke(e.ProgressPercentage);
            };

            string tempFname = Path.GetTempFileName();
            await webClient.DownloadFileTaskAsync(url, tempFname);

            if (ZipTools.IsCompressedData(tempFname))
            {
                return(new ObjectResult <object>(1, "Package succesfully downloaded!", tempFname));
            }
            else
            {
                ObjectResult <object> obj = JsonConvert.DeserializeObject <ObjectResult <object> >(File.ReadAllText(tempFname));
                obj.code    = 0;
                obj.content = tempFname;
                return(obj);
            }
        }
Ejemplo n.º 2
0
    /// <summary>
    /// 下载文件
    /// </summary>
    /// <param name="downloadList"></param>
    public void DownloadFiles(List <DownloadDataEntity> downloadList, OnDownloadComplete onComplete, OnDownloadProgressChanged onDownloadProgressChanged)
    {
        this.onDownloadProgressChanged = onDownloadProgressChanged;
        onDownloadComplete             = onComplete;
        TotalSize              = 0;
        TotalCount             = 0;
        m_CurrentDownloadSize  = 0;
        m_CurrentDownloadCount = 0;
        for (int i = 0; i < m_Routine.Length; ++i)
        {
            if (m_Routine[i] == null)
            {
                m_Routine[i] = gameObject.AddComponent <AssetBundleDownloadRoutine>();
            }
        }
        TotalCount = downloadList.Count;
        for (int i = 0; i < downloadList.Count; ++i)
        {
            m_nRoutineIndex = i % m_Routine.Length;

            m_Routine[m_nRoutineIndex].AddDownload(downloadList[i]);

            TotalSize += downloadList[i].Size;
        }

        for (int i = 0; i < m_Routine.Length; ++i)
        {
            m_Routine[i].StartDownload();
        }
    }
Ejemplo n.º 3
0
        internal async Task UpdateAsync()
        {
            App.Window.Dispatcher.Invoke(() =>
            {
                MainWindow.DownloadDialog.ShowAsync();
                MainWindow.DownloadDialog.DownloadUpdateAsync(this);
            });

            OnDownloadProgressChanged?.Invoke(0);

            WebClient webClient = new WebClient();

            webClient.DownloadProgressChanged += (sender, e) =>
            {
                OnDownloadProgressChanged?.Invoke(e.ProgressPercentage);
            };

            string tempFname = Path.GetTempFileName();
            await webClient.DownloadFileTaskAsync(UpdateUrl, tempFname);

            OnDownloaded?.Invoke();

            Thread.Sleep(3000);

            string oldFilename = Assembly.GetExecutingAssembly().Location;

            string ps = Resources.UpdateScript.Replace("##01", tempFname).Replace("##02", oldFilename);

            ExecuteCommand(ps);
            Environment.Exit(0);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 下载文件
 /// </summary>
 /// <returns></returns>
 public void DownFile()
 {
     Task.Run(() =>
     {
         if (_updateModel == null)
         {
             OnError?.Invoke("请先执行比较版本步骤");
             OnStep?.Invoke(UpdateStepEnum.Fail);
             return;
         }
         OnStep?.Invoke(UpdateStepEnum.DownFile);
         // 升级下载文件地址校验
         if (string.IsNullOrWhiteSpace(UpdateJsonAddress) || !_updateModel.DownFileAddress.MatchUrl())
         {
             OnError?.Invoke($"下载升级文件地址校验失败:{_updateModel.DownFileAddress}");
             OnStep?.Invoke(UpdateStepEnum.Fail);
             return;
         }
         _updateModel.DownFileSavePath = $"{DownFileSavePath}\\{Path.GetFileName(_updateModel.DownFileAddress)}";
         // 创建目录
         if (!Directory.Exists(DownFileSavePath))
         {
             Directory.CreateDirectory(DownFileSavePath);
         }
         // 验证证书
         ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, er) => true;
         // 下载
         var webClient = new WebClient();
         webClient.Headers.Add("User-Agent", "Mozilla / 5.0(compatible; MSIE 9.0; Windows NT 6.1; Trident / 5.0)");
         webClient.DownloadProgressChanged += (sender, e) =>
         {
             OnDownloadProgressChanged?.Invoke(e);
         };
         webClient.DownloadFileCompleted += (sender, e) =>
         {
             if (e.Error != null)
             {
                 OnError?.Invoke($"下载升级文件失败:{e.Error.Message} 地址:{_updateModel.DownFileAddress}");
                 OnStep?.Invoke(UpdateStepEnum.Fail);
                 return;
             }
             OnDownloadFileCompleted?.Invoke();
             if (IsSilent && IsExtract)
             {
                 // 解压文件
                 ExtractFile();
             }
             else
             {
                 // 删除文件
                 DeleteFile();
                 OnStep?.Invoke(UpdateStepEnum.Complete);
             }
         };
         // 开始下载
         webClient.DownloadFileAsync(new Uri(_updateModel.DownFileAddress), _updateModel.DownFileSavePath);
     });
 }
Ejemplo n.º 5
0
 public void DownLoadPackage(string url, string version, int size, Action <string> onDownloadPackageComplete, OnDownloadProgressChanged onDownloadProgressChanged)
 {
     this.onDownloadProgressChanged = onDownloadProgressChanged;
     m_CurrentDownloadSize          = 0;
     m_CurrentDownloadCount         = 0;
     TotalSize  = size;
     TotalCount = 1;
     StartCoroutine(DownLoadPackageCoroutine(url, version, size, onDownloadPackageComplete));
 }
Ejemplo n.º 6
0
 internal void TriggerDownloadProgressChangedEvent(DownloadDetails details, long downloadedSize, long totalSize)
 {
     OnDownloadProgressChanged?.Invoke(this, details, downloadedSize, totalSize);
 }
Ejemplo n.º 7
0
        public IEnumerator DownloadingFiles(List <UrlFilePath> urlFilePaths, ICancellationToken cancellationToken, OnDownloadCompleted onCompleted, OnDownloadProgressChanged onProgressChanged)
        {
            bool noError = true;

            for (int i = 0; i < urlFilePaths.Count; i++)
            {
                if (cancellationToken.Canceled)
                {
                    noError = false;
                    break;
                }
                int         currentI     = i;
                int         currentCount = urlFilePaths.Count;
                UrlFilePath item         = urlFilePaths[i];
                yield return(GameLogicUpdateSystem.StartCoroutine(DownloadingFile(item.Url, item.FilePath, cancellationToken,
                                                                                  (ne) => { if (!ne)
                                                                                            {
                                                                                                noError = ne;
                                                                                            }
                                                                                  },
                                                                                  (percents) =>
                {
                    int currentPercents = Mathf.RoundToInt((100 * currentI + percents) / (float)currentCount);
                    onProgressChanged?.Invoke(currentPercents);
                })));
            }
            onCompleted?.Invoke(noError);
        }
Ejemplo n.º 8
0
 public void DownloadFilesAsync(List <UrlFilePath> urlFilePaths, ICancellationToken cancellationToken, OnDownloadCompleted onCompleted, OnDownloadProgressChanged onProgressChanged)
 => GameLogicUpdateSystem.StartCoroutine(DownloadingFiles(urlFilePaths, cancellationToken, onCompleted, onProgressChanged));
Ejemplo n.º 9
0
        private IEnumerator DownloadingFile(string url, string filePath, ICancellationToken cancellationToken, OnDownloadCompleted onCompleted, OnDownloadProgressChanged onProgressChanged)
        {
            bool anyError = true;

            using (UnityWebRequest unityWebRequest = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET))
            {
                unityWebRequest.downloadHandler = new DownloadHandlerFile(filePath)
                {
                    removeFileOnAbort = true
                };
                UnityWebRequestAsyncOperation asyncOperation = unityWebRequest.SendWebRequest();

                int lastPercents = 0;
                while (!asyncOperation.isDone)
                {
                    yield return(new WaitForEndOfFrame());

                    if (cancellationToken.Canceled)
                    {
                        unityWebRequest.Abort();
                        break;
                    }

                    int currentPercents = Mathf.RoundToInt(asyncOperation.progress * 100f);
                    if (lastPercents < currentPercents)
                    {
                        onProgressChanged?.Invoke(currentPercents);
                        lastPercents = currentPercents;
                    }
                }

                anyError = unityWebRequest.isNetworkError || unityWebRequest.isHttpError;
                if (anyError)
                {
                    Debug.LogError(unityWebRequest.error);
                }
                else
                {
                    Debug.Log($"{nameof(HttpClient)}: File successfully downloaded and saved to " + filePath);
                }
            }
            onCompleted?.Invoke(!anyError);
        }
Ejemplo n.º 10
0
 public void DownloadFileAsync(UrlFilePath urlFilePath, ICancellationToken cancellationToken, OnDownloadCompleted onCompleted, OnDownloadProgressChanged onProgressChanged)
 => DownloadFileAsync(urlFilePath.Url, urlFilePath.FilePath, cancellationToken, onCompleted, onProgressChanged);
Ejemplo n.º 11
0
 public void DownloadFileAsync(string url, string filePath, ICancellationToken cancellationToken, OnDownloadCompleted onCompleted, OnDownloadProgressChanged onProgressChanged)
 => GameLogicUpdateSystem.StartCoroutine(DownloadingFile(url, filePath, cancellationToken, onCompleted, onProgressChanged));
Ejemplo n.º 12
0
 private void onDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
 {
     OnDownloadProgressChanged?.Invoke(sender, e);
 }