This commit is contained in:
2022-06-07 11:13:03 +03:00
parent 195c082cd7
commit c66ff52d94
8 changed files with 131 additions and 31 deletions

View File

@@ -3,13 +3,14 @@ using PluginManager.Others;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
namespace PluginManager
{
internal class AppConfig
{
public Dictionary<string, string> ApplicationVariables { get; set; }
public Dictionary<string, object> ApplicationVariables { get; set; }
public List<string> ProtectedKeyWords { get; set; }
}
@@ -17,26 +18,30 @@ namespace PluginManager
{
private static AppConfig appConfig = null;
public static bool AddValueToVariables(string key, string value, bool isProtected)
public static bool AddValueToVariables<T>(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 string? GetValue(string key)
public static T? GetValue<T>(string key)
{
if (!appConfig.ApplicationVariables.ContainsKey(key)) return null;
return appConfig.ApplicationVariables[key];
if (!appConfig.ApplicationVariables.ContainsKey(key)) return default;
JsonElement element = (JsonElement)appConfig.ApplicationVariables[key];
return element.Deserialize<T>();
}
public static bool SetValue(string key, string value)
public static bool SetValue<T>(string key, T value)
{
if (!appConfig.ApplicationVariables.ContainsKey(key)) return false;
if (appConfig.ProtectedKeyWords.Contains(key)) return false;
appConfig.ApplicationVariables[key] = value;
if (value == null) return false;
appConfig.ApplicationVariables[key] = JsonSerializer.SerializeToElement(value);
SaveConfig();
return true;
}
@@ -45,6 +50,7 @@ namespace PluginManager
{
appConfig.ApplicationVariables.Remove(key);
appConfig.ProtectedKeyWords.Remove(key);
SaveConfig();
return true;
}
@@ -63,13 +69,13 @@ namespace PluginManager
Functions.WriteLogFile($"Loaded {appConfig.ApplicationVariables.Keys.Count} application variables.\nLoaded {appConfig.ProtectedKeyWords.Count} readonly variables.");
}
else
appConfig = new() { ApplicationVariables = new Dictionary<string, string>(), ProtectedKeyWords = new List<string>() };
appConfig = new() { ApplicationVariables = new Dictionary<string, object>(), ProtectedKeyWords = new List<string>() };
}
public static string? GetKey(string value) => appConfig.ApplicationVariables.Keys.FirstOrDefault(x => appConfig.ApplicationVariables[x] == value);
public static bool ContainsValue(string value) => appConfig.ApplicationVariables.ContainsValue(value);
public static bool ContainsKey(string key) => appConfig.ApplicationVariables.ContainsKey(key);
public static Dictionary<string, string> GetAllVariables() => new Dictionary<string, string>(appConfig.ApplicationVariables);
public static Dictionary<string, object> GetAllVariables() => new(appConfig.ApplicationVariables);
}
}