Moved items and reimplemented SettingsDictionary.cs
This commit is contained in:
@@ -107,10 +107,10 @@ public class Boot
|
||||
{
|
||||
if (arg.Message.Contains("401"))
|
||||
{
|
||||
Config.Data.Remove("token");
|
||||
Config.AppSettings.Remove("token");
|
||||
Config.Logger.Log("The token is invalid. Please restart the bot and enter a valid token.", this,
|
||||
LogLevel.ERROR);
|
||||
Config.Data.Save();
|
||||
await Config.AppSettings.SaveToFile();
|
||||
await Task.Delay(4000);
|
||||
Environment.Exit(0);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using PluginManager.Bot;
|
||||
using PluginManager.Others;
|
||||
@@ -13,8 +10,7 @@ public class Config
|
||||
{
|
||||
private static bool IsLoaded;
|
||||
public static DBLogger Logger;
|
||||
public static Json<string, string>? Data;
|
||||
// public static Json<string, string>? Plugins;
|
||||
public static SettingsDictionary<string, string>? AppSettings;
|
||||
|
||||
internal static Boot? _DiscordBotClient;
|
||||
|
||||
@@ -22,8 +18,7 @@ public class Config
|
||||
|
||||
public static async Task Initialize()
|
||||
{
|
||||
if (IsLoaded)
|
||||
return;
|
||||
if (IsLoaded) return;
|
||||
|
||||
Directory.CreateDirectory("./Data/Resources");
|
||||
Directory.CreateDirectory("./Data/Plugins");
|
||||
@@ -31,11 +26,10 @@ public class Config
|
||||
Directory.CreateDirectory("./Data/Logs/Logs");
|
||||
Directory.CreateDirectory("./Data/Logs/Errors");
|
||||
|
||||
Data = new Json<string, string>("./Data/Resources/config.json");
|
||||
// Plugins = new Json<string, string>("./Data/Resources/Plugins.json");
|
||||
AppSettings = new SettingsDictionary<string, string>("./Data/Resources/config.json");
|
||||
|
||||
Data["LogFolder"] = "./Data/Logs/Logs";
|
||||
Data["ErrorFolder"] = "./Data/Logs/Errors";
|
||||
AppSettings["LogFolder"] = "./Data/Logs/Logs";
|
||||
AppSettings["ErrorFolder"] = "./Data/Logs/Errors";
|
||||
|
||||
Logger = new DBLogger();
|
||||
|
||||
@@ -45,129 +39,5 @@ public class Config
|
||||
|
||||
Logger.Log("Config initialized", LogLevel.INFO);
|
||||
}
|
||||
|
||||
public class Json<TKey, TValue> : IDictionary<TKey, TValue>
|
||||
{
|
||||
private readonly string _file = "";
|
||||
private readonly IDictionary<TKey, TValue>? _dictionary;
|
||||
|
||||
public Json(string file)
|
||||
{
|
||||
if (file is null) throw new FileLoadException("The file can not be null");
|
||||
if(!File.Exists(file))
|
||||
File.Create(file).Close();
|
||||
|
||||
_dictionary = PrivateReadConfig(file).GetAwaiter().GetResult();
|
||||
_file = file;
|
||||
}
|
||||
|
||||
public virtual void Add(TKey key, TValue value)
|
||||
{
|
||||
_dictionary.Add(key, value);
|
||||
}
|
||||
|
||||
public void Clear() { _dictionary.Clear(); }
|
||||
|
||||
public bool ContainsKey(TKey key)
|
||||
{
|
||||
if (_dictionary == null)
|
||||
throw new Exception("Dictionary is null");
|
||||
|
||||
return _dictionary.ContainsKey(key);
|
||||
}
|
||||
|
||||
public virtual ICollection<TKey> Keys => _dictionary.Keys;
|
||||
|
||||
public virtual ICollection<TValue> Values => _dictionary.Values;
|
||||
|
||||
public int Count => _dictionary.Count;
|
||||
|
||||
public bool IsReadOnly => _dictionary.IsReadOnly;
|
||||
|
||||
public virtual TValue this[TKey key]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_dictionary.TryGetValue(key, out var value)) return value;
|
||||
throw new Exception("Key not found in dictionary " + key + " (Json )" + GetType().Name +
|
||||
")");
|
||||
}
|
||||
set
|
||||
{
|
||||
if (_dictionary.ContainsKey(key))
|
||||
_dictionary[key] = value;
|
||||
else _dictionary.Add(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual bool TryGetValue(TKey key, out TValue value)
|
||||
{
|
||||
return _dictionary.TryGetValue(key, out value);
|
||||
}
|
||||
|
||||
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 async Task Save()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_file))
|
||||
await Functions.SaveToJsonFile(_file, _dictionary);
|
||||
}
|
||||
|
||||
private async Task<Dictionary<TKey, TValue>> PrivateReadConfig(string file)
|
||||
{
|
||||
if (!File.Exists(file))
|
||||
{
|
||||
var dictionary = new Dictionary<TKey, TValue>();
|
||||
await Functions.SaveToJsonFile(file, _dictionary);
|
||||
return dictionary;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var d = await Functions.ConvertFromJson<Dictionary<TKey, TValue>>(file);
|
||||
if (d is null)
|
||||
throw new Exception("Failed to read config file");
|
||||
|
||||
return d;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
File.Delete(file);
|
||||
return new Dictionary<TKey, TValue>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ namespace PluginManager.Database;
|
||||
public class SqlDatabase
|
||||
{
|
||||
private readonly SQLiteConnection Connection;
|
||||
private readonly string ConnectionString;
|
||||
|
||||
/// <summary>
|
||||
/// Initialize a SQL connection by specifing its private path
|
||||
@@ -22,8 +21,8 @@ public class SqlDatabase
|
||||
fileName = Path.Combine("./Data/Resources", fileName);
|
||||
if (!File.Exists(fileName))
|
||||
SQLiteConnection.CreateFile(fileName);
|
||||
ConnectionString = $"URI=file:{fileName}";
|
||||
Connection = new SQLiteConnection(ConnectionString);
|
||||
var connectionString = $"URI=file:{fileName}";
|
||||
Connection = new SQLiteConnection(connectionString);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -16,10 +16,10 @@ public static class ArchiveManager
|
||||
{
|
||||
if (isInitialized) throw new Exception("ArchiveManager is already initialized");
|
||||
|
||||
if (!Config.Data.ContainsKey("ArchiveFolder"))
|
||||
Config.Data["ArchiveFolder"] = "./Data/PAKS/";
|
||||
if (!Config.AppSettings.ContainsKey("ArchiveFolder"))
|
||||
Config.AppSettings["ArchiveFolder"] = "./Data/PAKS/";
|
||||
|
||||
archiveFolder = Config.Data["ArchiveFolder"];
|
||||
archiveFolder = Config.AppSettings["ArchiveFolder"];
|
||||
|
||||
isInitialized = true;
|
||||
}
|
||||
|
||||
@@ -76,42 +76,7 @@ public static class Functions
|
||||
progress?.Report(totalBytesRead);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Save to JSON file
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The class type</typeparam>
|
||||
/// <param name="file">The file path</param>
|
||||
/// <param name="Data">The values</param>
|
||||
/// <returns></returns>
|
||||
public static async Task SaveToJsonFile<T>(string file, T Data)
|
||||
{
|
||||
var str = new MemoryStream();
|
||||
await JsonSerializer.SerializeAsync(str, Data, typeof(T), new JsonSerializerOptions { WriteIndented = true });
|
||||
await File.WriteAllBytesAsync(file, str.ToArray());
|
||||
await str.FlushAsync();
|
||||
str.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert json text or file to some kind of data
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The data type</typeparam>
|
||||
/// <param name="input">The file or json text</param>
|
||||
/// <returns></returns>
|
||||
public static async Task<T> ConvertFromJson<T>(string input)
|
||||
{
|
||||
Stream text;
|
||||
if (File.Exists(input))
|
||||
text = new MemoryStream(await File.ReadAllBytesAsync(input));
|
||||
else
|
||||
text = new MemoryStream(Encoding.ASCII.GetBytes(input));
|
||||
text.Position = 0;
|
||||
var obj = await JsonSerializer.DeserializeAsync<T>(text);
|
||||
await text.FlushAsync();
|
||||
text.Close();
|
||||
return (obj ?? default)!;
|
||||
}
|
||||
|
||||
|
||||
public static T SelectRandomValueOf<T>()
|
||||
{
|
||||
|
||||
47
PluginManager/Others/JsonManager.cs
Normal file
47
PluginManager/Others/JsonManager.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PluginManager;
|
||||
|
||||
public class JsonManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Save to JSON file
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The class type</typeparam>
|
||||
/// <param name="file">The file path</param>
|
||||
/// <param name="Data">The values</param>
|
||||
/// <returns></returns>
|
||||
public static async Task SaveToJsonFile<T>(string file, T Data)
|
||||
{
|
||||
var str = new MemoryStream();
|
||||
await JsonSerializer.SerializeAsync(str, Data, typeof(T), new JsonSerializerOptions { WriteIndented = true });
|
||||
await File.WriteAllBytesAsync(file, str.ToArray());
|
||||
await str.FlushAsync();
|
||||
str.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert json text or file to some kind of data
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The data type</typeparam>
|
||||
/// <param name="input">The file or json text</param>
|
||||
/// <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));
|
||||
else
|
||||
text = new MemoryStream(Encoding.ASCII.GetBytes(input));
|
||||
text.Position = 0;
|
||||
var obj = await JsonSerializer.DeserializeAsync<T>(text);
|
||||
await text.FlushAsync();
|
||||
text.Close();
|
||||
return (obj ?? default)!;
|
||||
}
|
||||
}
|
||||
@@ -16,8 +16,8 @@ public class DBLogger
|
||||
|
||||
public DBLogger()
|
||||
{
|
||||
_logFolder = Config.Data["LogFolder"];
|
||||
_errFolder = Config.Data["ErrorFolder"];
|
||||
_logFolder = Config.AppSettings["LogFolder"];
|
||||
_errFolder = Config.AppSettings["ErrorFolder"];
|
||||
}
|
||||
|
||||
public IReadOnlyList<LogMessage> Logs => LogHistory;
|
||||
@@ -69,9 +69,9 @@ public class DBLogger
|
||||
public async Task SaveToFile(bool ErrorsOnly = true)
|
||||
{
|
||||
if(!ErrorsOnly)
|
||||
await Functions.SaveToJsonFile(_logFolder + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".json",
|
||||
await JsonManager.SaveToJsonFile(_logFolder + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".json",
|
||||
LogHistory);
|
||||
await Functions.SaveToJsonFile(_errFolder + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".json",
|
||||
await JsonManager.SaveToJsonFile(_errFolder + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".json",
|
||||
ErrorHistory);
|
||||
}
|
||||
}
|
||||
|
||||
102
PluginManager/Others/SettingsDictionary.cs
Normal file
102
PluginManager/Others/SettingsDictionary.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PluginManager.Others;
|
||||
|
||||
public class SettingsDictionary<TKey, TValue> : IDictionary<TKey, TValue>
|
||||
{
|
||||
public string _file { get; set; }
|
||||
private IDictionary<TKey, TValue>? _dictionary;
|
||||
|
||||
public SettingsDictionary(string file)
|
||||
{
|
||||
if (file is null)
|
||||
throw new FileLoadException("The file can not be null");
|
||||
|
||||
if (!File.Exists(file))
|
||||
File.Create(file).Close();
|
||||
|
||||
_file = file;
|
||||
LoadFromFile();
|
||||
}
|
||||
|
||||
public async Task SaveToFile()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_file))
|
||||
await JsonManager.SaveToJsonFile(_file, _dictionary);
|
||||
}
|
||||
|
||||
private void LoadFromFile()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_file))
|
||||
_dictionary = JsonManager.ConvertFromJson<IDictionary<TKey, TValue>>(_file).Result;
|
||||
}
|
||||
|
||||
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
|
||||
{
|
||||
return _dictionary!.GetEnumerator();
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return ((IEnumerable) _dictionary!).GetEnumerator();
|
||||
}
|
||||
|
||||
public void Add(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
this._dictionary!.Add(item);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
this._dictionary!.Clear();
|
||||
}
|
||||
|
||||
public bool Contains(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
return this._dictionary!.Contains(item);
|
||||
}
|
||||
|
||||
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
|
||||
{
|
||||
this._dictionary!.CopyTo(array, arrayIndex);
|
||||
}
|
||||
|
||||
public bool Remove(KeyValuePair<TKey, TValue> item)
|
||||
{
|
||||
return this._dictionary!.Remove(item);
|
||||
}
|
||||
|
||||
public int Count => _dictionary!.Count;
|
||||
public bool IsReadOnly => _dictionary!.IsReadOnly;
|
||||
public void Add(TKey key, TValue value)
|
||||
{
|
||||
this._dictionary!.Add(key, value);
|
||||
}
|
||||
|
||||
public bool ContainsKey(TKey key)
|
||||
{
|
||||
return this._dictionary!.ContainsKey(key);
|
||||
}
|
||||
|
||||
public bool Remove(TKey key)
|
||||
{
|
||||
return this._dictionary!.Remove(key);
|
||||
}
|
||||
|
||||
public bool TryGetValue(TKey key, out TValue value)
|
||||
{
|
||||
return this._dictionary!.TryGetValue(key, out value);
|
||||
}
|
||||
|
||||
public TValue this[TKey key]
|
||||
{
|
||||
get => this._dictionary![key];
|
||||
set => this._dictionary![key] = value;
|
||||
}
|
||||
|
||||
public ICollection<TKey> Keys => _dictionary!.Keys;
|
||||
public ICollection<TValue> Values => _dictionary!.Values;
|
||||
}
|
||||
Reference in New Issue
Block a user