From c0901058315cf8ef8d7e44fab5836d9ed086b06a Mon Sep 17 00:00:00 2001 From: Marcel Baumgartner Date: Wed, 26 Apr 2023 20:13:29 +0200 Subject: [PATCH] Implemented forge version switcher --- Moonlight/App/Helpers/ParseHelper.cs | 14 +- Moonlight/App/Services/ForgeService.cs | 37 +++++ Moonlight/Program.cs | 4 +- .../ServerControl/ServerSettings.razor | 3 + .../Settings/ForgeVersionSetting.razor | 147 ++++++++++++++++++ 5 files changed, 200 insertions(+), 5 deletions(-) create mode 100644 Moonlight/App/Services/ForgeService.cs create mode 100644 Moonlight/Shared/Components/ServerControl/Settings/ForgeVersionSetting.razor diff --git a/Moonlight/App/Helpers/ParseHelper.cs b/Moonlight/App/Helpers/ParseHelper.cs index ecc983dd..eddd4f2b 100644 --- a/Moonlight/App/Helpers/ParseHelper.cs +++ b/Moonlight/App/Helpers/ParseHelper.cs @@ -1,15 +1,23 @@ -namespace Moonlight.App.Helpers; +using Logging.Net; + +namespace Moonlight.App.Helpers; public static class ParseHelper { public static int MinecraftToInt(string raw) { - var versionWithoutPre = raw.Split("-")[0]; + var versionWithoutPre = raw.Split("_")[0]; + versionWithoutPre = versionWithoutPre.Split("-")[0]; + + // Fuck you 1.7.10 ;) + versionWithoutPre = versionWithoutPre.Replace("1.7.10", "1.7"); if (versionWithoutPre.Count(x => x == "."[0]) == 1) versionWithoutPre += ".0"; - return int.Parse(versionWithoutPre.Replace(".", "")); + var x = versionWithoutPre.Replace(".", ""); + + return int.Parse(x); } public static string FirstPartStartingWithNumber(string raw) diff --git a/Moonlight/App/Services/ForgeService.cs b/Moonlight/App/Services/ForgeService.cs new file mode 100644 index 00000000..ef23550f --- /dev/null +++ b/Moonlight/App/Services/ForgeService.cs @@ -0,0 +1,37 @@ +using System.Text; +using Moonlight.App.Helpers; + +namespace Moonlight.App.Services; + +public class ForgeService +{ + private readonly HttpClient Client; + + public ForgeService() + { + Client = new(); + } + + // Key: 1.9.4-recommended Value: 12.17.0.2317 + public async Task> GetVersions() + { + var data = await Client.GetAsync( + "https://files.minecraftforge.net/net/minecraftforge/forge/promotions_slim.json"); + + var json = new ConfigurationBuilder().AddJsonStream( + new MemoryStream(Encoding.ASCII.GetBytes( + await data.Content.ReadAsStringAsync() + ) + ) + ).Build(); + + var d = new Dictionary(); + + foreach (var section in json.GetSection("promos").GetChildren()) + { + d.Add(section.Key, section.Value!); + } + + return d; + } +} \ No newline at end of file diff --git a/Moonlight/Program.cs b/Moonlight/Program.cs index 33f4900d..07aaa267 100644 --- a/Moonlight/Program.cs +++ b/Moonlight/Program.cs @@ -106,6 +106,7 @@ namespace Moonlight builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddScoped(); + builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); @@ -113,8 +114,6 @@ namespace Moonlight builder.Services.AddScoped(); builder.Services.AddScoped(); - builder.Services.AddSingleton(); - // Loggers builder.Services.AddScoped(); builder.Services.AddScoped(); @@ -143,6 +142,7 @@ namespace Moonlight builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); + builder.Services.AddSingleton(); // Third party services builder.Services.AddBlazorTable(); diff --git a/Moonlight/Shared/Components/ServerControl/ServerSettings.razor b/Moonlight/Shared/Components/ServerControl/ServerSettings.razor index cd17ceed..8b575376 100644 --- a/Moonlight/Shared/Components/ServerControl/ServerSettings.razor +++ b/Moonlight/Shared/Components/ServerControl/ServerSettings.razor @@ -37,6 +37,9 @@ if(Tags.Contains("paperversion")) Settings.Add("Paper version", typeof(PaperVersionSetting)); + if(Tags.Contains("forgeversion")) + Settings.Add("Forge version", typeof(ForgeVersionSetting)); + if(Tags.Contains("join2start")) Settings.Add("Join2Start", typeof(Join2StartSetting)); diff --git a/Moonlight/Shared/Components/ServerControl/Settings/ForgeVersionSetting.razor b/Moonlight/Shared/Components/ServerControl/Settings/ForgeVersionSetting.razor new file mode 100644 index 00000000..c1f75be4 --- /dev/null +++ b/Moonlight/Shared/Components/ServerControl/Settings/ForgeVersionSetting.razor @@ -0,0 +1,147 @@ +@using Moonlight.App.Services +@using Microsoft.EntityFrameworkCore +@using Moonlight.App.Database.Entities +@using Moonlight.App.Repositories +@using Moonlight.App.Repositories.Servers +@using Logging.Net +@using Moonlight.App.Helpers + +@inject ServerService ServerService +@inject ServerRepository ServerRepository +@inject ImageRepository ImageRepository +@inject ForgeService ForgeService +@inject SmartTranslateService TranslationService + +
+
+ + + + + + +
+
+ +@code +{ + [CascadingParameter] + public Server CurrentServer { get; set; } + + private Dictionary Versions = new(); + private string CurrentVersion = ""; + + + private async Task Load(LazyLoader lazyLoader) + { + Versions = await ForgeService.GetVersions(); + + var vars = CurrentServer.Variables; + var versionVar = vars.FirstOrDefault(x => x.Key == "FORGE_VERSION"); + + // Live migration + if (versionVar == null) + { + CurrentServer.Variables.Add(new () + { + Key = "FORGE_VERSION", + Value = LatestVersion() + }); + + ServerRepository.Update(CurrentServer); + + versionVar = vars.First(x => x.Key == "FORGE_VERSION"); + } + else + { + if (string.IsNullOrEmpty(versionVar.Value)) + { + versionVar.Value = LatestVersion(); + ServerRepository.Update(CurrentServer); + } + } + + CurrentVersion = versionVar.Value; + + await InvokeAsync(StateHasChanged); + } + + private string DisplayToData(string display) + { + return display + .Replace("-recommended", "") + .Replace("-latest", "") + "-" + Versions[display]; + } + + private string LatestVersion() + { + var versionsSorted = Versions.Keys + .OrderByDescending(ParseHelper.MinecraftToInt); + + return DisplayToData(versionsSorted.First()); + } + + private async Task Save() + { + var vars = CurrentServer.Variables; + var versionVar = vars.First(x => x.Key == "FORGE_VERSION"); + + versionVar.Value = CurrentVersion; + + // This searches for the display name of a version using the constructed full version + var version = ParseHelper.MinecraftToInt( + Versions.First( + x => DisplayToData(x.Key) == CurrentVersion).Key); + + var serverImage = ImageRepository + .Get() + .Include(x => x.DockerImages) + .First(x => x.Id == CurrentServer.Image.Id); + + var dockerImages = serverImage.DockerImages; + + var dockerImageToUpdate = dockerImages.Last(); + + if (version < 1130) + { + dockerImageToUpdate = dockerImages.First(x => x.Name.Contains("8")); + } + + if (version >= 1130) + { + dockerImageToUpdate = dockerImages.First(x => x.Name.Contains("11")); + } + + if (version >= 1170) + { + dockerImageToUpdate = dockerImages.First(x => x.Name.Contains("16")); + } + + if (version >= 1190) + { + dockerImageToUpdate = dockerImages.First(x => x.Name.Contains("17")); + } + + CurrentServer.DockerImageIndex = dockerImages.IndexOf(dockerImageToUpdate); + + ServerRepository.Update(CurrentServer); + + await ServerService.Reinstall(CurrentServer); + } +} \ No newline at end of file