feat: automatic configuration checking

This commit is contained in:
CyberL1 2024-05-23 14:19:49 +02:00
parent 62864d58e1
commit 25c98d1687
3 changed files with 20 additions and 25 deletions

View File

@ -33,7 +33,7 @@ namespace Minecraft_Realms_Emulator.Helpers
public Configuration? GetSetting(string key) public Configuration? GetSetting(string key)
{ {
var setting = Db.Configuration.Find(key); var setting = Db.Configuration.Find(FirstCharToLowerCase(key));
if (setting == null) return null; if (setting == null) return null;
@ -43,5 +43,13 @@ namespace Minecraft_Realms_Emulator.Helpers
Value = JsonConvert.DeserializeObject(setting.Value) Value = JsonConvert.DeserializeObject(setting.Value)
}; };
} }
private static string FirstCharToLowerCase(string str)
{
if (!string.IsNullOrEmpty(str) && char.IsUpper(str[0]))
return str.Length == 1 ? char.ToLower(str[0]).ToString() : char.ToLower(str[0]) + str[1..];
return str;
}
} }
} }

View File

@ -1,6 +1,6 @@
namespace Minecraft_Realms_Emulator.Helpers.Config namespace Minecraft_Realms_Emulator.Helpers.Config
{ {
public class Settings : Dictionary<string, string> public class Settings
{ {
public string DefaultServerAddress { get; set; } = "127.0.0.1"; public string DefaultServerAddress { get; set; } = "127.0.0.1";
public string NewsLink { get; set; } = "https://github.com/CyberL1/Minecraft-Realms-Emulator"; public string NewsLink { get; set; } = "https://github.com/CyberL1/Minecraft-Realms-Emulator";

View File

@ -1,7 +1,6 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Minecraft_Realms_Emulator.Data; using Minecraft_Realms_Emulator.Data;
using Minecraft_Realms_Emulator.Entities; using Minecraft_Realms_Emulator.Entities;
using Minecraft_Realms_Emulator.Enums;
using Minecraft_Realms_Emulator.Helpers.Config; using Minecraft_Realms_Emulator.Helpers.Config;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -19,31 +18,19 @@ namespace Minecraft_Realms_Emulator.Helpers
var config = new ConfigHelper(db); var config = new ConfigHelper(db);
var settings = new Settings(); var settings = new Settings();
if (config.GetSetting(nameof(SettingsEnum.newsLink)) == null) foreach (var property in settings.GetType().GetProperties())
{ {
db.Configuration.Add(new Configuration var name = property.Name;
{ var value = property.GetValue(settings);
Key = nameof(SettingsEnum.newsLink),
Value = JsonConvert.SerializeObject(settings.NewsLink)
});
}
if (config.GetSetting(nameof(SettingsEnum.defaultServerAddress)) == null)
{
db.Configuration.Add(new Configuration
{
Key = nameof(SettingsEnum.defaultServerAddress),
Value = JsonConvert.SerializeObject(settings.DefaultServerAddress)
});
}
if (config.GetSetting(nameof(SettingsEnum.trialMode)) == null) if (config.GetSetting(name) == null)
{
db.Configuration.Add(new Configuration
{ {
Key = nameof(SettingsEnum.trialMode), db.Configuration.Add(new Configuration
Value = JsonConvert.SerializeObject(settings.TrialMode) {
}); Key = name,
Value = JsonConvert.SerializeObject(value)
});
}
} }
db.SaveChanges(); db.SaveChanges();