patch: listplugs, plugin version system and progress bar

This commit is contained in:
2022-08-25 15:15:47 +03:00
parent b98f57fcf8
commit f6442af30c
10 changed files with 210 additions and 55 deletions

View File

@@ -181,7 +181,7 @@ namespace PluginManager
SaveConfig(SaveType.NORMAL);
}
public static async void SaveConfig(SaveType type)
public static async Task SaveConfig(SaveType type)
{
if (type == SaveType.NORMAL)
{

View File

@@ -82,30 +82,48 @@ public class ConsoleCommandsHandler
if (pluginsLoaded)
return;
var loader = new PluginLoader(client!);
ConsoleColor cc = Console.ForegroundColor;
loader.onCMDLoad += (name, typeName, success, exception) =>
{
Console.ForegroundColor = ConsoleColor.Green;
if (name == null || name.Length < 2)
name = typeName;
if (success)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("[CMD] Successfully loaded command : " + name);
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[CMD] Failed to load command : " + name + " because " + exception!.Message);
Console.ForegroundColor = ConsoleColor.Red;
}
Console.ForegroundColor = cc;
};
loader.onEVELoad += (name, typeName, success, exception) =>
{
if (name == null || name.Length < 2)
name = typeName;
Console.ForegroundColor = ConsoleColor.Green;
if (success)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("[EVENT] Successfully loaded event : " + name);
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("[EVENT] Failed to load event : " + name + " because " + exception!.Message);
Console.ForegroundColor = ConsoleColor.Red;
}
Console.ForegroundColor = cc;
};
loader.LoadPlugins();
Console.ForegroundColor = cc;
pluginsLoaded = true;
}
);
@@ -178,14 +196,14 @@ public class ConsoleCommandsHandler
Console.WriteLine($"Extracting {split[1]}");
var proc = 0f;
var isExtracting = true;
var bar = new Console_Utilities.ProgressBar { Max = 100f, Color = ConsoleColor.Green };
var bar = new Console_Utilities.ProgressBar(ProgressBarType.NORMAL) { Max = 100f, Color = ConsoleColor.Green };
IProgress<float> extractProgress = new Progress<float>(value => { proc = value; });
new Thread(new Task(() =>
{
while (isExtracting)
{
bar.Update(ProgressBarType.NORMAL, proc);
bar.Update(proc);
if (proc >= 99.9f)
isExtracting = false;
Thread.Sleep(500);
@@ -194,10 +212,10 @@ public class ConsoleCommandsHandler
).Start
).Start();
await Functions.ExtractArchive("./" + split[1], "./", extractProgress, UnzipProgressType.PercentageFromTotalSize);
bar.Update(ProgressBarType.NORMAL, 100f);
bar.Update(100f);
isExtracting = false;
await Task.Delay(1000);
bar.Update(ProgressBarType.NORMAL, 100);
bar.Update(100);
Console.WriteLine("\n");
File.Delete("./" + split[1]);
}
@@ -207,7 +225,7 @@ public class ConsoleCommandsHandler
}
VersionString? ver = await VersionString.GetVersionOfPackageFromWeb(name);
if (ver is null) throw new Exception("Incorrect version");
Config.SetPluginVersion(name, $"{ver.PackageID}.{ver.PackageMainVersion}.{ver.PackageCheckVersion}");
Config.SetPluginVersion(name, $"{ver.PackageVersionID}.{ver.PackageMainVersion}.{ver.PackageCheckVersion}");
// Console.WriteLine();
isDownloading = false;
@@ -255,17 +273,30 @@ public class ConsoleCommandsHandler
}
);
AddCommand("sd", "Shuts down the discord bot", () =>
AddCommand("sd", "Shuts down the discord bot", async () =>
{
if (client is null)
return;
client.StopAsync();
client.DisposeAsync();
Config.SaveConfig(SaveType.NORMAL);
Config.SaveConfig(SaveType.BACKUP);
Console.WriteLine("Bot is closing in 2 seconds ! Please wait to save data !");
Thread.Sleep(2000);
bool run = true;
var t = new Thread(() =>
{
Console_Utilities.ProgressBar bar = new Console_Utilities.ProgressBar(ProgressBarType.NO_END);
while (run)
{
bar.Update(1);
Thread.Sleep(50);
}
});
t.Start();
await Config.SaveConfig(SaveType.NORMAL);
await Config.SaveConfig(SaveType.BACKUP);
await Task.Delay(4000);
run = false;
Console.WriteLine();
await client.StopAsync();
await client.DisposeAsync();
Environment.Exit(0);
}
);
@@ -346,7 +377,7 @@ public class ConsoleCommandsHandler
Console.WriteLine("Found: " + tuple.ToString());
Config.PluginConfig.InstalledPlugins.Remove(tuple);
Config.RemovePluginVersion(plugName);
Config.SaveConfig(SaveType.NORMAL);
await Config.SaveConfig(SaveType.NORMAL);
}
Console.WriteLine("Removed the plugin DLL. Checking for other files ...");

View File

@@ -69,7 +69,7 @@ public class PluginLoader
{
string name = new FileInfo(file).Name.Split('.')[0];
if (!Config.PluginVersionsContainsKey(name))
Config.SetPluginVersion(name, (await VersionString.GetVersionOfPackageFromWeb(name))?.PackageID + ".0.0");
Config.SetPluginVersion(name, (await VersionString.GetVersionOfPackageFromWeb(name))?.PackageVersionID + ".0.0");
if (await PluginUpdater.CheckForUpdates(name))
await PluginUpdater.Download(name);
@@ -84,7 +84,7 @@ public class PluginLoader
{
string name = new FileInfo(file).Name.Split('.')[0];
if (!Config.PluginVersionsContainsKey(name))
Config.SetPluginVersion(name, (await VersionString.GetVersionOfPackageFromWeb(name))?.PackageID + ".0.0");
Config.SetPluginVersion(name, (await VersionString.GetVersionOfPackageFromWeb(name))?.PackageVersionID + ".0.0");
if (await PluginUpdater.CheckForUpdates(name))
await PluginUpdater.Download(name);
@@ -94,7 +94,7 @@ public class PluginLoader
//Save the new config file (after the updates)
Config.SaveConfig(SaveType.NORMAL);
await Config.SaveConfig(SaveType.NORMAL);
//Load all plugins

View File

@@ -9,7 +9,7 @@ namespace PluginManager.Online.Helpers
{
public class VersionString
{
public int PackageID;
public int PackageVersionID;
public int PackageMainVersion;
public int PackageCheckVersion;
@@ -18,7 +18,7 @@ namespace PluginManager.Online.Helpers
string[] data = version.Split('.');
try
{
PackageID = int.Parse(data[0]);
PackageVersionID = int.Parse(data[0]);
PackageMainVersion = int.Parse(data[1]);
PackageCheckVersion = int.Parse(data[2]);
}
@@ -28,21 +28,25 @@ namespace PluginManager.Online.Helpers
}
}
public static bool operator >(VersionString s1, VersionString s2)
{
if (s1.PackageID != s2.PackageID) throw new Exception("Can not compare two different paks");
if (s1.PackageMainVersion > s2.PackageMainVersion) return true;
if (s1.PackageMainVersion == s2.PackageMainVersion && s1.PackageCheckVersion > s2.PackageCheckVersion) return true;
return false;
}
#region operators
public static bool operator >(VersionString s1, VersionString s2)
{
if (s1.PackageVersionID > s2.PackageVersionID) return true;
if (s1.PackageVersionID == s2.PackageVersionID)
{
if (s1.PackageMainVersion > s2.PackageMainVersion) return true;
if (s1.PackageMainVersion == s2.PackageMainVersion && s1.PackageCheckVersion > s2.PackageCheckVersion) return true;
}
return false;
}
public static bool operator <(VersionString s1, VersionString s2) => !(s1 > s2) && s1 != s2;
public static bool operator ==(VersionString s1, VersionString s2)
{
if (s1.PackageID == s2.PackageID && s1.PackageMainVersion == s2.PackageMainVersion && s1.PackageCheckVersion == s2.PackageCheckVersion) return true;
if (s1.PackageVersionID == s2.PackageVersionID && s1.PackageMainVersion == s2.PackageMainVersion && s1.PackageCheckVersion == s2.PackageCheckVersion) return true;
return false;
}
@@ -55,12 +59,14 @@ namespace PluginManager.Online.Helpers
public override string ToString()
{
return "{PackageID: " + PackageID + ", PackageVersion: " + PackageMainVersion + ", PackageCheckVersion: " + PackageCheckVersion + "}";
return "{PackageID: " + PackageVersionID + ", PackageVersion: " + PackageMainVersion + ", PackageCheckVersion: " + PackageCheckVersion + "}";
}
public string ToShortString()
{
return $"{PackageID}.{PackageMainVersion}.{PackageCheckVersion}";
if (PackageVersionID == 0 && PackageCheckVersion == 0 && PackageMainVersion == 0)
return "Unknown";
return $"{PackageVersionID}.{PackageMainVersion}.{PackageCheckVersion}";
}
public static VersionString? GetVersionOfPackage(string pakName)

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using PluginManager.Online.Helpers;
using PluginManager.Others;
using OperatingSystem = PluginManager.Others.OperatingSystem;
@@ -39,7 +40,7 @@ public class PluginsManager
var op = Functions.GetOperatingSystem();
var len = lines.Length;
string[] titles = { "Name", "Description", "Plugin Type", "Libraries", "Installed" };
string[] titles = { "Name", "Description", "Type", "Version", "Installed" };
data.Add(new[] { "-", "-", "-", "-", "-" });
data.Add(titles);
data.Add(new[] { "-", "-", "-", "-", "-" });
@@ -56,11 +57,7 @@ public class PluginsManager
display[0] = content[0];
display[1] = content[1];
display[2] = content[2];
if (content.Length == 6 && (content[5] != null || content[5].Length > 2))
display[3] = ((await ServerCom.ReadTextFromURL(content[5])).Count + 1).ToString();
else
display[3] = "1";
display[3] = (await VersionString.GetVersionOfPackageFromWeb(content[0]) ?? new VersionString("0.0.0")).ToShortString();
if (Config.PluginConfig.Contains(content[0]) || Config.PluginConfig.Contains(content[0]))
display[4] = "✓";
else
@@ -75,8 +72,7 @@ public class PluginsManager
display[0] = content[0];
display[1] = content[1];
display[2] = content[2];
if (content.Length == 6 && (content[5] != null || content[5].Length > 2))
display[3] = ((await ServerCom.ReadTextFromURL(content[5])).Count + 1).ToString();
display[3] = (await VersionString.GetVersionOfPackageFromWeb(content[0]) ?? new VersionString("0.0.0")).ToShortString();
if (Config.PluginConfig.Contains(content[0]) || Config.PluginConfig.Contains(content[0]))
display[4] = "✓";
else

View File

@@ -54,7 +54,7 @@ namespace PluginManager.Online
bool isDownloading = true;
float c_progress = 0;
Console_Utilities.ProgressBar pbar = new Console_Utilities.ProgressBar { Max = 100f, NoColor = true };
Console_Utilities.ProgressBar pbar = new Console_Utilities.ProgressBar(ProgressBarType.NORMAL) { Max = 100f, NoColor = true };
IProgress<float> progress = new Progress<float>(percent => { c_progress = percent; });
@@ -63,7 +63,7 @@ namespace PluginManager.Online
{
while (isDownloading)
{
pbar.Update(ProgressBarType.NORMAL, c_progress);
pbar.Update(c_progress);
if (c_progress == 100f)
break;
Thread.Sleep(500);
@@ -76,7 +76,7 @@ namespace PluginManager.Online
c_progress = pbar.Max;
pbar.Update(ProgressBarType.NORMAL, 100f);
pbar.Update(100f);
isDownloading = false;
}
}

View File

@@ -2,7 +2,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace PluginManager.Others
{
@@ -33,15 +32,21 @@ namespace PluginManager.Others
/// </summary>
public class ProgressBar
{
public ProgressBar(ProgressBarType type)
{
this.type = type;
}
public float Max { get; init; }
public ConsoleColor Color { get; init; }
public bool NoColor { get; init; }
public ProgressBarType type { get; set; }
private int BarLength = 32;
private int position = 1;
private bool positive = true;
public void Update(ProgressBarType type, float progress)
public void Update(float progress)
{
switch (type)
{
@@ -52,6 +57,8 @@ namespace PluginManager.Others
if (progress <= 99.9f)
UpdateNoEnd();
return;
default:
return;
}
}
@@ -81,6 +88,8 @@ namespace PluginManager.Others
Console.CursorLeft = 1;
float onechunk = 30.0f / Max;
int position = 1;
for (int i = 0; i < onechunk * progress; i++)
{
Console.BackgroundColor = NoColor ? ConsoleColor.Black : this.Color;
@@ -88,14 +97,14 @@ namespace PluginManager.Others
Console.Write("#");
}
for (int i = position; i <= BarLength - 1; i++)
for (int i = position; i < BarLength; i++)
{
Console.BackgroundColor = NoColor ? ConsoleColor.Black : ConsoleColor.DarkGray;
Console.CursorLeft = position++;
Console.Write(" ");
}
Console.CursorLeft = 35;
Console.CursorLeft = BarLength + 4;
Console.BackgroundColor = ConsoleColor.Black;
if (progress.CanAproximateTo(Max))
Console.Write(progress + " % ✓");