using PluginManager.Others; using System.IO; using System.Text.Json; using System.Threading.Tasks; using System.Collections.Generic; namespace PluginManager { internal class AppConfig { public Dictionary? ApplicationVariables { get; set; } public List? ProtectedKeyWords { get; set; } } public static class Config { private static AppConfig? appConfig { get; set; } public static bool AddValueToVariables(string key, T value, bool isProtected) { if (appConfig!.ApplicationVariables!.ContainsKey(key)) return false; if (value == null) return false; appConfig.ApplicationVariables.Add(key, value); if (isProtected) appConfig.ProtectedKeyWords!.Add(key); SaveConfig(); return true; } public static T? GetValue(string key) { if (!appConfig!.ApplicationVariables!.ContainsKey(key)) return default; try { JsonElement element = (JsonElement)appConfig.ApplicationVariables[key]; return element.Deserialize(); } catch { return (T)appConfig.ApplicationVariables[key]; } } public static bool SetValue(string key, T value) { if (!appConfig!.ApplicationVariables!.ContainsKey(key)) return false; if (appConfig.ProtectedKeyWords!.Contains(key)) return false; if (value == null) return false; appConfig.ApplicationVariables[key] = JsonSerializer.SerializeToElement(value); SaveConfig(); return true; } public static bool RemoveKey(string key) { appConfig!.ApplicationVariables!.Remove(key); appConfig.ProtectedKeyWords!.Remove(key); SaveConfig(); return true; } public static async void SaveConfig() { string path = Functions.dataFolder + "config.json"; await Functions.SaveToJsonFile(path, appConfig!); } public static async Task LoadConfig() { string path = Functions.dataFolder + "config.json"; if (File.Exists(path)) { appConfig = await Functions.ConvertFromJson(path); Functions.WriteLogFile($"Loaded {appConfig.ApplicationVariables!.Keys.Count} application variables.\nLoaded {appConfig.ProtectedKeyWords!.Count} readonly variables."); } else appConfig = new() { ApplicationVariables = new Dictionary(), ProtectedKeyWords = new List() }; } public static bool ContainsValue(T value) => appConfig!.ApplicationVariables!.ContainsValue(value!); public static bool ContainsKey(string key) => appConfig!.ApplicationVariables!.ContainsKey(key); public static Dictionary GetAllVariables() => new(appConfig!.ApplicationVariables!); } }