change: add logging and fall back to x.mctaylors.ru for authlib downloads

This commit is contained in:
Octol1ttle 2024-12-30 13:27:50 +05:00
parent a6a86036e2
commit 53e98e8b72
Signed by: Octol1ttle
GPG key ID: B77C34313AEE1FFF

View file

@ -7,6 +7,10 @@ using System.Xml;
const string mavenMetadataUrl = "https://maven.ely.by/releases/by/ely/authlib/maven-metadata.xml";
const string authlibInjectorUrl =
"https://github.com/yushijinhun/authlib-injector/releases/download/v1.2.5/authlib-injector-1.2.5.jar";
const string metadataOutputFile = "epl_metadata.json";
Console.WriteLine($"Using Authlib metadata from: {mavenMetadataUrl}");
Console.WriteLine($"Will insert authlib-injector link: {authlibInjectorUrl}");
var client = new HttpClient();
var metadataXml = await client.GetStringAsync(mavenMetadataUrl);
@ -30,6 +34,7 @@ foreach (var versionObject in versions)
if (newElyPatchNumber > existingElyPatchNumber)
{
versionsDict.Remove(authlibVersion);
Console.WriteLine($"Discarded patched version '{existing}'");
}
else
{
@ -38,6 +43,7 @@ foreach (var versionObject in versions)
}
versionsDict.Add(authlibVersion, fullVersion);
Console.WriteLine($"Mapped Authlib version '{authlibVersion}' to patched version '{fullVersion}'");
}
versionsDict = versionsDict.OrderByDescending(kvp =>
@ -63,21 +69,30 @@ versionsDict = versionsDict.OrderByDescending(kvp =>
var overrides = new JsonObject();
foreach (var version in versionsDict)
{
// TODO: replace with GitHub repo
var downloadUrl = $"https://llaun.ch/repo/libraries/by/ely/authlib/{version.Value}/authlib-{version.Value}.jar";
byte[] result;
try
var (success, result) = await TryDownload(downloadUrl);
if (!success)
{
result = await client.GetByteArrayAsync(downloadUrl);
downloadUrl = $"https://x.mctaylors.ru/epl/authlib/authlib-{version.Value}.jar";
(success, result) = await TryDownload(downloadUrl);
if (success)
{
Console.Error.WriteLine($"Using x.mctaylors.ru for authlib '{version.Value}'");
}
else
{
Console.Error.WriteLine($"Unable to find authlib '{version.Value}'");
continue;
}
}
catch (HttpRequestException e)
else
{
if (e.StatusCode != HttpStatusCode.NotFound) throw;
continue;
Console.WriteLine($"Using llaun.ch for authlib '{version.Value}'");
}
var sha1 = Convert.ToHexStringLower(SHA1.HashData(result));
var size = result.Length;
var sha1 = Convert.ToHexStringLower(SHA1.HashData(result!));
var size = result!.Length;
overrides[version.Key] = new JsonObject
{
["url"] = downloadUrl,
@ -98,5 +113,22 @@ var root = new JsonObject
}
};
await using var output = File.Create("epl_metadata.json");
await JsonSerializer.SerializeAsync(output, root, new JsonSerializerOptions { WriteIndented = true });
await using var output = File.Create(metadataOutputFile);
Console.WriteLine($"Writing metadata to '{Path.GetFullPath(metadataOutputFile)}'");
await JsonSerializer.SerializeAsync(output, root, new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine("Complete!");
return;
async Task<(bool success, byte[]? output)> TryDownload(string url)
{
try
{
return (true, await client.GetByteArrayAsync(url));
}
catch (HttpRequestException e)
{
if (e.StatusCode != HttpStatusCode.NotFound) throw;
return (false, null);
}
}