diff --git a/Minecraft-Realms-Emulator/Helpers/Config/ConfigHelper.cs b/Minecraft-Realms-Emulator/Helpers/Config/ConfigHelper.cs index 34e9182..a00c848 100644 --- a/Minecraft-Realms-Emulator/Helpers/Config/ConfigHelper.cs +++ b/Minecraft-Realms-Emulator/Helpers/Config/ConfigHelper.cs @@ -33,7 +33,7 @@ namespace Minecraft_Realms_Emulator.Helpers public Configuration? GetSetting(string key) { - var setting = Db.Configuration.Find(key); + var setting = Db.Configuration.Find(FirstCharToLowerCase(key)); if (setting == null) return null; @@ -43,5 +43,13 @@ namespace Minecraft_Realms_Emulator.Helpers 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; + } } } diff --git a/Minecraft-Realms-Emulator/Helpers/Config/Settings.cs b/Minecraft-Realms-Emulator/Helpers/Config/Settings.cs index 7ee2c50..a412673 100644 --- a/Minecraft-Realms-Emulator/Helpers/Config/Settings.cs +++ b/Minecraft-Realms-Emulator/Helpers/Config/Settings.cs @@ -1,6 +1,6 @@ namespace Minecraft_Realms_Emulator.Helpers.Config { - public class Settings : Dictionary + public class Settings { public string DefaultServerAddress { get; set; } = "127.0.0.1"; public string NewsLink { get; set; } = "https://github.com/CyberL1/Minecraft-Realms-Emulator"; diff --git a/Minecraft-Realms-Emulator/Helpers/Database.cs b/Minecraft-Realms-Emulator/Helpers/Database.cs index 644f1e2..f2a12d2 100644 --- a/Minecraft-Realms-Emulator/Helpers/Database.cs +++ b/Minecraft-Realms-Emulator/Helpers/Database.cs @@ -1,7 +1,6 @@ using Microsoft.EntityFrameworkCore; using Minecraft_Realms_Emulator.Data; using Minecraft_Realms_Emulator.Entities; -using Minecraft_Realms_Emulator.Enums; using Minecraft_Realms_Emulator.Helpers.Config; using Newtonsoft.Json; @@ -19,31 +18,19 @@ namespace Minecraft_Realms_Emulator.Helpers var config = new ConfigHelper(db); var settings = new Settings(); - if (config.GetSetting(nameof(SettingsEnum.newsLink)) == null) + foreach (var property in settings.GetType().GetProperties()) { - db.Configuration.Add(new Configuration - { - 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) - }); - } + var name = property.Name; + var value = property.GetValue(settings); - if (config.GetSetting(nameof(SettingsEnum.trialMode)) == null) - { - db.Configuration.Add(new Configuration + if (config.GetSetting(name) == null) { - Key = nameof(SettingsEnum.trialMode), - Value = JsonConvert.SerializeObject(settings.TrialMode) - }); + db.Configuration.Add(new Configuration + { + Key = name, + Value = JsonConvert.SerializeObject(value) + }); + } } db.SaveChanges();