Game version management

This commit is contained in:
2024-12-27 21:11:12 +05:00
parent 1f663902e2
commit cb85b132c3
17 changed files with 339 additions and 159 deletions

View File

@@ -1,8 +1,35 @@
namespace Mlaumcherb.Client.Avalonia.классы;
public record VersionSearchParams
{
public bool ReleaseTypeAllowed;
public bool SnapshotTypeAllowed;
public bool OldTypeAllowed;
public bool OtherTypeAllowed;
}
public class GameVersionCatalog
{
[JsonRequired] public List<RemoteVersionDescriptorProps> versions { get; set; } = null!;
/// <returns>empty list if couldn't find any remote versions</returns>
public List<GameVersionProps> GetDownloadableVersions(VersionSearchParams p)
{
var _versionPropsList = new List<GameVersionProps>();
foreach (var r in versions)
{
bool match = r.type switch
{
"release" => p.ReleaseTypeAllowed,
"snapshot" => p.SnapshotTypeAllowed,
"old_alpha" or "old_beta" => p.OldTypeAllowed,
_ => p.OtherTypeAllowed
};
if(match)
_versionPropsList.Add(new GameVersionProps(r.id, r.url));
}
return _versionPropsList;
}
}
public class AssetProperties
@@ -19,9 +46,9 @@ public class AssetIndex
public class RemoteVersionDescriptorProps
{
[JsonRequired] public string id { get; set; } = "";
[JsonRequired] public string type { get; set; } = "";
[JsonRequired] public string url { get; set; } = "";
[JsonRequired] public string sha1 { get; set; } = "";
[JsonRequired] public DateTime time { get; set; }
[JsonRequired] public DateTime releaseTime { get; set; }
[JsonRequired] public string type { get; set; } = "";
public string sha1 { get; set; } = "";
public DateTime time { get; set; }
public DateTime releaseTime { get; set; }
}

View File

@@ -10,10 +10,10 @@ public class GameVersionDescriptor
[JsonRequired] public DateTime releaseTime { get; set; }
[JsonRequired] public string type { get; set; } = "";
[JsonRequired] public string mainClass { get; set; } = "";
[JsonRequired] public Downloads downloads { get; set; } = null!;
[JsonRequired] public List<Library> libraries { get; set; } = null!;
[JsonRequired] public AssetIndexProperties assetIndex { get; set; } = null!;
[JsonRequired] public string assets { get; set; } = "";
public Downloads? downloads { get; set; } = null;
public JavaVersion javaVersion { get; set; } = new() { component = "jre-legacy", majorVersion = 8 };
public string? minecraftArguments { get; set; }
public ArgumentsNew? arguments { get; set; }

View File

@@ -1,4 +1,5 @@
using Mlaumcherb.Client.Avalonia.холопы;
using Mlaumcherb.Client.Avalonia.зримое;
using Mlaumcherb.Client.Avalonia.холопы;
namespace Mlaumcherb.Client.Avalonia.классы;
@@ -13,13 +14,14 @@ public class GameVersionProps : IComparable<GameVersionProps>, IEquatable<GameVe
get => _isDownloaded;
set
{
bool downloadCompleted = value && !_isDownloaded;
_isDownloaded = value;
if(downloadCompleted)
OnDownloadCompleted?.Invoke();
if(_isDownloaded != value)
{
_isDownloaded = value;
StatusChanged?.Invoke(value);
}
}
}
public event Action? OnDownloadCompleted;
public event Action<bool>? StatusChanged;
public GameVersionProps(string name, string? url)
{
@@ -29,6 +31,16 @@ public class GameVersionProps : IComparable<GameVersionProps>, IEquatable<GameVe
IsDownloaded = File.Exists(PathHelper.GetVersionJarFilePath(name));
}
public void DeleteFiles()
{
IsDownloaded = false;
LauncherApp.Logger.LogInfo(Name, "Deleting files...");
var verdir = PathHelper.GetVersionDir(Name);
if(Directory.Exists(verdir))
Directory.Delete(verdir);
LauncherApp.Logger.LogInfo(Name, "Files deleted");
}
public override string ToString() => Name;
public override int GetHashCode() => Name.GetHashCode();

View File

@@ -7,11 +7,19 @@ public class Libraries
{
private static readonly string[] enabled_features = [];
public record JarLib(string name, IOPath jarFilePath, Artifact artifact);
public record NativeLib(string name, IOPath jarFilePath, Artifact artifact, Extract? extractionOptions)
public record JarLib(string name, IOPath? jarFilePath, Artifact artifact);
public record NativeLib(string name, IOPath? jarFilePath, Artifact artifact, Extract? extractionOptions)
: JarLib(name, jarFilePath, artifact);
public IReadOnlyCollection<JarLib> Libs { get; }
private IOPath? TryGetJarFilePath(Artifact artifact)
{
if(string.IsNullOrEmpty(artifact.url))
return null;
string urlTail = artifact.url.AsSpan().After("://").After('/').ToString();
return Path.Concat(PathHelper.GetLibrariesDir(), urlTail);
}
public Libraries(GameVersionDescriptor descriptor)
{
@@ -54,10 +62,8 @@ public class Libraries
// skipping duplicates (WHO THE HELL CREATES THIS DISCRIPTORS AAAAAAAAA)
if(!libHashes.Add(artifact.sha1))
continue;
string urlTail = artifact.url.AsSpan().After("://").After('/').ToString();
IOPath jarFilePath = Path.Concat(PathHelper.GetLibrariesDir(), urlTail);
libs.Add(new NativeLib(l.name, jarFilePath, artifact, l.extract));
libs.Add(new NativeLib(l.name, TryGetJarFilePath(artifact), artifact, l.extract));
}
else
{
@@ -69,9 +75,7 @@ public class Libraries
if(!libHashes.Add(artifact.sha1))
continue;
string urlTail = artifact.url.AsSpan().After("://").After('/').ToString();
IOPath jarFilePath = Path.Concat(PathHelper.GetLibrariesDir(), urlTail);
libs.Add(new JarLib(l.name, jarFilePath, artifact));
libs.Add(new JarLib(l.name, TryGetJarFilePath(artifact), artifact));
}
}

View File

@@ -0,0 +1,18 @@
using Mlaumcherb.Client.Avalonia.сеть;
namespace Mlaumcherb.Client.Avalonia.классы;
public class VersionCatalogProps
{
[JsonRequired] public required string Name { get; init; }
[JsonRequired] public required string Url { get; init; }
public override string ToString() => Name;
public async Task<GameVersionCatalog> GetVersionCatalogAsync()
{
return await NetworkHelper.DownloadStringAndDeserialize<GameVersionCatalog>(Url);
}
}