Skip to content

Commit

Permalink
Updated plugin init temporarilty (We will require plugins to implemen…
Browse files Browse the repository at this point in the history
…t a second basic fn later)
  • Loading branch information
tristanpoland committed Nov 7, 2024
1 parent 0cb2843 commit ec6d3c0
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 17 deletions.
12 changes: 8 additions & 4 deletions plugin-api/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -168,15 +172,15 @@ 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<RwLock<Vec<horizon_data_types::Player>>>) -> HashMap<&'static str, LoadedPlugin> {{")?;
writeln!(file, " let mut plugins = HashMap::new();\n")?;

// Add each plugin to the HashMap
for (name, _, _) in plugin_paths {
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, " );")?;
}
Expand Down
8 changes: 6 additions & 2 deletions plugin-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<RwLock<Vec<horizon_data_types::Player>>>) {
let plugins = plugin_imports::load_plugins(socket, players);

let my_test_plugin = get_plugin!(test_plugin, plugins);
let result = my_test_plugin.thing();
Expand Down
2 changes: 1 addition & 1 deletion plugin-api/src/lib_old.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ pub trait BaseAPI: Send + Sync {
}

pub trait PlayersAPI: Send + Sync {
// async fn get_online_players() -> Vec<Player> {
// async fn get_online_players() -> Arc<RwLock<Vec<horizon_data_types::Player>>> {
// get_online_players().await
// }
}
Expand Down
8 changes: 6 additions & 2 deletions plugin-api/src/plugin_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<RwLock<Vec<horizon_data_types::Player>>>) -> 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),
}
);

Expand Down
16 changes: 9 additions & 7 deletions plugins/test_plugin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<Player>);
}

pub trait Plugin_Construct {
// If you want default implementations, mark them with 'default'
fn new(socket: SocketRef, players: Vec<Player>) -> Plugin;
fn new(socket: SocketRef, players: Arc<RwLock<Vec<horizon_data_types::Player>>>) -> Plugin;
}

// Implement constructor separately
impl Plugin_Construct for Plugin {
fn new(socket: SocketRef, players: Vec<Player>) -> Plugin {
fn new(socket: SocketRef, players: Arc<RwLock<Vec<horizon_data_types::Player>>>) -> Plugin {
println!("Hello from the test plugin!!!!!");
self::Plugin::setup_listeners(socket, players);
setup_listeners(socket, players);

Plugin {}
}
Expand All @@ -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<Player>) {
socket.on("foo", || println!("bar"));
}

fn setup_listeners(socket: SocketRef, players: Arc<RwLock<Vec<horizon_data_types::Player>>>) {
socket.on("foo", || println!("bar"));
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -208,7 +209,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

// 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;
Expand Down

0 comments on commit ec6d3c0

Please sign in to comment.