Skip to content

Commit

Permalink
添加Quilt安装器
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSpring114 committed Dec 16, 2023
1 parent 8cc44ec commit 2c5049c
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 22 deletions.
5 changes: 2 additions & 3 deletions MinecraftLaunch.Test/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@

var result = await installer.InstallAsync();

var fInstaller = new FabricInstaller(new GameResolver(gameFolder).GetGameEntity("1.16.5"),
(await FabricInstaller.EnumerableFromVersionAsync("1.16.5")).FirstOrDefault(),
source: MirrorDownloadManager.Mcbbs);
var fInstaller = new QuiltInstaller(new GameResolver(gameFolder).GetGameEntity("1.16.5"),
(await QuiltInstaller.EnumerableFromVersionAsync("1.16.5")).FirstOrDefault());

fInstaller.ProgressChanged += (_, x) => {
Console.Clear();
Expand Down
44 changes: 44 additions & 0 deletions MinecraftLaunch/Classes/Models/Install/QuiltBuildEntry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace MinecraftLaunch.Classes.Models.Install {
public record QuiltBuildEntry {
[JsonPropertyName("intermediary")]
public QuiltMavenItem Intermediary { get; set; }

[JsonPropertyName("loader")]
public QuiltMavenItem Loader { get; set; }

[JsonPropertyName("launcherMeta")]
public QuiltLauncherMeta LauncherMeta { get; set; }

[JsonIgnore]
public string BuildVersion => Loader.Version;

[JsonIgnore]
public string McVersion => Intermediary.Version;

[JsonIgnore]
public string DisplayVersion => $"{McVersion}-{Loader.Version}";
}

public record QuiltLauncherMeta {
[JsonPropertyName("mainClass")]
public Dictionary<string, string> MainClass { get; set; }
}

public record QuiltMavenItem {
[JsonPropertyName("separator")]
public string Separator { get; set; }

[JsonPropertyName("maven")]
public string Maven { get; set; }

[JsonPropertyName("version")]
public string Version { get; set; }
}
}
12 changes: 2 additions & 10 deletions MinecraftLaunch/Components/Installer/ForgeInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,8 @@
using System.Threading.Tasks;

namespace MinecraftLaunch.Components.Installer {
public class ForgeInstaller : IInstaller {
public event EventHandler<EventArgs> Completed;

public event EventHandler<ProgressChangedEventArgs> ProgressChanged;

public ValueTask<bool> InstallAsync() {
throw new NotImplementedException();
}

public void ReportProgress(double progress, string progressStatus, TaskStatus status) {
public class ForgeInstaller : InstallerBase {
public override ValueTask<bool> InstallAsync() {
throw new NotImplementedException();
}
}
Expand Down
72 changes: 63 additions & 9 deletions MinecraftLaunch/Components/Installer/QuiltInstaller.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,72 @@
using MinecraftLaunch.Classes.Interfaces;
using MinecraftLaunch.Classes.Models.Event;
using Flurl.Http;
using System.Text.Json;
using System.Text.Json.Nodes;
using MinecraftLaunch.Extensions;
using MinecraftLaunch.Components.Resolver;
using MinecraftLaunch.Classes.Models.Game;
using MinecraftLaunch.Classes.Models.Install;
using MinecraftLaunch.Classes.Models.Download;
using System.Diagnostics;

namespace MinecraftLaunch.Components.Installer {
public class QuiltInstaller : IInstaller {
public event EventHandler<EventArgs> Completed;
public class QuiltInstaller(GameEntry inheritedFrom, QuiltBuildEntry entry, string customId = default, MirrorDownloadSource source = default) : InstallerBase {
private readonly string _customId = customId;
private QuiltBuildEntry _quiltBuildEntry = entry;
private readonly GameEntry _inheritedFrom = inheritedFrom;

public event EventHandler<ProgressChangedEventArgs> ProgressChanged;
public override async ValueTask<bool> InstallAsync() {
/*
* Parse build
*/
ReportProgress(0.0d, "Start parse build", TaskStatus.Created);
string url = $"https://meta.quiltmc.org/v3/versions/loader/{_quiltBuildEntry.McVersion}/{_quiltBuildEntry.BuildVersion}/profile/json";
var versionInfoNode = JsonNode.Parse(await url.GetStringAsync());
var libraries = LibrariesResolver.GetLibrariesFromJsonArray(versionInfoNode
.GetEnumerable("libraries"),
_inheritedFrom.GameFolderPath);


/*
* Download dependent resources
*/
ReportProgress(0.25d, "Start downloading dependent resources", TaskStatus.WaitingToRun);
foreach (var library in libraries) {
Debug.WriteLine(library.Url);
}

await libraries.DownloadResourceEntrysAsync(source, x => {
ReportProgress(x.ToPercentage().ToPercentage(0.25d, 0.75d), $"Downloading dependent resources:{x.CompletedCount}/{x.TotalCount}",
TaskStatus.Running);
});

/*
* Write information to version json
*/
ReportProgress(0.85d, "Write information to version json", TaskStatus.WaitingToRun);
if (!string.IsNullOrEmpty(_customId)) {
versionInfoNode = versionInfoNode.SetString("id", _customId);
}

var id = versionInfoNode.GetString("id");
var jsonFile = new FileInfo(Path.Combine(_inheritedFrom.GameFolderPath,
"versions", id, $"{id}.json"));

if (!jsonFile.Directory.Exists) {
jsonFile.Directory.Create();
}

File.WriteAllText(jsonFile.FullName, versionInfoNode.ToString());
ReportProgress(1.0d, "Installation is complete", TaskStatus.Canceled);
return true;

public ValueTask<bool> InstallAsync() {
throw new NotImplementedException();
}

public void ReportProgress(double progress, string progressStatus, TaskStatus status) {
throw new NotImplementedException();
public static async ValueTask<IEnumerable<QuiltBuildEntry>> EnumerableFromVersionAsync(string mcVersion) {
string url = $"https://meta.quiltmc.org/v3/versions/loader/{mcVersion}";
string json = await url.GetStringAsync();

var entries = JsonSerializer.Deserialize<List<QuiltBuildEntry>>(json);
return entries;
}
}
}

0 comments on commit 2c5049c

Please sign in to comment.