From 2c5049cd3e730c8a2fa5ed5253d3e4d0a4dd48b4 Mon Sep 17 00:00:00 2001 From: YangSpring114 Date: Sat, 16 Dec 2023 13:47:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Quilt=E5=AE=89=E8=A3=85?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MinecraftLaunch.Test/Program.cs | 5 +- .../Classes/Models/Install/QuiltBuildEntry.cs | 44 ++++++++++++ .../Components/Installer/ForgeInstaller.cs | 12 +--- .../Components/Installer/QuiltInstaller.cs | 72 ++++++++++++++++--- 4 files changed, 111 insertions(+), 22 deletions(-) create mode 100644 MinecraftLaunch/Classes/Models/Install/QuiltBuildEntry.cs diff --git a/MinecraftLaunch.Test/Program.cs b/MinecraftLaunch.Test/Program.cs index aa47212..d31c895 100644 --- a/MinecraftLaunch.Test/Program.cs +++ b/MinecraftLaunch.Test/Program.cs @@ -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(); diff --git a/MinecraftLaunch/Classes/Models/Install/QuiltBuildEntry.cs b/MinecraftLaunch/Classes/Models/Install/QuiltBuildEntry.cs new file mode 100644 index 0000000..98b0d59 --- /dev/null +++ b/MinecraftLaunch/Classes/Models/Install/QuiltBuildEntry.cs @@ -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 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; } + } +} diff --git a/MinecraftLaunch/Components/Installer/ForgeInstaller.cs b/MinecraftLaunch/Components/Installer/ForgeInstaller.cs index b8bbb4a..25096f8 100644 --- a/MinecraftLaunch/Components/Installer/ForgeInstaller.cs +++ b/MinecraftLaunch/Components/Installer/ForgeInstaller.cs @@ -7,16 +7,8 @@ using System.Threading.Tasks; namespace MinecraftLaunch.Components.Installer { - public class ForgeInstaller : IInstaller { - public event EventHandler Completed; - - public event EventHandler ProgressChanged; - - public ValueTask InstallAsync() { - throw new NotImplementedException(); - } - - public void ReportProgress(double progress, string progressStatus, TaskStatus status) { + public class ForgeInstaller : InstallerBase { + public override ValueTask InstallAsync() { throw new NotImplementedException(); } } diff --git a/MinecraftLaunch/Components/Installer/QuiltInstaller.cs b/MinecraftLaunch/Components/Installer/QuiltInstaller.cs index 83dfb18..8741727 100644 --- a/MinecraftLaunch/Components/Installer/QuiltInstaller.cs +++ b/MinecraftLaunch/Components/Installer/QuiltInstaller.cs @@ -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 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 ProgressChanged; + public override async ValueTask 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 InstallAsync() { - throw new NotImplementedException(); } - public void ReportProgress(double progress, string progressStatus, TaskStatus status) { - throw new NotImplementedException(); + public static async ValueTask> EnumerableFromVersionAsync(string mcVersion) { + string url = $"https://meta.quiltmc.org/v3/versions/loader/{mcVersion}"; + string json = await url.GetStringAsync(); + + var entries = JsonSerializer.Deserialize>(json); + return entries; } } }