diff --git a/common/src/lib.rs b/common/src/lib.rs index 6bef59fea8..5e325c6137 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -17,8 +17,6 @@ option_zip )] -pub extern crate plugin_api; - pub mod assets; pub mod astar; pub mod character; @@ -61,3 +59,4 @@ pub mod volumes; pub use combat::{Damage, DamageSource, GroupTarget, Knockback}; pub use explosion::{Explosion, RadiusEffect}; pub use loadout_builder::LoadoutBuilder; +pub use plugin_api; diff --git a/common/src/plugin/module.rs b/common/src/plugin/module.rs index 5041584a50..979e4722b9 100644 --- a/common/src/plugin/module.rs +++ b/common/src/plugin/module.rs @@ -24,7 +24,7 @@ impl PluginModule { let module = compile(&wasm_data).map_err(|e| PluginModuleError::Compile(e))?; let instance = module .instantiate(&imports! {"env" => { - "send_action" => func!(read_action), + "raw_emit_actions" => func!(read_action), }}).map_err(|e| PluginModuleError::Instantiate(e))?; Ok(Self { diff --git a/common/sys/src/state.rs b/common/sys/src/state.rs index b9743ae637..67ea4efd7e 100644 --- a/common/sys/src/state.rs +++ b/common/sys/src/state.rs @@ -181,7 +181,7 @@ impl State { // Load plugins from asset directory ecs.insert(match PluginMgr::from_assets() { Ok(plugin_mgr) => { - if let Err(e) = plugin_mgr.execute_event("on_load", &plugin_api::events::PluginLoadEvent {}) { + if let Err(e) = plugin_mgr.execute_event("on_load", &plugin_api::event::PluginLoadEvent {}) { tracing::error!(?e, "Failed to run plugin init"); info!("Error occurred when loading plugins. Running without plugins instead."); PluginMgr::default() diff --git a/plugin/api/src/lib.rs b/plugin/api/src/lib.rs index 1bcd78ee45..02c557dcfd 100644 --- a/plugin/api/src/lib.rs +++ b/plugin/api/src/lib.rs @@ -12,15 +12,19 @@ pub trait Event: Serialize + DeserializeOwned + Send + Sync{ type Response: Serialize + DeserializeOwned + Send + Sync; } +#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)] +pub enum PluginMode { + Server, + Client, + Singleplayer, // To be used later when we no longer start up an entirely new server for singleplayer +} + // TODO: Unify this with common/src/comp/uid.rs:Uid #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] pub struct Uid(pub u64); -pub mod events { - - use crate::Uid; - - use super::Event; +pub mod event { + use super::*; use serde::{Serialize,Deserialize}; #[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)] @@ -62,7 +66,9 @@ pub mod events { } #[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)] - pub struct PluginLoadEvent; + pub struct PluginLoadEvent { + pub mode: PluginMode, + } impl Event for PluginLoadEvent { type Response = (); diff --git a/plugin/hello/Cargo.toml b/plugin/hello/Cargo.toml index 2368f8ca00..07014803f5 100644 --- a/plugin/hello/Cargo.toml +++ b/plugin/hello/Cargo.toml @@ -11,3 +11,7 @@ crate-type = ["cdylib"] plugin-rt = { package = "veloren-plugin-rt", path = "../rt" } [workspace] + +[profile.dev] +opt-level = "s" +debug = false diff --git a/plugin/hello/src/lib.rs b/plugin/hello/src/lib.rs index 1d15335a46..82f0e7a62d 100644 --- a/plugin/hello/src/lib.rs +++ b/plugin/hello/src/lib.rs @@ -1,11 +1,11 @@ extern crate plugin_rt; use plugin_rt::*; -use plugin_rt::api::{Action, events::*}; +use plugin_rt::api::{Action, event::*}; #[event_handler] pub fn on_load(load: PluginLoadEvent) -> () { - send_actions(vec![Action::Print("This is a test".to_owned())]); + emit_action(Action::Print("This is a test".to_owned())); println!("Hello world"); } @@ -16,7 +16,7 @@ pub fn on_command_testplugin(command: ChatCommandEvent) -> Result, S #[event_handler] pub fn on_player_join(input: PlayerJoinEvent) -> PlayerJoinResult { - send_actions(vec![Action::PlayerSendMessage(input.player_id,format!("Welcome {} on our server",input.player_name))]); + emit_action(Action::PlayerSendMessage(input.player_id,format!("Welcome {} on our server",input.player_name))); if input.player_name == "Cheater123" { PlayerJoinResult::CloseConnection } else { diff --git a/plugin/rt/src/lib.rs b/plugin/rt/src/lib.rs index fdbb5b773e..467dc75936 100644 --- a/plugin/rt/src/lib.rs +++ b/plugin/rt/src/lib.rs @@ -9,13 +9,17 @@ use serde::de::DeserializeOwned; use serde::Serialize; extern "C" { - fn send_action(ptr: *const u8, len: usize); + fn raw_emit_actions(ptr: *const u8, len: usize); } -pub fn send_actions(action: Vec) { - let ret = bincode::serialize(&action).unwrap(); +pub fn emit_action(action: api::Action) { + emit_actions(vec![action]) +} + +pub fn emit_actions(actions: Vec) { + let ret = bincode::serialize(&actions).unwrap(); unsafe { - send_action(ret.as_ptr(), ret.len()); + raw_emit_actions(ret.as_ptr(), ret.len()); } } diff --git a/rust-toolchain b/rust-toolchain index 34e8182a4e..e4a9396eff 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -nightly-2020-12-10 +nightly-2020-12-12 diff --git a/server/src/lib.rs b/server/src/lib.rs index 1dc02f3182..933bb3ee76 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -996,10 +996,10 @@ impl Server { command.execute(self, entity, args); } else { let plugin_manager = self.state.ecs().read_resource::(); - let rs = plugin_manager.execute_event(&format!("on_command_{}",&kwd), &common::plugin_api::events::ChatCommandEvent { + let rs = plugin_manager.execute_event(&format!("on_command_{}",&kwd), &common::plugin_api::event::ChatCommandEvent { command: kwd.clone(), command_args: args.split(" ").map(|x| x.to_owned()).collect(), - player: common::plugin_api::events::Player { + player: common::plugin_api::event::Player { id: (*(self.state.ecs().read_storage::().get(entity).expect("Can't get player UUID [This should never appen]"))).into() }, }); @@ -1049,7 +1049,7 @@ impl Server { ); } } - + } }