2022-06-17 11:43:23 +00:00
|
|
|
//! Plugins loader
|
|
|
|
//!
|
|
|
|
//! ## Writing plugins
|
|
|
|
//!
|
|
|
|
//! Create a new project `cargo new --lib plugin`
|
|
|
|
//!
|
|
|
|
//! Set a `crate-type` in Cargo.toml (to build a `.so` plugin)
|
|
|
|
//!
|
|
|
|
//! ```toml
|
|
|
|
//! [lib]
|
|
|
|
//! crate-type = ["dylib"]
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Add a `servers` and `async-trait` dependencies to Cargo.toml
|
|
|
|
//!
|
|
|
|
//! ```toml
|
|
|
|
//! [dependencies]
|
|
|
|
//! async-trait = "0.1.56"
|
2022-06-17 11:58:53 +00:00
|
|
|
//! servers = "0.1.0"
|
2022-06-17 11:43:23 +00:00
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! ### Command plugin
|
|
|
|
//!
|
|
|
|
//! In file `src/lib.rs`
|
|
|
|
//!
|
2022-06-20 21:22:45 +00:00
|
|
|
//! ```no_run
|
2022-06-17 11:43:23 +00:00
|
|
|
//! use async_trait::async_trait;
|
|
|
|
//! use servers::{
|
2022-06-20 21:22:45 +00:00
|
|
|
//! plugins::{Command, Plugin, PluginManagerType, Registrar},
|
2022-06-17 11:43:23 +00:00
|
|
|
//! tcp::Client,
|
|
|
|
//! };
|
|
|
|
//!
|
|
|
|
//! struct PluginTest;
|
|
|
|
//!
|
2022-06-20 21:22:45 +00:00
|
|
|
//! /// Create a new plugin.
|
2022-06-17 11:43:23 +00:00
|
|
|
//! #[async_trait]
|
|
|
|
//! impl Plugin for PluginTest {
|
|
|
|
//! /// Name of the plugin.
|
|
|
|
//! fn name(&self) -> &'static str {
|
|
|
|
//! "test"
|
|
|
|
//! }
|
|
|
|
//!
|
2022-06-20 21:22:45 +00:00
|
|
|
//! /// A function will be executed when plugin loading.
|
|
|
|
//! /// Usally used for initialization.
|
|
|
|
//! async fn on_plugin_load(&self) {}
|
2022-06-17 11:43:23 +00:00
|
|
|
//! }
|
|
|
|
//!
|
2022-06-20 21:22:45 +00:00
|
|
|
//! /// Create a new command.
|
2022-06-17 11:43:23 +00:00
|
|
|
//! #[async_trait]
|
|
|
|
//! impl Command for PluginTest {
|
|
|
|
//! /// Command name
|
|
|
|
//! fn name(&self) -> &'static str {
|
|
|
|
//! "/test"
|
|
|
|
//! }
|
|
|
|
//!
|
2022-06-20 21:22:45 +00:00
|
|
|
//! /// Help message of the command
|
2022-06-17 11:43:23 +00:00
|
|
|
//! fn help(&self) -> &'static str {
|
|
|
|
//! "test command"
|
|
|
|
//! }
|
|
|
|
//!
|
2022-06-20 21:22:45 +00:00
|
|
|
//! /// Command function
|
|
|
|
//! async fn execute(&self, client: &mut Client, _args: Vec<&str>, _commands: &PluginManagerType) {
|
|
|
|
//! client.send("content").expect("send message")
|
2022-06-17 11:43:23 +00:00
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//!
|
2022-06-20 21:41:08 +00:00
|
|
|
//! /// Register plugin
|
2022-06-17 11:43:23 +00:00
|
|
|
//! #[no_mangle]
|
2022-06-20 21:22:45 +00:00
|
|
|
//! pub fn plugin_entry(registrar: &mut dyn Registrar) {
|
|
|
|
//! registrar.register_plugin(Box::new(PluginTest));
|
|
|
|
//! registrar.register_command(Box::new(PluginTest));
|
2022-06-17 11:43:23 +00:00
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! ### Event plugin
|
|
|
|
//!
|
|
|
|
//! In file `src/lib.rs`
|
|
|
|
//!
|
2022-06-20 21:22:45 +00:00
|
|
|
//! ```no_run
|
2022-06-17 11:43:23 +00:00
|
|
|
//! use async_trait::async_trait;
|
|
|
|
//! use servers::{
|
2022-06-20 21:22:45 +00:00
|
|
|
//! plugins::{Event, Plugin, PluginManagerType, Registrar},
|
2022-06-17 11:43:23 +00:00
|
|
|
//! tcp::Client,
|
|
|
|
//! };
|
|
|
|
//!
|
|
|
|
//! struct PluginTest;
|
|
|
|
//!
|
2022-06-20 21:22:45 +00:00
|
|
|
//! /// Create a new plugin.
|
2022-06-17 11:43:23 +00:00
|
|
|
//! #[async_trait]
|
|
|
|
//! impl Plugin for PluginTest {
|
|
|
|
//! /// Name of the plugin.
|
|
|
|
//! fn name(&self) -> &'static str {
|
|
|
|
//! "test"
|
|
|
|
//! }
|
|
|
|
//!
|
2022-06-20 21:22:45 +00:00
|
|
|
//! /// A function will be executed when plugin loading.
|
|
|
|
//! /// Usally used for initialization.
|
|
|
|
//! async fn on_plugin_load(&self) {}
|
2022-06-17 11:43:23 +00:00
|
|
|
//! }
|
|
|
|
//!
|
2022-06-20 21:22:45 +00:00
|
|
|
//! /// Create a new event
|
2022-06-17 11:43:23 +00:00
|
|
|
//! #[async_trait]
|
|
|
|
//! impl Event for PluginTest {
|
2022-06-20 21:22:45 +00:00
|
|
|
//! /// Event name (onConnect or onSend)
|
2022-06-17 11:43:23 +00:00
|
|
|
//! fn name(&self) -> &'static str {
|
|
|
|
//! "onConnect"
|
|
|
|
//! }
|
|
|
|
//!
|
2022-06-20 21:22:45 +00:00
|
|
|
//! /// Event function
|
|
|
|
//! async fn execute(&self, client: &mut Client) {
|
|
|
|
//! client
|
|
|
|
//! .send(&format!("Welcome {}", client.stream.peer_addr().unwrap()))
|
|
|
|
//! .expect("send message")
|
2022-06-17 11:43:23 +00:00
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//!
|
2022-06-20 21:41:08 +00:00
|
|
|
//! /// Register plugin
|
2022-06-17 11:43:23 +00:00
|
|
|
//! #[no_mangle]
|
2022-06-20 21:22:45 +00:00
|
|
|
//! pub fn plugin_entry(registrar: &mut dyn Registrar) {
|
|
|
|
//! registrar.register_plugin(Box::new(PluginTest));
|
|
|
|
//! registrar.register_event(Box::new(PluginTest));
|
2022-06-17 11:43:23 +00:00
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! ## Build plugin
|
|
|
|
//!
|
|
|
|
//! To build plugin run command: `cargo build --release`
|
|
|
|
//!
|
|
|
|
//! The compiled plugin can be found in `./target/release/libplugin.so`
|
|
|
|
//!
|
|
|
|
//! Move compiled plugin to the `plugin` directory where servers is located
|
|
|
|
|
2022-06-05 14:08:13 +00:00
|
|
|
mod loader;
|
2022-06-20 21:22:45 +00:00
|
|
|
mod types;
|
2022-06-05 14:08:13 +00:00
|
|
|
|
|
|
|
pub use loader::*;
|
2022-06-20 21:22:45 +00:00
|
|
|
pub use types::*;
|