diff --git a/Cargo.lock b/Cargo.lock index 51fc8237d4..287984ec15 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -727,13 +727,6 @@ dependencies = [ "pin-project-lite", ] -[[package]] -name = "common-api" -version = "0.1.0" -dependencies = [ - "serde", -] - [[package]] name = "conrod_core" version = "0.63.0" @@ -3753,15 +3746,6 @@ dependencies = [ "web-sys", ] -[[package]] -name = "plugin-api-derive" -version = "0.1.0" -dependencies = [ - "proc-macro2 1.0.24", - "quote 1.0.7", - "syn 1.0.54", -] - [[package]] name = "png" version = "0.16.7" @@ -5558,7 +5542,6 @@ dependencies = [ "arraygen", "authc", "bincode", - "common-api", "criterion", "crossbeam", "csv", @@ -5593,6 +5576,7 @@ dependencies = [ "tracing", "tracy-client", "vek 0.12.0", + "veloren-plugin-api", "wasmer-runtime", ] @@ -5600,12 +5584,28 @@ dependencies = [ name = "veloren-plugin-api" version = "0.1.0" dependencies = [ - "bincode", - "common-api", - "plugin-api-derive", "serde", ] +[[package]] +name = "veloren-plugin-derive" +version = "0.1.0" +dependencies = [ + "proc-macro2 1.0.24", + "quote 1.0.7", + "syn 1.0.54", +] + +[[package]] +name = "veloren-plugin-rt" +version = "0.1.0" +dependencies = [ + "bincode", + "serde", + "veloren-plugin-api", + "veloren-plugin-derive", +] + [[package]] name = "veloren-server" version = "0.8.0" @@ -5787,6 +5787,7 @@ dependencies = [ "tracy-client", "vek 0.12.0", "veloren-common", + "veloren-plugin-api", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index ce8d132762..4689614679 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,9 @@ cargo-features = ["named-profiles","profile-overrides"] members = [ "common", "common/sys", - "common/common-api", + "plugin/rt", + "plugin/api", + "plugin/derive", "client", "plugin-api", "server", diff --git a/common/Cargo.toml b/common/Cargo.toml index 556b1271c0..219c1b7e51 100644 --- a/common/Cargo.toml +++ b/common/Cargo.toml @@ -66,7 +66,7 @@ tracy-client = { version = "0.9.0", optional = true } # Plugins wasmer-runtime = "0.17.1" bincode = "1.3.1" -common-api = {path = "./common-api"} +plugin-api = { package = "veloren-plugin-api", path = "../plugin/api"} [dev-dependencies] #bench diff --git a/common/src/lib.rs b/common/src/lib.rs index 4de06ca912..145d62a593 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -17,8 +17,6 @@ option_zip )] -pub extern crate common_api; - pub mod assets; pub mod astar; pub mod character; diff --git a/common/src/plugin/mod.rs b/common/src/plugin/mod.rs index 4865f2d3c2..7055691b04 100644 --- a/common/src/plugin/mod.rs +++ b/common/src/plugin/mod.rs @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize}; use std::{collections::{HashMap, HashSet}, fs, io::Read, path::{Path, PathBuf}}; use tracing::{error, info}; -use common_api::Event; +use plugin_api::Event; use self::{ errors::PluginError, module::{PluginModule, PreparedEventQuery}}; diff --git a/common/src/plugin/module.rs b/common/src/plugin/module.rs index b1505ec4ad..5041584a50 100644 --- a/common/src/plugin/module.rs +++ b/common/src/plugin/module.rs @@ -5,7 +5,7 @@ use parking_lot::Mutex; use wasmer_runtime::*; use super::errors::{PluginError, PluginModuleError}; -use common_api::{Action, Event}; +use plugin_api::{Action, Event}; // This represent a WASM function interface pub type Function<'a> = Func<'a, (i32, u32), i32>; @@ -26,7 +26,7 @@ impl PluginModule { .instantiate(&imports! {"env" => { "send_action" => func!(read_action), }}).map_err(|e| PluginModuleError::Instantiate(e))?; - + Ok(Self { events: instance.exports.into_iter().map(|(name, _)| name).collect(), wasm_instance: Arc::new(Mutex::new(instance)), @@ -143,4 +143,4 @@ pub fn read_action(ctx: &mut Ctx, ptr: u32, len: u32) { } } } -} \ No newline at end of file +} diff --git a/common/sys/Cargo.toml b/common/sys/Cargo.toml index 3d65c5544c..f646593b7e 100644 --- a/common/sys/Cargo.toml +++ b/common/sys/Cargo.toml @@ -34,3 +34,6 @@ serde = { version = "1.0.110", features = ["derive"] } # Tracy tracy-client = { version = "0.9.0", optional = true } + +# Plugins +plugin-api = { package = "veloren-plugin-api", path = "../../plugin/api"} diff --git a/common/sys/src/state.rs b/common/sys/src/state.rs index 6860d4eb5d..b9743ae637 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", &common::common_api::events::PluginLoadEvent {}) { + if let Err(e) = plugin_mgr.execute_event("on_load", &plugin_api::events::PluginLoadEvent {}) { tracing::error!(?e, "Failed to run plugin init"); info!("Error occurred when loading plugins. Running without plugins instead."); PluginMgr::default() diff --git a/common/common-api/Cargo.toml b/plugin/api/Cargo.toml similarity index 72% rename from common/common-api/Cargo.toml rename to plugin/api/Cargo.toml index 27baec7453..6811eb028f 100644 --- a/common/common-api/Cargo.toml +++ b/plugin/api/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "common-api" +name = "veloren-plugin-api" version = "0.1.0" authors = ["ccgauche "] edition = "2018" @@ -7,4 +7,4 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -serde = {version = "1.0.118", features = ["derive"]} \ No newline at end of file +serde = {version = "1.0.118", features = ["derive"]} diff --git a/common/common-api/src/lib.rs b/plugin/api/src/lib.rs similarity index 99% rename from common/common-api/src/lib.rs rename to plugin/api/src/lib.rs index 9218fb62f8..2619f5de9e 100644 --- a/common/common-api/src/lib.rs +++ b/plugin/api/src/lib.rs @@ -1,6 +1,5 @@ use serde::{Serialize, de::DeserializeOwned, Deserialize}; - #[derive(Deserialize,Serialize,Debug)] pub enum Action { ServerClose, @@ -14,9 +13,9 @@ pub trait Event: Serialize + DeserializeOwned{ } pub mod events { - use super::Event; use serde::{Serialize,Deserialize}; + #[derive(Serialize, Deserialize, Debug)] pub struct PlayerJoinEvent { pub player_name: String, @@ -54,4 +53,4 @@ pub mod events { // Self::None // } // } -} \ No newline at end of file +} diff --git a/plugin-api/plugin-api-derive/Cargo.toml b/plugin/derive/Cargo.toml similarity index 87% rename from plugin-api/plugin-api-derive/Cargo.toml rename to plugin/derive/Cargo.toml index cec091ffe7..f6a9e882ad 100644 --- a/plugin-api/plugin-api-derive/Cargo.toml +++ b/plugin/derive/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "plugin-api-derive" +name = "veloren-plugin-derive" version = "0.1.0" authors = ["ccgauche "] edition = "2018" @@ -12,4 +12,4 @@ proc-macro = true [dependencies] proc-macro2 = "1.0.24" syn = { version = "1.0.54", features = ["full","extra-traits"]} -quote = "1.0.7" \ No newline at end of file +quote = "1.0.7" diff --git a/plugin-api/plugin-api-derive/src/lib.rs b/plugin/derive/src/lib.rs similarity index 84% rename from plugin-api/plugin-api-derive/src/lib.rs rename to plugin/derive/src/lib.rs index 023e89a19f..5a8562cf9c 100644 --- a/plugin-api/plugin-api-derive/src/lib.rs +++ b/plugin/derive/src/lib.rs @@ -12,16 +12,16 @@ pub fn export_function(_args: TokenStream, item: TokenStream) -> TokenStream { let fn_name = sig.ident; // function name/identifier let fn_args = sig.inputs; // comma separated args let fn_return = sig.output; // comma separated args - + let out: proc_macro2::TokenStream = quote! { #[no_mangle] pub fn #fn_name(intern__ptr: i32, intern__len: u32) -> i32 { - let input = plugin_api::read_input(intern__ptr,intern__len).unwrap(); + let input = plugin_rt::read_input(intern__ptr,intern__len).unwrap(); fn inner(#fn_args) #fn_return { #fn_body } - plugin_api::write_output(&inner(input)) + plugin_rt::write_output(&inner(input)) } }; out.into() -} \ No newline at end of file +} diff --git a/plugin-api/hello/.gitignore b/plugin/hello/.gitignore similarity index 100% rename from plugin-api/hello/.gitignore rename to plugin/hello/.gitignore diff --git a/plugin-api/hello/Cargo.lock b/plugin/hello/Cargo.lock similarity index 94% rename from plugin-api/hello/Cargo.lock rename to plugin/hello/Cargo.lock index e2fb998e8c..28c9d0d77f 100644 --- a/plugin-api/hello/Cargo.lock +++ b/plugin/hello/Cargo.lock @@ -16,27 +16,12 @@ version = "1.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" -[[package]] -name = "common-api" -version = "0.1.0" -dependencies = [ - "serde", -] - [[package]] name = "hello" version = "0.1.0" dependencies = [ "veloren-plugin-api", -] - -[[package]] -name = "plugin_proc" -version = "0.1.0" -dependencies = [ - "proc-macro2", - "quote", - "syn", + "veloren-plugin-rt", ] [[package]] @@ -98,8 +83,24 @@ checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" name = "veloren-plugin-api" version = "0.1.0" dependencies = [ - "bincode", - "common-api", - "plugin_proc", "serde", ] + +[[package]] +name = "veloren-plugin-derive" +version = "0.1.0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "veloren-plugin-rt" +version = "0.1.0" +dependencies = [ + "bincode", + "serde", + "veloren-plugin-api", + "veloren-plugin-derive", +] diff --git a/plugin-api/hello/Cargo.toml b/plugin/hello/Cargo.toml similarity index 58% rename from plugin-api/hello/Cargo.toml rename to plugin/hello/Cargo.toml index abd8ad457e..bb4d5fc40e 100644 --- a/plugin-api/hello/Cargo.toml +++ b/plugin/hello/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" crate-type = ["cdylib"] [dependencies] -plugin-api = { package = "veloren-plugin-api", path = ".." } +plugin-rt = { package = "veloren-plugin-rt", path = "../rt" } +plugin-api = { package = "veloren-plugin-api", path = "../api" } [workspace] diff --git a/plugin-api/hello/src/lib.rs b/plugin/hello/src/lib.rs similarity index 85% rename from plugin-api/hello/src/lib.rs rename to plugin/hello/src/lib.rs index 1bb679a2a8..0801bcad5e 100644 --- a/plugin-api/hello/src/lib.rs +++ b/plugin/hello/src/lib.rs @@ -1,10 +1,12 @@ -use plugin_api::{*,events::*}; +pub extern crate plugin_rt; + +use plugin_rt::*; +use plugin_api::{Action, events::*}; #[export_function] pub fn on_load(load: PluginLoadEvent) -> () { send_actions(vec![Action::Print("This is a test".to_owned())]); println!("Hello world"); - } #[export_function] diff --git a/plugin-api/Cargo.toml b/plugin/rt/Cargo.toml similarity index 54% rename from plugin-api/Cargo.toml rename to plugin/rt/Cargo.toml index 21e49dd52f..7ec08682b3 100644 --- a/plugin-api/Cargo.toml +++ b/plugin/rt/Cargo.toml @@ -1,13 +1,11 @@ [package] -name = "veloren-plugin-api" +name = "veloren-plugin-rt" version = "0.1.0" authors = ["Joshua Barretto "] edition = "2018" [dependencies] -common-api = { path = "../common/common-api" } -plugin-api-derive = { path = "./plugin-api-derive"} +plugin-api = { package = "veloren-plugin-api", path = "../api" } +plugin-derive = { package = "veloren-plugin-derive", path = "../derive"} serde = {version = "1.0.118", features = ["derive"]} bincode = "1.3.1" - -# spin = "0.7" diff --git a/plugin-api/src/lib.rs b/plugin/rt/src/lib.rs similarity index 79% rename from plugin-api/src/lib.rs rename to plugin/rt/src/lib.rs index cd910fca26..e5f459a6b3 100644 --- a/plugin-api/src/lib.rs +++ b/plugin/rt/src/lib.rs @@ -1,12 +1,7 @@ #![feature(const_fn)] - -pub extern crate plugin_api_derive; -pub extern crate common_api; - -pub use common_api::*; - -pub use plugin_api_derive::*; +pub use plugin_api as api; +pub use plugin_derive::*; use serde::de::DeserializeOwned; use serde::Serialize; @@ -15,7 +10,7 @@ extern "C" { fn send_action(ptr: *const u8, len: usize); } -pub fn send_actions(action: Vec) { +pub fn send_actions(action: Vec) { let ret = bincode::serialize(&action).unwrap(); unsafe { send_action(ret.as_ptr(), ret.len()); @@ -23,7 +18,7 @@ pub fn send_actions(action: Vec) { } pub fn read_input(ptr: i32, len: u32) -> Result where T: DeserializeOwned{ - let slice = unsafe { + let slice = unsafe { ::std::slice::from_raw_parts(ptr as _, len as _) }; bincode::deserialize(slice).map_err(|_|"Failed to deserialize function input") @@ -36,4 +31,4 @@ pub fn write_output(value: impl Serialize) -> i32 { ::std::ptr::write(1 as _, len); } ret.as_ptr() as _ -} \ No newline at end of file +}