mirror of https://github.com/MedzikUser/servers
doc: update
This commit is contained in:
parent
d4f7fb650b
commit
b9af11c34e
|
@ -6,7 +6,7 @@ use servers::{
|
|||
|
||||
struct PluginTest;
|
||||
|
||||
/// Create a new plugin.
|
||||
/// Create a new plugin
|
||||
#[async_trait]
|
||||
impl Plugin for PluginTest {
|
||||
/// Name of the plugin.
|
||||
|
@ -19,7 +19,7 @@ impl Plugin for PluginTest {
|
|||
async fn on_plugin_load(&self) {}
|
||||
}
|
||||
|
||||
/// Create a new command.
|
||||
/// Create a new command
|
||||
#[async_trait]
|
||||
impl Command for PluginTest {
|
||||
/// Command name
|
||||
|
|
|
@ -4,8 +4,8 @@ mod help;
|
|||
|
||||
use crate::plugins::Command;
|
||||
|
||||
/// Register default commands
|
||||
/// Register build-in commands
|
||||
pub fn register_commands() -> Vec<Box<dyn Command>> {
|
||||
// create Vector with Commands
|
||||
// create array with build-in commands
|
||||
vec![Box::new(help::CommandHelp)]
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//! Plugins loader
|
||||
//!
|
||||
//! ## Writing plugins
|
||||
//! # Writing a plugins
|
||||
//!
|
||||
//! Create a new project `cargo new --lib plugin`
|
||||
//!
|
||||
|
@ -19,20 +19,15 @@
|
|||
//! servers = "0.1.0"
|
||||
//! ```
|
||||
//!
|
||||
//! ### Command plugin
|
||||
//!
|
||||
//! In file `src/lib.rs`
|
||||
//!
|
||||
//! ```no_run
|
||||
//! use async_trait::async_trait;
|
||||
//! use servers::{
|
||||
//! plugins::{Command, Plugin, PluginManagerType, Registrar},
|
||||
//! tcp::Client,
|
||||
//! };
|
||||
//! use servers::{plugins::{Plugin, Registrar}, tcp::Client};
|
||||
//!
|
||||
//! struct PluginTest;
|
||||
//!
|
||||
//! /// Create a new plugin.
|
||||
//! /// Create a new plugin
|
||||
//! #[async_trait]
|
||||
//! impl Plugin for PluginTest {
|
||||
//! /// Name of the plugin.
|
||||
|
@ -45,7 +40,37 @@
|
|||
//! async fn on_plugin_load(&self) {}
|
||||
//! }
|
||||
//!
|
||||
//! /// Create a new command.
|
||||
//! /// Register plugin
|
||||
//! #[no_mangle]
|
||||
//! pub fn plugin_entry(registrar: &mut dyn Registrar) {
|
||||
//! registrar.register_plugin(Box::new(PluginTest));
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! ## Add command
|
||||
//!
|
||||
//! ```no_run
|
||||
//! use async_trait::async_trait;
|
||||
//! use servers::{
|
||||
//! plugins::{Command, PluginManagerType, Registrar},
|
||||
//! tcp::Client,
|
||||
//! };
|
||||
//! #
|
||||
//! # struct PluginTest;
|
||||
//! #
|
||||
//! # #[async_trait]
|
||||
//! # impl servers::plugins::Plugin for PluginTest {
|
||||
//! # /// Name of the plugin.
|
||||
//! # fn name(&self) -> &'static str {
|
||||
//! # "test"
|
||||
//! # }
|
||||
//! #
|
||||
//! # /// A function will be executed when plugin loading.
|
||||
//! # /// Usally used for initialization.
|
||||
//! # async fn on_plugin_load(&self) {}
|
||||
//! # }
|
||||
//!
|
||||
//! /// Create a new command
|
||||
//! #[async_trait]
|
||||
//! impl Command for PluginTest {
|
||||
//! /// Command name
|
||||
|
@ -60,43 +85,42 @@
|
|||
//!
|
||||
//! /// Command function
|
||||
//! async fn execute(&self, client: &mut Client, _args: Vec<&str>, _commands: &PluginManagerType) {
|
||||
//! client.send("content").expect("send message")
|
||||
//! client.send("Command executed!").expect("send message")
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! /// Register plugin
|
||||
//! #[no_mangle]
|
||||
//! pub fn plugin_entry(registrar: &mut dyn Registrar) {
|
||||
//! registrar.register_plugin(Box::new(PluginTest));
|
||||
//! # registrar.register_plugin(Box::new(PluginTest));
|
||||
//! registrar.register_command(Box::new(PluginTest));
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! ### Event plugin
|
||||
//! ## Add event
|
||||
//!
|
||||
//! In file `src/lib.rs`
|
||||
//!
|
||||
//! ```no_run
|
||||
//! use async_trait::async_trait;
|
||||
//! use servers::{
|
||||
//! plugins::{Event, Plugin, PluginManagerType, Registrar},
|
||||
//! plugins::{Event, Registrar},
|
||||
//! tcp::Client,
|
||||
//! };
|
||||
//!
|
||||
//! struct PluginTest;
|
||||
//!
|
||||
//! /// Create a new plugin.
|
||||
//! #[async_trait]
|
||||
//! impl Plugin for PluginTest {
|
||||
//! /// Name of the plugin.
|
||||
//! fn name(&self) -> &'static str {
|
||||
//! "test"
|
||||
//! }
|
||||
//!
|
||||
//! /// A function will be executed when plugin loading.
|
||||
//! /// Usally used for initialization.
|
||||
//! async fn on_plugin_load(&self) {}
|
||||
//! }
|
||||
//! #
|
||||
//! # struct PluginTest;
|
||||
//! #
|
||||
//! # #[async_trait]
|
||||
//! # impl servers::plugins::Plugin for PluginTest {
|
||||
//! # /// Name of the plugin.
|
||||
//! # fn name(&self) -> &'static str {
|
||||
//! # "test"
|
||||
//! # }
|
||||
//! #
|
||||
//! # /// A function will be executed when plugin loading.
|
||||
//! # /// Usally used for initialization.
|
||||
//! # async fn on_plugin_load(&self) {}
|
||||
//! # }
|
||||
//!
|
||||
//! /// Create a new event
|
||||
//! #[async_trait]
|
||||
|
@ -117,7 +141,7 @@
|
|||
//! /// Register plugin
|
||||
//! #[no_mangle]
|
||||
//! pub fn plugin_entry(registrar: &mut dyn Registrar) {
|
||||
//! registrar.register_plugin(Box::new(PluginTest));
|
||||
//! # registrar.register_plugin(Box::new(PluginTest));
|
||||
//! registrar.register_event(Box::new(PluginTest));
|
||||
//! }
|
||||
//! ```
|
||||
|
@ -126,9 +150,9 @@
|
|||
//!
|
||||
//! To build plugin run command: `cargo build --release`
|
||||
//!
|
||||
//! The compiled plugin can be found in `./target/release/libplugin.so`
|
||||
//! The compiled plugin can be found in `target/release/libplugin.so`
|
||||
//!
|
||||
//! Move compiled plugin to the `plugin` directory where servers is located
|
||||
//! Move (or create a symlink) the built plugin to the `plugin/` directory in the server root directory.
|
||||
|
||||
mod loader;
|
||||
mod types;
|
||||
|
|
|
@ -14,7 +14,7 @@ pub trait Plugin: Any + Send + Sync {
|
|||
async fn on_plugin_load(&self);
|
||||
}
|
||||
|
||||
/// Add a new command
|
||||
/// Add a command to the plugin.
|
||||
#[async_trait]
|
||||
pub trait Command: Any + Send + Sync {
|
||||
/// Name of the command.
|
||||
|
@ -30,7 +30,7 @@ pub trait Command: Any + Send + Sync {
|
|||
);
|
||||
}
|
||||
|
||||
/// Add a new function that will be executed when the event occurs
|
||||
/// Add a new function that will be executed when the event occurs.
|
||||
#[async_trait]
|
||||
pub trait Event: Any + Send + Sync {
|
||||
/// Event name (onConnect or onSend)
|
||||
|
@ -39,13 +39,13 @@ pub trait Event: Any + Send + Sync {
|
|||
async fn execute(&self, client: &mut Client);
|
||||
}
|
||||
|
||||
/// Plugin Manager
|
||||
/// Plugin Manager with all plugins features.
|
||||
pub struct PluginManager {
|
||||
/// Vector with loaded plugins.
|
||||
/// Array with loaded plugins.
|
||||
pub plugins: Vec<Box<dyn Plugin>>,
|
||||
/// Vector with all commands.
|
||||
/// Array with all commands.
|
||||
pub commands: Vec<Box<dyn Command>>,
|
||||
/// Vector with all events.
|
||||
/// Array with all events.
|
||||
pub events: Vec<Box<dyn Event>>,
|
||||
}
|
||||
|
||||
|
@ -66,7 +66,7 @@ impl Default for PluginManager {
|
|||
}
|
||||
}
|
||||
|
||||
/// Type of the [PluginManager]
|
||||
/// Arc type of the [PluginManager].
|
||||
pub type PluginManagerType = Arc<PluginManager>;
|
||||
|
||||
/// Plugin Registrar
|
||||
|
|
Loading…
Reference in New Issue