Skip to content

Commit

Permalink
Merge pull request CmlLib#53 from dkrsk/add-quilt
Browse files Browse the repository at this point in the history
add QuiltMC version loader
  • Loading branch information
AlphaBs authored Apr 26, 2022
2 parents d216e61 + 28ebc0c commit 8401241
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
16 changes: 16 additions & 0 deletions CmlLib/Core/Installer/QuiltMC/QuiltLoader.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
114 changes: 114 additions & 0 deletions CmlLib/Core/Installer/QuiltMC/QuiltVersionLoader.cs
Original file line number Diff line number Diff line change
@@ -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<MVersionCollection> GetVersionMetadatasAsync()
=> internalGetVersionMetadatasAsync(sync: false);

private async Task<MVersionCollection> 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<MVersionMetadata> parseVersions(string res, string loader)
{
var jarr = JArray.Parse(res);
var versionList = new List<MVersionMetadata>(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<QuiltLoader[]> 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<QuiltLoader>(jarr.Count);
foreach (var item in jarr)
{
var obj = item.ToObject<QuiltLoader>();
if (obj != null)
loaderList.Add(obj);
}

return loaderList.ToArray();
}
}
}

0 comments on commit 8401241

Please sign in to comment.