88 lines
3.2 KiB
C#
88 lines
3.2 KiB
C#
using System.Net.Http;
|
||
using System.Net.Http.Headers;
|
||
using Млаумчерб.Клиент.зримое;
|
||
using Млаумчерб.Клиент.классы;
|
||
|
||
namespace Млаумчерб.Клиент.сеть;
|
||
|
||
public static class Сеть
|
||
{
|
||
private static HttpClient _http = new();
|
||
|
||
static Сеть()
|
||
{
|
||
// thanks for Sashok :3
|
||
// https://github.com/new-sashok724/Launcher/blob/23485c3f7de6620d2c6b7b2dd9339c3beb6a0366/Launcher/source/helper/IOHelper.java#L259
|
||
_http.DefaultRequestHeaders.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
|
||
}
|
||
|
||
public static Task<string> GetString(string url, CancellationToken ct = default) => _http.GetStringAsync(url, ct);
|
||
public static Task<Stream> GetStream(string url, CancellationToken ct = default) => _http.GetStreamAsync(url, ct);
|
||
|
||
public static async Task DownloadFile(string url, Stream outStream, CancellationToken ct = default,
|
||
params TransformStream.TransformFuncDelegate[] transforms)
|
||
{
|
||
await using var pipe = new TransformStream(await GetStream(url, ct));
|
||
if (transforms.Length > 0)
|
||
pipe.AddTransforms(transforms);
|
||
await pipe.CopyToAsync(outStream, ct);
|
||
}
|
||
|
||
public static async Task DownloadFile(string url, IOPath outPath, CancellationToken ct = default,
|
||
params TransformStream.TransformFuncDelegate[] transforms)
|
||
{
|
||
await using var file = File.OpenWrite(outPath);
|
||
await DownloadFile(url, file, ct, transforms);
|
||
}
|
||
|
||
public static async Task<T> DownloadStringAndDeserialize<T>(string url)
|
||
{
|
||
var text = await _http.GetStringAsync(url);
|
||
var result = JsonConvert.DeserializeObject<T>(text)
|
||
?? throw new Exception($"can't deserialize {typeof(T).Name}");
|
||
return result;
|
||
}
|
||
|
||
private static readonly string[] VERSION_MANIFEST_URLS =
|
||
{
|
||
"https://piston-meta.mojang.com/mc/game/version_manifest_v2.json"
|
||
};
|
||
|
||
private static async Task<List<RemoteVersionDescriptorProps>> GetRemoteVersionDescriptorsAsync()
|
||
{
|
||
List<RemoteVersionDescriptorProps> descriptors = new();
|
||
foreach (var url in VERSION_MANIFEST_URLS)
|
||
{
|
||
try
|
||
{
|
||
var catalog = await DownloadStringAndDeserialize<GameVersionCatalog>(url);
|
||
descriptors.AddRange(catalog.versions);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Приложение.Логгер.LogWarn(nameof(Сеть), ex);
|
||
}
|
||
}
|
||
|
||
return descriptors;
|
||
}
|
||
|
||
private static List<GameVersionProps>? _versionPropsList;
|
||
|
||
/// <returns>empty list if couldn't find any remote versions</returns>
|
||
public static async Task<IReadOnlyList<GameVersionProps>> GetDownloadableVersions()
|
||
{
|
||
if (_versionPropsList == null)
|
||
{
|
||
_versionPropsList = new();
|
||
var rvdlist = await GetRemoteVersionDescriptorsAsync();
|
||
foreach (var r in rvdlist)
|
||
{
|
||
if (r.type == "release")
|
||
_versionPropsList.Add(new GameVersionProps(r.id, r.url));
|
||
}
|
||
}
|
||
|
||
return _versionPropsList;
|
||
}
|
||
} |