2022-06-20 21:22:45 +00:00
|
|
|
use std::{any::Any, sync::Arc};
|
|
|
|
|
|
|
|
use async_trait::async_trait;
|
|
|
|
|
|
|
|
use crate::tcp::Client;
|
|
|
|
|
2022-06-23 09:07:42 +00:00
|
|
|
/// Custom Result alias, imported from [anyhow::Result].
|
|
|
|
pub type Result<T> = anyhow::Result<T>;
|
|
|
|
|
2022-06-20 21:22:45 +00:00
|
|
|
/// A plugin wich allows you to add extra functionality.
|
|
|
|
#[async_trait]
|
|
|
|
pub trait Plugin: Any + Send + Sync {
|
|
|
|
/// Name of the plugin.
|
|
|
|
fn name(&self) -> &'static str;
|
|
|
|
/// A function will be executed when plugin loading.
|
|
|
|
/// Usally used for initialization.
|
|
|
|
async fn on_plugin_load(&self);
|
|
|
|
}
|
|
|
|
|
2022-06-21 10:33:13 +00:00
|
|
|
/// Add a command to the plugin.
|
2022-06-20 21:22:45 +00:00
|
|
|
#[async_trait]
|
|
|
|
pub trait Command: Any + Send + Sync {
|
|
|
|
/// Name of the command.
|
|
|
|
fn name(&self) -> &'static str;
|
|
|
|
/// Help message of the command.
|
|
|
|
fn help(&self) -> &'static str;
|
|
|
|
/// Command function
|
|
|
|
async fn execute(
|
|
|
|
&self,
|
|
|
|
client: &mut Client,
|
|
|
|
args: Vec<&str>,
|
|
|
|
plugin_manager: &PluginManagerType,
|
2022-06-23 09:07:42 +00:00
|
|
|
) -> Result<()>;
|
2022-06-20 21:22:45 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 10:33:13 +00:00
|
|
|
/// Add a new function that will be executed when the event occurs.
|
2022-06-20 21:22:45 +00:00
|
|
|
#[async_trait]
|
|
|
|
pub trait Event: Any + Send + Sync {
|
|
|
|
/// Event name (onConnect or onSend)
|
|
|
|
fn name(&self) -> &'static str;
|
|
|
|
/// Event function
|
2022-06-23 09:07:42 +00:00
|
|
|
async fn execute(&self, client: &mut Client) -> Result<()>;
|
2022-06-20 21:22:45 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 10:33:13 +00:00
|
|
|
/// Plugin Manager with all plugins features.
|
2022-06-20 21:22:45 +00:00
|
|
|
pub struct PluginManager {
|
2022-06-21 10:33:13 +00:00
|
|
|
/// Array with loaded plugins.
|
2022-06-20 21:22:45 +00:00
|
|
|
pub plugins: Vec<Box<dyn Plugin>>,
|
2022-06-21 10:33:13 +00:00
|
|
|
/// Array with all commands.
|
2022-06-20 21:22:45 +00:00
|
|
|
pub commands: Vec<Box<dyn Command>>,
|
2022-06-21 10:33:13 +00:00
|
|
|
/// Array with all events.
|
2022-06-20 21:22:45 +00:00
|
|
|
pub events: Vec<Box<dyn Event>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PluginManager {
|
|
|
|
/// Create an empty [PluginManager]
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
plugins: Vec::new(),
|
|
|
|
commands: Vec::new(),
|
|
|
|
events: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for PluginManager {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 10:33:13 +00:00
|
|
|
/// Arc type of the [PluginManager].
|
2022-06-20 21:22:45 +00:00
|
|
|
pub type PluginManagerType = Arc<PluginManager>;
|
|
|
|
|
|
|
|
/// Plugin Registrar
|
|
|
|
pub trait Registrar {
|
|
|
|
/// Function to register the plugin
|
|
|
|
fn register_plugin(&mut self, plugin: Box<dyn Plugin>);
|
|
|
|
/// Function to register the command
|
|
|
|
fn register_command(&mut self, command: Box<dyn Command>);
|
|
|
|
/// Function to register the event
|
|
|
|
fn register_event(&mut self, event: Box<dyn Event>);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Registrar for PluginManager {
|
|
|
|
fn register_plugin(&mut self, plugin: Box<dyn Plugin>) {
|
|
|
|
self.plugins.push(plugin)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn register_command(&mut self, command: Box<dyn Command>) {
|
|
|
|
self.commands.push(command)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn register_event(&mut self, event: Box<dyn Event>) {
|
|
|
|
self.events.push(event)
|
|
|
|
}
|
|
|
|
}
|