From 1a427e102a952fb99c0a82bf4ae754c144c44677 Mon Sep 17 00:00:00 2001 From: Octol1ttle Date: Sat, 28 Dec 2024 14:04:02 +0500 Subject: [PATCH] Initial commit --- .gitignore | 8 +++ EPLMetadataGenerator.csproj | 10 ++++ EPLMetadataGenerator.sln | 16 ++++++ Program.cs | 102 ++++++++++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 .gitignore create mode 100644 EPLMetadataGenerator.csproj create mode 100644 EPLMetadataGenerator.sln create mode 100644 Program.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2375bf1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.idea/ +*.user +bin/ +obj/ +/packages/ +riderModule.iml +/_ReSharper.Caches/ +/.vs/ \ No newline at end of file diff --git a/EPLMetadataGenerator.csproj b/EPLMetadataGenerator.csproj new file mode 100644 index 0000000..85b4959 --- /dev/null +++ b/EPLMetadataGenerator.csproj @@ -0,0 +1,10 @@ + + + + Exe + net9.0 + enable + enable + + + diff --git a/EPLMetadataGenerator.sln b/EPLMetadataGenerator.sln new file mode 100644 index 0000000..d44d2ca --- /dev/null +++ b/EPLMetadataGenerator.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EPLMetadataGenerator", "EPLMetadataGenerator.csproj", "{91C078A4-FA5F-4A15-A8D4-1A68E04EE264}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {91C078A4-FA5F-4A15-A8D4-1A68E04EE264}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {91C078A4-FA5F-4A15-A8D4-1A68E04EE264}.Debug|Any CPU.Build.0 = Debug|Any CPU + {91C078A4-FA5F-4A15-A8D4-1A68E04EE264}.Release|Any CPU.ActiveCfg = Release|Any CPU + {91C078A4-FA5F-4A15-A8D4-1A68E04EE264}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..8317c32 --- /dev/null +++ b/Program.cs @@ -0,0 +1,102 @@ +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(); +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 }); \ No newline at end of file