Reimplemented Command Actions.

This commit is contained in:
2023-06-07 20:36:11 +03:00
parent bcd9245502
commit 0b2f1e6ab6
19 changed files with 867 additions and 681 deletions

View File

@@ -88,6 +88,9 @@ public class Boot
await Task.Delay(2000);
Config._DiscordBotClient = this;
while (!isReady) ;
}

View File

@@ -5,19 +5,24 @@ using System.IO;
using System.Collections.Generic;
using PluginManager.Others;
using System.Collections;
using PluginManager.Online.Helpers;
using PluginManager.Others.Logger;
namespace PluginManager;
public static class Config
public class Config
{
private static bool IsLoaded = false;
public static DBLogger Logger;
public static Json<string, string> Data;
public static Json<string, string> Plugins;
internal static Bot.Boot? _DiscordBotClient;
public static Bot.Boot? DiscordBot
{
get => _DiscordBotClient;
}
public static async Task Initialize()
{
if (IsLoaded)

View File

@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using PluginManager.Others;
namespace PluginManager.Interfaces
{
public interface ICommandAction
{
public string ActionName { get; }
public string? Description { get; }
public string? Usage { get; }
public InternalActionRunType RunType { get; }
public Task Execute(string[]? args);
}
}

View File

@@ -0,0 +1,76 @@
using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Reflection;
using PluginManager.Others.Actions;
using PluginManager.Interfaces;
using System.Collections;
namespace PluginManager.Loaders
{
public class ActionsLoader
{
public delegate void ActionLoaded(string name, string typeName, bool success, Exception? e = null);
public event ActionLoaded? ActionLoadedEvent;
private string actionFolder = @"./Data/Actions/";
private string actionExtension = "dll";
public ActionsLoader(string path, string extension)
{
actionFolder = path;
actionExtension = extension;
}
public async Task<List<ICommandAction>?> Load()
{
Directory.CreateDirectory(actionFolder);
var files = Directory.GetFiles(actionFolder, $"*.{actionExtension}", SearchOption.AllDirectories);
List<ICommandAction> actions = new List<ICommandAction>();
foreach (var file in files)
{
try
{
Assembly.LoadFrom(file);
}
catch (Exception e)
{
ActionLoadedEvent?.Invoke(file, "", false, e);
}
}
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => typeof(ICommandAction).IsAssignableFrom(p) && !p.IsInterface);
foreach (var type in types)
{
try
{
var action = (ICommandAction) Activator.CreateInstance(type);
if (action.ActionName == null)
{
ActionLoadedEvent?.Invoke(action.ActionName, type.Name, false);
continue;
}
if(action.RunType == PluginManager.Others.InternalActionRunType.ON_STARTUP)
await action.Execute(null);
ActionLoadedEvent?.Invoke(action.ActionName, type.Name, true);
actions.Add(action);
}
catch (Exception e)
{
ActionLoadedEvent?.Invoke(type.Name, type.Name, false, e);
}
}
return actions;
}
}
}

View File

@@ -110,7 +110,10 @@ namespace PluginManager.Loaders
Exception = null,
IsLoaded = true,
PluginName = type.FullName,
TypeName = typeof(T) == typeof(DBCommand) ? "DBCommand" : typeof(T) == typeof(DBEvent) ? "DBEvent" : "DBSlashCommand",
TypeName = typeof(T) == typeof(DBCommand) ? "DBCommand" :
typeof(T) == typeof(DBEvent) ? "DBEvent" :
typeof(T) == typeof(DBSlashCommand) ? "DBSlashCommand" :
null,
Plugin = plugin
}
);

View File

@@ -9,6 +9,7 @@ using Discord.WebSocket;
using PluginManager.Interfaces;
using PluginManager.Online;
using PluginManager.Others;
namespace PluginManager.Loaders;

View File

@@ -4,6 +4,24 @@ namespace PluginManager.Online.Helpers;
public class VersionString
{
private bool Equals(VersionString other)
{
return PackageCheckVersion == other.PackageCheckVersion && PackageMainVersion == other.PackageMainVersion && PackageVersionID == other.PackageVersionID;
}
public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((VersionString)obj);
}
public override int GetHashCode()
{
return HashCode.Combine(PackageCheckVersion, PackageMainVersion, PackageVersionID);
}
public int PackageCheckVersion;
public int PackageMainVersion;
public int PackageVersionID;
@@ -13,9 +31,22 @@ public class VersionString
var data = version.Split('.');
try
{
PackageVersionID = int.Parse(data[0]);
PackageMainVersion = int.Parse(data[1]);
PackageCheckVersion = int.Parse(data[2]);
if (data.Length == 3)
{
PackageVersionID = int.Parse(data[0]);
PackageMainVersion = int.Parse(data[1]);
PackageCheckVersion = int.Parse(data[2]);
}
else if (data.Length == 4)
{
// ignore the first item data[0]
PackageVersionID = int.Parse(data[1]);
PackageMainVersion = int.Parse(data[2]);
PackageCheckVersion = int.Parse(data[3]);
}
else
throw new Exception("Invalid version string");
}
catch (Exception ex)
{

View File

@@ -1,51 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PluginManager.Others.Actions
{
public class ActionManager
{
public List<InternalAction> Actions { get; private set; }
private bool _isInitialized = false;
public ActionManager()
{
if(_isInitialized) return;
Actions = new List<InternalAction>();
_isInitialized = true;
}
public bool ActionExists(string name)
{
if(!_isInitialized) throw new Exception("ActionManager is not initialized");
return Actions.Any(x => x.Name == name);
}
public void AddAction(InternalAction action)
{
if(!_isInitialized) throw new Exception("ActionManager is not initialized");
Actions.Add(action);
}
public void ExecuteAction(string name, string[] args)
{
if(!_isInitialized) throw new Exception("ActionManager is not initialized");
var action = Actions.FirstOrDefault(x => x.Name == name);
if(action == null) throw new Exception($"Action {name} not found");
action.Invoke(args);
}
public async Task ExecuteActionAsync(string name, string[] args)
{
if(!_isInitialized) throw new Exception("ActionManager is not initialized");
var action = Actions.FirstOrDefault(x => x.Name == name);
if(action == null) throw new Exception($"Action {name} not found");
await action.InvokeAsync(args);
}
}
}

View File

@@ -1,40 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PluginManager.Others.Actions
{
public class InternalAction
{
public string? Name { get; init; }
public Action<string[]> Action { get; init; }
public InternalAction(string name, Action<string[]> action)
{
Name = name;
Action = action;
}
public InternalAction(string name, Action action)
{
Name = name;
Action = (o) =>
{
action();
return;
};
}
public void Invoke(string[] args)
{
Action(args);
}
public async Task InvokeAsync(string[] args)
{
await Task.Run(() => Action(args));
}
}
}

View File

@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using PluginManager.Interfaces;
using PluginManager.Loaders;
namespace PluginManager.Others.Actions
{
public class InternalActionManager
{
public ActionsLoader loader;
public Dictionary<string, ICommandAction> Actions = new Dictionary<string, ICommandAction>();
public InternalActionManager(string path, string extension)
{
loader = new ActionsLoader(path, extension);
}
public async Task Initialize()
{
loader.ActionLoadedEvent += OnActionLoaded;
var m_actions = await loader.Load();
if(m_actions == null) return;
foreach(var action in m_actions)
Actions.Add(action.ActionName, action);
}
private void OnActionLoaded(string name, string typeName, bool success, Exception? e)
{
if (!success)
{
Config.Logger.Error(e);
return;
}
Config.Logger.Log($"Action {name} loaded successfully", typeName, LogLevel.INFO);
}
public async Task<string> Execute(string actionName, string[]? args)
{
if (!Actions.ContainsKey(actionName))
{
Config.Logger.Log($"Action {actionName} not found", "InternalActionManager", LogLevel.WARNING);
return "Action not found";
}
try{
await Actions[actionName].Execute(args);
return "Action executed";
}catch(Exception e){
Config.Logger.Log(e.Message, "InternalActionManager", LogLevel.ERROR);
return e.Message;
}
}
}
}

View File

@@ -35,3 +35,8 @@ public enum SaveType
BACKUP
}
public enum InternalActionRunType
{
ON_STARTUP,
ON_CALL
}

View File

@@ -29,13 +29,14 @@ namespace PluginManager.Others.Logger
}
public void Log(string message, string sender = "unknown", LogLevel type = LogLevel.INFO) => Log(new LogMessage(message, type, sender));
public void Error(Exception? e) => Log(e.Message, e.Source, LogLevel.ERROR);
public void Log(LogMessage message)
{
if(LogEvent is not null)
LogEvent?.Invoke(message.Message, message.Type);
if (message.Type != LogLevel.NONE)
if (message.Type != LogLevel.ERROR && message.Type != LogLevel.CRITICAL)
LogHistory.Add(message);
else
ErrorHistory.Add(message);