Improved download speed and started using Spectre.Console package
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using DiscordBot.Utilities;
|
||||
using PluginManager;
|
||||
@@ -7,16 +9,17 @@ using PluginManager.Interfaces;
|
||||
using PluginManager.Loaders;
|
||||
using PluginManager.Online;
|
||||
using PluginManager.Others;
|
||||
using Spectre.Console;
|
||||
|
||||
namespace DiscordBot.Bot.Actions;
|
||||
|
||||
public class Plugin : ICommandAction
|
||||
{
|
||||
private bool pluginsLoaded;
|
||||
public string ActionName => "plugin";
|
||||
public string Description => "Manages plugins. Use plugin help for more info.";
|
||||
public string Usage => "plugin [help|list|load|install|refresh]";
|
||||
public InternalActionRunType RunType => InternalActionRunType.ON_CALL;
|
||||
private bool pluginsLoaded;
|
||||
public string ActionName => "plugin";
|
||||
public string Description => "Manages plugins. Use plugin help for more info.";
|
||||
public string Usage => "plugin [help|list|load|install|refresh]";
|
||||
public InternalActionRunType RunType => InternalActionRunType.ON_CALL;
|
||||
|
||||
public async Task Execute(string[] args)
|
||||
{
|
||||
@@ -31,10 +34,10 @@ public class Plugin : ICommandAction
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
var manager = new PluginsManager();
|
||||
|
||||
switch ( args[0] )
|
||||
switch (args[0])
|
||||
{
|
||||
case "refresh":
|
||||
await Program.internalActionManager.Refresh();
|
||||
@@ -67,8 +70,8 @@ public class Plugin : ICommandAction
|
||||
pluginsLoaded = true;
|
||||
break;
|
||||
}
|
||||
|
||||
var cc = Console.ForegroundColor;
|
||||
|
||||
var cc = Console.ForegroundColor;
|
||||
loader.onCMDLoad += (name, typeName, success, exception) =>
|
||||
{
|
||||
if (name == null || name.Length < 2)
|
||||
@@ -87,7 +90,7 @@ public class Plugin : ICommandAction
|
||||
else
|
||||
Console.WriteLine("[CMD] Failed to load command : " + name + " because " +
|
||||
exception!.Message
|
||||
);
|
||||
);
|
||||
}
|
||||
|
||||
Console.ForegroundColor = cc;
|
||||
@@ -126,7 +129,7 @@ public class Plugin : ICommandAction
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine("[SLASH] Failed to load command : " + name + " because " +
|
||||
exception!.Message
|
||||
);
|
||||
);
|
||||
}
|
||||
|
||||
Console.ForegroundColor = cc;
|
||||
@@ -134,7 +137,7 @@ public class Plugin : ICommandAction
|
||||
|
||||
loader.LoadPlugins();
|
||||
Console.ForegroundColor = cc;
|
||||
pluginsLoaded = true;
|
||||
pluginsLoaded = true;
|
||||
break;
|
||||
|
||||
case "install":
|
||||
@@ -150,68 +153,116 @@ public class Plugin : ICommandAction
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var pluginData = await manager.GetPluginLinkByName(pluginName);
|
||||
if (pluginData == null || pluginData.Length == 0)
|
||||
{
|
||||
Console.WriteLine($"Plugin {pluginName} not found. Please check the spelling and try again.");
|
||||
break;
|
||||
}
|
||||
|
||||
var pluginType = pluginData[0];
|
||||
var pluginLink = pluginData[1];
|
||||
var pluginRequirements = pluginData[2];
|
||||
|
||||
|
||||
Console.WriteLine("Downloading plugin...");
|
||||
//download plugin progress bar for linux and windows terminals
|
||||
var spinner = new ConsoleUtilities.Spinner();
|
||||
spinner.Start();
|
||||
IProgress<float> progress = new Progress<float>(p => { spinner.Message = $"Downloading {pluginName}... {Math.Round(p, 2)}% "; });
|
||||
await ServerCom.DownloadFileAsync(pluginLink, $"./Data/{pluginType}s/{pluginName}.dll", progress);
|
||||
spinner.Stop();
|
||||
Console.WriteLine();
|
||||
|
||||
if (pluginRequirements == string.Empty)
|
||||
{
|
||||
Console.WriteLine("Finished installing " + pluginName + " successfully");
|
||||
Console.WriteLine("Reloading plugins list...");
|
||||
await Program.internalActionManager.Execute("plugin", "load");
|
||||
await Program.internalActionManager.Refresh();
|
||||
|
||||
Console.WriteLine("Finished reloading plugins list");
|
||||
break;
|
||||
}
|
||||
|
||||
Console.WriteLine("Downloading plugin requirements...");
|
||||
var requirementsURLs = await ServerCom.ReadTextFromURL(pluginRequirements);
|
||||
|
||||
foreach (var requirement in requirementsURLs)
|
||||
{
|
||||
if (requirement.Length < 2)
|
||||
continue;
|
||||
var reqdata = requirement.Split(',');
|
||||
var url = reqdata[0];
|
||||
var filename = reqdata[1];
|
||||
|
||||
Console.WriteLine($"Downloading {filename}... ");
|
||||
progress = new Progress<float>(p => { spinner.Message = $"Downloading {filename}... {Math.Round(p, 2)}% "; });
|
||||
spinner.Start();
|
||||
await ServerCom.DownloadFileAsync(url, $"./{filename}", progress);
|
||||
spinner.Stop();
|
||||
progress.Report(1);
|
||||
await Task.Delay(1000);
|
||||
Console.WriteLine("Downloaded " + filename + " successfully");
|
||||
}
|
||||
|
||||
Console.WriteLine("Finished installing " + pluginName + " successfully");
|
||||
|
||||
Console.WriteLine("Reloading plugins list...");
|
||||
await Program.internalActionManager.Execute("plugin", "load", "-q");
|
||||
await Program.internalActionManager.Refresh();
|
||||
|
||||
Console.WriteLine("Finished reloading plugins list");
|
||||
await DownloadPlugin(manager, pluginName);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RefreshPlugins()
|
||||
{
|
||||
Console.WriteLine("Reloading plugins list...");
|
||||
await Program.internalActionManager.Execute("plugin", "load");
|
||||
await Program.internalActionManager.Refresh();
|
||||
|
||||
Console.WriteLine("Finished reloading plugins list");
|
||||
}
|
||||
|
||||
|
||||
public async Task DownloadPlugin(PluginsManager manager, string pluginName)
|
||||
{
|
||||
var pluginData = await manager.GetPluginLinkByName(pluginName);
|
||||
if (pluginData.Length == 0)
|
||||
{
|
||||
Console.WriteLine($"Plugin {pluginName} not found. Please check the spelling and try again.");
|
||||
return;
|
||||
}
|
||||
|
||||
var pluginType = pluginData[0];
|
||||
var pluginLink = pluginData[1];
|
||||
var pluginRequirements = pluginData[2];
|
||||
|
||||
|
||||
await AnsiConsole.Progress()
|
||||
.Columns(new ProgressColumn[]
|
||||
{
|
||||
new TaskDescriptionColumn(),
|
||||
new ProgressBarColumn(),
|
||||
new PercentageColumn()
|
||||
})
|
||||
.StartAsync(async ctx =>
|
||||
{
|
||||
var downloadTask = ctx.AddTask("Downloading plugin...");
|
||||
|
||||
IProgress<float> progress = new Progress<float>(p => { downloadTask.Value = p; });
|
||||
|
||||
await ServerCom.DownloadFileAsync(pluginLink, $"./Data/{pluginType}s/{pluginName}.dll", progress);
|
||||
|
||||
downloadTask.Increment(100);
|
||||
|
||||
ctx.Refresh();
|
||||
});
|
||||
|
||||
if (pluginRequirements == string.Empty)
|
||||
{
|
||||
Console.WriteLine("Finished installing " + pluginName + " successfully");
|
||||
await RefreshPlugins();
|
||||
return;
|
||||
}
|
||||
|
||||
List<string> requirementsUrLs = new();
|
||||
|
||||
await AnsiConsole.Progress()
|
||||
.Columns(new ProgressColumn[]
|
||||
{
|
||||
new TaskDescriptionColumn(),
|
||||
new ProgressBarColumn(),
|
||||
new PercentageColumn()
|
||||
})
|
||||
.StartAsync(async ctx =>
|
||||
{
|
||||
var gatherInformationTask = ctx.AddTask("Gathering info...");
|
||||
gatherInformationTask.IsIndeterminate = true;
|
||||
requirementsUrLs = await ServerCom.ReadTextFromURL(pluginRequirements);
|
||||
await Task.Delay(2000);
|
||||
gatherInformationTask.Increment(100);
|
||||
});
|
||||
|
||||
await AnsiConsole.Progress()
|
||||
.Columns(new ProgressColumn[]
|
||||
{
|
||||
new TaskDescriptionColumn(),
|
||||
new ProgressBarColumn(),
|
||||
new PercentageColumn()
|
||||
})
|
||||
.StartAsync(async ctx =>
|
||||
{
|
||||
List<Tuple<ProgressTask, IProgress<float>, Task>> downloadTasks = new();
|
||||
|
||||
foreach (var info in requirementsUrLs)
|
||||
{
|
||||
if (info.Length < 2) continue;
|
||||
string[] data = info.Split(',');
|
||||
string url = data[0];
|
||||
string fileName = data[1];
|
||||
|
||||
var task = ctx.AddTask($"Downloading {fileName}...");
|
||||
IProgress<float> progress = new Progress<float>(p =>
|
||||
{
|
||||
task.Value = p;
|
||||
});
|
||||
|
||||
var downloadTask = ServerCom.DownloadFileAsync(url, $"./{fileName}", progress);
|
||||
downloadTasks.Add(new Tuple<ProgressTask, IProgress<float>, Task>(task, progress, downloadTask));
|
||||
}
|
||||
|
||||
foreach (var task in downloadTasks)
|
||||
{
|
||||
await task.Item3;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
await RefreshPlugins();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user