Redesigned the DiscordBotCore by splitting it into multiple projects. Created a WebUI and preparing to remove the DiscordBot application

This commit is contained in:
2025-04-04 22:07:30 +03:00
parent 62ba5ec63d
commit a4afb28f36
2290 changed files with 76694 additions and 17052 deletions

View File

@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="2.3.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DiscordBotCore.Configuration\DiscordBotCore.Configuration.csproj" />
<ProjectReference Include="..\DiscordBotCore.Logging\DiscordBotCore.Logging.csproj" />
<ProjectReference Include="..\DiscordBotCore.Networking\DiscordBotCore.Networking.csproj" />
<ProjectReference Include="..\DiscordBotCore.Utilities\DiscordBotCore.Utilities.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,14 @@
using DiscordBotCore.PluginManagement.Models;
namespace DiscordBotCore.PluginManagement.Helpers;
public interface IPluginRepository
{
public Task<List<OnlinePlugin>> GetAllPlugins(int operatingSystem, bool includeNotApproved);
public Task<OnlinePlugin?> GetPluginById(int pluginId);
public Task<OnlinePlugin?> GetPluginByName(string pluginName, int operatingSystem, bool includeNotApproved);
public Task<List<OnlineDependencyInfo>> GetDependenciesForPlugin(int pluginId);
}

View File

@@ -0,0 +1,9 @@
namespace DiscordBotCore.PluginManagement.Helpers;
public interface IPluginRepositoryConfiguration
{
public string BaseUrl { get; }
public string PluginRepositoryLocation { get; }
public string DependenciesRepositoryLocation { get; }
}

View File

@@ -0,0 +1,123 @@
using System.Net.Mime;
using DiscordBotCore.Logging;
using DiscordBotCore.PluginManagement.Models;
using DiscordBotCore.Utilities;
using Microsoft.AspNetCore.Http.Extensions;
namespace DiscordBotCore.PluginManagement.Helpers;
public class PluginRepository : IPluginRepository
{
private readonly IPluginRepositoryConfiguration _pluginRepositoryConfiguration;
private readonly HttpClient _httpClient;
private readonly ILogger _logger;
public PluginRepository(IPluginRepositoryConfiguration pluginRepositoryConfiguration, ILogger logger)
{
_pluginRepositoryConfiguration = pluginRepositoryConfiguration;
_httpClient = new HttpClient();
_httpClient.BaseAddress = new Uri(_pluginRepositoryConfiguration.BaseUrl);
_logger = logger;
}
public async Task<List<OnlinePlugin>> GetAllPlugins(int operatingSystem, bool includeNotApproved)
{
string url = CreateUrlWithQueryParams(_pluginRepositoryConfiguration.PluginRepositoryLocation,
"get-all-plugins", new Dictionary<string, string>
{
{ "operatingSystem", operatingSystem.ToString() },
{ "includeNotApproved", includeNotApproved.ToString() }
});
HttpResponseMessage response = await _httpClient.GetAsync(url);
if (!response.IsSuccessStatusCode)
{
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)
{
string url = CreateUrlWithQueryParams(_pluginRepositoryConfiguration.PluginRepositoryLocation,
"get-plugin", new Dictionary<string, string>
{
{ "pluginId", pluginId.ToString() },
{ "includeNotApproved", "false" }
});
HttpResponseMessage response = await _httpClient.GetAsync(url);
if (!response.IsSuccessStatusCode)
{
return null;
}
string content = await response.Content.ReadAsStringAsync();
OnlinePlugin plugin = await JsonManager.ConvertFromJson<OnlinePlugin>(content);
return plugin;
}
public async Task<OnlinePlugin?> GetPluginByName(string pluginName, int operatingSystem, bool includeNotApproved)
{
string url = CreateUrlWithQueryParams(_pluginRepositoryConfiguration.PluginRepositoryLocation,
"get-plugin-by-name", new Dictionary<string, string>
{
{ "pluginName", pluginName },
{ "operatingSystem", operatingSystem.ToString() },
{ "includeNotApproved", includeNotApproved.ToString() }
});
HttpResponseMessage response = await _httpClient.GetAsync(url);
if (!response.IsSuccessStatusCode)
{
_logger.Log($"Plugin {pluginName} not found");
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)
{
string url = CreateUrlWithQueryParams(_pluginRepositoryConfiguration.DependenciesRepositoryLocation,
"get-dependencies-for-plugin", new Dictionary<string, string>
{
{ "pluginId", pluginId.ToString() }
});
HttpResponseMessage response = await _httpClient.GetAsync(url);
if(!response.IsSuccessStatusCode)
{
_logger.Log($"Failed to get dependencies for plugin with ID {pluginId}");
return [];
}
string content = await response.Content.ReadAsStringAsync();
List<OnlineDependencyInfo> dependencies = await JsonManager.ConvertFromJson<List<OnlineDependencyInfo>>(content);
return dependencies;
}
private string CreateUrlWithQueryParams(string baseUrl, string endpoint, Dictionary<string, string> queryParams)
{
QueryBuilder queryBuilder = new QueryBuilder();
foreach (var(key,value) in queryParams)
{
queryBuilder.Add(key, value);
}
string queryString = queryBuilder.ToQueryString().ToString();
string url = baseUrl + endpoint + queryString;
return url;
}
}

View File

@@ -0,0 +1,22 @@
using System.Text.Json.Serialization;
namespace DiscordBotCore.PluginManagement.Helpers;
public class PluginRepositoryConfiguration : IPluginRepositoryConfiguration
{
public static PluginRepositoryConfiguration Default => new ("http://localhost:8080/api/v1/",
"plugin/",
"dependency/");
public string BaseUrl { get; }
public string PluginRepositoryLocation { get; }
public string DependenciesRepositoryLocation { get; }
[JsonConstructor]
public PluginRepositoryConfiguration(string baseUrl, string pluginRepositoryLocation, string dependenciesRepositoryLocation)
{
BaseUrl = baseUrl;
PluginRepositoryLocation = pluginRepositoryLocation;
DependenciesRepositoryLocation = dependenciesRepositoryLocation;
}
}

View File

@@ -0,0 +1,20 @@
using DiscordBotCore.PluginManagement.Models;
namespace DiscordBotCore.PluginManagement;
public interface IPluginManager
{
Task<List<OnlinePlugin>> GetPluginsList();
Task<OnlinePlugin?> GetPluginDataByName(string pluginName);
Task AppendPluginToDatabase(LocalPlugin pluginData);
Task<List<LocalPlugin>> GetInstalledPlugins();
Task<bool> IsPluginInstalled(string pluginName);
Task<bool> MarkPluginToUninstall(string pluginName);
Task UninstallMarkedPlugins();
Task<string?> GetDependencyLocation(string dependencyName);
Task<string?> GetDependencyLocation(string dependencyName, string pluginName);
string GenerateDependencyRelativePath(string pluginName, string dependencyPath);
Task InstallPlugin(OnlinePlugin plugin, IProgress<InstallationProgressIndicator> progress);
Task<Tuple<Dictionary<string, string>, List<OnlineDependencyInfo>>> GatherInstallDataForPlugin(OnlinePlugin plugin);
Task SetEnabledStatus(string pluginName, bool status);
}

View File

@@ -0,0 +1,16 @@
namespace DiscordBotCore.PluginManagement.Models;
public class InstallationProgressIndicator
{
private readonly Dictionary<string, float> _DownloadProgress;
public InstallationProgressIndicator()
{
_DownloadProgress = new Dictionary<string, float>();
}
public void SetProgress(string fileName, float progress)
{
_DownloadProgress[fileName] = progress;
}
}

View File

@@ -0,0 +1,49 @@
using System.Text.Json.Serialization;
namespace DiscordBotCore.PluginManagement.Models;
public class LocalPlugin
{
public string PluginName { get; private set; }
public string PluginVersion { get; private set; }
public string FilePath { get; private set; }
public Dictionary<string, string> ListOfExecutableDependencies {get; private set;}
public bool IsMarkedToUninstall {get; internal set;}
public bool IsOfflineAdded { get; internal set; }
public bool IsEnabled { get; internal set; }
[JsonConstructor]
public LocalPlugin(string pluginName, string pluginVersion, string filePath, Dictionary<string, string> listOfExecutableDependencies, bool isMarkedToUninstall, bool isOfflineAdded, bool isEnabled)
{
PluginName = pluginName;
PluginVersion = pluginVersion;
ListOfExecutableDependencies = listOfExecutableDependencies;
IsMarkedToUninstall = isMarkedToUninstall;
FilePath = filePath;
IsOfflineAdded = isOfflineAdded;
IsEnabled = isEnabled;
}
private LocalPlugin(string pluginName, string pluginVersion, string filePath,
Dictionary<string, string> listOfExecutableDependencies)
{
PluginName = pluginName;
PluginVersion = pluginVersion;
ListOfExecutableDependencies = listOfExecutableDependencies;
IsMarkedToUninstall = false;
FilePath = filePath;
IsOfflineAdded = false;
IsEnabled = true;
}
public static LocalPlugin FromOnlineInfo(OnlinePlugin plugin, List<OnlineDependencyInfo> dependencies, string downloadLocation)
{
LocalPlugin localPlugin = new LocalPlugin(
plugin.PluginName, plugin.LatestVersion, downloadLocation,
dependencies.Where(dependency => dependency.IsExecutable)
.ToDictionary(dependency => dependency.DependencyName, dependency => dependency.DownloadLocation)
);
return localPlugin;
}
}

View File

@@ -0,0 +1,23 @@
using System.Text.Json.Serialization;
namespace DiscordBotCore.PluginManagement.Models;
public class OnlineDependencyInfo
{
public string DependencyName { get; private set; }
[JsonPropertyName("dependencyLink")]
public string DownloadLink { get; private set; }
[JsonPropertyName("dependencyLocation")]
public string DownloadLocation { get; private set; }
public bool IsExecutable { get; private set; }
[JsonConstructor]
public OnlineDependencyInfo(string dependencyName, string downloadLink, string downloadLocation, bool isExecutable)
{
DependencyName = dependencyName;
DownloadLink = downloadLink;
DownloadLocation = downloadLocation;
IsExecutable = isExecutable;
}
}

View File

@@ -0,0 +1,27 @@
using System.Text.Json.Serialization;
namespace DiscordBotCore.PluginManagement.Models;
public class OnlinePlugin
{
public int PluginId { get; private set; }
public string PluginName { get; private set; }
public string PluginDescription { get; private set; }
public string LatestVersion { get; private set; }
public string PluginAuthor { get; private set; }
public string PluginLink { get; private set; }
public int OperatingSystem { get; private set; }
[JsonConstructor]
public OnlinePlugin(int pluginId, string pluginName, string pluginDescription, string latestVersion,
string pluginAuthor, string pluginLink, int operatingSystem)
{
PluginId = pluginId;
PluginName = pluginName;
PluginDescription = pluginDescription;
LatestVersion = latestVersion;
PluginAuthor = pluginAuthor;
PluginLink = pluginLink;
OperatingSystem = operatingSystem;
}
}

View File

@@ -0,0 +1,262 @@
using DiscordBotCore.Logging;
using DiscordBotCore.Networking;
using DiscordBotCore.PluginManagement.Helpers;
using DiscordBotCore.PluginManagement.Models;
using DiscordBotCore.Utilities;
using DiscordBotCore.Configuration;
using OperatingSystem = DiscordBotCore.Utilities.OperatingSystem;
namespace DiscordBotCore.PluginManagement;
public sealed class PluginManager : IPluginManager
{
private static readonly string _LibrariesBaseFolder = "Libraries";
private readonly IPluginRepository _PluginRepository;
private readonly ILogger _Logger;
private readonly IConfiguration _Configuration;
public PluginManager(IPluginRepository pluginRepository, ILogger logger, IConfiguration configuration)
{
_PluginRepository = pluginRepository;
_Logger = logger;
_Configuration = configuration;
}
public async Task<List<OnlinePlugin>> GetPluginsList()
{
int os = OperatingSystem.GetOperatingSystemInt();
var onlinePlugins = await _PluginRepository.GetAllPlugins(os, false);
if (!onlinePlugins.Any())
{
_Logger.Log($"No plugins found for operatingSystem: {OperatingSystem.GetOperatingSystemString((OperatingSystem.OperatingSystemEnum)os)}", LogType.Warning);
return [];
}
return onlinePlugins;
}
public async Task<OnlinePlugin?> GetPluginDataByName(string pluginName)
{
int os = OperatingSystem.GetOperatingSystemInt();
var plugin = await _PluginRepository.GetPluginByName(pluginName, os, false);
if (plugin == null)
{
_Logger.Log($"Plugin {pluginName} not found in the repository for operating system {OperatingSystem.GetOperatingSystemString((OperatingSystem.OperatingSystemEnum)os)}.", LogType.Warning);
return null;
}
return plugin;
}
private async Task RemovePluginFromDatabase(string pluginName)
{
string? pluginDatabaseFile = _Configuration.Get<string>("PluginDatabase");
if (pluginDatabaseFile is null)
{
throw new Exception("Plugin database file not found");
}
List<LocalPlugin> installedPlugins = await JsonManager.ConvertFromJson<List<LocalPlugin>>(await File.ReadAllTextAsync(pluginDatabaseFile));
installedPlugins.RemoveAll(p => p.PluginName == pluginName);
await JsonManager.SaveToJsonFile(pluginDatabaseFile, installedPlugins);
}
public async Task AppendPluginToDatabase(LocalPlugin pluginData)
{
string? pluginDatabaseFile = _Configuration.Get<string>("PluginDatabase");
if (pluginDatabaseFile is null)
{
throw new Exception("Plugin database file not found");
}
List<LocalPlugin> installedPlugins = await JsonManager.ConvertFromJson<List<LocalPlugin>>(await File.ReadAllTextAsync(pluginDatabaseFile));
foreach (var dependency in pluginData.ListOfExecutableDependencies)
{
pluginData.ListOfExecutableDependencies[dependency.Key] = dependency.Value;
}
installedPlugins.Add(pluginData);
await JsonManager.SaveToJsonFile(pluginDatabaseFile, installedPlugins);
}
public async Task<List<LocalPlugin>> GetInstalledPlugins()
{
string? pluginDatabaseFile = _Configuration.Get<string>("PluginDatabase");
if (pluginDatabaseFile is null)
{
throw new Exception("Plugin database file not found");
}
return await JsonManager.ConvertFromJson<List<LocalPlugin>>(await File.ReadAllTextAsync(pluginDatabaseFile));
}
public async Task<bool> IsPluginInstalled(string pluginName)
{
string? pluginDatabaseFile = _Configuration.Get<string>("PluginDatabase");
if (pluginDatabaseFile is null)
{
throw new Exception("Plugin database file not found");
}
List<LocalPlugin> installedPlugins = await JsonManager.ConvertFromJson<List<LocalPlugin>>(await File.ReadAllTextAsync(pluginDatabaseFile));
return installedPlugins.Any(plugin => plugin.PluginName == pluginName);
}
public async Task<bool> MarkPluginToUninstall(string pluginName)
{
List<LocalPlugin> installedPlugins = await GetInstalledPlugins();
List<LocalPlugin> info = installedPlugins.Where(info => info.PluginName == pluginName).ToList();
if (!info.Any())
return false;
foreach (var item in info)
{
await RemovePluginFromDatabase(item.PluginName);
item.IsMarkedToUninstall = true;
await AppendPluginToDatabase(item);
}
return true;
}
public async Task UninstallMarkedPlugins()
{
IEnumerable<LocalPlugin> installedPlugins = (await GetInstalledPlugins()).AsEnumerable();
IEnumerable<LocalPlugin> pluginsToRemove = installedPlugins.Where(plugin => plugin.IsMarkedToUninstall).AsEnumerable();
foreach (var plugin in pluginsToRemove)
{
await UninstallPlugin(plugin);
}
}
private async Task UninstallPlugin(LocalPlugin LocalPlugin)
{
File.Delete(LocalPlugin.FilePath);
foreach (var dependency in LocalPlugin.ListOfExecutableDependencies)
File.Delete(dependency.Value);
await RemovePluginFromDatabase(LocalPlugin.PluginName);
if (Directory.Exists($"{_LibrariesBaseFolder}/{LocalPlugin.PluginName}"))
Directory.Delete($"{_LibrariesBaseFolder}/{LocalPlugin.PluginName}", true);
}
public async Task<string?> GetDependencyLocation(string dependencyName)
{
List<LocalPlugin> installedPlugins = await GetInstalledPlugins();
foreach (var plugin in installedPlugins)
{
if (plugin.ListOfExecutableDependencies.TryGetValue(dependencyName, out var dependencyPath))
{
string relativePath = GenerateDependencyRelativePath(plugin.PluginName, dependencyPath);
return relativePath;
}
}
return null;
}
public async Task<string?> GetDependencyLocation(string dependencyName, string pluginName)
{
List<LocalPlugin> installedPlugins = await GetInstalledPlugins();
foreach (var plugin in installedPlugins)
{
if (plugin.PluginName == pluginName && plugin.ListOfExecutableDependencies.ContainsKey(dependencyName))
{
string dependencyPath = plugin.ListOfExecutableDependencies[dependencyName];
string relativePath = GenerateDependencyRelativePath(pluginName, dependencyPath);
return relativePath;
}
}
return null;
}
public string GenerateDependencyRelativePath(string pluginName, string dependencyPath)
{
string relative = $"./{_LibrariesBaseFolder}/{pluginName}/{dependencyPath}";
return relative;
}
public async Task InstallPlugin(OnlinePlugin plugin, IProgress<InstallationProgressIndicator> progress)
{
List<OnlineDependencyInfo> dependencies = await _PluginRepository.GetDependenciesForPlugin(plugin.PluginId);
string? pluginsFolder = _Configuration.Get<string>("PluginFolder");
if (pluginsFolder is null)
{
throw new Exception("Plugin folder not found");
}
string downloadLocation = $"{pluginsFolder}/{plugin.PluginName}.dll";
InstallationProgressIndicator installationProgressIndicator = new InstallationProgressIndicator();
IProgress<float> downloadProgress = new Progress<float>(fileProgress =>
{
installationProgressIndicator.SetProgress(plugin.PluginName, fileProgress);
progress.Report(installationProgressIndicator);
});
FileDownloader fileDownloader = new FileDownloader(plugin.PluginLink, downloadLocation);
await fileDownloader.DownloadFile(downloadProgress.Report);
ParallelDownloadExecutor executor = new ParallelDownloadExecutor();
foreach (var dependency in dependencies)
{
string dependencyLocation = GenerateDependencyRelativePath(plugin.PluginName, dependency.DownloadLocation);
Action<float> dependencyProgress = new Action<float>(fileProgress =>
{
installationProgressIndicator.SetProgress(dependency.DependencyName, fileProgress);
progress.Report(installationProgressIndicator);
});
executor.AddTask(dependency.DownloadLink, dependencyLocation, dependencyProgress);
}
await executor.ExecuteAllTasks();
}
public async Task<Tuple<Dictionary<string, string>, List<OnlineDependencyInfo>>> GatherInstallDataForPlugin(OnlinePlugin plugin)
{
List<OnlineDependencyInfo> dependencies = await _PluginRepository.GetDependenciesForPlugin(plugin.PluginId);
string? pluginsFolder = _Configuration.Get<string>("PluginFolder");
if (pluginsFolder is null)
{
throw new Exception("Plugin folder not found");
}
string downloadLocation = $"{pluginsFolder}/{plugin.PluginName}.dll";
var downloads = new Dictionary<string, string> { { downloadLocation, plugin.PluginLink } };
foreach(var dependency in dependencies)
{
string dependencyLocation = GenerateDependencyRelativePath(plugin.PluginName, dependency.DownloadLocation);
downloads.Add(dependencyLocation, dependency.DownloadLink);
}
return (downloads, dependencies).ToTuple();
}
public async Task SetEnabledStatus(string pluginName, bool status)
{
var plugins = await GetInstalledPlugins();
var plugin = plugins.Find(p => p.PluginName == pluginName);
if (plugin == null)
return;
plugin.IsEnabled = status;
await RemovePluginFromDatabase(pluginName);
await AppendPluginToDatabase(plugin);
}
}