This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
using System.Linq;
|
||||
using System.Reflection.Metadata.Ecma335;
|
||||
using Newtonsoft.Json.Converters;
|
||||
|
||||
namespace PluginManager.Items
|
||||
{
|
||||
@@ -193,22 +194,43 @@ namespace PluginManager.Items
|
||||
if (args.Length != 2) return;
|
||||
if (!Config.ContainsKey(args[1])) return;
|
||||
|
||||
string data = Config.GetValue(args[1]);
|
||||
string data = Config.GetValue<string>(args[1]);
|
||||
Console.WriteLine($"{args[1]} => {data}");
|
||||
}
|
||||
);
|
||||
|
||||
AddCommand("addv", "add variable to the system variables", async (args) =>
|
||||
AddCommand("add", "add variable to the system variables\nadd [key] [value] [isReadOnly=true/false]", async (args) =>
|
||||
{
|
||||
if (args.Length < 3) return;
|
||||
string in1 = args[1];
|
||||
if (!Config.ContainsKey(in1))
|
||||
Config.AddValueToVariables(in1, Functions.MergeStrings(args, 2), false);
|
||||
else
|
||||
Config.SetValue(in1, Functions.MergeStrings(args, 2));
|
||||
if (args.Length < 4) return;
|
||||
string key = args[1];
|
||||
string value = args[2];
|
||||
bool isReadOnly = args[3].Equals("true", StringComparison.CurrentCultureIgnoreCase);
|
||||
|
||||
Console.WriteLine($"Updated config file with the following command: {in1} => {Config.GetValue(in1)}");
|
||||
Config.SaveConfig();
|
||||
try
|
||||
{
|
||||
if (Config.ContainsKey(key)) return;
|
||||
if (int.TryParse(value, out int intValue))
|
||||
Config.AddValueToVariables(key, intValue, isReadOnly);
|
||||
else if (bool.TryParse(value, out bool boolValue))
|
||||
Config.AddValueToVariables(key, boolValue, isReadOnly);
|
||||
else if (float.TryParse(value, out float floatValue))
|
||||
Config.AddValueToVariables(key, floatValue, isReadOnly);
|
||||
else if (double.TryParse(value, out double doubleValue))
|
||||
Config.AddValueToVariables(key, doubleValue, isReadOnly);
|
||||
else if (uint.TryParse(value, out uint uintValue))
|
||||
Config.AddValueToVariables(key, uintValue, isReadOnly);
|
||||
else if (long.TryParse(value, out long longValue))
|
||||
Config.AddValueToVariables(key, longValue, isReadOnly);
|
||||
else if (byte.TryParse(value, out byte byteValue))
|
||||
Config.AddValueToVariables(key, byteValue, isReadOnly);
|
||||
else
|
||||
Config.AddValueToVariables(key, value, isReadOnly);
|
||||
Console.WriteLine($"Updated config file with the following command: {args[1]} => {value}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.ToString());
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
@@ -216,7 +238,6 @@ namespace PluginManager.Items
|
||||
{
|
||||
if (args.Length < 2) return;
|
||||
Config.RemoveKey(args[1]);
|
||||
Config.SaveConfig();
|
||||
}
|
||||
);
|
||||
|
||||
@@ -227,7 +248,7 @@ namespace PluginManager.Items
|
||||
data.Add(new string[] { "-", "-" });
|
||||
data.Add(new string[] { "Key", "Value" });
|
||||
data.Add(new string[] { "-", "-" });
|
||||
foreach (var kvp in d) data.Add(new string[] { kvp.Key, kvp.Value });
|
||||
foreach (var kvp in d) data.Add(new string[] { kvp.Key, kvp.Value.ToString() });
|
||||
data.Add(new string[] { "-", "-" });
|
||||
Console_Utilities.FormatAndAlignTable(data);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user