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

@@ -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);