Change download method WebClient on the AltoHttp

Маерович Сергей 1 Reputation point
2022-08-19T13:05:50.697+00:00

Good day, question, I can’t master AltoHttp, and I can’t change the download method from WebClient to AltoHttp
My Class:

using System;  
using System.Net;  
using System.IO;  
using System.Threading.Tasks;  
using Launcher.Game.Interfaces;  
using Launcher.Game.Events;  
using System.Diagnostics;  
using System.Threading;  
using AltoHttp;  
  
namespace Launcher.Game  
{  
  
  
    public class Downloader  
    {  
        private readonly string master_url;  
        private readonly IDownloadProvider downloadProvider;  
        private long currentDownloadedSize;  
        private long prevDownloadedSize;  
        private double speed;  
        private Stopwatch stopwatch = new Stopwatch();  
  
        /// <summary>  
        /// Представляет функциональность для закачки файлов  
        /// </summary>  
        /// <param name="MasterUrl">Ссылка, откуда будут скачиваться файлы.</param>  
        /// <param name="provider">Провайдер для загрузки.</param>  
        /// <param name="ntds">Необходимый размер закачки.</param>  
        public Downloader(string MasterUrl, IDownloadProvider provider, long ntds)  
        {  
            master_url = MasterUrl;  
            downloadProvider = provider;  
            currentDownloadedSize = ntds;  
        }  
  
        /// <summary>  
        /// Функционал загрузки файлов  
        /// </summary>  
        /// <param name="path">Абсолютный путь, куда файлы будут сохраняться</param>  
        /// <param name="progressChanged">Событие, вызывающееся при изменении прогресса (при подключении прогрессбара указать)</param>  
        /// <param name="completed">Событие, вызывающиеся при завершении закачки (при подключении прогрессбара указать)</param>  
        public async Task DownloadFiles(string path)  
        {  
  
            using (WebClient wc = new WebClient())  
            {  
                wc.DownloadProgressChanged += Wc_DownloadProgressChanged;  
                foreach (var fileName in downloadProvider.Files)  
                {  
                    CreateNeccessaryDirs(path + @"\" + fileName.Value);  
                    if (File.Exists(path + @"\" + fileName.Value))  
                    {  
                        continue;   
                    }  
                    prevDownloadedSize = 0L;  
                    StartDownload?.Invoke(this, new StartDownloadFileArgs { FileName = fileName.Value });  
                    stopwatch.Restart();  
                    await wc.DownloadFileTaskAsync(new Uri(master_url + "/" + fileName.Key), path + @"\" + fileName.Value);  
                    stopwatch.Stop();  
                }  
                ProgressChanged?.Invoke(this, new Events.DownloadProgressChangedEventArgs() { NewPercentage = 100 });  
            }  
  
        }  
  
        private void Wc_DownloadProgressChanged(object sender, System.Net.DownloadProgressChangedEventArgs e)  
        {  
            if (prevDownloadedSize == 0)  
            {  
                prevDownloadedSize = e.BytesReceived / 1024;  
            }  
  
            if (stopwatch.Elapsed.TotalSeconds >= 1)  
            {  
                var dKBytes = (e.BytesReceived / 1024) - prevDownloadedSize;  
                currentDownloadedSize += dKBytes;  
                speed = dKBytes / stopwatch.Elapsed.TotalSeconds;  
                prevDownloadedSize = 0L;  
                OnProgressChanged();  
                stopwatch.Restart();  
            }  
        }  
  
  
        private void CreateNeccessaryDirs(string fileName)  
        {  
            var idx = fileName.LastIndexOf("\\");  
            fileName = fileName.Remove(idx);  
            Directory.CreateDirectory(fileName);  
        }  
        private int GetPercentage() => (int)Math.Round((currentDownloadedSize / (float)downloadProvider.Size) * 100);   
  
        private void OnProgressChanged()  
        {  
            Events.DownloadProgressChangedEventArgs eventInfo;  
            eventInfo = new Events.DownloadProgressChangedEventArgs() { NewPercentage = GetPercentage(),   
                Speed = Math.Round(speed), DownloadedSize = currentDownloadedSize, AllSize = downloadProvider.Size };  
             
            ProgressChanged?.Invoke(this, eventInfo);  
        }  
          
  
        public event DownloadProgressChanged ProgressChanged;  
  
        public event StartDownloadEvent StartDownload;  
    }  
}  
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,164 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,505 questions
{count} votes