ModpackManifest deleted

This commit is contained in:
Timerix 2024-12-31 18:27:49 +05:00
parent e5deb3f3d4
commit 968ff99987
11 changed files with 77 additions and 74 deletions

View File

@ -93,7 +93,7 @@
</Slider>
<CheckBox IsChecked="{Binding #window.UpdateGameFiles}">
Проверить файлы игры
Обновить файлы игры
</CheckBox>
<CheckBox IsChecked="{Binding #window.EnableJavaDownload}">
Скачивать джаву

View File

@ -53,7 +53,7 @@ public class InstalledGameVersionProps : IComparable<InstalledGameVersionProps>,
}
// Descriptor is downloaded here
if (HashHelper.CheckFileSHA1(DescriptorPath, DescriptorProps?.sha1, checkForUpdates))
if (!HashHelper.CheckFileSHA1(DescriptorPath, DescriptorProps?.sha1, checkForUpdates))
{
if (DescriptorProps is null)
throw new NullReferenceException(

View File

@ -1,30 +0,0 @@
using Mlaumcherb.Client.Avalonia.холопы;
namespace Mlaumcherb.Client.Avalonia.классы;
public class ModpackFilesManifest
{
public class FileProps
{
[JsonRequired] public string sha1 { get; set; } = "";
public bool allow_edit { get; set; } = false;
}
/// relative_path, hash
[JsonRequired] public Dictionary<string, FileProps> files { get; set; } = new();
public bool CheckFiles(IOPath basedir, bool checkHashes, HashSet<IOPath> unmatchedFilesLocalPaths)
{
foreach (var p in files)
{
var localPath = new IOPath(p.Key);
var realPath = Path.Concat(basedir, localPath);
if (!HashHelper.CheckFileSHA1(realPath, p.Value.sha1, checkHashes && !p.Value.allow_edit))
{
unmatchedFilesLocalPaths.Add(localPath);
}
}
return unmatchedFilesLocalPaths.Count == 0;
}
}

View File

@ -1,4 +1,6 @@
namespace Mlaumcherb.Client.Avalonia.классы;
using Mlaumcherb.Client.Avalonia.холопы;
namespace Mlaumcherb.Client.Avalonia.классы;
public class MyModpackRemoteProps
{
@ -8,12 +10,40 @@ public class MyModpackRemoteProps
public class MyModpackV1
{
// 1
[JsonRequired] public int format_version { get; set; }
[JsonRequired] public string name { get; set; } = "";
// zip archive with all files
[JsonRequired] public Artifact zip { get; set; } = null!;
// ModpackFilesManifest
[JsonRequired] public Artifact manifest { get; set; } = null!;
/// relative_path, hash
// ReSharper disable once CollectionNeverUpdated.Global
[JsonRequired] public Dictionary<string, FileProps> files { get; set; } = new();
public class FileProps
{
[JsonRequired] public string sha1 { get; set; } = "";
// disable hash validation, allowing users to edit this file
public bool allow_edit { get; set; }
// don't re-download file if it was deleted by user
public bool optional { get; set; }
}
public bool CheckFiles(IOPath basedir, bool checkHashes, bool downloadOptionalFiles, HashSet<IOPath> unmatchedFilesLocalPaths)
{
foreach (var p in files)
{
if(!downloadOptionalFiles && p.Value.optional)
continue;
var localPath = new IOPath(p.Key);
var realPath = Path.Concat(basedir, localPath);
if (!HashHelper.CheckFileSHA1(realPath, p.Value.sha1, checkHashes && !p.Value.allow_edit))
{
unmatchedFilesLocalPaths.Add(localPath);
}
}
return unmatchedFilesLocalPaths.Count == 0;
}
}

View File

@ -42,23 +42,25 @@ public static class NetworkHelper
}
public static async Task<string> ReadOrDownload(IOPath filePath, string url,
/// <returns>(file_content, didDownload)</returns>
public static async Task<(string, bool)> ReadOrDownload(IOPath filePath, string url,
string? sha1, bool checkHashes)
{
if (HashHelper.CheckFileSHA1(filePath, sha1, checkHashes))
return File.ReadAllText(filePath);
return (File.ReadAllText(filePath), false);
string txt = await NetworkHelper.GetString(url);
File.WriteAllText(filePath, txt);
return txt;
return (txt, true);
}
public static async Task<T> ReadOrDownloadAndDeserialize<T>(IOPath filePath, string url,
/// <returns>(file_content, didDownload)</returns>
public static async Task<(T, bool)> ReadOrDownloadAndDeserialize<T>(IOPath filePath, string url,
string? sha1, bool checkHashes)
{
string text = await ReadOrDownload(filePath, url, sha1, checkHashes);
(string text, bool didDownload) = await ReadOrDownload(filePath, url, sha1, checkHashes);
var result = JsonConvert.DeserializeObject<T>(text)
?? throw new Exception($"can't deserialize {typeof(T).Name}");
return result;
return (result, didDownload);
}
}

View File

@ -22,24 +22,26 @@ public class AssetsDownloadTaskFactory : INetworkTaskFactory
public async Task<NetworkTask?> CreateAsync(bool checkHashes)
{
NetworkTask? networkTask = null;
if (!await CheckFilesAsync(checkHashes))
return new NetworkTask(
{
networkTask = new NetworkTask(
$"assets '{_descriptor.assetIndex.id}'",
GetTotalSize(),
Download
);
}
return null;
return networkTask;
}
private async Task<bool> CheckFilesAsync(bool checkHashes)
{
var assetIndex = await ReadOrDownloadAndDeserialize<AssetIndex>(
(AssetIndex assetIndex, _) = await ReadOrDownloadAndDeserialize<AssetIndex>(
_indexFilePath,
_descriptor.assetIndex.url,
_descriptor.assetIndex.sha1,
checkHashes);
_assetsToDownload.Clear();
// removing duplicates for Dictionary (idk how can it be possible, but Newtonsoft.Json creates them)
HashSet<string> assetHashes = new HashSet<string>();
foreach (var pair in assetIndex.objects)

View File

@ -28,11 +28,14 @@ public class JavaDownloadTaskFactory : INetworkTaskFactory
NetworkTask? networkTask = null;
if (!CheckFiles(checkHashes))
networkTask = new(
$"java runtime '{_descriptor.javaVersion.component}'",
GetTotalSize(),
Download
);
{
networkTask = new NetworkTask(
$"java runtime '{_descriptor.javaVersion.component}'",
GetTotalSize(),
Download
);
}
return networkTask;
}

View File

@ -24,11 +24,14 @@ public class LibrariesDownloadTaskFactory : INetworkTaskFactory
{
NetworkTask? networkTask = null;
if (!CheckFiles(checkHashes))
{
networkTask = new NetworkTask(
$"libraries '{_descriptor.id}'",
GetTotalSize(),
Download
);
}
return Task.FromResult(networkTask);
}

View File

@ -28,19 +28,16 @@ public class ModpackDownloadTaskFactory : INetworkTaskFactory
public class MyModpackV1DownloadTaskFactory : INetworkTaskFactory
{
private IOPath _modpackManifesPath;
private readonly GameVersionDescriptor _descriptor;
private IOPath _modpackDescriptorPath;
private IOPath _versionDir;
private MyModpackV1? _modpack;
private ModpackFilesManifest? _modpackManifest;
private HashSet<IOPath> _filesToDosnload = new();
public MyModpackV1DownloadTaskFactory(GameVersionDescriptor descriptor)
{
_descriptor = descriptor;
_modpackDescriptorPath = PathHelper.GetModpackDescriptorPath(_descriptor.id);
_modpackManifesPath = PathHelper.GetModpackManifestPath(_descriptor.id);
_versionDir = PathHelper.GetVersionDir(_descriptor.id);
}
@ -48,9 +45,8 @@ public class MyModpackV1DownloadTaskFactory : INetworkTaskFactory
{
if(_descriptor.modpack is null)
throw new ArgumentNullException(nameof(_descriptor.modpack));
_modpack = await NetworkHelper.ReadOrDownloadAndDeserialize<MyModpackV1>(
(_modpack, bool didDownloadModpackDescriptor) = await NetworkHelper.ReadOrDownloadAndDeserialize<MyModpackV1>(
_modpackDescriptorPath,
_descriptor.modpack.artifact.url,
_descriptor.modpack.artifact.sha1,
@ -58,21 +54,18 @@ public class MyModpackV1DownloadTaskFactory : INetworkTaskFactory
if (_modpack.format_version != _descriptor.modpack.format_version)
throw new Exception($"Modpack.format_version mismatches descriptor.modpack.version: " +
$"{_modpack.format_version} != {_descriptor.modpack.format_version}");
_modpackManifest = await NetworkHelper.ReadOrDownloadAndDeserialize<ModpackFilesManifest>(
_modpackManifesPath,
_modpack.manifest.url,
_modpack.manifest.sha1,
checkHashes);
if(!_modpackManifest.CheckFiles(_versionDir, checkHashes, _filesToDosnload))
return new NetworkTask(
NetworkTask? networkTask = null;
if(!_modpack.CheckFiles(_versionDir, checkHashes, didDownloadModpackDescriptor, _filesToDosnload))
{
networkTask = new NetworkTask(
$"modpack '{_descriptor.assetIndex.id}'",
_modpack.zip.size,
Download
);
}
return null;
return networkTask;
}
private async Task Download(NetworkProgressReporter pr, CancellationToken ct)

View File

@ -20,11 +20,14 @@ public class VersionJarDownloadTaskFactory : INetworkTaskFactory
{
NetworkTask? networkTask = null;
if (!CheckFiles(checkHashes))
{
networkTask = new NetworkTask(
$"game version jar '{_descriptor.id}'",
GetTotalSize(),
Download
);
$"game version jar '{_descriptor.id}'",
GetTotalSize(),
Download
);
}
return Task.FromResult(networkTask);
}

View File

@ -57,9 +57,6 @@ public static class PathHelper
public static IOPath GetModpackDescriptorPath(string game_version) =>
Path.Concat(GetVersionDir(game_version), "timerix_modpack.json");
public static IOPath GetModpackManifestPath(string game_version) =>
Path.Concat(GetVersionDir(game_version), "timerix_modpack_files.json");
public static IOPath GetCacheDir() =>
Path.Concat(GetRootFullPath(), "cache");