EPLMetadataGenerator/Program.cs
2024-12-28 14:04:02 +05:00

102 lines
No EOL
2.9 KiB
C#

using System.Net;
using System.Security.Cryptography;
using System.Text.Json;
using System.Text.Json.Nodes;
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";
var client = new HttpClient();
var metadataXml = await client.GetStringAsync(mavenMetadataUrl);
var document = new XmlDocument();
document.LoadXml(metadataXml);
var versioning = document["metadata"]!["versioning"];
var versions = versioning!["versions"]!.GetElementsByTagName("version");
var versionsDict = new Dictionary<string, string>();
foreach (var versionObject in versions)
{
var versionElement = versionObject as XmlElement;
var fullVersion = versionElement!.InnerText;
var authlibVersion = fullVersion.Split('-').First();
if (versionsDict.TryGetValue(authlibVersion, out var existing))
{
var newElyPatchNumber = Convert.ToInt32(fullVersion.Split('.').Last());
var existingElyPatchNumber = Convert.ToInt32(existing.Split('.').Last());
if (newElyPatchNumber > existingElyPatchNumber)
{
versionsDict.Remove(authlibVersion);
}
else
{
continue;
}
}
versionsDict.Add(authlibVersion, fullVersion);
}
versionsDict = versionsDict.OrderByDescending(kvp =>
{
var versionNumbers = kvp.Key.Split('.');
var score = 0;
if (versionNumbers.Length > 0)
{
score += 1_000_000 * Convert.ToInt32(versionNumbers[0]);
}
if (versionNumbers.Length > 1)
{
score += 1_000 * Convert.ToInt32(versionNumbers[1]);
}
if (versionNumbers.Length > 2)
{
score += Convert.ToInt32(versionNumbers[2]);
}
return score;
}).ToDictionary();
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
{
result = await client.GetByteArrayAsync(downloadUrl);
}
catch (HttpRequestException e)
{
if (e.StatusCode != HttpStatusCode.NotFound) throw;
continue;
}
var sha1 = Convert.ToHexStringLower(SHA1.HashData(result));
var size = result.Length;
overrides[version.Key] = new JsonObject
{
["url"] = downloadUrl,
["sha1"] = sha1,
["size"] = size
};
}
var root = new JsonObject
{
["overrides"] = new JsonObject
{
["com.mojang:authlib"] = overrides
},
["extras"] = new JsonObject
{
["authlib-injector"] = authlibInjectorUrl
}
};
await using var output = File.Create("epl_metadata.json");
await JsonSerializer.SerializeAsync(output, root, new JsonSerializerOptions { WriteIndented = true });