From 28ebc0cecc84163d359f1aded0f09df6ae22fbef Mon Sep 17 00:00:00 2001 From: Damian Date: Fri, 22 Apr 2022 20:43:27 +0300 Subject: [PATCH] add QuiltMC version loader --- CmlLib/Core/Installer/QuiltMC/QuiltLoader.cs | 16 +++ .../Installer/QuiltMC/QuiltVersionLoader.cs | 114 ++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 CmlLib/Core/Installer/QuiltMC/QuiltLoader.cs create mode 100644 CmlLib/Core/Installer/QuiltMC/QuiltVersionLoader.cs diff --git a/CmlLib/Core/Installer/QuiltMC/QuiltLoader.cs b/CmlLib/Core/Installer/QuiltMC/QuiltLoader.cs new file mode 100644 index 0000000..3739b35 --- /dev/null +++ b/CmlLib/Core/Installer/QuiltMC/QuiltLoader.cs @@ -0,0 +1,16 @@ +using Newtonsoft.Json; + +namespace CmlLib.Core.Installer.QuiltMC +{ + public class QuiltLoader + { + [JsonProperty("separator")] + public string? Separator { get; set; } + [JsonProperty("build")] + public string? Build { get; set; } + [JsonProperty("maven")] + public string? Maven { get; set; } + [JsonProperty("version")] + public string? Version { get; set; } + } +} diff --git a/CmlLib/Core/Installer/QuiltMC/QuiltVersionLoader.cs b/CmlLib/Core/Installer/QuiltMC/QuiltVersionLoader.cs new file mode 100644 index 0000000..df8cbe6 --- /dev/null +++ b/CmlLib/Core/Installer/QuiltMC/QuiltVersionLoader.cs @@ -0,0 +1,114 @@ +using CmlLib.Core.Version; +using Newtonsoft.Json.Linq; +using System.Collections.Generic; +using System.Net; +using System.Threading.Tasks; +using CmlLib.Core.VersionLoader; + +namespace CmlLib.Core.Installer.QuiltMC +{ + public class QuiltVersionLoader : IVersionLoader + { + public static readonly string ApiServer = "https://meta.quiltmc.org"; + private static readonly string LoaderUrl = ApiServer + "/v3/versions/loader"; + + public string? LoaderVersion { get; set; } + + protected string GetVersionName(string version, string loaderVersion) + { + return $"quilt-loader-{loaderVersion}-{version}"; + } + + public MVersionCollection GetVersionMetadatas() + => internalGetVersionMetadatasAsync(sync: true).GetAwaiter().GetResult(); + + public Task GetVersionMetadatasAsync() + => internalGetVersionMetadatasAsync(sync: false); + + private async Task internalGetVersionMetadatasAsync(bool sync) + { + if (string.IsNullOrEmpty(LoaderVersion)) + { + QuiltLoader[] loaders; + if (sync) + loaders = GetQuiltLoaders(); + else + loaders = await GetQuiltLoadersAsync().ConfigureAwait(false); + + LoaderVersion = loaders[0].Version; + if (loaders.Length == 0 || string.IsNullOrEmpty(LoaderVersion)) + throw new KeyNotFoundException("can't find loaders"); + } + + string url = "https://meta.quiltmc.org/v3/versions/game"; + string res; + using (var wc = new WebClient()) + { + if (sync) + res = wc.DownloadString(url); + else + res = await wc.DownloadStringTaskAsync(url).ConfigureAwait(false); + } + + var versions = parseVersions(res, LoaderVersion!); + return new MVersionCollection(versions.ToArray()); + } + + private List parseVersions(string res, string loader) + { + var jarr = JArray.Parse(res); + var versionList = new List(jarr.Count); + + foreach (var item in jarr) + { + string? versionName = item["version"]?.ToString(); + if (string.IsNullOrEmpty(versionName)) + continue; + + string jsonUrl = $"{ApiServer}/v3/versions/loader/{versionName}/{loader}/profile/json"; + + string id = GetVersionName(versionName, loader); + var versionMetadata = new WebVersionMetadata(id) + { + MType = MVersionType.Custom, + Path = jsonUrl, + Type = "quilt" + }; + versionList.Add(versionMetadata); + } + + return versionList; + } + + public QuiltLoader[] GetQuiltLoaders() + { + using var wc = new WebClient(); + var res = wc.DownloadString(LoaderUrl); + + return parseLoaders(res); + } + + public async Task GetQuiltLoadersAsync() + { + using var wc = new WebClient(); + var res = await wc.DownloadStringTaskAsync(LoaderUrl) + .ConfigureAwait(false); + + return parseLoaders(res); + } + + private QuiltLoader[] parseLoaders(string res) + { + var jarr = JArray.Parse(res); + var loaderList = new List(jarr.Count); + foreach (var item in jarr) + { + var obj = item.ToObject(); + if (obj != null) + loaderList.Add(obj); + } + + return loaderList.ToArray(); + } + } +}