Updated plugin installation and plugin loading

This commit is contained in:
2025-04-06 20:47:00 +03:00
parent 87c889266b
commit 4ab8438a7c
14 changed files with 122 additions and 67 deletions

View File

@@ -45,7 +45,7 @@ public class PluginRepository : IPluginRepository
public async Task<OnlinePlugin?> GetPluginById(int pluginId)
{
string url = CreateUrlWithQueryParams(_pluginRepositoryConfiguration.PluginRepositoryLocation,
"get-plugin", new Dictionary<string, string>
"get-by-id", new Dictionary<string, string>
{
{ "pluginId", pluginId.ToString() },
{ "includeNotApproved", "false" }
@@ -66,7 +66,7 @@ public class PluginRepository : IPluginRepository
public async Task<OnlinePlugin?> GetPluginByName(string pluginName, int operatingSystem, bool includeNotApproved)
{
string url = CreateUrlWithQueryParams(_pluginRepositoryConfiguration.PluginRepositoryLocation,
"get-plugin-by-name", new Dictionary<string, string>
"get-by-name", new Dictionary<string, string>
{
{ "pluginName", pluginName },
{ "operatingSystem", operatingSystem.ToString() },
@@ -89,7 +89,7 @@ public class PluginRepository : IPluginRepository
public async Task<List<OnlineDependencyInfo>> GetDependenciesForPlugin(int pluginId)
{
string url = CreateUrlWithQueryParams(_pluginRepositoryConfiguration.DependenciesRepositoryLocation,
"get-dependencies-for-plugin", new Dictionary<string, string>
"get-by-plugin-id", new Dictionary<string, string>
{
{ "pluginId", pluginId.ToString() }
});

View File

@@ -6,6 +6,7 @@ public interface IPluginManager
{
Task<List<OnlinePlugin>> GetPluginsList();
Task<OnlinePlugin?> GetPluginDataByName(string pluginName);
Task<OnlinePlugin?> GetPluginDataById(int pluginId);
Task AppendPluginToDatabase(LocalPlugin pluginData);
Task<List<LocalPlugin>> GetInstalledPlugins();
Task<bool> IsPluginInstalled(string pluginName);
@@ -14,7 +15,7 @@ public interface IPluginManager
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 InstallPlugin(OnlinePlugin plugin, IProgress<float> progress);
Task<Tuple<Dictionary<string, string>, List<OnlineDependencyInfo>>> GatherInstallDataForPlugin(OnlinePlugin plugin);
Task SetEnabledStatus(string pluginName, bool status);
}

View File

@@ -1,16 +0,0 @@
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

@@ -50,6 +50,18 @@ public sealed class PluginManager : IPluginManager
return plugin;
}
public async Task<OnlinePlugin?> GetPluginDataById(int pluginId)
{
var plugin = await _PluginRepository.GetPluginById(pluginId);
if (plugin is null)
{
_Logger.Log($"Plugin {pluginId} not found in the repository.", this, LogType.Warning);
return null;
}
return plugin;
}
private async Task RemovePluginFromDatabase(string pluginName)
{
string? pluginDatabaseFile = _Configuration.Get<string>("PluginDatabase");
@@ -72,8 +84,9 @@ public sealed class PluginManager : IPluginManager
{
throw new Exception("Plugin database file not found");
}
List<LocalPlugin> installedPlugins = await GetInstalledPlugins();
List<LocalPlugin> installedPlugins = await JsonManager.ConvertFromJson<List<LocalPlugin>>(await File.ReadAllTextAsync(pluginDatabaseFile));
foreach (var dependency in pluginData.ListOfExecutableDependencies)
{
pluginData.ListOfExecutableDependencies[dependency.Key] = dependency.Value;
@@ -194,7 +207,7 @@ public sealed class PluginManager : IPluginManager
return relative;
}
public async Task InstallPlugin(OnlinePlugin plugin, IProgress<InstallationProgressIndicator> progress)
public async Task InstallPlugin(OnlinePlugin plugin, IProgress<float> progress)
{
List<OnlineDependencyInfo> dependencies = await _PluginRepository.GetDependenciesForPlugin(plugin.Id);
string? pluginsFolder = _Configuration.Get<string>("PluginFolder");
@@ -205,13 +218,7 @@ public sealed class PluginManager : IPluginManager
string downloadLocation = $"{pluginsFolder}/{plugin.Name}.dll";
InstallationProgressIndicator installationProgressIndicator = new InstallationProgressIndicator();
IProgress<float> downloadProgress = new Progress<float>(fileProgress =>
{
installationProgressIndicator.SetProgress(plugin.Name, fileProgress);
progress.Report(installationProgressIndicator);
});
IProgress<float> downloadProgress = new Progress<float>(progress.Report);
FileDownloader fileDownloader = new FileDownloader(plugin.DownloadLink, downloadLocation);
await fileDownloader.DownloadFile(downloadProgress.Report);
@@ -221,16 +228,14 @@ public sealed class PluginManager : IPluginManager
foreach (var dependency in dependencies)
{
string dependencyLocation = GenerateDependencyRelativePath(plugin.Name, dependency.DownloadLocation);
Action<float> dependencyProgress = new Action<float>(fileProgress =>
{
installationProgressIndicator.SetProgress(dependency.DependencyName, fileProgress);
progress.Report(installationProgressIndicator);
});
executor.AddTask(dependency.DownloadLink, dependencyLocation, dependencyProgress);
executor.AddTask(dependency.DownloadLink, dependencyLocation, progress.Report);
}
await executor.ExecuteAllTasks();
LocalPlugin localPlugin = LocalPlugin.FromOnlineInfo(plugin, dependencies, downloadLocation);
await AppendPluginToDatabase(localPlugin);
}
public async Task<Tuple<Dictionary<string, string>, List<OnlineDependencyInfo>>> GatherInstallDataForPlugin(OnlinePlugin plugin)