diff --git a/DiscordBot/Bot/Actions/Exit.cs b/DiscordBot/Bot/Actions/Exit.cs index 9fb9d5f..922b2b1 100644 --- a/DiscordBot/Bot/Actions/Exit.cs +++ b/DiscordBot/Bot/Actions/Exit.cs @@ -10,7 +10,7 @@ public class Exit : ICommandAction { public string ActionName => "exit"; public string Description => "Exits the bot and saves the config. Use exit help for more info."; - public string Usage => "exit [help|force]"; + public string Usage => "exit [help|force (-f)]"; public InternalActionRunType RunType => InternalActionRunType.ON_CALL; public async Task Execute(string[] args) @@ -29,9 +29,10 @@ public class Exit : ICommandAction case "help": Console.WriteLine("Usage : exit [help|force]"); Console.WriteLine("help : Displays this message"); - Console.WriteLine("force : Exits the bot without saving the config"); + Console.WriteLine("force | -f : Exits the bot without saving the config"); break; - + + case "-f": case "force": Config.Logger.Log("Exiting (FORCE)...", "Exit", LogLevel.WARNING, false); Environment.Exit(0); diff --git a/PluginManager/Others/ArchiveManager.cs b/PluginManager/Others/ArchiveManager.cs index c4a77cb..89fa858 100644 --- a/PluginManager/Others/ArchiveManager.cs +++ b/PluginManager/Others/ArchiveManager.cs @@ -2,6 +2,7 @@ using System.IO; using System.IO.Compression; using System.Linq; +using System.Runtime.Serialization.Formatters.Binary; using System.Threading.Tasks; namespace PluginManager.Others; @@ -9,7 +10,7 @@ namespace PluginManager.Others; public static class ArchiveManager { private static string? archiveFolder; - public static bool isInitialized { get; private set; } + public static bool isInitialized { get; private set; } public static void Initialize() { @@ -23,6 +24,41 @@ public static class ArchiveManager isInitialized = true; } + /// + /// Read a file from a zip archive. The output is a byte array + /// + /// The file name in the archive + /// The archive location on the disk + /// An array of bytes that represents the Stream value from the file that was read inside the archive + public async static Task ReadStreamFromPakAsync(string fileName, string archName) + { + if (!isInitialized) throw new Exception("ArchiveManager is not initialized"); + + archName = archiveFolder + archName; + + if (!File.Exists(archName)) + throw new Exception("Failed to load file !"); + + byte[]? data = null; + + using (var zip = ZipFile.OpenRead(archName)) + { + var entry = zip.Entries.FirstOrDefault(entry => entry.FullName == fileName || entry.Name == fileName); + if (entry is null) throw new Exception("File not found in archive"); + + var MemoryStream = new MemoryStream(); + + var stream = entry.Open(); + await stream.CopyToAsync(MemoryStream); + data = MemoryStream.ToArray(); + + stream.Close(); + MemoryStream.Close(); + } + + return data; + } + /// /// Read data from a file that is inside an archive (ZIP format) /// @@ -40,19 +76,19 @@ public static class ArchiveManager { 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(); - } - } + 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; } @@ -73,7 +109,7 @@ public static class ArchiveManager /// The type of progress /// public static async Task ExtractArchive( - string zip, string folder, IProgress progress, + string zip, string folder, IProgress progress, UnzipProgressType type) { if (!isInitialized) throw new Exception("ArchiveManager is not initialized"); @@ -82,7 +118,7 @@ public static class ArchiveManager { if (type == UnzipProgressType.PERCENTAGE_FROM_NUMBER_OF_FILES) { - var totalZIPFiles = archive.Entries.Count(); + var totalZIPFiles = archive.Entries.Count(); var currentZIPFile = 0; foreach (var entry in archive.Entries) { @@ -97,7 +133,7 @@ public static class ArchiveManager catch (Exception ex) { Config.Logger.Log($"Failed to extract {entry.Name}. Exception: {ex.Message}", - "Archive Manager", LogLevel.ERROR); + "Archive Manager", LogLevel.ERROR); } currentZIPFile++; @@ -130,7 +166,7 @@ public static class ArchiveManager catch (Exception ex) { Config.Logger.Log($"Failed to extract {entry.Name}. Exception: {ex.Message}", - "Archive Manager", LogLevel.ERROR); + "Archive Manager", LogLevel.ERROR); } await Task.Delay(10); @@ -140,4 +176,4 @@ public static class ArchiveManager } } } -} +} \ No newline at end of file