Skip to content

Commit

Permalink
Merge branch 'master' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaBs committed Apr 26, 2022
2 parents 56e71bd + bd0a949 commit e1efd6a
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 9 deletions.
5 changes: 3 additions & 2 deletions CmlLib/Core/Installer/FabricMC/FabricVersionLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ private async Task<MVersionCollection> internalGetVersionMetadatasAsync(bool syn
else
loaders = await GetFabricLoadersAsync().ConfigureAwait(false);

LoaderVersion = loaders[0].Version;
if (loaders.Length == 0 || string.IsNullOrEmpty(LoaderVersion))
if (loaders.Length == 0 || string.IsNullOrEmpty(loaders[0].Version))
throw new KeyNotFoundException("can't find loaders");

LoaderVersion = loaders[0].Version;
}

string url = "https://meta.fabricmc.net/v2/versions/game/intermediary";
Expand Down
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; }
}
}
115 changes: 115 additions & 0 deletions CmlLib/Core/Installer/QuiltMC/QuiltVersionLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
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);

if (loaders.Length == 0 || string.IsNullOrEmpty(loaders[0].Version))
throw new KeyNotFoundException("can't find loaders");

LoaderVersion = loaders[0].Version;
}

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();
}
}
}
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

[![Discord](https://img.shields.io/discord/795952027443527690?label=discord\&logo=discord\&style=for-the-badge)](https://discord.gg/cDW2pvwHSc)

CmlLib.Core is minecraft launcher library for .NET\
Support all version, with Forge
CmlLib.Core is a Minecraft launcher library for .NET\
It supports all versions, including Forge

[한국어 README](https://github.com/AlphaBs/CmlLib.Core/blob/master/docs/README-kr.md)

Expand All @@ -26,15 +26,15 @@ Support all version, with Forge
* Install Java runtime
* Install Forge, LiteLoader, FabricMC
* Launch with options (direct server connecting, screen resolution)
* Cross-platform (windows, ubuntu, macOS)
* Cross-platform (Windows, Linux, macOS)

[Go to wiki for all features](https://github.com/CmlLib/CmlLib.Core/wiki)
[Go to the wiki for all features](https://github.com/CmlLib/CmlLib.Core/wiki)

## Install

Install the [CmlLib.Core Nuget package](https://www.nuget.org/packages/CmlLib.Core)

or download the dll files in [Releases](https://github.com/AlphaBs/CmlLib.Core/releases) and add references to them in your project.
or download the DLL files in [Releases](https://github.com/AlphaBs/CmlLib.Core/releases) and add references to them in your project.

Write this at the top of your source code:

Expand All @@ -45,10 +45,10 @@ using CmlLib.Core.Auth;

## Documentation

There are many features for custom launcher. Read wiki to use all features.\
There are many features for custom launchers. Read the wiki to see all of the features.\
**Official documentation: [wiki](https://github.com/CmlLib/CmlLib.Core/wiki)**

## QuickStart
## Quick start

### Microsoft Xbox Login

Expand Down

0 comments on commit e1efd6a

Please sign in to comment.