From ec6d3c049b857ee9665da4a14e2f7fa9e43bc286 Mon Sep 17 00:00:00 2001 From: "Tristan Poland (Trident_For_U)" <34868944+tristanpoland@users.noreply.github.com> Date: Thu, 7 Nov 2024 11:12:26 -0500 Subject: [PATCH] Updated plugin init temporarilty (We will require plugins to implement a second basic fn later) --- plugin-api/build.rs | 12 ++++++++---- plugin-api/src/lib.rs | 8 ++++++-- plugin-api/src/lib_old.rs | 2 +- plugin-api/src/plugin_imports.rs | 8 ++++++-- plugins/test_plugin/src/lib.rs | 16 +++++++++------- src/main.rs | 2 +- 6 files changed, 31 insertions(+), 17 deletions(-) diff --git a/plugin-api/build.rs b/plugin-api/build.rs index e12fe29..e5ae4f8 100644 --- a/plugin-api/build.rs +++ b/plugin-api/build.rs @@ -153,8 +153,12 @@ fn generate_imports_file(plugin_paths: &[(String, String, String)]) -> std::io:: writeln!(file, "// This file is automatically generated by build.rs")?; writeln!(file, "// Do not edit this file manually!\n")?; writeln!(file, "use horizon_plugin_api::Plugin;")?; - writeln!(file, "use std::collections::HashMap;\n")?; - + writeln!(file, "use std::collections::HashMap;")?; + writeln!(file, "use horizon_data_types::Player;")?; + writeln!(file, "use std::sync::RwLock;")?; + writeln!(file, "use std::sync::Arc;")?; + writeln!(file, "use socketioxide::extract::SocketRef;\n")?; + // Write the imports for (name, _, _) in plugin_paths { writeln!(file, "use {};", name)?; @@ -168,7 +172,7 @@ fn generate_imports_file(plugin_paths: &[(String, String, String)]) -> std::io:: writeln!(file, "}}\n")?; // Write the plugin loading function - writeln!(file, "pub fn load_plugins() -> HashMap<&'static str, LoadedPlugin> {{")?; + writeln!(file, "pub fn load_plugins(socket: SocketRef, players: Arc>>) -> HashMap<&'static str, LoadedPlugin> {{")?; writeln!(file, " let mut plugins = HashMap::new();\n")?; // Add each plugin to the HashMap @@ -176,7 +180,7 @@ fn generate_imports_file(plugin_paths: &[(String, String, String)]) -> std::io:: writeln!(file, " plugins.insert(")?; writeln!(file, " \"{}\",", name)?; writeln!(file, " LoadedPlugin {{")?; - writeln!(file, " instance: {}::Plugin::new(),", name)?; + writeln!(file, " instance: {}::Plugin::new(socket, players),", name)?; writeln!(file, " }}")?; writeln!(file, " );")?; } diff --git a/plugin-api/src/lib.rs b/plugin-api/src/lib.rs index ffa0ab5..cc74732 100644 --- a/plugin-api/src/lib.rs +++ b/plugin-api/src/lib.rs @@ -1,6 +1,10 @@ use std::collections::HashMap; +use horizon_data_types::Player; +use socketioxide::{extract::SocketRef, socket}; pub use test_plugin; +use std::sync::RwLock; +use std::sync::Arc; mod plugin_imports; mod proposal; @@ -67,8 +71,8 @@ struct Plugin { api_versin: Version } -pub fn load_all() { - let plugins = plugin_imports::load_plugins(); +pub fn load_all(socket: SocketRef, players: Arc>>) { + let plugins = plugin_imports::load_plugins(socket, players); let my_test_plugin = get_plugin!(test_plugin, plugins); let result = my_test_plugin.thing(); diff --git a/plugin-api/src/lib_old.rs b/plugin-api/src/lib_old.rs index e6e19fe..302cf15 100644 --- a/plugin-api/src/lib_old.rs +++ b/plugin-api/src/lib_old.rs @@ -251,7 +251,7 @@ pub trait BaseAPI: Send + Sync { } pub trait PlayersAPI: Send + Sync { -// async fn get_online_players() -> Vec { +// async fn get_online_players() -> Arc>> { // get_online_players().await // } } diff --git a/plugin-api/src/plugin_imports.rs b/plugin-api/src/plugin_imports.rs index 7505c81..c37b34b 100644 --- a/plugin-api/src/plugin_imports.rs +++ b/plugin-api/src/plugin_imports.rs @@ -3,6 +3,10 @@ use horizon_plugin_api::Plugin; use std::collections::HashMap; +use horizon_data_types::Player; +use std::sync::RwLock; +use std::sync::Arc; +use socketioxide::extract::SocketRef; use test_plugin; use test_plugin::Plugin_Construct; @@ -11,13 +15,13 @@ pub struct LoadedPlugin { pub instance: Plugin, } -pub fn load_plugins() -> HashMap<&'static str, LoadedPlugin> { +pub fn load_plugins(socket: SocketRef, players: Arc>>) -> HashMap<&'static str, LoadedPlugin> { let mut plugins = HashMap::new(); plugins.insert( "test_plugin", LoadedPlugin { - instance: test_plugin::Plugin::new(), + instance: test_plugin::Plugin::new(socket, players), } ); diff --git a/plugins/test_plugin/src/lib.rs b/plugins/test_plugin/src/lib.rs index 56747bf..06d9d42 100644 --- a/plugins/test_plugin/src/lib.rs +++ b/plugins/test_plugin/src/lib.rs @@ -1,23 +1,24 @@ use horizon_data_types::Player; use socketioxide::extract::SocketRef; pub use horizon_plugin_api::Plugin; +use std::sync::RwLock; +use std::sync::Arc; // Define the trait properly pub trait Plugin_API { fn thing(&self) -> String; - fn setup_listeners(socket: SocketRef, players: Vec); } pub trait Plugin_Construct { // If you want default implementations, mark them with 'default' - fn new(socket: SocketRef, players: Vec) -> Plugin; + fn new(socket: SocketRef, players: Arc>>) -> Plugin; } // Implement constructor separately impl Plugin_Construct for Plugin { - fn new(socket: SocketRef, players: Vec) -> Plugin { + fn new(socket: SocketRef, players: Arc>>) -> Plugin { println!("Hello from the test plugin!!!!!"); - self::Plugin::setup_listeners(socket, players); + setup_listeners(socket, players); Plugin {} } @@ -29,8 +30,9 @@ impl Plugin_API for Plugin { fn thing(&self) -> String { "Hello from specific plugin implementation!".to_string() } +} - fn setup_listeners(socket: SocketRef, players: Vec) { - socket.on("foo", || println!("bar")); - } + +fn setup_listeners(socket: SocketRef, players: Arc>>) { + socket.on("foo", || println!("bar")); } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 4193bc1..1d78451 100644 --- a/src/main.rs +++ b/src/main.rs @@ -103,6 +103,7 @@ impl HorizonServer { let player = Player::new(socket.clone(), Uuid::new_v4()); players::init(socket.clone(), pool.players.clone()); + plugin_test_api::load_all(socket.clone(), pool.players.clone()); pool.players.write().unwrap().push(player.clone()); log_debug!(pool.logger, "PLAYER", "Player {} (UUID: {}) added to pool", @@ -208,7 +209,6 @@ async fn main() -> Result<(), Box> { // Create and start server let server = HorizonServer::new(); - plugin_test_api::load_all(); log_info!(LOGGER, "STARTUP", "Server startup completed in {:?}", init_time.elapsed()); server.start().await;