Updated Dependency to accept DependencyName as parameter

This commit is contained in:
2024-06-07 21:01:10 +03:00
parent 1a5f0cbede
commit 9a8ddb5388
14 changed files with 152 additions and 83 deletions
@@ -19,4 +19,6 @@ public interface ICommandAction
public InternalActionRunType RunType { get; }
public Task Execute(string[]? args);
public void ExecuteStartup() { }
}
+2 -2
View File
@@ -56,8 +56,8 @@ public class ActionsLoader
continue;
}
if (action.RunType == InternalActionRunType.ON_STARTUP)
await action.Execute(null);
if (action.RunType != InternalActionRunType.ON_CALL)
action.ExecuteStartup();
ActionLoadedEvent?.Invoke(action.ActionName, type.Name, true);
actions.Add(action);
+47 -10
View File
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using DiscordBotCore.Others;
using DiscordBotCore.Plugin;
@@ -58,7 +59,13 @@ public class PluginManager
public async Task<PluginOnlineInfo?> GetPluginDataByName(string pluginName)
{
List<PluginOnlineInfo>? plugins = await GetPluginsList();
var result = plugins?.Find(p => p.Name == pluginName);
if(plugins == null)
return null;
// try to get the best matching plugin using the pluginName as a search query
PluginOnlineInfo? result = plugins.Find(pl => pl.Name.ToLower().Contains(pluginName.ToLower()));
if(result == null) return null;
return result;
}
@@ -145,12 +152,45 @@ public class PluginManager
{
File.Delete(pluginInfo.FilePath);
foreach(string dependency in pluginInfo.ListOfDependancies)
File.Delete(dependency);
foreach (var dependency in pluginInfo.ListOfDependancies)
File.Delete(dependency.Value);
await RemovePluginFromDatabase(pluginInfo.PluginName);
}
public async Task<string> GetDependencyLocation(string dependencyName)
{
List<PluginInfo> installedPlugins = await GetInstalledPlugins();
foreach (var plugin in installedPlugins)
{
if (plugin.ListOfDependancies.ContainsKey(dependencyName))
return plugin.ListOfDependancies[dependencyName];
}
throw new Exception("Dependency not found");
}
public async Task<string> GetDependencyLocation(string pluginName, string dependencyName)
{
PluginOnlineInfo? pluginData = await GetPluginDataByName(pluginName);
if(pluginData == null)
throw new Exception("Plugin not found");
var dependency = pluginData.Dependencies.Find(dep => dep.DependencyName == dependencyName);
if(dependency == null)
throw new Exception("Dependency not found");
return dependency.DownloadLocation;
}
public string GenerateDependencyLocation(string pluginName, string dependencyName)
{
return Path.Combine(Environment.CurrentDirectory, $"Libraries/{pluginName}/{dependencyName}");
}
public async Task InstallPlugin(PluginOnlineInfo pluginData, IProgress<float>? installProgress)
{
installProgress?.Report(0f);
@@ -171,7 +211,9 @@ public class PluginManager
if (pluginData.HasFileDependencies)
foreach (var dependency in pluginData.Dependencies)
{
await ServerCom.DownloadFileAsync(dependency.DownloadLink, dependency.DownloadLocation, progress);
string dependencyLocation = GenerateDependencyLocation(pluginData.Name, dependency.DownloadLocation);
await ServerCom.DownloadFileAsync(dependency.DownloadLink, dependencyLocation, progress);
currentProgress += stepProgress;
}
@@ -186,12 +228,7 @@ public class PluginManager
currentProgress += stepProgress;
}
PluginInfo pluginInfo = new PluginInfo(
pluginData.Name,
pluginData.Version,
pluginData.Dependencies.Select(dep => dep.DownloadLocation)
.ToList()
);
PluginInfo pluginInfo = PluginInfo.FromOnlineInfo(pluginData);
await AppendPluginToDatabase(pluginInfo);
}
+4 -1
View File
@@ -48,7 +48,10 @@ public static class ServerCom
using (var client = new HttpClient())
{
client.Timeout = TimeSpan.FromMinutes(5);
if(Directory.Exists(Path.GetDirectoryName(location)) == false)
{
Directory.CreateDirectory(Path.GetDirectoryName(location));
}
using (var file = new FileStream(location, FileMode.Create, FileAccess.Write, FileShare.None))
{
await client.DownloadFileAsync(URL, file, progress, downloadedBytes);
@@ -45,6 +45,12 @@ public class InternalActionManager
try
{
if (Actions[actionName].RunType == InternalActionRunType.ON_STARTUP)
{
Application.CurrentApplication.Logger.Log($"Action {actionName} is not executable", this, LogType.ERROR);
return false;
}
await Actions[actionName].Execute(args);
return true;
}
+2 -1
View File
@@ -22,7 +22,8 @@ public enum UnzipProgressType
public enum InternalActionRunType
{
ON_STARTUP,
ON_CALL
ON_CALL,
BOTH
}
[Flags]
@@ -1,12 +1,18 @@
using System.Text.Json.Serialization;
namespace DiscordBotCore.Plugin;
public class OnlineDependencyInfo
{
public string DependencyName { get; private set; }
public string DownloadLink { get; private set; }
public string DownloadLocation { get; private set; }
public OnlineDependencyInfo(string downloadLink, string downloadLocation)
[JsonConstructor]
public OnlineDependencyInfo(string dependencyName, string downloadLink, string downloadLocation)
{
DependencyName = dependencyName;
DownloadLink = downloadLink;
DownloadLocation = downloadLocation;
}
+4 -4
View File
@@ -11,11 +11,11 @@ public class PluginInfo
public string PluginName { get; private set; }
public PluginVersion PluginVersion { get; private set; }
public string FilePath { get; private set; }
public List<string> ListOfDependancies {get; private set;}
public Dictionary<string, string> ListOfDependancies {get; private set;}
public bool IsMarkedToUninstall {get; internal set;}
[JsonConstructor]
public PluginInfo(string pluginName, PluginVersion pluginVersion, List<string> listOfDependancies, bool isMarkedToUninstall)
public PluginInfo(string pluginName, PluginVersion pluginVersion, Dictionary<string, string> listOfDependancies, bool isMarkedToUninstall)
{
PluginName = pluginName;
PluginVersion = pluginVersion;
@@ -24,7 +24,7 @@ public class PluginInfo
FilePath = $"{Application.CurrentApplication.ApplicationEnvironmentVariables["PluginFolder"]}/{pluginName}.dll";
}
public PluginInfo(string pluginName, PluginVersion pluginVersion, List<string> listOfDependancies)
public PluginInfo(string pluginName, PluginVersion pluginVersion, Dictionary<string, string> listOfDependancies)
{
PluginName = pluginName;
PluginVersion = pluginVersion;
@@ -35,6 +35,6 @@ public class PluginInfo
public static PluginInfo FromOnlineInfo(PluginOnlineInfo onlineInfo)
{
return new PluginInfo(onlineInfo.Name, onlineInfo.Version, onlineInfo.Dependencies.Select(dep => dep.DownloadLocation).ToList());
return new PluginInfo(onlineInfo.Name, onlineInfo.Version, onlineInfo.Dependencies.Select(dep => new KeyValuePair<string, string>(dep.DependencyName, dep.DownloadLocation)).ToDictionary());
}
}