cleaning up PluginManager (phase 1)
This commit is contained in:
51
PluginManager/Others/Actions/ActionManager.cs
Normal file
51
PluginManager/Others/Actions/ActionManager.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
40
PluginManager/Others/Actions/InternalAction.cs
Normal file
40
PluginManager/Others/Actions/InternalAction.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user