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

View File

@ -53,7 +53,7 @@ public class InstalledGameVersionProps : IComparable<InstalledGameVersionProps>,
} }
// Descriptor is downloaded here // Descriptor is downloaded here
if (HashHelper.CheckFileSHA1(DescriptorPath, DescriptorProps?.sha1, checkForUpdates)) if (!HashHelper.CheckFileSHA1(DescriptorPath, DescriptorProps?.sha1, checkForUpdates))
{ {
if (DescriptorProps is null) if (DescriptorProps is null)
throw new NullReferenceException( 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 public class MyModpackRemoteProps
{ {
@ -8,12 +10,40 @@ public class MyModpackRemoteProps
public class MyModpackV1 public class MyModpackV1
{ {
// 1 // 1
[JsonRequired] public int format_version { get; set; } [JsonRequired] public int format_version { get; set; }
[JsonRequired] public string name { get; set; } = ""; [JsonRequired] public string name { get; set; } = "";
// zip archive with all files // zip archive with all files
[JsonRequired] public Artifact zip { get; set; } = null!; [JsonRequired] public Artifact zip { get; set; } = null!;
// ModpackFilesManifest /// relative_path, hash
[JsonRequired] public Artifact manifest { get; set; } = null!; // 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) string? sha1, bool checkHashes)
{ {
if (HashHelper.CheckFileSHA1(filePath, sha1, checkHashes)) if (HashHelper.CheckFileSHA1(filePath, sha1, checkHashes))
return File.ReadAllText(filePath); return (File.ReadAllText(filePath), false);
string txt = await NetworkHelper.GetString(url); string txt = await NetworkHelper.GetString(url);
File.WriteAllText(filePath, txt); 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? 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) var result = JsonConvert.DeserializeObject<T>(text)
?? throw new Exception($"can't deserialize {typeof(T).Name}"); ?? 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) public async Task<NetworkTask?> CreateAsync(bool checkHashes)
{ {
NetworkTask? networkTask = null;
if (!await CheckFilesAsync(checkHashes)) if (!await CheckFilesAsync(checkHashes))
return new NetworkTask( {
networkTask = new NetworkTask(
$"assets '{_descriptor.assetIndex.id}'", $"assets '{_descriptor.assetIndex.id}'",
GetTotalSize(), GetTotalSize(),
Download Download
); );
}
return null; return networkTask;
} }
private async Task<bool> CheckFilesAsync(bool checkHashes) private async Task<bool> CheckFilesAsync(bool checkHashes)
{ {
var assetIndex = await ReadOrDownloadAndDeserialize<AssetIndex>( (AssetIndex assetIndex, _) = await ReadOrDownloadAndDeserialize<AssetIndex>(
_indexFilePath, _indexFilePath,
_descriptor.assetIndex.url, _descriptor.assetIndex.url,
_descriptor.assetIndex.sha1, _descriptor.assetIndex.sha1,
checkHashes); checkHashes);
_assetsToDownload.Clear();
// removing duplicates for Dictionary (idk how can it be possible, but Newtonsoft.Json creates them) // removing duplicates for Dictionary (idk how can it be possible, but Newtonsoft.Json creates them)
HashSet<string> assetHashes = new HashSet<string>(); HashSet<string> assetHashes = new HashSet<string>();
foreach (var pair in assetIndex.objects) foreach (var pair in assetIndex.objects)

View File

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

View File

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

View File

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

View File

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

View File

@ -58,9 +58,6 @@ public static class PathHelper
public static IOPath GetModpackDescriptorPath(string game_version) => public static IOPath GetModpackDescriptorPath(string game_version) =>
Path.Concat(GetVersionDir(game_version), "timerix_modpack.json"); 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() => public static IOPath GetCacheDir() =>
Path.Concat(GetRootFullPath(), "cache"); Path.Concat(GetRootFullPath(), "cache");
public static IOPath GetInstalledVersionCatalogPath() => public static IOPath GetInstalledVersionCatalogPath() =>