Actions are now loaded together with all plugins. Called the LoadPlugins at startup

This commit is contained in:
2024-06-08 19:17:15 +03:00
parent 9a8ddb5388
commit d9d5c05313
14 changed files with 148 additions and 236 deletions

View File

@@ -9,6 +9,44 @@ namespace DiscordBotCore.Online.Helpers;
internal static class OnlineFunctions
{
/// <summary>
/// Copy one Stream to another <see langword="async" />
/// </summary>
/// <param name="stream">The base stream</param>
/// <param name="destination">The destination stream</param>
/// <param name="bufferSize">The buffer to read</param>
/// <param name="progress">The progress</param>
/// <param name="cancellationToken">The cancellation token</param>
/// <exception cref="ArgumentNullException">Triggered if any <see cref="Stream" /> is empty</exception>
/// <exception cref="ArgumentOutOfRangeException">Triggered if <paramref name="bufferSize" /> is less then or equal to 0</exception>
/// <exception cref="InvalidOperationException">Triggered if <paramref name="stream" /> is not readable</exception>
/// <exception cref="ArgumentException">Triggered in <paramref name="destination" /> is not writable</exception>
public static async Task CopyToOtherStreamAsync(
this Stream stream, Stream destination, int bufferSize,
IProgress<long>? progress = null,
CancellationToken cancellationToken = default)
{
if (stream == null) throw new ArgumentNullException(nameof(stream));
if (destination == null) throw new ArgumentNullException(nameof(destination));
if (bufferSize <= 0) throw new ArgumentOutOfRangeException(nameof(bufferSize));
if (!stream.CanRead) throw new InvalidOperationException("The stream is not readable.");
if (!destination.CanWrite)
throw new ArgumentException("Destination stream is not writable", nameof(destination));
var buffer = new byte[bufferSize];
long totalBytesRead = 0;
int bytesRead;
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken)
.ConfigureAwait(false)) != 0)
{
await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
totalBytesRead += bytesRead;
progress?.Report(totalBytesRead);
}
}
/// <summary>
/// Downloads a <see cref="Stream" /> and saves it to another <see cref="Stream" />.
/// </summary>

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.IO;
using System.Linq;
@@ -88,7 +89,11 @@ public class PluginManager
public async Task AppendPluginToDatabase(PluginInfo pluginData)
{
List<PluginInfo> installedPlugins = await JsonManager.ConvertFromJson<List<PluginInfo>>(await File.ReadAllTextAsync(Application.CurrentApplication.PluginDatabase));
foreach (var dependency in pluginData.ListOfDependancies)
{
pluginData.ListOfDependancies[dependency.Key] = GenerateDependencyLocation(pluginData.PluginName, dependency.Value);
}
installedPlugins.Add(pluginData);
await JsonManager.SaveToJsonFile(Application.CurrentApplication.PluginDatabase, installedPlugins);
}
@@ -123,15 +128,19 @@ public class PluginManager
public async Task<bool> MarkPluginToUninstall(string pluginName)
{
List<PluginInfo> installedPlugins = await GetInstalledPlugins();
PluginInfo? info = installedPlugins.Find(info => info.PluginName == pluginName);
IEnumerable<PluginInfo> installedPlugins = await GetInstalledPlugins();
IEnumerable<PluginInfo> info = installedPlugins.Where(info => info.PluginName == pluginName).AsEnumerable();
if(info == null)
if(!info.Any())
return false;
await RemovePluginFromDatabase(pluginName);
info.IsMarkedToUninstall = true;
await AppendPluginToDatabase(info);
foreach (var item in info)
{
await RemovePluginFromDatabase(item.PluginName);
item.IsMarkedToUninstall = true;
await AppendPluginToDatabase(item);
}
return true;
@@ -139,11 +148,11 @@ public class PluginManager
public async Task UninstallMarkedPlugins()
{
List<PluginInfo> installedPlugins = await GetInstalledPlugins();
foreach(PluginInfo plugin in installedPlugins)
{
if(!plugin.IsMarkedToUninstall) continue;
IEnumerable<PluginInfo> installedPlugins = (await GetInstalledPlugins()).AsEnumerable();
IEnumerable<PluginInfo> pluginsToRemove = installedPlugins.Where(plugin => plugin.IsMarkedToUninstall).AsEnumerable();
foreach (var plugin in pluginsToRemove)
{
await UninstallPlugin(plugin);
}
}
@@ -171,21 +180,6 @@ public class PluginManager
throw new Exception("Dependency not found");
}
public async Task<string> GetDependencyLocation(string pluginName, string dependencyName)
{
PluginOnlineInfo? pluginData = await GetPluginDataByName(pluginName);
if(pluginData == null)
throw new Exception("Plugin not found");
var dependency = pluginData.Dependencies.Find(dep => dep.DependencyName == dependencyName);
if(dependency == null)
throw new Exception("Dependency not found");
return dependency.DownloadLocation;
}
public string GenerateDependencyLocation(string pluginName, string dependencyName)
{
return Path.Combine(Environment.CurrentDirectory, $"Libraries/{pluginName}/{dependencyName}");