Skip to content

Downloader

Class Description

The Downloader class is an abstract base class designed to handle the downloading of update packages for applications. It primarily manages the download process for both full update packages and patch files. The class allows for authentication through an Authenticator property and monitors download progress through the OnDownloadProgress action. It is equipped to handle different platforms and application versions, making it versatile and adaptable for various update scenarios.

Example Usage

To utilize the Downloader class, it must be subclassed and its abstract methods DownloadAsync and DownloadPatchAsync must be implemented. The DownloadSync should download the base version of the game (e.g. v1.0, v2.0), while the DownloadPatchAsync method is responsible for downloading patches (e.g. v1.1, v1.2). Below is an example of how to extend the Downloader class for a specific platform:

using ByteCobra.Updater.Models;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace ByteCobra.Updater
{
    public class CustomDownloader : Downloader
    {
        public CustomDownloader(Action<float> onDownloadProgress) 
          : base(onDownloadProgress) {}

        protected override Authenticator? Authenticator => new CustomAuthenticator();

        public override async Task<PackageZipFile?> DownloadAsync(
          string applicationName, 
          Platform platform,
          ushort major, 
          ushort minor, 
          CancellationToken cancellationToken)
        {
            // Implement custom download logic here
        }

        public override async Task<PackageZipFile?> DownloadPatchAsync(
          string applicationName,
          Platform platform, 
          ushort major, 
          ushort minor, 
          CancellationToken cancellationToken)
        {
            // Implement custom patch download logic here
        }
    }
}

In this example, CustomDownloader is a subclass of Downloader specifically designed for a custom use case. It provides implementations for the abstract methods DownloadAsync and DownloadPatchAsync, allowing for specific download behavior tailored to the application's needs. The Authenticator is also customized to provide authentication specific to the application's security requirements. This design allows for flexible and targeted download strategies in different update scenarios.