changed to .json file instead of database for settings

This commit is contained in:
2023-03-24 21:52:03 +02:00
parent 7e2fa02d07
commit 460a85944a
15 changed files with 207 additions and 312 deletions

View File

@@ -60,17 +60,17 @@ public class Boot
/// <returns>Task</returns>
public async Task Awake(DiscordSocketConfig? config = null)
{
if(config is null)
config = new DiscordSocketConfig
{
if (config is null)
config = new DiscordSocketConfig
{
AlwaysDownloadUsers = true,
AlwaysDownloadUsers = true,
//Disable system clock checkup (for responses at slash commands)
UseInteractionSnowflakeDate = false,
//Disable system clock checkup (for responses at slash commands)
UseInteractionSnowflakeDate = false,
GatewayIntents = GatewayIntents.All
};
GatewayIntents = GatewayIntents.All
};
client = new DiscordSocketClient(config);
service = new CommandService();
@@ -105,7 +105,7 @@ public class Boot
{
if (arg.Message.Contains("401"))
{
Config.Variables.RemoveKey("token");
Config.Data.Remove("token");
Logger.LogError("The token is invalid. Please restart the bot and enter a valid token.");
await Task.Delay(4000);
Environment.Exit(0);

View File

@@ -2,15 +2,21 @@
using System.Threading.Tasks;
using System.IO;
using System.Collections.Generic;
using PluginManager.Others;
using System.Collections;
using PluginManager.Online.Helpers;
using PluginManager.Database;
namespace PluginManager;
public static class Config
{
private static bool IsLoaded = false;
public static async Task Initialize(string DatabaseName, bool isConsole)
public static Json<string, string> Data;
public static Json<string, string> Plugins;
public static async Task Initialize(bool isConsole)
{
if (IsLoaded)
return;
@@ -19,200 +25,126 @@ public static class Config
Directory.CreateDirectory("./Data/Plugins");
Directory.CreateDirectory("./Data/PAKS");
Settings.sqlDatabase = new SqlDatabase(DatabaseName);
await Settings.sqlDatabase.Open();
if (!await Settings.sqlDatabase.TableExistsAsync("Plugins"))
await Settings.sqlDatabase.CreateTableAsync("Plugins", "PluginName", "Version");
if (!await Settings.sqlDatabase.TableExistsAsync("Variables"))
await Settings.sqlDatabase.CreateTableAsync("Variables", "VarName", "Value", "ReadOnly");
Data = new Json<string, string>("./Data/Resources/config.json");
Plugins = new Json<string, string>("./Data/Resources/Plugins.json");
IsLoaded = true;
Logger.Initialize(isConsole);
PluginManager.Others.ArchiveManager.Initialize();
ArchiveManager.Initialize();
if(isConsole)
if (isConsole)
Logger.LogEvent += (message) => { Console.Write(message); };
}
public static class Variables
public class Json<TKey, TValue> : IDictionary<TKey, TValue>
{
public static async Task<string?> GetValueAsync(string VarName)
protected IDictionary<TKey, TValue> _dictionary;
public Json(IDictionary<TKey, TValue> dictionary)
{
if (!IsLoaded)
throw new Exception("Config is not loaded");
return await Settings.sqlDatabase.GetValueAsync("Variables", "VarName", VarName, "Value");
_dictionary = dictionary;
}
public static string? GetValue(string VarName)
public Json(string file)
{
if (!IsLoaded)
throw new Exception("Config is not loaded");
return Settings.sqlDatabase.GetValue("Variables", "VarName", VarName, "Value");
_dictionary = PrivateReadConfig(file).GetAwaiter().GetResult();
}
public static async Task SetValueAsync(string VarName, string Value)
public virtual void Add(TKey key, TValue value)
{
if (!IsLoaded)
throw new Exception("Config is not loaded");
if (await IsReadOnlyAsync(VarName))
throw new Exception($"Variable ({VarName}) is read only and can not be changed to {Value}");
await Settings.sqlDatabase.SetValueAsync("Variables", "VarName", VarName, "Value", Value);
_dictionary.Add(key, value);
}
public static void SetValue(string VarName, string Value)
public void Clear() { _dictionary.Clear(); }
public bool ContainsKey(TKey key)
{
if (!IsLoaded)
throw new Exception("Config is not loaded");
if (IsReadOnly(VarName))
throw new Exception($"Variable ({VarName}) is read only and can not be changed to {Value}");
Settings.sqlDatabase.SetValue("Variables", "VarName", VarName, "Value", Value);
if (_dictionary == null)
throw new Exception("Dictionary is null");
return _dictionary.ContainsKey(key);
}
public virtual ICollection<TKey> Keys => _dictionary.Keys;
public static async Task<bool> IsReadOnlyAsync(string VarName)
{
if (!IsLoaded)
throw new Exception("Config is not loaded");
return (await Settings.sqlDatabase.GetValueAsync("Variables", "VarName", VarName, "ReadOnly")).Equals("true", StringComparison.CurrentCultureIgnoreCase);
}
public virtual ICollection<TValue> Values => _dictionary.Values;
public static bool IsReadOnly(string VarName)
{
if (!IsLoaded)
throw new Exception("Config is not loaded");
return (Settings.sqlDatabase.GetValue("Variables", "VarName", VarName, "ReadOnly")).Equals("true", StringComparison.CurrentCultureIgnoreCase);
}
public int Count => _dictionary.Count;
public static async Task SetReadOnlyAsync(string VarName, bool ReadOnly)
{
if (!IsLoaded)
throw new Exception("Config is not loaded");
await Settings.sqlDatabase.SetValueAsync("Variables", "VarName", VarName, "ReadOnly", ReadOnly ? "true" : "false");
}
public bool IsReadOnly => _dictionary.IsReadOnly;
public static void SetReadOnly(string VarName, bool ReadOnly)
public virtual TValue this[TKey key]
{
if (!IsLoaded)
throw new Exception("Config is not loaded");
Settings.sqlDatabase.SetValue("Variables", "VarName", VarName, "ReadOnly", ReadOnly ? "true" : "false");
}
public static async Task<bool> ExistsAsync(string VarName)
{
if (!IsLoaded)
throw new Exception("Config is not loaded");
return await Settings.sqlDatabase.KeyExistsAsync("Variables", "VarName", VarName);
}
public static bool Exists(string VarName)
{
if (!IsLoaded)
throw new Exception("Config is not loaded");
return Settings.sqlDatabase.KeyExists("Variables", "VarName", VarName);
}
public static async Task AddAsync(string VarName, string Value, bool ReadOnly = false)
{
if (!IsLoaded)
throw new Exception("Config is not loaded");
if (await ExistsAsync(VarName))
get
{
await SetValueAsync(VarName, Value);
await SetReadOnlyAsync(VarName, ReadOnly);
return;
}
await Settings.sqlDatabase.InsertAsync("Variables", VarName, Value, ReadOnly ? "true" : "false");
}
if (_dictionary.TryGetValue(key, out TValue value)) return value;
return default;
public static void Add(string VarName, string Value, bool ReadOnly = false)
{
if (!IsLoaded)
throw new Exception("Config is not loaded");
if (Exists(VarName))
}
set
{
if (GetValue(VarName) == Value)
return;
SetValue(VarName, Value);
SetReadOnly(VarName, ReadOnly);
return;
if (_dictionary.ContainsKey(key))
_dictionary[key] = value;
else _dictionary.Add(key, value);
}
Settings.sqlDatabase.Insert("Variables", VarName, Value, ReadOnly ? "true" : "false");
}
public static async Task RemoveKeyAsync(string VarName)
public virtual bool TryGetValue(TKey key, out TValue value)
{
if (!IsLoaded)
throw new Exception("Config is not loaded");
await Settings.sqlDatabase.RemoveKeyAsync("Variables", "VarName", VarName);
return _dictionary.TryGetValue(key, out value);
}
public static void RemoveKey(string VarName)
private async Task<Dictionary<TKey, TValue>> PrivateReadConfig(string file)
{
if (!IsLoaded)
throw new Exception("Config is not loaded");
Settings.sqlDatabase.RemoveKey("Variables", "VarName", VarName);
if (!File.Exists(file))
{
var dictionary = new Dictionary<TKey, TValue>();
await Functions.SaveToJsonFile(file, _dictionary);
return dictionary;
}
var d = await Functions.ConvertFromJson<Dictionary<TKey, TValue>>(file);
if (d is null)
throw new Exception("Failed to read config file");
return d;
}
public bool Remove(TKey key)
{
return _dictionary.Remove(key);
}
public void Add(KeyValuePair<TKey, TValue> item)
{
_dictionary.Add(item);
}
public bool Contains(KeyValuePair<TKey, TValue> item)
{
return _dictionary.Contains(item);
}
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
_dictionary.CopyTo(array, arrayIndex);
}
public bool Remove(KeyValuePair<TKey, TValue> item)
{
return _dictionary.Remove(item);
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return _dictionary.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)_dictionary).GetEnumerator();
}
}
public static class Plugins
{
public static async Task<string> GetVersionAsync(string pluginName)
{
if (!IsLoaded)
throw new Exception("Config is not loaded yet");
string result = await Settings.sqlDatabase.GetValueAsync("Plugins", "PluginName", pluginName, "Version");
if (result is null)
return "0.0.0";
return result;
}
public static string GetVersion(string pluginName)
{
if (!IsLoaded)
throw new Exception("Config is not loaded yet");
string result = Settings.sqlDatabase.GetValue("Plugins", "PluginName", pluginName, "Version");
if (result is null)
return "0.0.0";
return result;
}
public static async Task SetVersionAsync(string pluginName, VersionString version)
{
if (!IsLoaded)
throw new Exception("Config is not loaded yet");
if (!await Settings.sqlDatabase.KeyExistsAsync("Plugins", "PluginName", pluginName))
{
await Settings.sqlDatabase.InsertAsync("Plugins", pluginName, version.ToShortString());
return;
}
await Settings.sqlDatabase.SetValueAsync("Plugins", "PluginName", pluginName, "Version", version.ToShortString());
}
public static void SetVersion(string pluginName, VersionString version)
{
if (!IsLoaded)
throw new Exception("Config is not loaded yet");
if (!Settings.sqlDatabase.KeyExists("Plugins", "PluginName", pluginName))
{
Settings.sqlDatabase.Insert("Plugins", pluginName, version.ToShortString());
return;
}
Settings.sqlDatabase.SetValue("Plugins", "PluginName", pluginName, "Version", version.ToShortString());
}
}
}
}

View File

@@ -254,7 +254,7 @@ public class ConsoleCommandsHandler
var ver = await ServerCom.GetVersionOfPackageFromWeb(name);
if (ver is null) throw new Exception("Incorrect version");
await Config.Plugins.SetVersionAsync(name, ver);
Config.Plugins[name] = ver.ToShortString();
isDownloading = false;
@@ -267,15 +267,15 @@ public class ConsoleCommandsHandler
{
if (args.Length != 2)
return;
if (!Config.Variables.Exists(args[1]))
if (!Config.Data.ContainsKey(args[1]))
return;
var data = Config.Variables.GetValue(args[1]);
var data = Config.Data[args[1]];
Logger.WriteLine($"{args[1]} => {data}");
}
);
AddCommand("add", "add variable to the system variables", "add [key] [value] [isReadOnly=true/false]", args =>
AddCommand("add", "add variable to the system variables", "add [key] [value]", args =>
{
if (args.Length < 4)
return;
@@ -285,7 +285,7 @@ public class ConsoleCommandsHandler
try
{
Config.Variables.Add(key, value, isReadOnly);
Config.Data[key] = value;
Logger.WriteLine($"Updated config file with the following command: {args[1]} => {value}");
}
catch (Exception ex)
@@ -299,7 +299,7 @@ public class ConsoleCommandsHandler
{
if (args.Length < 2)
return;
Config.Variables.RemoveKey(args[1]);
Config.Data.Remove(args[1]);
}
);
@@ -308,7 +308,8 @@ public class ConsoleCommandsHandler
if (client is null)
return;
Settings.sqlDatabase.Stop();
await Functions.SaveToJsonFile(Functions.dataFolder + "config.json", Config.Data);
await Functions.SaveToJsonFile(Functions.dataFolder + "Plugins.json", Config.Plugins);
await client.StopAsync();
await client.DisposeAsync();
await Task.Delay(1000);

View File

@@ -80,8 +80,8 @@ public class PluginLoader
var version = await ServerCom.GetVersionOfPackageFromWeb(name);
if (version is null)
return;
if (Config.Plugins.GetVersion(name) is not null)
Config.Plugins.SetVersion(name, version);
if (Config.Plugins[name] is not null)
Config.Plugins[name] = version.ToShortString();
if (await PluginUpdater.CheckForUpdates(name))
await PluginUpdater.Download(name);

View File

@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Numerics;
using Discord;
namespace PluginManager
@@ -17,17 +18,19 @@ namespace PluginManager
public static void Initialize(bool console)
{
if (isInitialized) throw new Exception("Logger is already initialized");
if (isInitialized)
throw new Exception("Logger is already initialized");
if (!Config.Variables.Exists("LogFolder"))
Config.Variables.Add("LogFolder", "./Data/Output/Logs/");
if (!Config.Data.ContainsKey("LogFolder"))
Config.Data.Add("LogFolder", "./Data/Output/Logs/");
if (!Config.Variables.Exists("ErrorFolder"))
Config.Variables.Add("ErrorFolder", "./Data/Output/Errors/");
if (!Config.Data.ContainsKey("ErrorFolder"))
Config.Data.Add("ErrorFolder", "./Data/Output/Errors/");
isInitialized = true;
logFolder = Config.Variables.GetValue("LogFolder");
errFolder = Config.Variables.GetValue("ErrorFolder");
logFolder = Config.Data["LogFolder"];
errFolder = Config.Data["ErrorFolder"];
isConsole = console;
}
@@ -105,7 +108,8 @@ namespace PluginManager
public static void WriteColored(string message, ConsoleColor color)
{
if (!isInitialized) throw new Exception("Logger is not initialized");
if(!isConsole) {
if (!isConsole)
{
LogEvent?.Invoke(message);
return;
}

View File

@@ -91,9 +91,7 @@ public static class ServerCom
public static VersionString? GetVersionOfPackage(string pakName)
{
if (Config.Plugins.GetVersion(pakName) is null)
return null;
return new VersionString(Config.Plugins.GetVersion(pakName));
return new VersionString(Config.Plugins[pakName]);
}
public static async Task<VersionString?> GetVersionOfPackageFromWeb(string pakName)

View File

@@ -15,11 +15,12 @@ namespace PluginManager.Others
{
if (isInitialized) throw new Exception("ArchiveManager is already initialized");
if (!Config.Variables.Exists("ArchiveFolder"))
Config.Variables.Add("ArchiveFolder", "./Data/PAKS/");
if (!Config.Data.ContainsKey("ArchiveFolder"))
Config.Data["ArchiveFolder"] = "./Data/PAKS/";
archiveFolder = Config.Data["ArchiveFolder"];
isInitialized = true;
archiveFolder = Config.Variables.GetValue("ArchiveFolder");
}
/// <summary>

View File

@@ -94,6 +94,7 @@ public static class Functions
/// <returns></returns>
public static async Task<T> ConvertFromJson<T>(string input)
{
Console.WriteLine(input);
Stream text;
if (File.Exists(input))
text = new MemoryStream(await File.ReadAllBytesAsync(input));

View File

@@ -1,17 +0,0 @@
using PluginManager.Database;
namespace PluginManager
{
public class Settings
{
public static class Variables
{
public static string WebsiteURL = "https://wizzy69.github.io/SethDiscordBot";
public static string UpdaterURL = "https://github.com/Wizzy69/installer/releases/download/release-1-discordbot/Updater.zip";
}
public static SqlDatabase sqlDatabase;
}
}