From 2052eb634ae7527b9e8481ce877c523c3fae3be5 Mon Sep 17 00:00:00 2001 From: Andrei Tudor Date: Sat, 6 Jul 2024 18:42:45 +0300 Subject: [PATCH] Updated plugin installation --- DiscordBot/Bot/Actions/Extra/PluginMethods.cs | 2 +- DiscordBot/Entry.cs | 3 +- DiscordBotCore/Application.cs | 17 +++++++++- .../PluginManager/IPluginManager.cs | 3 +- DiscordBotCore/Online/PluginManager.cs | 33 +++++++++++++++---- .../Exceptions/DependencyNotFoundException.cs | 18 ++++++++++ 6 files changed, 66 insertions(+), 10 deletions(-) create mode 100644 DiscordBotCore/Others/Exceptions/DependencyNotFoundException.cs diff --git a/DiscordBot/Bot/Actions/Extra/PluginMethods.cs b/DiscordBot/Bot/Actions/Extra/PluginMethods.cs index 620e1f5..1f3623a 100644 --- a/DiscordBot/Bot/Actions/Extra/PluginMethods.cs +++ b/DiscordBot/Bot/Actions/Extra/PluginMethods.cs @@ -192,7 +192,7 @@ internal static class PluginMethods await Parallel.ForEachAsync(downloadTasks, options, async (tuple, token) => { tuple.Item1.IsIndeterminate = false; - string downloadLocation = Application.CurrentApplication.PluginManager.GenerateDependencyLocation(pluginName, tuple.Item4); + string downloadLocation = Application.CurrentApplication.PluginManager.GenerateDependencyRelativePath(pluginName, tuple.Item4); await ServerCom.DownloadFileAsync(tuple.Item3, downloadLocation, tuple.Item2); } ); diff --git a/DiscordBot/Entry.cs b/DiscordBot/Entry.cs index caacfcf..ab1cb45 100644 --- a/DiscordBot/Entry.cs +++ b/DiscordBot/Entry.cs @@ -6,6 +6,7 @@ using System.Reflection; namespace DiscordBot; + public static class Entry { /// @@ -58,7 +59,7 @@ public static class Entry static Assembly LoadFromSameFolder(object sender, ResolveEventArgs args) { string requestingAssembly = args.RequestingAssembly?.GetName().Name; - var folderPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, $"Libraries\\{requestingAssembly}"); + var folderPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!, $"Libraries/{requestingAssembly}"); var assemblyPath = Path.Combine(folderPath, new AssemblyName(args.Name).Name + ".dll"); if (!File.Exists(assemblyPath)) return null; diff --git a/DiscordBotCore/Application.cs b/DiscordBotCore/Application.cs index 0c81172..bb549ed 100644 --- a/DiscordBotCore/Application.cs +++ b/DiscordBotCore/Application.cs @@ -9,8 +9,9 @@ using System; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; +using DiscordBotCore.Others.Exceptions; + - namespace DiscordBotCore { /// @@ -120,5 +121,19 @@ namespace DiscordBotCore } public static string GetPluginFullPath() => _PluginsFolder; + + public static async Task GetPluginDependencyPath(string dependencyName, string? pluginName = null) + { + string? dependencyLocation; + if(pluginName is null) + dependencyLocation = await Application.CurrentApplication.PluginManager.GetDependencyLocation(dependencyName); + else + dependencyLocation = await Application.CurrentApplication.PluginManager.GetDependencyLocation(dependencyName, pluginName); + + if(dependencyLocation is null) + throw new DependencyNotFoundException($"Dependency {dependencyName} not found", pluginName); + + return dependencyLocation; + } } } diff --git a/DiscordBotCore/Interfaces/PluginManager/IPluginManager.cs b/DiscordBotCore/Interfaces/PluginManager/IPluginManager.cs index 371c217..31e30d0 100644 --- a/DiscordBotCore/Interfaces/PluginManager/IPluginManager.cs +++ b/DiscordBotCore/Interfaces/PluginManager/IPluginManager.cs @@ -13,8 +13,9 @@ namespace DiscordBotCore.Interfaces.PluginManager Task AppendPluginToDatabase(PluginInfo pluginData); Task CheckForUpdates(); Task ExecutePluginInstallScripts(List listOfDependencies); - string GenerateDependencyLocation(string pluginName, string dependencyName); + string GenerateDependencyRelativePath(string pluginName, string dependencyPath); Task GetDependencyLocation(string dependencyName); + Task GetDependencyLocation(string pluginName, string dependencyName); Task> GetInstalledPlugins(); Task GetPluginDataByName(string pluginName); Task?> GetPluginsList(); diff --git a/DiscordBotCore/Online/PluginManager.cs b/DiscordBotCore/Online/PluginManager.cs index f68b9c8..3fbe918 100644 --- a/DiscordBotCore/Online/PluginManager.cs +++ b/DiscordBotCore/Online/PluginManager.cs @@ -90,7 +90,7 @@ public class PluginManager : IPluginManager List installedPlugins = await JsonManager.ConvertFromJson>(await File.ReadAllTextAsync(Application.CurrentApplication.PluginDatabase)); foreach (var dependency in pluginData.ListOfExecutableDependencies) { - pluginData.ListOfExecutableDependencies[dependency.Key] = GenerateDependencyLocation(pluginData.PluginName, dependency.Value); + pluginData.ListOfExecutableDependencies[dependency.Key] = dependency.Value; } installedPlugins.Add(pluginData); @@ -175,16 +175,37 @@ public class PluginManager : IPluginManager foreach (var plugin in installedPlugins) { - if (plugin.ListOfExecutableDependencies.ContainsKey(dependencyName)) - return plugin.ListOfExecutableDependencies[dependencyName]; + if (plugin.ListOfExecutableDependencies.TryGetValue(dependencyName, out var dependencyPath)) + { + string relativePath = GenerateDependencyRelativePath(plugin.PluginName, dependencyPath); + return relativePath; + } + } + + return null; + } + + public async Task GetDependencyLocation(string dependencyName, string pluginName) + { + List 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 GenerateDependencyLocation(string pluginName, string dependencyName) + public string GenerateDependencyRelativePath(string pluginName, string dependencyPath) { - return Path.Combine(Environment.CurrentDirectory, $"Libraries/{pluginName}/{dependencyName}"); + string relative = $"./Libraries/{pluginName}/{dependencyPath}"; + return relative; } public async Task InstallPlugin(PluginOnlineInfo pluginData, IProgress? installProgress) @@ -208,7 +229,7 @@ public class PluginManager : IPluginManager if (pluginData.HasFileDependencies) foreach (var dependency in pluginData.Dependencies) { - string dependencyLocation = GenerateDependencyLocation(pluginData.Name, dependency.DownloadLocation); + string dependencyLocation = GenerateDependencyRelativePath(pluginData.Name, dependency.DownloadLocation); await ServerCom.DownloadFileAsync(dependency.DownloadLink, dependencyLocation, progress); currentProgress += stepProgress; diff --git a/DiscordBotCore/Others/Exceptions/DependencyNotFoundException.cs b/DiscordBotCore/Others/Exceptions/DependencyNotFoundException.cs new file mode 100644 index 0000000..32fd5d4 --- /dev/null +++ b/DiscordBotCore/Others/Exceptions/DependencyNotFoundException.cs @@ -0,0 +1,18 @@ +using System; + +namespace DiscordBotCore.Others.Exceptions; + +public class DependencyNotFoundException : Exception +{ + private string PluginName { get; set; } + public DependencyNotFoundException(string message): base(message) + { + + } + + public DependencyNotFoundException(string message, string pluginName): base(message) + { + this.PluginName = pluginName; + } + +}