mlaumcherb/Mlaumcherb.Client.Avalonia/сеть/TaskFactories/VersionJarDownloadTaskFactory.cs
2024-12-31 20:03:31 +05:00

55 lines
1.9 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Mlaumcherb.Client.Avalonia.зримое;
using Mlaumcherb.Client.Avalonia.классы;
using Mlaumcherb.Client.Avalonia.холопы;
using static Mlaumcherb.Client.Avalonia.сеть.NetworkHelper;
namespace Mlaumcherb.Client.Avalonia.сеть.TaskFactories;
public class VersionJarDownloadTaskFactory : INetworkTaskFactory
{
private GameVersionDescriptor _descriptor;
private IOPath _filePath;
public VersionJarDownloadTaskFactory(GameVersionDescriptor descriptor)
{
_descriptor = descriptor;
_filePath = PathHelper.GetVersionJarFilePath(_descriptor.id);
}
public Task<NetworkTask?> CreateAsync(bool checkHashes)
{
NetworkTask? networkTask = null;
if (!CheckFiles(checkHashes))
{
networkTask = new NetworkTask(
$"game version jar '{_descriptor.id}'",
GetTotalSize(),
Download
);
}
return Task.FromResult(networkTask);
}
private bool CheckFiles(bool checkHashes)
{
return HashHelper.CheckFileSHA1(_filePath, _descriptor.downloads?.client.sha1, checkHashes);
}
private long GetTotalSize()
{
if(_descriptor.downloads is null)
return 0;
return _descriptor.downloads.client.size;
}
private async Task Download(NetworkProgressReporter pr, CancellationToken ct)
{
if (_descriptor.downloads is null)
throw new Exception($"can't download game version jar '{_descriptor.id}' because it has no download url");
LauncherApp.Logger.LogInfo(nameof(NetworkHelper), $"started downloading game version jar '{_descriptor.id}'");
await DownloadFile(_descriptor.downloads.client.url, _filePath, ct, pr.AddBytesCount);
LauncherApp.Logger.LogInfo(nameof(NetworkHelper), $"finished downloading game version jar '{_descriptor.id}'");
}
}