Updated plugin installation
This commit is contained in:
@@ -9,8 +9,9 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using DiscordBotCore.Others.Exceptions;
|
||||
|
||||
|
||||
|
||||
namespace DiscordBotCore
|
||||
{
|
||||
/// <summary>
|
||||
@@ -120,5 +121,19 @@ namespace DiscordBotCore
|
||||
}
|
||||
|
||||
public static string GetPluginFullPath() => _PluginsFolder;
|
||||
|
||||
public static async Task<string> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,8 +13,9 @@ namespace DiscordBotCore.Interfaces.PluginManager
|
||||
Task AppendPluginToDatabase(PluginInfo pluginData);
|
||||
Task CheckForUpdates();
|
||||
Task ExecutePluginInstallScripts(List<OnlineScriptDependencyInfo> listOfDependencies);
|
||||
string GenerateDependencyLocation(string pluginName, string dependencyName);
|
||||
string GenerateDependencyRelativePath(string pluginName, string dependencyPath);
|
||||
Task<string?> GetDependencyLocation(string dependencyName);
|
||||
Task<string?> GetDependencyLocation(string pluginName, string dependencyName);
|
||||
Task<List<PluginInfo>> GetInstalledPlugins();
|
||||
Task<PluginOnlineInfo?> GetPluginDataByName(string pluginName);
|
||||
Task<List<PluginOnlineInfo>?> GetPluginsList();
|
||||
|
||||
@@ -90,7 +90,7 @@ public class PluginManager : IPluginManager
|
||||
List<PluginInfo> installedPlugins = await JsonManager.ConvertFromJson<List<PluginInfo>>(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<string?> GetDependencyLocation(string dependencyName, string pluginName)
|
||||
{
|
||||
List<PluginInfo> 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<float>? 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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user