Reorganised plugin crates

This commit is contained in:
Joshua Barretto 2020-12-12 13:01:54 +00:00
parent 05aee7df18
commit 780f54ac90
18 changed files with 77 additions and 77 deletions

41
Cargo.lock generated
View File

@ -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]]

View File

@ -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",

View File

@ -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

View File

@ -17,8 +17,6 @@
option_zip
)]
pub extern crate common_api;
pub mod assets;
pub mod astar;
pub mod character;

View File

@ -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}};

View File

@ -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) {
}
}
}
}
}

View File

@ -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"}

View File

@ -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()

View File

@ -1,5 +1,5 @@
[package]
name = "common-api"
name = "veloren-plugin-api"
version = "0.1.0"
authors = ["ccgauche <gaucheron.laurent@gmail.com>"]
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"]}
serde = {version = "1.0.118", features = ["derive"]}

View File

@ -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
// }
// }
}
}

View File

@ -1,5 +1,5 @@
[package]
name = "plugin-api-derive"
name = "veloren-plugin-derive"
version = "0.1.0"
authors = ["ccgauche <gaucheron.laurent@gmail.com>"]
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"
quote = "1.0.7"

View File

@ -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()
}
}

View File

@ -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",
]

View File

@ -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]

View File

@ -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]

View File

@ -1,13 +1,11 @@
[package]
name = "veloren-plugin-api"
name = "veloren-plugin-rt"
version = "0.1.0"
authors = ["Joshua Barretto <joshua.s.barretto@gmail.com>"]
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"

View File

@ -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<Action>) {
pub fn send_actions(action: Vec<api::Action>) {
let ret = bincode::serialize(&action).unwrap();
unsafe {
send_action(ret.as_ptr(), ret.len());
@ -23,7 +18,7 @@ pub fn send_actions(action: Vec<Action>) {
}
pub fn read_input<T>(ptr: i32, len: u32) -> Result<T, &'static str> 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 _
}
}