BuIlDaLiBlE / BetterHI3Launcher

A much better Honkai Impact 3rd launcher.
The Unlicense
102 stars 21 forks source link

Moving from HttpClientHelper to Hi3HelperCore.Http for HTTP Download #52

Closed neon-nyan closed 2 years ago

neon-nyan commented 2 years ago

Pull Request Overview

This Pull-Request will introduce the new submodule for replacing old HttpClientHelper and DownloadPartialAdapter with Hi3HelperCore.Http for more better multi-session download implementation and to fix Janky thread and file conflict issue caused by the old module.

The implementation for the Hi3HelperCore.Http is also more simple and straight forward without needing separate adapter (Unlike the old HttpClientHelper module). The base implementation example of the module would be like this:

using System.Threading;         // Import Threading for CancellationTokenSource
using System.Threading.Tasks;   // Import Threading.Tasks for Task
using Hi3Helper.Http;           // Import the Namespace for the module

CancellationTokenSource token = new();  // Initialize CancellationToken

// Do your download method here
public static async Task YourMethodHere()
{
    byte Sessions = 4;                      // Set 4 Parallel Sessions
    bool Overwrite = true;                  // Overwrite and don't continue if the previous session has been exist
    Http client = new();                    // Initialize Http module

    // Do Multiple Session Download
    await client.DownloadMultisession("https://yourURL.here/yourFile.here", @"C:\YourPath\YourOut.File", Overwrite, Sessions, token.Token);
    // Merge Session Chunks After Download
    await client.MergeMultisession(@"C:\YourPath\YourOut.File", Sessions, token.Token);
}

// Do your Cancel method here
public static void YourCancelHere() => this.token.Cancel();

Major Changes

Minor Changes

Changes In Action

As shown in this video, we could see that the Pause and Resume mechanism is now faster with no crash between the previous threads (thanks to the direct usage of the Hi3HelperCore.Http into the methods instead of bridging it from the DownloadPartialAdapter instead) while the progress also delayed for some moment to wait until all the sessions become ready.

BuIlDaLiBlE commented 2 years ago

Mostly looks great, I'll merge right away then.