patch
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
|
||||||
|
using PluginManager;
|
||||||
using PluginManager.Interfaces;
|
using PluginManager.Interfaces;
|
||||||
using PluginManager.Others;
|
using PluginManager.Others;
|
||||||
|
|
||||||
using DiscordLibCommands = Discord.Commands;
|
using DiscordLibCommands = Discord.Commands;
|
||||||
using DiscordLib = Discord;
|
|
||||||
using OperatingSystem = PluginManager.Others.OperatingSystem;
|
using OperatingSystem = PluginManager.Others.OperatingSystem;
|
||||||
|
|
||||||
namespace DiscordBot.Discord.Commands;
|
namespace DiscordBot.Discord.Commands;
|
||||||
@@ -79,7 +81,7 @@ internal class Restart : DBCommand
|
|||||||
switch (OS)
|
switch (OS)
|
||||||
{
|
{
|
||||||
case OperatingSystem.WINDOWS:
|
case OperatingSystem.WINDOWS:
|
||||||
Functions.WriteLogFile("Restarting the bot with the following arguments: \"" + cmd + "\"");
|
Logger.WriteLogFile("Restarting the bot with the following arguments: \"" + cmd + "\"");
|
||||||
Process.Start("./DiscordBot.exe", cmd);
|
Process.Start("./DiscordBot.exe", cmd);
|
||||||
break;
|
break;
|
||||||
case OperatingSystem.LINUX:
|
case OperatingSystem.LINUX:
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ using Discord;
|
|||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
|
|
||||||
using static PluginManager.Others.Functions;
|
using PluginManager;
|
||||||
|
|
||||||
namespace DiscordBot.Discord.Core;
|
namespace DiscordBot.Discord.Core;
|
||||||
|
|
||||||
@@ -98,7 +98,7 @@ internal class Boot
|
|||||||
|
|
||||||
private async Task Client_LoggedOut()
|
private async Task Client_LoggedOut()
|
||||||
{
|
{
|
||||||
WriteLogFile("Successfully Logged Out");
|
Logger.WriteLine("Successfully Logged Out");
|
||||||
await Log(new LogMessage(LogSeverity.Info, "Boot", "Successfully logged out from discord !"));
|
await Log(new LogMessage(LogSeverity.Info, "Boot", "Successfully logged out from discord !"));
|
||||||
|
|
||||||
/* var cmds = await client.GetGlobalApplicationCommandsAsync();
|
/* var cmds = await client.GetGlobalApplicationCommandsAsync();
|
||||||
@@ -117,7 +117,7 @@ internal class Boot
|
|||||||
private Task LoggedIn()
|
private Task LoggedIn()
|
||||||
{
|
{
|
||||||
Console.Title = "CONNECTED";
|
Console.Title = "CONNECTED";
|
||||||
WriteLogFile("The bot has been logged in at " + DateTime.Now.ToShortDateString() + " (" +
|
Logger.WriteLine("The bot has been logged in at " + DateTime.Now.ToShortDateString() + " (" +
|
||||||
DateTime.Now.ToShortTimeString() + ")"
|
DateTime.Now.ToShortTimeString() + ")"
|
||||||
);
|
);
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
@@ -129,7 +129,7 @@ internal class Boot
|
|||||||
{
|
{
|
||||||
case LogSeverity.Error:
|
case LogSeverity.Error:
|
||||||
case LogSeverity.Critical:
|
case LogSeverity.Critical:
|
||||||
WriteErrFile(message.Message);
|
Logger.WriteErrFile(message.Message);
|
||||||
|
|
||||||
Console.ForegroundColor = ConsoleColor.Red;
|
Console.ForegroundColor = ConsoleColor.Red;
|
||||||
Console.WriteLine("[ERROR] " + message.Message);
|
Console.WriteLine("[ERROR] " + message.Message);
|
||||||
@@ -139,7 +139,7 @@ internal class Boot
|
|||||||
|
|
||||||
case LogSeverity.Info:
|
case LogSeverity.Info:
|
||||||
case LogSeverity.Debug:
|
case LogSeverity.Debug:
|
||||||
WriteLogFile(message.Message);
|
Logger.WriteLogFile(message.Message);
|
||||||
|
|
||||||
Console.ForegroundColor = ConsoleColor.Cyan;
|
Console.ForegroundColor = ConsoleColor.Cyan;
|
||||||
Console.WriteLine("[INFO] " + message.Message);
|
Console.WriteLine("[INFO] " + message.Message);
|
||||||
|
|||||||
@@ -7,9 +7,10 @@ using Discord.Commands;
|
|||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
|
|
||||||
using PluginManager.Loaders;
|
using PluginManager.Loaders;
|
||||||
using PluginManager.Others;
|
|
||||||
using PluginManager.Others.Permissions;
|
using PluginManager.Others.Permissions;
|
||||||
|
|
||||||
|
using static PluginManager.Logger;
|
||||||
|
|
||||||
namespace DiscordBot.Discord.Core;
|
namespace DiscordBot.Discord.Core;
|
||||||
|
|
||||||
internal class CommandHandler
|
internal class CommandHandler
|
||||||
|
|||||||
@@ -309,7 +309,7 @@ public class Program
|
|||||||
Config.Variables.Add("LaunchMessage",
|
Config.Variables.Add("LaunchMessage",
|
||||||
"An error occured while closing the bot last time. Please consider closing the bot using the &rsd&c method !\nThere is a risk of losing all data or corruption of the save file, which in some cases requires to reinstall the bot !",
|
"An error occured while closing the bot last time. Please consider closing the bot using the &rsd&c method !\nThere is a risk of losing all data or corruption of the save file, which in some cases requires to reinstall the bot !",
|
||||||
false);
|
false);
|
||||||
Functions.WriteErrFile(ex.ToString());
|
Logger.WriteErrFile(ex.ToString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -318,6 +318,14 @@ public class Program
|
|||||||
|
|
||||||
private static async Task PreLoadComponents()
|
private static async Task PreLoadComponents()
|
||||||
{
|
{
|
||||||
|
Settings.sqlDatabase = new SqlDatabase(Functions.dataFolder + "SetDB.dat");
|
||||||
|
|
||||||
|
await Settings.sqlDatabase.Open();
|
||||||
|
await Config.Initialize();
|
||||||
|
Logger.Initialize(true);
|
||||||
|
ArchiveManager.Initialize();
|
||||||
|
|
||||||
|
|
||||||
Logger.LogEvent += (message) =>
|
Logger.LogEvent += (message) =>
|
||||||
{
|
{
|
||||||
Console.Write(message);
|
Console.Write(message);
|
||||||
@@ -330,13 +338,6 @@ public class Program
|
|||||||
Directory.CreateDirectory("./Data/Plugins");
|
Directory.CreateDirectory("./Data/Plugins");
|
||||||
Directory.CreateDirectory("./Data/PAKS");
|
Directory.CreateDirectory("./Data/PAKS");
|
||||||
|
|
||||||
Settings.sqlDatabase = new SqlDatabase(Functions.dataFolder + "SetDB.dat");
|
|
||||||
|
|
||||||
await Settings.sqlDatabase.Open();
|
|
||||||
await Config.Initialize();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (await Config.Variables.ExistsAsync("DeleteLogsAtStartup"))
|
if (await Config.Variables.ExistsAsync("DeleteLogsAtStartup"))
|
||||||
if (await Config.Variables.GetValueAsync("DeleteLogsAtStartup") == "true")
|
if (await Config.Variables.GetValueAsync("DeleteLogsAtStartup") == "true")
|
||||||
foreach (var file in Directory.GetFiles("./Output/Logs/"))
|
foreach (var file in Directory.GetFiles("./Output/Logs/"))
|
||||||
@@ -364,7 +365,7 @@ public class Program
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Functions.WriteErrFile(ex.Message);
|
Logger.WriteErrFile(ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -451,7 +452,7 @@ public class Program
|
|||||||
await ServerCom.DownloadFileNoProgressAsync(
|
await ServerCom.DownloadFileNoProgressAsync(
|
||||||
"https://github.com/Wizzy69/installer/releases/download/release-1-discordbot/Updater.zip",
|
"https://github.com/Wizzy69/installer/releases/download/release-1-discordbot/Updater.zip",
|
||||||
"./Updater.zip");
|
"./Updater.zip");
|
||||||
await Functions.ExtractArchive("./Updater.zip", "./", null,
|
await ArchiveManager.ExtractArchive("./Updater.zip", "./", null,
|
||||||
UnzipProgressType.PercentageFromTotalSize);
|
UnzipProgressType.PercentageFromTotalSize);
|
||||||
await Config.Variables.SetValueAsync("UpdaterVersion", updaternewversion);
|
await Config.Variables.SetValueAsync("UpdaterVersion", updaternewversion);
|
||||||
File.Delete("Updater.zip");
|
File.Delete("Updater.zip");
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ public class ConsoleCommandsHandler
|
|||||||
|
|
||||||
public ConsoleCommandsHandler(DiscordSocketClient client)
|
public ConsoleCommandsHandler(DiscordSocketClient client)
|
||||||
{
|
{
|
||||||
|
if (!Logger.isConsole) throw new Exception("Can not use ConsoleCommandsHandler for Non console apps");
|
||||||
this.client = client;
|
this.client = client;
|
||||||
InitializeBasicCommands();
|
InitializeBasicCommands();
|
||||||
|
|
||||||
@@ -242,7 +243,7 @@ public class ConsoleCommandsHandler
|
|||||||
var bar = new Utilities.ProgressBar(
|
var bar = new Utilities.ProgressBar(
|
||||||
ProgressBarType.NO_END);
|
ProgressBarType.NO_END);
|
||||||
bar.Start();
|
bar.Start();
|
||||||
await Functions.ExtractArchive("./" + split[1], "./", null,
|
await ArchiveManager.ExtractArchive("./" + split[1], "./", null,
|
||||||
UnzipProgressType.PercentageFromTotalSize);
|
UnzipProgressType.PercentageFromTotalSize);
|
||||||
bar.Stop("Extracted");
|
bar.Stop("Extracted");
|
||||||
Logger.WriteLine("\n");
|
Logger.WriteLine("\n");
|
||||||
@@ -471,6 +472,8 @@ public class ConsoleCommandsHandler
|
|||||||
|
|
||||||
public static async Task ExecuteCommad(string command)
|
public static async Task ExecuteCommad(string command)
|
||||||
{
|
{
|
||||||
|
if (!Logger.isConsole)
|
||||||
|
throw new Exception("Can not use console based commands on non console based application !");
|
||||||
var args = command.Split(' ');
|
var args = command.Split(' ');
|
||||||
foreach (var item in commandList.ToList())
|
foreach (var item in commandList.ToList())
|
||||||
if (item.CommandName == args[0])
|
if (item.CommandName == args[0])
|
||||||
@@ -482,11 +485,13 @@ public class ConsoleCommandsHandler
|
|||||||
|
|
||||||
public bool HandleCommand(string command, bool removeCommandExecution = true)
|
public bool HandleCommand(string command, bool removeCommandExecution = true)
|
||||||
{
|
{
|
||||||
|
if (Logger.isConsole)
|
||||||
Console.ForegroundColor = ConsoleColor.White;
|
Console.ForegroundColor = ConsoleColor.White;
|
||||||
var args = command.Split(' ');
|
var args = command.Split(' ');
|
||||||
foreach (var item in commandList.ToList())
|
foreach (var item in commandList.ToList())
|
||||||
if (item.CommandName == args[0])
|
if (item.CommandName == args[0])
|
||||||
{
|
{
|
||||||
|
if (Logger.isConsole)
|
||||||
if (removeCommandExecution)
|
if (removeCommandExecution)
|
||||||
{
|
{
|
||||||
Console.SetCursorPosition(0, Console.CursorTop - 1);
|
Console.SetCursorPosition(0, Console.CursorTop - 1);
|
||||||
|
|||||||
@@ -4,8 +4,6 @@ using System.IO;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
||||||
using PluginManager.Others;
|
|
||||||
|
|
||||||
namespace PluginManager.Loaders;
|
namespace PluginManager.Loaders;
|
||||||
|
|
||||||
internal class LoaderArgs : EventArgs
|
internal class LoaderArgs : EventArgs
|
||||||
@@ -102,7 +100,7 @@ internal class Loader<T>
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Functions.WriteErrFile(ex.ToString());
|
Logger.WriteErrFile(ex.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ using System.Linq;
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
||||||
using PluginManager.Interfaces;
|
using PluginManager.Interfaces;
|
||||||
using PluginManager.Others;
|
|
||||||
|
|
||||||
namespace PluginManager.Loaders
|
namespace PluginManager.Loaders
|
||||||
{
|
{
|
||||||
@@ -46,8 +45,18 @@ namespace PluginManager.Loaders
|
|||||||
|
|
||||||
var files = Directory.GetFiles(path, $"*.{extension}", SearchOption.AllDirectories);
|
var files = Directory.GetFiles(path, $"*.{extension}", SearchOption.AllDirectories);
|
||||||
foreach (var file in files)
|
foreach (var file in files)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
Assembly.LoadFrom(file);
|
Assembly.LoadFrom(file);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Logger.WriteLine(ex.Message);
|
||||||
|
Logger.WriteLine("PluginName: " + new FileInfo(file).Name.Split('.')[0] + " not loaded");
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (FileLoaded != null)
|
if (FileLoaded != null)
|
||||||
{
|
{
|
||||||
var args = new LoaderArgs
|
var args = new LoaderArgs
|
||||||
@@ -116,7 +125,7 @@ namespace PluginManager.Loaders
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Functions.WriteErrFile(ex.ToString());
|
Logger.WriteErrFile(ex.ToString());
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ using Discord.WebSocket;
|
|||||||
using PluginManager.Interfaces;
|
using PluginManager.Interfaces;
|
||||||
using PluginManager.Online;
|
using PluginManager.Online;
|
||||||
using PluginManager.Online.Updates;
|
using PluginManager.Online.Updates;
|
||||||
using PluginManager.Others;
|
|
||||||
|
|
||||||
namespace PluginManager.Loaders;
|
namespace PluginManager.Loaders;
|
||||||
|
|
||||||
@@ -95,11 +94,11 @@ public class PluginLoader
|
|||||||
Events = new List<DBEvent>();
|
Events = new List<DBEvent>();
|
||||||
SlashCommands = new List<DBSlashCommand>();
|
SlashCommands = new List<DBSlashCommand>();
|
||||||
|
|
||||||
Functions.WriteLogFile("Starting plugin loader ... Client: " + _client.CurrentUser.Username);
|
Logger.WriteLogFile("Starting plugin loader ... Client: " + _client.CurrentUser.Username);
|
||||||
Logger.WriteLine("Loading plugins");
|
Logger.WriteLine("Loading plugins");
|
||||||
|
|
||||||
var loader = new LoaderV2("./Data/Plugins", "dll");
|
var loader = new LoaderV2("./Data/Plugins", "dll");
|
||||||
loader.FileLoaded += (args) => Functions.WriteLogFile($"{args.PluginName} file Loaded");
|
loader.FileLoaded += (args) => Logger.WriteLogFile($"{args.PluginName} file Loaded");
|
||||||
loader.PluginLoaded += Loader_PluginLoaded;
|
loader.PluginLoaded += Loader_PluginLoaded;
|
||||||
var res = loader.Load();
|
var res = loader.Load();
|
||||||
Events = res.Item1;
|
Events = res.Item1;
|
||||||
|
|||||||
@@ -1,11 +1,34 @@
|
|||||||
using Discord;
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
using Discord;
|
||||||
|
|
||||||
namespace PluginManager
|
namespace PluginManager
|
||||||
{
|
{
|
||||||
public static class Logger
|
public static class Logger
|
||||||
{
|
{
|
||||||
|
|
||||||
public static bool isConsole = true;
|
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.Variables.Exists("LogFolder"))
|
||||||
|
Config.Variables.Add("LogFolder", "./Data/Output/Logs/");
|
||||||
|
|
||||||
|
if (!Config.Variables.Exists("ErrorFolder"))
|
||||||
|
Config.Variables.Add("ErrorFolder", "./Data/Output/Errors/");
|
||||||
|
|
||||||
|
isInitialized = true;
|
||||||
|
logFolder = Config.Variables.GetValue("LogFolder");
|
||||||
|
errFolder = Config.Variables.GetValue("ErrorFolder");
|
||||||
|
isConsole = console;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public delegate void LogEventHandler(string Message);
|
public delegate void LogEventHandler(string Message);
|
||||||
@@ -13,16 +36,19 @@ namespace PluginManager
|
|||||||
|
|
||||||
public static void Log(string Message)
|
public static void Log(string Message)
|
||||||
{
|
{
|
||||||
|
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||||
LogEvent?.Invoke(Message);
|
LogEvent?.Invoke(Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Log(string Message, params object[] Args)
|
public static void Log(string Message, params object[] Args)
|
||||||
{
|
{
|
||||||
|
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||||
LogEvent?.Invoke(string.Format(Message, Args));
|
LogEvent?.Invoke(string.Format(Message, Args));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Log(IMessage message, bool newLine)
|
public static void Log(IMessage message, bool newLine)
|
||||||
{
|
{
|
||||||
|
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||||
LogEvent?.Invoke(message.Content);
|
LogEvent?.Invoke(message.Content);
|
||||||
if (newLine)
|
if (newLine)
|
||||||
LogEvent?.Invoke("\n");
|
LogEvent?.Invoke("\n");
|
||||||
@@ -30,18 +56,21 @@ namespace PluginManager
|
|||||||
|
|
||||||
public static void WriteLine(string? message)
|
public static void WriteLine(string? message)
|
||||||
{
|
{
|
||||||
|
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||||
if (message is not null)
|
if (message is not null)
|
||||||
LogEvent?.Invoke(message + '\n');
|
LogEvent?.Invoke(message + '\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void LogError(System.Exception ex)
|
public static void LogError(System.Exception ex)
|
||||||
{
|
{
|
||||||
|
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||||
string message = "[ERROR]" + ex.Message;
|
string message = "[ERROR]" + ex.Message;
|
||||||
LogEvent?.Invoke(message + '\n');
|
LogEvent?.Invoke(message + '\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void LogError(string? message)
|
public static void LogError(string? message)
|
||||||
{
|
{
|
||||||
|
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||||
if (message is not null)
|
if (message is not null)
|
||||||
LogEvent?.Invoke("[ERROR]" + message + '\n');
|
LogEvent?.Invoke("[ERROR]" + message + '\n');
|
||||||
}
|
}
|
||||||
@@ -49,17 +78,51 @@ namespace PluginManager
|
|||||||
|
|
||||||
public static void WriteLine()
|
public static void WriteLine()
|
||||||
{
|
{
|
||||||
|
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||||
LogEvent?.Invoke("\n");
|
LogEvent?.Invoke("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Write(string message)
|
public static void Write(string message)
|
||||||
{
|
{
|
||||||
|
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||||
LogEvent?.Invoke(message);
|
LogEvent?.Invoke(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Write(char c)
|
public static void Write(char c)
|
||||||
{
|
{
|
||||||
|
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||||
LogEvent?.Invoke($"{c}");
|
LogEvent?.Invoke($"{c}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <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.AppendAllTextAsync(logsPath, LogMessage + " \n").Wait();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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, ErrMessage + " \n");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void WriteErrFile(this Exception ex)
|
||||||
|
{
|
||||||
|
if (!isInitialized) throw new Exception("Logger is not initialized");
|
||||||
|
WriteErrFile(ex.ToString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ public class PluginsManager
|
|||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
Logger.WriteLine("Failed to execute command: listplugs\nReason: " + exception.Message);
|
Logger.WriteLine("Failed to execute command: listplugs\nReason: " + exception.Message);
|
||||||
Functions.WriteErrFile(exception.ToString());
|
Logger.WriteErrFile(exception.ToString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,7 +117,7 @@ public class PluginsManager
|
|||||||
catch (Exception exception)
|
catch (Exception exception)
|
||||||
{
|
{
|
||||||
Logger.WriteLine("Failed to execute command: listplugs\nReason: " + exception.Message);
|
Logger.WriteLine("Failed to execute command: listplugs\nReason: " + exception.Message);
|
||||||
Functions.WriteErrFile(exception.ToString());
|
Logger.WriteErrFile(exception.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
return new string[] { null!, null!, null! };
|
return new string[] { null!, null!, null! };
|
||||||
|
|||||||
139
PluginManager/Others/ArchiveManager.cs
Normal file
139
PluginManager/Others/ArchiveManager.cs
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.IO.Compression;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace PluginManager.Others
|
||||||
|
{
|
||||||
|
public static class ArchiveManager
|
||||||
|
{
|
||||||
|
public static bool isInitialized { get; private set; }
|
||||||
|
private static string archiveFolder;
|
||||||
|
|
||||||
|
public static void Initialize()
|
||||||
|
{
|
||||||
|
if (isInitialized) throw new Exception("ArchiveManager is already initialized");
|
||||||
|
|
||||||
|
if (!Config.Variables.Exists("ArchiveFolder"))
|
||||||
|
Config.Variables.Add("ArchiveFolder", "./Data/PAKS/");
|
||||||
|
|
||||||
|
isInitialized = true;
|
||||||
|
archiveFolder = Config.Variables.GetValue("ArchiveFolder");
|
||||||
|
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Read data from a file that is inside an archive (ZIP format)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="FileName">The file name that is inside the archive or its full path</param>
|
||||||
|
/// <param name="archFile">The archive location from the PAKs folder</param>
|
||||||
|
/// <returns>A string that represents the content of the file or null if the file does not exists or it has no content</returns>
|
||||||
|
public static async Task<string> ReadFromPakAsync(string FileName, string archFile)
|
||||||
|
{
|
||||||
|
if (!isInitialized) throw new Exception("ArchiveManager is not initialized");
|
||||||
|
archFile = archiveFolder + archFile;
|
||||||
|
if (!File.Exists(archFile))
|
||||||
|
throw new Exception("Failed to load file !");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
string textValue = null;
|
||||||
|
using (var fs = new FileStream(archFile, FileMode.Open))
|
||||||
|
using (var zip = new ZipArchive(fs, ZipArchiveMode.Read))
|
||||||
|
{
|
||||||
|
foreach (var entry in zip.Entries)
|
||||||
|
if (entry.Name == FileName || entry.FullName == FileName)
|
||||||
|
using (var s = entry.Open())
|
||||||
|
using (var reader = new StreamReader(s))
|
||||||
|
{
|
||||||
|
textValue = await reader.ReadToEndAsync();
|
||||||
|
reader.Close();
|
||||||
|
s.Close();
|
||||||
|
fs.Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return textValue;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
await Task.Delay(100);
|
||||||
|
return await ReadFromPakAsync(FileName, archFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Extract zip to location
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="zip">The zip location</param>
|
||||||
|
/// <param name="folder">The target location</param>
|
||||||
|
/// <param name="progress">The progress that is updated as a file is processed</param>
|
||||||
|
/// <param name="type">The type of progress</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static async Task ExtractArchive(string zip, string folder, IProgress<float> progress,
|
||||||
|
UnzipProgressType type)
|
||||||
|
{
|
||||||
|
if (!isInitialized) throw new Exception("ArchiveManager is not initialized");
|
||||||
|
Directory.CreateDirectory(folder);
|
||||||
|
using (var archive = ZipFile.OpenRead(zip))
|
||||||
|
{
|
||||||
|
if (type == UnzipProgressType.PercentageFromNumberOfFiles)
|
||||||
|
{
|
||||||
|
var totalZIPFiles = archive.Entries.Count();
|
||||||
|
var currentZIPFile = 0;
|
||||||
|
foreach (var entry in archive.Entries)
|
||||||
|
{
|
||||||
|
if (entry.FullName.EndsWith("/")) // it is a folder
|
||||||
|
Directory.CreateDirectory(Path.Combine(folder, entry.FullName));
|
||||||
|
|
||||||
|
else
|
||||||
|
try
|
||||||
|
{
|
||||||
|
entry.ExtractToFile(Path.Combine(folder, entry.FullName), true);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Logger.WriteLine($"Failed to extract {entry.Name}. Exception: {ex.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
currentZIPFile++;
|
||||||
|
await Task.Delay(10);
|
||||||
|
if (progress != null)
|
||||||
|
progress.Report((float)currentZIPFile / totalZIPFiles * 100);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (type == UnzipProgressType.PercentageFromTotalSize)
|
||||||
|
{
|
||||||
|
ulong zipSize = 0;
|
||||||
|
|
||||||
|
foreach (var entry in archive.Entries)
|
||||||
|
zipSize += (ulong)entry.CompressedLength;
|
||||||
|
|
||||||
|
ulong currentSize = 0;
|
||||||
|
foreach (var entry in archive.Entries)
|
||||||
|
{
|
||||||
|
if (entry.FullName.EndsWith("/"))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(Path.Combine(folder, entry.FullName));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
entry.ExtractToFile(Path.Combine(folder, entry.FullName), true);
|
||||||
|
currentSize += (ulong)entry.CompressedLength;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Logger.WriteLine($"Failed to extract {entry.Name}. Exception: {ex.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
await Task.Delay(10);
|
||||||
|
if (progress != null)
|
||||||
|
progress.Report((float)currentSize / zipSize * 100);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.IO.Compression;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
using System.Security.Cryptography;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
@@ -26,90 +23,6 @@ public static class Functions
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static readonly string dataFolder = @"./Data/Resources/";
|
public static readonly string dataFolder = @"./Data/Resources/";
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The location for all logs
|
|
||||||
/// </summary>
|
|
||||||
public static readonly string logFolder = @"./Data/Output/Logs/";
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The location for all errors
|
|
||||||
/// </summary>
|
|
||||||
public static readonly string errFolder = @"./Data/Output/Errors/";
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Archives folder
|
|
||||||
/// </summary>
|
|
||||||
public static readonly string pakFolder = @"./Data/PAKS/";
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Read data from a file that is inside an archive (ZIP format)
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="FileName">The file name that is inside the archive or its full path</param>
|
|
||||||
/// <param name="archFile">The archive location from the PAKs folder</param>
|
|
||||||
/// <returns>A string that represents the content of the file or null if the file does not exists or it has no content</returns>
|
|
||||||
public static async Task<string> ReadFromPakAsync(string FileName, string archFile)
|
|
||||||
{
|
|
||||||
archFile = pakFolder + archFile;
|
|
||||||
if (!File.Exists(archFile))
|
|
||||||
throw new Exception("Failed to load file !");
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
string textValue = null;
|
|
||||||
using (var fs = new FileStream(archFile, FileMode.Open))
|
|
||||||
using (var zip = new ZipArchive(fs, ZipArchiveMode.Read))
|
|
||||||
{
|
|
||||||
foreach (var entry in zip.Entries)
|
|
||||||
if (entry.Name == FileName || entry.FullName == FileName)
|
|
||||||
using (var s = entry.Open())
|
|
||||||
using (var reader = new StreamReader(s))
|
|
||||||
{
|
|
||||||
textValue = await reader.ReadToEndAsync();
|
|
||||||
reader.Close();
|
|
||||||
s.Close();
|
|
||||||
fs.Close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return textValue;
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
await Task.Delay(100);
|
|
||||||
return await ReadFromPakAsync(FileName, archFile);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Write logs to file
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="LogMessage">The message to be wrote</param>
|
|
||||||
public static void WriteLogFile(string LogMessage)
|
|
||||||
{
|
|
||||||
var logsPath = logFolder + $"{DateTime.Today.ToShortDateString().Replace("/", "-").Replace("\\", "-")} Log.txt";
|
|
||||||
Directory.CreateDirectory(logFolder);
|
|
||||||
File.AppendAllText(logsPath, LogMessage + " \n");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Write error to file
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="ErrMessage">The message to be wrote</param>
|
|
||||||
public static void WriteErrFile(string ErrMessage)
|
|
||||||
{
|
|
||||||
var errPath = errFolder +
|
|
||||||
$"{DateTime.Today.ToShortDateString().Replace("/", "-").Replace("\\", "-")} Error.txt";
|
|
||||||
Directory.CreateDirectory(errFolder);
|
|
||||||
File.AppendAllText(errPath, ErrMessage + " \n");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void WriteErrFile(this Exception ex)
|
|
||||||
{
|
|
||||||
WriteErrFile(ex.ToString());
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get the Operating system you are runnin on
|
/// Get the Operating system you are runnin on
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -162,79 +75,6 @@ public static class Functions
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Extract zip to location
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="zip">The zip location</param>
|
|
||||||
/// <param name="folder">The target location</param>
|
|
||||||
/// <param name="progress">The progress that is updated as a file is processed</param>
|
|
||||||
/// <param name="type">The type of progress</param>
|
|
||||||
/// <returns></returns>
|
|
||||||
public static async Task ExtractArchive(string zip, string folder, IProgress<float> progress,
|
|
||||||
UnzipProgressType type)
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(folder);
|
|
||||||
using (var archive = ZipFile.OpenRead(zip))
|
|
||||||
{
|
|
||||||
if (type == UnzipProgressType.PercentageFromNumberOfFiles)
|
|
||||||
{
|
|
||||||
var totalZIPFiles = archive.Entries.Count();
|
|
||||||
var currentZIPFile = 0;
|
|
||||||
foreach (var entry in archive.Entries)
|
|
||||||
{
|
|
||||||
if (entry.FullName.EndsWith("/")) // it is a folder
|
|
||||||
Directory.CreateDirectory(Path.Combine(folder, entry.FullName));
|
|
||||||
|
|
||||||
else
|
|
||||||
try
|
|
||||||
{
|
|
||||||
entry.ExtractToFile(Path.Combine(folder, entry.FullName), true);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Logger.WriteLine($"Failed to extract {entry.Name}. Exception: {ex.Message}");
|
|
||||||
}
|
|
||||||
|
|
||||||
currentZIPFile++;
|
|
||||||
await Task.Delay(10);
|
|
||||||
if (progress != null)
|
|
||||||
progress.Report((float)currentZIPFile / totalZIPFiles * 100);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (type == UnzipProgressType.PercentageFromTotalSize)
|
|
||||||
{
|
|
||||||
ulong zipSize = 0;
|
|
||||||
|
|
||||||
foreach (var entry in archive.Entries)
|
|
||||||
zipSize += (ulong)entry.CompressedLength;
|
|
||||||
|
|
||||||
ulong currentSize = 0;
|
|
||||||
foreach (var entry in archive.Entries)
|
|
||||||
{
|
|
||||||
if (entry.FullName.EndsWith("/"))
|
|
||||||
{
|
|
||||||
Directory.CreateDirectory(Path.Combine(folder, entry.FullName));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
entry.ExtractToFile(Path.Combine(folder, entry.FullName), true);
|
|
||||||
currentSize += (ulong)entry.CompressedLength;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Logger.WriteLine($"Failed to extract {entry.Name}. Exception: {ex.Message}");
|
|
||||||
}
|
|
||||||
|
|
||||||
await Task.Delay(10);
|
|
||||||
if (progress != null)
|
|
||||||
progress.Report((float)currentSize / zipSize * 100);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Save to JSON file
|
/// Save to JSON file
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -267,29 +107,4 @@ public static class Functions
|
|||||||
text.Close();
|
text.Close();
|
||||||
return (obj ?? default)!;
|
return (obj ?? default)!;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool TryReadValueFromJson(string input, string codeName, out JsonElement element)
|
|
||||||
{
|
|
||||||
Stream text;
|
|
||||||
if (File.Exists(input))
|
|
||||||
text = File.OpenRead(input);
|
|
||||||
|
|
||||||
else
|
|
||||||
text = new MemoryStream(Encoding.ASCII.GetBytes(input));
|
|
||||||
|
|
||||||
var jsonObject = JsonDocument.Parse(text);
|
|
||||||
|
|
||||||
var data = jsonObject.RootElement.TryGetProperty(codeName, out element);
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string CreateMD5(string input)
|
|
||||||
{
|
|
||||||
using (var md5 = MD5.Create())
|
|
||||||
{
|
|
||||||
var inputBytes = Encoding.ASCII.GetBytes(input);
|
|
||||||
var hashBytes = md5.ComputeHash(inputBytes);
|
|
||||||
return Convert.ToHexString(hashBytes);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user