Updated API for plugins to work with database from remote. Added PluginRepository and removed Installation Scripts

This commit is contained in:
2025-01-26 20:34:34 +02:00
parent 8b2169dc7b
commit 84b19e2069
35 changed files with 435 additions and 1056 deletions

View File

@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
namespace DiscordBotCore.Online;
public class FileDownloader
{
private readonly HttpClient _httpClient;
public List<Task> ListOfDownloadTasks { get; private set; }
private bool IsParallelDownloader { get; set; }
public Action FinishAction { get; private set; }
public FileDownloader(bool isParallelDownloader)
{
_httpClient = new HttpClient();
ListOfDownloadTasks = new List<Task>();
IsParallelDownloader = isParallelDownloader;
}
public FileDownloader(bool isParallelDownloader, Action finishAction)
{
_httpClient = new HttpClient();
ListOfDownloadTasks = new List<Task>();
IsParallelDownloader = isParallelDownloader;
FinishAction = finishAction;
}
public async Task StartDownloadTasks()
{
if (IsParallelDownloader)
{
await Task.WhenAll(ListOfDownloadTasks);
}
else
{
foreach (var task in ListOfDownloadTasks)
{
await task;
}
}
FinishAction?.Invoke();
}
public void AppendDownloadTask(string downloadLink, string downloadLocation, IProgress<float> progress)
{
ListOfDownloadTasks.Add(CreateDownloadTask(_httpClient, downloadLink, downloadLocation, progress));
}
public void AppendDownloadTask(string downloadLink, string downloadLocation, Action<float> progressCallback)
{
ListOfDownloadTasks.Add(CreateDownloadTask(_httpClient, downloadLink, downloadLocation, new Progress<float>(progressCallback)));
}
public static async Task CreateDownloadTask(HttpClient client, string url, string targetPath, IProgress<float> progress)
{
using var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
var totalBytes = response.Content.Headers.ContentLength ?? -1L;
var receivedBytes = 0L;
var targetDirectory = Path.GetDirectoryName(targetPath);
if (!string.IsNullOrEmpty(targetDirectory))
{
Directory.CreateDirectory(targetDirectory);
}
using var contentStream = await response.Content.ReadAsStreamAsync();
using var fileStream = new FileStream(targetPath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true);
var buffer = new byte[8192];
int bytesRead;
while ((bytesRead = await contentStream.ReadAsync(buffer)) > 0)
{
await fileStream.WriteAsync(buffer.AsMemory(0, bytesRead));
receivedBytes += bytesRead;
if (totalBytes > 0)
{
float calculatedProgress = (float)receivedBytes / totalBytes;
progress.Report(calculatedProgress);
}
}
progress.Report(1f);
}
}

View File

@@ -107,21 +107,4 @@ internal static class OnlineFunctions
using var client = new HttpClient();
return await client.GetStringAsync(url, cancellation);
}
internal static async Task<bool> IsInternetConnected()
{
bool result = false;
try
{
using var client = new HttpClient();
await client.GetStringAsync("files.wizzy-server.ro");
result = true;
}
catch
{
result = false;
}
return result;
}
}

View File

@@ -0,0 +1,89 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using DiscordBotCore.Interfaces.PluginManagement;
using DiscordBotCore.Others;
using DiscordBotCore.Plugin;
namespace DiscordBotCore.Online.Helpers;
internal class PluginRepository : IPluginRepository
{
private readonly IPluginRepositoryConfiguration _pluginRepositoryConfiguration;
private readonly HttpClient _httpClient;
internal PluginRepository(IPluginRepositoryConfiguration pluginRepositoryConfiguration)
{
_pluginRepositoryConfiguration = pluginRepositoryConfiguration;
_httpClient = new HttpClient();
_httpClient.BaseAddress = new System.Uri(_pluginRepositoryConfiguration.BaseUrl);
}
public async Task<List<OnlinePlugin>> GetAllPlugins()
{
HttpResponseMessage response =
await _httpClient.GetAsync(_pluginRepositoryConfiguration.PluginRepositoryLocation + "get-all-plugins");
if (!response.IsSuccessStatusCode)
{
Application.Log("Failed to get all plugins from the repository", LogType.Warning);
return [];
}
string content = await response.Content.ReadAsStringAsync();
List<OnlinePlugin> plugins = await JsonManager.ConvertFromJson<List<OnlinePlugin>>(content);
return plugins;
}
public async Task<OnlinePlugin?> GetPluginById(int pluginId)
{
HttpResponseMessage response =
await _httpClient.GetAsync(_pluginRepositoryConfiguration.PluginRepositoryLocation +
$"get-plugin/{pluginId}");
if (!response.IsSuccessStatusCode)
{
Application.Log("Failed to get plugin from the repository", LogType.Warning);
return null;
}
string content = await response.Content.ReadAsStringAsync();
OnlinePlugin plugin = await JsonManager.ConvertFromJson<OnlinePlugin>(content);
return plugin;
}
public async Task<OnlinePlugin?> GetPluginByName(string pluginName)
{
HttpResponseMessage response =
await _httpClient.GetAsync(_pluginRepositoryConfiguration.PluginRepositoryLocation +
$"get-plugin-by-name/{pluginName}");
if (!response.IsSuccessStatusCode)
{
Application.Log("Failed to get plugin from the repository", LogType.Warning);
return null;
}
string content = await response.Content.ReadAsStringAsync();
OnlinePlugin plugin = await JsonManager.ConvertFromJson<OnlinePlugin>(content);
return plugin;
}
public async Task<List<OnlineDependencyInfo>> GetDependenciesForPlugin(int pluginId)
{
HttpResponseMessage response = await _httpClient.GetAsync(_pluginRepositoryConfiguration.DependenciesRepositoryLocation + $"get-dependencies-for-plugin/{pluginId}");
if(!response.IsSuccessStatusCode)
{
Application.Log("Failed to get dependencies for plugin from the repository", LogType.Warning);
return [];
}
string content = await response.Content.ReadAsStringAsync();
List<OnlineDependencyInfo> dependencies = await JsonManager.ConvertFromJson<List<OnlineDependencyInfo>>(content);
return dependencies;
}
}

View File

@@ -0,0 +1,19 @@
using DiscordBotCore.Interfaces.PluginManagement;
namespace DiscordBotCore.Online.Helpers;
public class PluginRepositoryConfiguration : IPluginRepositoryConfiguration
{
public static PluginRepositoryConfiguration Default => new ("http://localhost:5097/api/v1/", "plugins-repository/", "dependencies-repository/");
public string BaseUrl { get; }
public string PluginRepositoryLocation { get; }
public string DependenciesRepositoryLocation { get; }
public PluginRepositoryConfiguration(string baseUrl, string pluginRepositoryLocation, string dependenciesRepositoryLocation)
{
BaseUrl = baseUrl;
PluginRepositoryLocation = pluginRepositoryLocation;
DependenciesRepositoryLocation = dependenciesRepositoryLocation;
}
}

View File

@@ -1,20 +0,0 @@
using System.Text.Json.Serialization;
using DiscordBotCore.Interfaces.Updater;
namespace DiscordBotCore.Online.Helpers;
public class PluginVersion: Version
{
[JsonConstructor]
public PluginVersion(int major, int minor, int patch): base(major, minor, patch)
{
}
public PluginVersion(string versionAsString): base(versionAsString)
{
}
public override string ToString()
{
return ToShortString();
}
}

View File

@@ -3,56 +3,54 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using DiscordBotCore.Interfaces.PluginManagement;
using DiscordBotCore.Others;
using DiscordBotCore.Plugin;
using DiscordBotCore.Repository;
using DiscordBotCore.Updater.Plugins;
namespace DiscordBotCore.Online;
public sealed class PluginManager
{
private static readonly string _LibrariesBaseFolder = "Libraries";
private readonly PluginRepository _PluginRepository;
private readonly IPluginRepository _PluginRepository;
internal InstallingPluginInformation? InstallingPluginInformation { get; private set; }
public PluginManager(PluginRepository pluginRepository)
internal PluginManager(IPluginRepository pluginRepository)
{
_PluginRepository = pluginRepository;
}
public async Task<List<PluginOnlineInfo>> GetPluginsList()
public async Task<List<OnlinePlugin>> GetPluginsList()
{
var jsonText = await _PluginRepository.JsonGetAllPlugins();
List<PluginOnlineInfo> result = await JsonManager.ConvertFromJson<List<PluginOnlineInfo>>(jsonText);
var currentOs = OperatingSystem.IsWindows() ? OSType.WINDOWS :
OperatingSystem.IsLinux() ? OSType.LINUX :
OperatingSystem.IsMacOS() ? OSType.MACOSX : OSType.NONE;
return result.FindAll(pl => (pl.SupportedOS & currentOs) != 0);
}
public async Task<PluginOnlineInfo?> GetPluginDataByName(string pluginName)
{
List<PluginOnlineInfo>? plugins = await GetPluginsList();
if (plugins is null)
var onlinePlugins = await _PluginRepository.GetAllPlugins();
if (!onlinePlugins.Any())
{
return null;
Application.Log("Failed to get all plugins from the repository", LogType.Warning);
return [];
}
PluginOnlineInfo? result = plugins.Find(pl => pl.Name.Contains(pluginName, StringComparison.CurrentCultureIgnoreCase));
if (result is null)
int os = OS.GetOperatingSystemInt();
var response = onlinePlugins.Where(plugin => plugin.OperatingSystem == os).ToList();
return response;
}
public async Task<OnlinePlugin?> GetPluginDataByName(string pluginName)
{
var plugin = await _PluginRepository.GetPluginByName(pluginName);
if (plugin == null)
{
Application.Log("Failed to get plugin from the repository", LogType.Warning);
return null;
}
return result;
return plugin;
}
public async Task RemovePluginFromDatabase(string pluginName)
private async Task RemovePluginFromDatabase(string pluginName)
{
List<PluginInfo> installedPlugins = await JsonManager.ConvertFromJson<List<PluginInfo>>(await File.ReadAllTextAsync(Application.CurrentApplication.PluginDatabase));
@@ -60,24 +58,6 @@ public sealed class PluginManager
await JsonManager.SaveToJsonFile(Application.CurrentApplication.PluginDatabase, installedPlugins);
}
public async Task ExecutePluginInstallScripts(List<OnlineScriptDependencyInfo> listOfDependencies)
{
string? console = Application.CurrentApplication.ApplicationEnvironmentVariables.Get<string>("console.terminal");
if (string.IsNullOrEmpty(console))
{
return;
}
string? cmd_prefix = Application.CurrentApplication.ApplicationEnvironmentVariables.Get<string>("console.cmd_prefix");
if (string.IsNullOrEmpty(cmd_prefix))
{
return;
}
foreach (var script in listOfDependencies)
await ServerCom.RunConsoleCommand(console, $"{cmd_prefix}{script.ScriptContent}");
}
public async Task AppendPluginToDatabase(PluginInfo pluginData)
{
List<PluginInfo> installedPlugins = await JsonManager.ConvertFromJson<List<PluginInfo>>(await File.ReadAllTextAsync(Application.CurrentApplication.PluginDatabase));
@@ -102,26 +82,10 @@ public sealed class PluginManager
return installedPlugins.Any(plugin => plugin.PluginName == pluginName);
}
public async Task CheckForUpdates()
{
var pluginUpdater = new PluginUpdater(this);
List<PluginInfo> installedPlugins = await GetInstalledPlugins();
foreach (var plugin in installedPlugins)
{
if (await pluginUpdater.HasUpdate(plugin.PluginName))
{
Application.CurrentApplication.Logger.Log("Updating plugin: " + plugin.PluginName, this, LogType.Info);
await pluginUpdater.UpdatePlugin(plugin.PluginName);
}
}
}
public async Task<bool> MarkPluginToUninstall(string pluginName)
{
IEnumerable<PluginInfo> installedPlugins = await GetInstalledPlugins();
IEnumerable<PluginInfo> info = installedPlugins.Where(info => info.PluginName == pluginName).AsEnumerable();
List<PluginInfo> installedPlugins = await GetInstalledPlugins();
List<PluginInfo> info = installedPlugins.Where(info => info.PluginName == pluginName).ToList();
if (!info.Any())
return false;
@@ -132,10 +96,8 @@ public sealed class PluginManager
item.IsMarkedToUninstall = true;
await AppendPluginToDatabase(item);
}
return true;
}
public async Task UninstallMarkedPlugins()
@@ -201,137 +163,54 @@ public sealed class PluginManager
return relative;
}
public async Task InstallPluginWithNoProgress(PluginOnlineInfo pluginData)
public async Task InstallPluginNoProgress(OnlinePlugin plugin)
{
InstallingPluginInformation = new InstallingPluginInformation() { PluginName = pluginData.Name };
// Calculate the total number of steps: main file + dependencies
int totalSteps = pluginData.HasFileDependencies ? pluginData.Dependencies.Count + 1 : 1;
// Each step contributes this percentage to the total progress
InstallingPluginInformation = new InstallingPluginInformation() { PluginName = plugin.PluginName };
List<OnlineDependencyInfo> dependencies = await _PluginRepository.GetDependenciesForPlugin(plugin.PluginId);
int totalSteps = dependencies.Count + 1;
float stepFraction = 100f / totalSteps;
// Tracks the current cumulative progress in percentage
float currentProgress = 0f;
InstallingPluginInformation.IsInstalling = true;
// Create a progress updater that maps the file's 0100 progress to its portion of the total progress
var progress = currentProgress;
IProgress<float> downloadProgress = new Progress<float>(fileProgress =>
{
// Map the file progress (0-100) to the total progress
InstallingPluginInformation.InstallationProgress = currentProgress + (fileProgress / 100f) * stepFraction;
InstallingPluginInformation.InstallationProgress = progress + (fileProgress / 100f) * stepFraction;
});
// Download the main plugin file and map its progress
await ServerCom.DownloadFileAsync(pluginData.DownLoadLink,
$"{Application.CurrentApplication.ApplicationEnvironmentVariables.Get<string>("PluginFolder")}/{pluginData.Name}.dll",
downloadProgress
);
// Update cumulative progress after the main file
await ServerCom.DownloadFileAsync(plugin.PluginLink,
$"{Application.CurrentApplication.ApplicationEnvironmentVariables.Get<string>("PluginFolder")}/{plugin.PluginName}.dll",
downloadProgress);
currentProgress += stepFraction;
// Download file dependencies if they exist
if (pluginData.HasFileDependencies)
if (dependencies.Count > 0)
{
foreach (var dependency in pluginData.Dependencies)
foreach (var dependency in dependencies)
{
string dependencyLocation =
GenerateDependencyRelativePath(pluginData.Name, dependency.DownloadLocation);
// Download dependency and map its progress
string dependencyLocation = GenerateDependencyRelativePath(plugin.PluginName, dependency.DownloadLocation);
await ServerCom.DownloadFileAsync(dependency.DownloadLink, dependencyLocation, downloadProgress);
// Update cumulative progress after each dependency
currentProgress += stepFraction;
}
}
// Run script dependencies if any (doesn't affect download progress percentage)
if (pluginData.HasScriptDependencies)
{
foreach (var scriptDependency in pluginData.ScriptDependencies)
{
string? console = Application.CurrentApplication.ApplicationEnvironmentVariables.Get<string>("console.terminal");
if (string.IsNullOrEmpty(console))
{
return;
}
string? cmdPrefix = Application.CurrentApplication.ApplicationEnvironmentVariables.Get<string>("console.cmd_prefix");
if (string.IsNullOrEmpty(cmdPrefix))
{
return;
}
string arguments = $"{cmdPrefix}{scriptDependency.ScriptContent}";
await ServerCom.RunConsoleCommand(console, arguments);
}
}
// Register the plugin in the database
PluginInfo pluginInfo = PluginInfo.FromOnlineInfo(pluginData);
await AppendPluginToDatabase(pluginInfo);
InstallingPluginInformation.IsInstalling = false; // Mark installation as complete
}
public async Task InstallPluginWithProgressBar(PluginOnlineInfo pluginData, IProgress<float>? installProgress)
{
installProgress?.Report(0f);
int totalSteps = pluginData.HasFileDependencies ? pluginData.Dependencies.Count + 1 : 1;
float stepProgress = 1f / totalSteps;
float currentProgress = 0f;
IProgress<float> progress = new Progress<float>((p) =>
PluginInfo pluginInfo = PluginInfo.FromOnlineInfo(plugin, dependencies);
await AppendPluginToDatabase(pluginInfo);
InstallingPluginInformation.IsInstalling = false;
}
public async Task<Tuple<Dictionary<string, string>, List<OnlineDependencyInfo>>> GatherInstallDataForPlugin(OnlinePlugin plugin)
{
List<OnlineDependencyInfo> dependencies = await _PluginRepository.GetDependenciesForPlugin(plugin.PluginId);
var downloads = new Dictionary<string, string> { { $"{Application.CurrentApplication.ApplicationEnvironmentVariables.Get<string>("PluginFolder")}/{plugin.PluginName}.dll", plugin.PluginLink } };
foreach(var dependency in dependencies)
{
installProgress?.Report(currentProgress + stepProgress * p);
});
await ServerCom.DownloadFileAsync(pluginData.DownLoadLink, $"{Application.CurrentApplication.ApplicationEnvironmentVariables.Get<string>("PluginFolder")}/{pluginData.Name}.dll", progress);
if (pluginData.HasFileDependencies)
foreach (var dependency in pluginData.Dependencies)
{
string dependencyLocation = GenerateDependencyRelativePath(pluginData.Name, dependency.DownloadLocation);
await ServerCom.DownloadFileAsync(dependency.DownloadLink, dependencyLocation, progress);
currentProgress += stepProgress;
}
if (pluginData.HasScriptDependencies)
{
foreach (var scriptDependency in pluginData.ScriptDependencies)
{
string? console = Application.CurrentApplication.ApplicationEnvironmentVariables.Get<string>("console.terminal");
if (string.IsNullOrEmpty(console))
{
return;
}
string? cmdPrefix = Application.CurrentApplication.ApplicationEnvironmentVariables.Get<string>("console.cmd_prefix");
if (string.IsNullOrEmpty(cmdPrefix))
{
return;
}
string arguments = $"{cmdPrefix}{scriptDependency.ScriptContent}";
await ServerCom.RunConsoleCommand(console, arguments);
}
string dependencyLocation = GenerateDependencyRelativePath(plugin.PluginName, dependency.DownloadLocation);
downloads.Add(dependencyLocation, dependency.DownloadLink);
}
PluginInfo pluginInfo = PluginInfo.FromOnlineInfo(pluginData);
await AppendPluginToDatabase(pluginInfo);
return (downloads, dependencies).ToTuple();
}
public async Task SetEnabledStatus(string pluginName, bool status)

View File

@@ -1,48 +1,17 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using DiscordBotCore.Online.Helpers;
using DiscordBotCore.Others;
namespace DiscordBotCore.Online;
public static class ServerCom
{
/// <summary>
/// Read all lines from a file async
/// </summary>
/// <param name="link">The link of the file</param>
/// <returns></returns>
public static async Task<List<string>> ReadTextFromURL(string link)
{
var response = await OnlineFunctions.DownloadStringAsync(link);
var lines = response.Split('\n');
return lines.ToList();
}
/// <summary>
/// Get all text from a file async
/// </summary>
/// <param name="link">The link of the file</param>
/// <returns></returns>
public static async Task<string> GetAllTextFromUrl(string link)
{
var response = await OnlineFunctions.DownloadStringAsync(link);
return response;
}
/// <summary>
/// Download file from url
/// </summary>
/// <param name="URL">The url to the file</param>
/// <param name="location">The location where to store the downloaded data</param>
/// <param name="progress">The <see cref="IProgress{T}" /> to track the download</param>
/// <returns></returns>
public static async Task DownloadFileAsync(
private static async Task DownloadFileAsync(
string URL, string location, IProgress<float>? progress,
IProgress<long>? downloadedBytes)
{
@@ -60,34 +29,8 @@ public static class ServerCom
}
}
public static async Task DownloadFileAsync(string URl, string location, IProgress<float> progress)
public static async Task DownloadFileAsync(string url, string location, IProgress<float> progress)
{
await DownloadFileAsync(URl, location, progress, null);
await DownloadFileAsync(url, location, progress, null);
}
public static async Task DownloadFileAsync(string url, string location)
{
await DownloadFileAsync(url, location, null, null);
}
public static Task CreateDownloadTask(string URl, string location)
{
return DownloadFileAsync(URl, location, null, null);
}
public static Task CreateDownloadTask(string URl, string location, IProgress<float> progress)
{
return DownloadFileAsync(URl, location, progress, null);
}
public static async Task RunConsoleCommand(string console, string command)
{
Process process = new();
process.StartInfo.FileName = console;
process.StartInfo.Arguments = command;
process.Start();
await process.WaitForExitAsync();
}
}