Removed plugins from the project and reworked the Plugin Manager

This commit is contained in:
Wizzy69
2023-04-29 19:35:19 +03:00
parent 0dc8cdbce5
commit bcef58a46b
17 changed files with 83 additions and 861 deletions

View File

@@ -61,9 +61,7 @@ internal class CommandHandler
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
ex.WriteErrFile();
Config.Logger.Log(ex.Message, "CommandHandler", TextType.ERROR);
}
return Task.CompletedTask;

View File

@@ -18,7 +18,7 @@ public static class Config
public static Json<string, string> Data;
public static Json<string, string> Plugins;
public static async Task Initialize(bool isConsole)
public static async Task Initialize()
{
if (IsLoaded)
return;
@@ -26,21 +26,21 @@ public static class Config
Directory.CreateDirectory("./Data/Resources");
Directory.CreateDirectory("./Data/Plugins");
Directory.CreateDirectory("./Data/PAKS");
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");
Logger = new DBLogger();
Config.Data["LogFolder"] = "./Data/Logs/Logs";
Config.Data["ErrorFolder"] = "./Data/Logs/Errors";
PluginManager.Logger.Initialize(isConsole);
Logger = new DBLogger();
ArchiveManager.Initialize();
IsLoaded = true;
if (isConsole)
PluginManager.Logger.LogEvent += (message) => { Console.Write(message); };
Logger.Log("Config initialized", TextType.NORMAL);
}

View File

@@ -60,9 +60,7 @@ namespace PluginManager.Loaders
}
catch (Exception ex)
{
Logger.WriteLine(ex.Message);
Logger.WriteLine("PluginName: " + new FileInfo(file).Name.Split('.')[0] + " not loaded");
Config.Logger.Log("PluginName: " + new FileInfo(file).Name.Split('.')[0] + " not loaded", this, Others.TextType.ERROR);
continue;
}
if (FileLoaded != null)
@@ -133,7 +131,7 @@ namespace PluginManager.Loaders
}
catch (Exception ex)
{
Logger.WriteErrFile(ex.ToString());
Config.Logger.Log(ex.Message, this, Others.TextType.ERROR);
return null;
}

View File

@@ -1,182 +0,0 @@
using System;
using System.IO;
using System.Numerics;
using PluginManager.Others;
using Discord;
namespace PluginManager
{
[Obsolete("Use Logger from PluginManager.Others.Logger namespace instead\nThis class will be removed soon")]
public static class Logger
{
public static bool isConsole { get; private set; }
private static bool isInitialized;
private static string? logFolder;
private static string? errFolder;
public static void Initialize(bool console)
{
if (isInitialized)
throw new Exception("Logger is already initialized");
if (!Config.Data.ContainsKey("LogFolder"))
Config.Data.Add("LogFolder", "./Data/Output/Logs/");
if (!Config.Data.ContainsKey("ErrorFolder"))
Config.Data.Add("ErrorFolder", "./Data/Output/Errors/");
isInitialized = true;
logFolder = Config.Data["LogFolder"];
errFolder = Config.Data["ErrorFolder"];
isConsole = console;
}
public delegate void LogEventHandler(string Message);
public static event LogEventHandler LogEvent;
public static void Log(string Message)
{
if (!isInitialized) throw new Exception("Logger is not initialized");
LogEvent?.Invoke(Message);
}
public static void Log(string Message, params object[] Args)
{
if (!isInitialized) throw new Exception("Logger is not initialized");
LogEvent?.Invoke(string.Format(Message, Args));
}
public static void Log(IMessage message, bool newLine)
{
if (!isInitialized) throw new Exception("Logger is not initialized");
LogEvent?.Invoke(message.Content);
if (newLine)
LogEvent?.Invoke("\n");
}
public static void WriteLine(string? message)
{
if (!isInitialized) throw new Exception("Logger is not initialized");
if (message is not null)
LogEvent?.Invoke(message + '\n');
}
public static void LogError(System.Exception ex)
{
if (!isInitialized) throw new Exception("Logger is not initialized");
string message = "[ERROR]" + ex.Message;
LogEvent?.Invoke(message + '\n');
}
public static void LogError(string? message)
{
if (!isInitialized) throw new Exception("Logger is not initialized");
if (message is not null)
LogEvent?.Invoke("[ERROR]" + message + '\n');
}
public static void WriteLine()
{
if (!isInitialized) throw new Exception("Logger is not initialized");
LogEvent?.Invoke("\n");
}
public static void Write(string message)
{
if (!isInitialized) throw new Exception("Logger is not initialized");
LogEvent?.Invoke(message);
}
public static void Write<T>(T c)
{
if (!isInitialized) throw new Exception("Logger is not initialized");
LogEvent?.Invoke($"{c}");
}
public static void Write<T>(T c, params object[] Args)
{
if (!isInitialized) throw new Exception("Logger is not initialized");
LogEvent?.Invoke(string.Format($"{c}", Args));
}
public static void WriteColored(string message, ConsoleColor color)
{
if (!isInitialized) throw new Exception("Logger is not initialized");
if (!isConsole)
{
LogEvent?.Invoke(message);
return;
}
var oldColor = Console.ForegroundColor;
Console.ForegroundColor = color;
LogEvent?.Invoke(message);
Console.ForegroundColor = oldColor;
}
public static void SetConsoleColor(TextType type)
{
if(!isConsole) return;
switch (type)
{
case TextType.NORMAL:
Console.ForegroundColor = ConsoleColor.White;
break;
case TextType.ERROR:
Console.ForegroundColor = ConsoleColor.Red;
break;
case TextType.WARNING:
Console.ForegroundColor = ConsoleColor.Yellow;
break;
case TextType.SUCCESS:
Console.ForegroundColor = ConsoleColor.Green;
break;
default:
Console.ForegroundColor = ConsoleColor.White;
break;
}
}
public static void ResetConsoleColor()
{
if (!isConsole) return;
Console.ForegroundColor = ConsoleColor.White;
}
/// <summary>
/// Write logs to file
/// </summary>
/// <param name="LogMessage">The message to be wrote</param>
public static void WriteLogFile(string LogMessage)
{
if (!isInitialized) throw new Exception("Logger is not initialized");
var logsPath = logFolder + $"{DateTime.Today.ToShortDateString().Replace("/", "-").Replace("\\", "-")} Log.txt";
Directory.CreateDirectory(logFolder);
File.AppendAllText(logsPath, $"[{DateTime.Today.ToShortTimeString()}] {LogMessage} \n");
}
/// <summary>
/// Write error to file
/// </summary>
/// <param name="ErrMessage">The message to be wrote</param>
public static void WriteErrFile(string ErrMessage)
{
if (!isInitialized) throw new Exception("Logger is not initialized");
var errPath = errFolder +
$"{DateTime.Today.ToShortDateString().Replace("/", "-").Replace("\\", "-")} Error.txt";
Directory.CreateDirectory(errFolder);
File.AppendAllText(errPath, $"[{DateTime.Today.ToShortTimeString()}] {ErrMessage} \n");
}
public static void WriteErrFile(this Exception ex)
{
if (!isInitialized) throw new Exception("Logger is not initialized");
WriteErrFile(ex.ToString());
}
}
}

View File

@@ -1,15 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PluginManager.WindowManagement.Controls
{
public class ConsoleOption
{
public string Text { get; set; }
public byte Index { get; set; }
public Action Action { get; set; }
}
}

View File

@@ -1,47 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using PluginManager.Others;
namespace PluginManager.WindowManagement.Controls
{
public class Label
{
public string Text { get; set; }
public TextType Type { get; set; } = TextType.NORMAL;
public void Show()
{
Logger.SetConsoleColor(Type);
Console.WriteLine(Text);
Logger.ResetConsoleColor();
}
public void Show(int x, int y)
{
Logger.SetConsoleColor(Type);
Console.SetCursorPosition(x, y);
Console.WriteLine(Text);
Logger.ResetConsoleColor();
}
public void Show(int x, int y, ConsoleColor color)
{
Console.SetCursorPosition(x, y);
Console.ForegroundColor = color;
Console.WriteLine(Text);
Console.ResetColor();
}
public void Show(int x, int y, ConsoleColor color, ConsoleColor background)
{
Console.SetCursorPosition(x, y);
Console.ForegroundColor = color;
Console.BackgroundColor = background;
Console.WriteLine(Text);
Console.ResetColor();
}
}
}

View File

@@ -1,26 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PluginManager.WindowManagement.Controls
{
public class TextBox
{
public Label Label { get; set; }
public string Text { get; private set; }
public Func<string, bool> IsValid { get; set; }
public bool SetText(string text)
{
if(IsValid(text))
{
Text = text;
return true;
}
return false;
}
}
}

View File

@@ -1,61 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using PluginManager.WindowManagement.Controls;
using PluginManager.Others;
namespace PluginManager.WindowManagement
{
public class InputBox
{
public string Title { get; set; }
public string Message { get; set; }
private List<TextBox> options = new List<TextBox>();
private List<Label> labels = new List<Label>();
private string InputStr = "=> ";
public List<string> Show()
{
List<string> result = new List<string>();
Console.Clear();
Console.WriteLine(Title);
Console.WriteLine(Message);
foreach (var label in labels)
label.Show();
foreach (var option in options)
{
option.Label.Show();
while (true)
{
Console.Write(InputStr);
if(option.SetText(Console.ReadLine()))
{
result.Add(option.Text);
break;
}
}
}
return result;
}
public void AddOption(string text, Func<string, bool> isValid)
{
options.Add(new TextBox() { Label = new Label() {Text = text}, IsValid = isValid });
}
public void AddLabel(string text, TextType type)
{
labels.Add(new Label() {Text = text, Type = type});
}
}
}

View File

@@ -1,70 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using PluginManager.WindowManagement.Controls;
namespace PluginManager.WindowManagement
{
public class MessageBox
{
public string Title { get; set; }
public string Message { get; set; }
private List<ConsoleOption> options = new List<ConsoleOption>();
public int OptionsCount {get => options.Count;}
public MessageBox(string title, string message)
{
Title = title;
Message = message;
}
public void AddOption(string text, Action action)
{
options.Add(new ConsoleOption() { Text = text, Index = (byte)(options.Count+1), Action = action});
}
public void AddRangeOptions(List<string> texts, List<Action> actions)
{
for (int i = 0; i < texts.Count; i++)
{
options.Add(new ConsoleOption() { Text = texts[i], Index = (byte)(options.Count + 1), Action = actions[i] });
}
}
public int Show()
{
Console.Clear();
Console.WriteLine(Title);
Console.WriteLine(Message);
foreach (var option in options)
{
Console.WriteLine($"{option.Index}. {option.Text}");
}
if(options.Count == 0)
{
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
return 0;
}
if(int.TryParse(Console.ReadLine(), out int result))
{
if(result > 0 && result <= options.Count)
{
if(options[result - 1].Action != null)
options[result - 1].Action();
return result;
}
}
return -1;
}
}
}