From 6d73ec7f24b8fc4efd9fa51796ff94645ce1d00f Mon Sep 17 00:00:00 2001 From: Wizzy69 Date: Mon, 26 Sep 2022 19:30:53 +0300 Subject: [PATCH] Linux compatibility update --- DiscordBot/DiscordBot.csproj | 2 +- DiscordBot/Program.cs | 22 ++++-- PluginManager/Items/ConsoleCommandsHandler.cs | 38 +++++++--- PluginManager/Others/Console Utilities.cs | 71 +++++++++++++++---- 4 files changed, 107 insertions(+), 26 deletions(-) diff --git a/DiscordBot/DiscordBot.csproj b/DiscordBot/DiscordBot.csproj index e4222d3..1c9e4d3 100644 --- a/DiscordBot/DiscordBot.csproj +++ b/DiscordBot/DiscordBot.csproj @@ -8,7 +8,7 @@ False True - 1.0.0.13 + 1.0.0.14 diff --git a/DiscordBot/Program.cs b/DiscordBot/Program.cs index 601c113..0228bc1 100644 --- a/DiscordBot/Program.cs +++ b/DiscordBot/Program.cs @@ -4,12 +4,14 @@ using System.Diagnostics; using System.IO; using System.Linq; using System.Reflection; +using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using Discord; using DiscordBot.Discord.Core; + using PluginManager; using PluginManager.Items; using PluginManager.Online; @@ -230,6 +232,11 @@ public class Program len = 0; } + if (len == 2 && args[0] == "/procKill") + { + Process.GetProcessById(int.Parse(args[1])).Kill(); + len = 0; + } var b = await StartNoGUI(); @@ -370,7 +377,7 @@ public class Program List onlineSettingsList = await ServerCom.ReadTextFromURL("https://raw.githubusercontent.com/Wizzy69/installer/discord-bot-files/OnlineData"); - main.Stop(); + main.Stop("Loaded online settings. Loading updates ..."); foreach (var key in onlineSettingsList) { if (key.Length <= 3 || !key.Contains(' ')) continue; @@ -394,7 +401,14 @@ public class Program else { string url = $"https://github.com/Wizzy69/SethDiscordBot/releases/download/v{newVersion}/net6.0_linux.zip"; - Process.Start("./Updater/Updater", $"/update {url} ./DiscordBot ./"); + Console_Utilities.ProgressBar bar = new Console_Utilities.ProgressBar(ProgressBarType.NO_END); + bar.Start(); + await ServerCom.DownloadFileNoProgressAsync(url, "./update.zip"); + await Functions.ExtractArchive("./update.zip", "./", null, UnzipProgressType.PercentageFromNumberOfFiles); + bar.Stop("Console is now Updated"); + + Process.Start("./DiscordBot /procKill " + Process.GetCurrentProcess().Id); + } //Environment.Exit(0); } @@ -402,7 +416,7 @@ public class Program break; case "UpdaterVersion": string updaternewversion = s[1]; - if (Config.UpdaterVersion != updaternewversion) + if (Config.UpdaterVersion != updaternewversion && Functions.GetOperatingSystem() == PluginManager.Others.OperatingSystem.WINDOWS) { Console.Clear(); Console.WriteLine("Installing updater ...\nDo NOT close the bot during update !"); @@ -413,7 +427,7 @@ public class Program Config.UpdaterVersion = updaternewversion; File.Delete("Updater.zip"); await Config.SaveConfig(SaveType.NORMAL); - bar.Stop(); + bar.Stop("Updater has been updated !"); Console.Clear(); } break; diff --git a/PluginManager/Items/ConsoleCommandsHandler.cs b/PluginManager/Items/ConsoleCommandsHandler.cs index 7fbae64..b44e1e7 100644 --- a/PluginManager/Items/ConsoleCommandsHandler.cs +++ b/PluginManager/Items/ConsoleCommandsHandler.cs @@ -14,9 +14,7 @@ using PluginManager.Interfaces; using PluginManager.Loaders; using PluginManager.Online; using PluginManager.Online.Helpers; -using PluginManager.Online.Updates; using PluginManager.Others; - namespace PluginManager.Items; public class ConsoleCommandsHandler @@ -162,8 +160,18 @@ public class ConsoleCommandsHandler path = "./Data/Plugins/" + info[0] + "s/" + name + "." + (info[0] == "Command" ? PluginLoader.pluginCMDExtension : PluginLoader.pluginEVEExtension); else path = $"./{info[1].Split('/')[info[1].Split('/').Length - 1]}"; - //Console.WriteLine("Downloading: " + path + " [" + info[1] + "]"); - await ServerCom.DownloadFileAsync(info[1], path); + if (Others.OperatingSystem.WINDOWS == Functions.GetOperatingSystem()) + { + await ServerCom.DownloadFileAsync(info[1], path); + } + else if (Others.OperatingSystem.LINUX == Functions.GetOperatingSystem()) + { + Others.Console_Utilities.ProgressBar bar = new Console_Utilities.ProgressBar(ProgressBarType.NO_END); + bar.Start(); + await ServerCom.DownloadFileNoProgressAsync(info[1], path); + bar.Stop("Plugin Downloaded !"); + } + if (info[0] == "Event") Config.PluginConfig.InstalledPlugins.Add(new(name, PluginType.Event)); else if (info[0] == "Command") @@ -187,7 +195,16 @@ public class ConsoleCommandsHandler var split = line.Split(','); Console.WriteLine($"\nDownloading item: {split[1]}"); if (File.Exists("./" + split[1])) File.Delete("./" + split[1]); - await ServerCom.DownloadFileAsync(split[0], "./" + split[1]); + if (Others.OperatingSystem.WINDOWS == Functions.GetOperatingSystem()) + await ServerCom.DownloadFileAsync(split[0], "./" + split[1]); + else if (Others.OperatingSystem.LINUX == Functions.GetOperatingSystem()) + { + Others.Console_Utilities.ProgressBar bar = new Console_Utilities.ProgressBar(ProgressBarType.NO_END); + bar.Start(); + await ServerCom.DownloadFileNoProgressAsync(split[0], "./" + split[1]); + bar.Stop("Item downloaded !"); + + } Console.WriteLine(); if (split[0].EndsWith(".pak")) File.Move("./" + split[1], "./Data/PAKS/" + split[1], true); @@ -197,7 +214,7 @@ public class ConsoleCommandsHandler var bar = new Console_Utilities.ProgressBar(ProgressBarType.NO_END);// { Max = 100f, Color = ConsoleColor.Green }; bar.Start(); await Functions.ExtractArchive("./" + split[1], "./", null, UnzipProgressType.PercentageFromTotalSize); - bar.Stop(); + bar.Stop("Extracted"); Console.WriteLine("\n"); File.Delete("./" + split[1]); } @@ -208,10 +225,12 @@ public class ConsoleCommandsHandler VersionString? ver = await VersionString.GetVersionOfPackageFromWeb(name); if (ver is null) throw new Exception("Incorrect version"); Config.SetPluginVersion(name, $"{ver.PackageVersionID}.{ver.PackageMainVersion}.{ver.PackageCheckVersion}"); - // Console.WriteLine(); isDownloading = false; + + } + ); @@ -264,11 +283,12 @@ public class ConsoleCommandsHandler bar.Start(); await Config.SaveConfig(SaveType.NORMAL); await Config.SaveConfig(SaveType.BACKUP); - await Task.Delay(4000); - bar.Stop(); + bar.Stop("Saved config !"); Console.WriteLine(); await client.StopAsync(); await client.DisposeAsync(); + + await Task.Delay(1000); Environment.Exit(0); } diff --git a/PluginManager/Others/Console Utilities.cs b/PluginManager/Others/Console Utilities.cs index 81800ea..818042e 100644 --- a/PluginManager/Others/Console Utilities.cs +++ b/PluginManager/Others/Console Utilities.cs @@ -42,12 +42,15 @@ namespace PluginManager.Others public bool NoColor { get; init; } public ProgressBarType type { get; set; } + public int TotalLength { get; private set; } + private int BarLength = 32; private int position = 1; private bool positive = true; private bool isRunning; + public async void Start() { if (type != ProgressBarType.NO_END) @@ -63,6 +66,23 @@ namespace PluginManager.Others } } + public async void Start(string message) + { + if (type != ProgressBarType.NO_END) + throw new Exception("Only NO_END progress bar can use this method"); + if (isRunning) + throw new Exception("This progress bar is already running"); + + isRunning = true; + + TotalLength = message.Length + BarLength + 5; + while (isRunning) + { + UpdateNoEnd(message); + await System.Threading.Tasks.Task.Delay(100); + } + } + public void Stop() { if (type != ProgressBarType.NO_END) @@ -72,20 +92,45 @@ namespace PluginManager.Others isRunning = false; } + public void Stop(string message) + { + Stop(); + + if (message is not null) + { + Console.CursorLeft = 0; + for (int i = 0; i < BarLength + message.Length + 1; i++) + Console.Write(" "); + Console.CursorLeft = 0; + Console.WriteLine(message); + } + } + public void Update(float progress) { - switch (type) - { - case ProgressBarType.NORMAL: - UpdateNormal(progress); - return; - case ProgressBarType.NO_END: - if (progress <= 99.9f) - UpdateNoEnd(); - return; - default: - return; - } + if (type == ProgressBarType.NO_END) + throw new Exception("This function is for progress bars with end"); + + UpdateNormal(progress); + } + + private void UpdateNoEnd(string message) + { + Console.CursorLeft = 0; + Console.Write("["); + for (int i = 1; i <= position; i++) + Console.Write(" "); + Console.Write("<==()==>"); + position += positive ? 1 : -1; + for (int i = position; i <= BarLength - 1 - (positive ? 0 : 2); i++) + Console.Write(" "); + Console.Write("] " + message); + + + + + if (position == BarLength - 1 || position == 1) + positive = !positive; } private void UpdateNoEnd() @@ -101,6 +146,8 @@ namespace PluginManager.Others Console.Write("]"); + + if (position == BarLength - 1 || position == 1) positive = !positive; }