Skip to content

Commit

Permalink
Adding settings for the new Hi3Helper.Http
Browse files Browse the repository at this point in the history
  • Loading branch information
neon-nyan committed Aug 25, 2024
1 parent 29c1cc8 commit 3b57b28
Show file tree
Hide file tree
Showing 10 changed files with 590 additions and 43 deletions.
15 changes: 15 additions & 0 deletions CollapseLauncher/Classes/Extension/NumberExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Hi3Helper.Shared.Region;
using System;
using System.Runtime.CompilerServices;

namespace CollapseLauncher.Extension
{
internal static class NumberExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static double ClampLimitedSpeedNumber(this double speed)
=> LauncherConfig.DownloadSpeedLimitCached > 0 ?
Math.Min(LauncherConfig.DownloadSpeedLimitCached, speed) :
speed;
}
}
55 changes: 20 additions & 35 deletions CollapseLauncher/Classes/Interfaces/Class/ProgressBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected virtual async void _httpClient_FetchAssetProgress(int size, DownloadPr
{
if (await CheckIfNeedRefreshStopwatch())
{
double speed = downloadProgress.BytesDownloaded / _stopwatch.Elapsed.TotalSeconds;
double speed = (downloadProgress.BytesDownloaded / _stopwatch.Elapsed.TotalSeconds).ClampLimitedSpeedNumber();
TimeSpan timeLeftSpan = ((downloadProgress.BytesTotal - downloadProgress.BytesDownloaded) / speed).ToTimeSpanNormalized();
double percentage = ConverterTool.GetPercentageNumber(downloadProgress.BytesDownloaded, downloadProgress.BytesTotal, 2);

Expand Down Expand Up @@ -144,13 +144,13 @@ protected virtual async void _httpClient_RepairAssetProgress(int size, DownloadP
Interlocked.Add(ref _progressAllSizeCurrent, size);
if (await CheckIfNeedRefreshStopwatch())
{
double speed = _progressAllSizeCurrent / _downloadSpeedRefreshStopwatch.Elapsed.TotalSeconds;
double speed = (_progressAllSizeCurrent / _stopwatch.Elapsed.TotalSeconds).ClampLimitedSpeedNumber();
TimeSpan timeLeftSpan = ((_progressAllSizeCurrent - _progressAllSizeTotal) / speed).ToTimeSpanNormalized();
double percentage = ConverterTool.GetPercentageNumber(_progressAllSizeCurrent, _progressAllSizeTotal, 2);
double percentagePerFile = ConverterTool.GetPercentageNumber(downloadProgress.BytesDownloaded, downloadProgress.BytesTotal, 2);

lock (_progress!)
{
_progress.ProgressPerFilePercentage = percentage;
_progress.ProgressPerFilePercentage = percentagePerFile;
_progress.ProgressPerFileSizeCurrent = downloadProgress.BytesDownloaded;
_progress.ProgressPerFileSizeTotal = downloadProgress.BytesTotal;
_progress.ProgressAllSizeCurrent = _progressAllSizeCurrent;
Expand Down Expand Up @@ -258,7 +258,7 @@ protected virtual async void _httpClient_UpdateAssetProgress(int size, DownloadP
if (await CheckIfNeedRefreshStopwatch())
{
double speed = _progressAllSizeCurrent / _stopwatch.Elapsed.TotalSeconds;
TimeSpan timeLeftSpan = ((_progressAllSizeTotal - _progressAllSizeCurrent) / speed).ToTimeSpanNormalized();
TimeSpan timeLeftSpan = ((_progressAllSizeTotal - _progressAllSizeCurrent) / speed.ClampLimitedSpeedNumber()).ToTimeSpanNormalized();
double percentage = ConverterTool.GetPercentageNumber(_progressAllSizeCurrent, _progressAllSizeTotal, 2);

// Update current progress percentages and speed
Expand Down Expand Up @@ -952,40 +952,25 @@ protected bool SummarizeStatusAndProgress(List<T1> assetIndex, string msgIfFound
}

protected virtual bool IsArrayMatch(ReadOnlySpan<byte> source, ReadOnlySpan<byte> target) => source.SequenceEqual(target);


#nullable enable
protected virtual async Task RunDownloadTask(long assetSize, string assetPath, string assetURL,
DownloadClient downloadClient, DownloadProgressDelegate downloadProgress, CancellationToken token, bool isOverwrite = true)
DownloadClient downloadClient, DownloadProgressDelegate downloadProgress, CancellationToken token, bool isOverwrite = true,
EventHandler<long>? downloadSpeedLimitChanged = null)
{
// Always do multi-session download with the new DownloadClient regardless of any sizes (if applicable)
if (assetSize < 10 << 10)
{
using FileStream fileStream = File.Open(
EnsureCreationOfDirectory(assetPath),
isOverwrite ?
FileMode.Create :
FileMode.OpenOrCreate,
FileAccess.ReadWrite,
FileShare.ReadWrite);

await downloadClient.DownloadAsync(
assetURL,
fileStream,
!isOverwrite,
progressDelegateAsync: downloadProgress,
cancelToken: token
);
}
else
{
await downloadClient.DownloadAsync(
assetURL,
EnsureCreationOfDirectory(assetPath),
isOverwrite,
progressDelegateAsync: downloadProgress,
cancelToken: token
);
}
await downloadClient.DownloadAsync(
assetURL,
EnsureCreationOfDirectory(assetPath),
isOverwrite,
sessionChunkSize: LauncherConfig.DownloadChunkSize,
progressDelegateAsync: downloadProgress,
cancelToken: token,
downloadSpeedLimitEvent: downloadSpeedLimitChanged,
initialDownloadSpeed: LauncherConfig.DownloadSpeedLimitCached
);
}
#nullable restore

protected virtual async Task RunDownloadTask(long assetSize, string assetPath, string assetURL, Http _httpClient, CancellationToken token)
{
Expand Down
4 changes: 2 additions & 2 deletions CollapseLauncher/Classes/RegionManagement/FallbackCDNUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ private static async ValueTask<bool> TryGetCDNContent(CDNURLProperty cdnProp, Do
if (!urlStatus.Item1) return false;

// Continue to get the content and return true if successful
await downloadClient.DownloadAsync(urlStatus.Item2, outputStream, false, HttpInstance_DownloadProgressAdapter, null, null, token);
await downloadClient.DownloadAsync(urlStatus.Item2, outputStream, false, HttpInstance_DownloadProgressAdapter, null, null, cancelToken:token);
return true;
}
// Handle the error and log it. If fails, then log it and return false
Expand All @@ -415,7 +415,7 @@ private static async ValueTask<bool> TryGetCDNContent(CDNURLProperty cdnProp, Do
{
// If the CDN marked to not supporting the partial download, then use single thread mode download.
using FileStream stream = File.Create(outputPath);
await downloadClient.DownloadAsync(urlStatus.Item2, stream, false, HttpInstance_DownloadProgressAdapter, null, null, token);
await downloadClient.DownloadAsync(urlStatus.Item2, stream, false, HttpInstance_DownloadProgressAdapter, null, null, cancelToken:token);
return true;
}
await downloadClient.DownloadAsync(urlStatus.Item2, outputPath, true, progressDelegateAsync: HttpInstance_DownloadProgressAdapter, maxConnectionSessions: parallelThread, cancelToken: token);
Expand Down
Loading

0 comments on commit 3b57b28

Please sign in to comment.