This commit is contained in:
Marcel Baumgartner 2023-04-26 20:30:04 +02:00
commit 7d72e41558
5 changed files with 200 additions and 5 deletions

View file

@ -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)

View file

@ -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<Dictionary<string, string>> 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<string, string>();
foreach (var section in json.GetSection("promos").GetChildren())
{
d.Add(section.Key, section.Value!);
}
return d;
}
}

View file

@ -106,6 +106,7 @@ namespace Moonlight
builder.Services.AddSingleton<DateTimeService>();
builder.Services.AddSingleton<EventSystem>();
builder.Services.AddScoped<FileDownloadService>();
builder.Services.AddScoped<ForgeService>();
builder.Services.AddScoped<GoogleOAuth2Service>();
builder.Services.AddScoped<DiscordOAuth2Service>();
@ -113,8 +114,6 @@ namespace Moonlight
builder.Services.AddScoped<SubscriptionService>();
builder.Services.AddScoped<SubscriptionAdminService>();
builder.Services.AddSingleton<CleanupService>();
// Loggers
builder.Services.AddScoped<SecurityLogService>();
builder.Services.AddScoped<AuditLogService>();
@ -143,6 +142,7 @@ namespace Moonlight
builder.Services.AddSingleton<DiscordBotService>();
builder.Services.AddSingleton<StatisticsCaptureService>();
builder.Services.AddSingleton<DiscordNotificationService>();
builder.Services.AddSingleton<CleanupService>();
// Third party services
builder.Services.AddBlazorTable();

View file

@ -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));

View file

@ -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
<div class="col">
<div class="card card-body">
<LazyLoader Load="Load">
<label class="mb-2 form-label"><TL>Forge version</TL></label>
<select class="mb-2 form-select" @bind="CurrentVersion">
@foreach (var version in Versions.Keys)
{
if (DisplayToData(version) == CurrentVersion)
{
<option value="@(DisplayToData(version))" selected="">@(version)</option>
}
else
{
<option value="@(DisplayToData(version))">@(version)</option>
}
}
</select>
<WButton
OnClick="Save"
Text="@(TranslationService.Translate("Change"))"
WorkingText="@(TranslationService.Translate("Changing"))"
CssClasses="btn-primary">
</WButton>
</LazyLoader>
</div>
</div>
@code
{
[CascadingParameter]
public Server CurrentServer { get; set; }
private Dictionary<string, string> 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);
}
}