mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Fixed comments
- Would be better to remove the iterator and just collect with a loop to avoid extra allocations - tructure - A HashSet is probably better - Usefull -> Useful - I'd have thought plugin-api-derive is a better name
This commit is contained in:
parent
ee7fb990c3
commit
c95f57cafd
4
Cargo.lock
generated
4
Cargo.lock
generated
@ -3754,7 +3754,7 @@ dependencies = [
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "plugin_proc"
|
||||
name = "plugin-api-derive"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.24",
|
||||
@ -5602,7 +5602,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"bincode",
|
||||
"common-api",
|
||||
"plugin_proc",
|
||||
"plugin-api-derive",
|
||||
"serde",
|
||||
]
|
||||
|
||||
|
Binary file not shown.
@ -4,12 +4,7 @@ pub mod module;
|
||||
|
||||
use crate::assets::ASSETS_PATH;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fs,
|
||||
io::Read,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use std::{collections::{HashMap, HashSet}, fs, io::Read, path::{Path, PathBuf}};
|
||||
use tracing::{error, info};
|
||||
|
||||
use common_api::Event;
|
||||
@ -19,8 +14,8 @@ use self::{ errors::PluginError, module::{PluginModule, PreparedEventQuery}};
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct PluginData {
|
||||
name: String,
|
||||
modules: Vec<PathBuf>,
|
||||
dependencies: Vec<String>,
|
||||
modules: HashSet<PathBuf>,
|
||||
dependencies: HashSet<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@ -93,9 +88,12 @@ impl PluginMgr {
|
||||
}
|
||||
|
||||
pub fn execute_prepared<T>(&self, event_name: &str,event: &PreparedEventQuery<T>) -> Result<Vec<T::Response>, PluginError> where T: Event {
|
||||
Ok(self.plugins.iter().map(|plugin| {
|
||||
plugin.execute_prepared(event_name, event)
|
||||
}).collect::<Result<Vec<Vec<T::Response>>, _>>()?.into_iter().flatten().collect::<Vec<T::Response>>())
|
||||
let mut out = Vec::new();
|
||||
for plugin in &self.plugins {
|
||||
let exe = plugin.execute_prepared(event_name, event)?;
|
||||
out.extend(exe);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
pub fn execute_event<T>(&self, event_name: &str,event: &T) -> Result<Vec<T::Response>, PluginError> where T: Event {
|
||||
|
@ -1,4 +1,4 @@
|
||||
use std::{marker::PhantomData, sync::Arc};
|
||||
use std::{collections::HashSet, marker::PhantomData, sync::Arc};
|
||||
|
||||
use error::RuntimeError;
|
||||
use parking_lot::Mutex;
|
||||
@ -11,15 +11,15 @@ use common_api::{Action, Event};
|
||||
pub type Function<'a> = Func<'a, (i32, u32), i32>;
|
||||
|
||||
#[derive(Clone)]
|
||||
// This tructure represent the WASM State of the plugin.
|
||||
// This structure represent the WASM State of the plugin.
|
||||
pub struct PluginModule {
|
||||
wasm_instance: Arc<Mutex<Instance>>,
|
||||
events: Vec<String>,
|
||||
events: HashSet<String>,
|
||||
}
|
||||
|
||||
impl PluginModule {
|
||||
|
||||
// This function take bytes from a WASM File and compile them
|
||||
// This function takes bytes from a WASM File and compile them
|
||||
pub fn new(wasm_data: &Vec<u8>) -> Result<Self,PluginModuleError> {
|
||||
let module = compile(&wasm_data).map_err(|e| PluginModuleError::Compile(e))?;
|
||||
let instance = module
|
||||
@ -33,7 +33,7 @@ impl PluginModule {
|
||||
})
|
||||
}
|
||||
|
||||
// This function try to execute an event for the current module will return None if the event doesn't exists
|
||||
// This function tries to execute an event for the current module. Will return None if the event doesn't exists
|
||||
pub fn try_execute<T>(
|
||||
&self,
|
||||
event_name: &str,
|
||||
@ -42,7 +42,7 @@ impl PluginModule {
|
||||
where
|
||||
T: Event,
|
||||
{
|
||||
if !self.events.iter().any(|x| x == event_name) {
|
||||
if !self.events.contains(event_name) {
|
||||
return None;
|
||||
}
|
||||
let bytes = {
|
||||
@ -61,14 +61,14 @@ impl PluginModule {
|
||||
}
|
||||
}
|
||||
|
||||
// This structure represent a Pre-encoded event object (Usefull to avoid reencoding for each module in every plugin)
|
||||
// This structure represent a Pre-encoded event object (Useful to avoid reencoding for each module in every plugin)
|
||||
pub struct PreparedEventQuery<T> {
|
||||
bytes: Vec<u8>,
|
||||
_phantom: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<T: Event> PreparedEventQuery<T> {
|
||||
// Create a prepared query from a event reference (Encode to bytes the struct)
|
||||
// Create a prepared query from an event reference (Encode to bytes the struct)
|
||||
// This Prepared Query is used by the `try_execute` method in `PluginModule`
|
||||
pub fn new(event: &T) -> Result<Self, PluginError>
|
||||
where
|
||||
|
@ -6,7 +6,7 @@ edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
common-api = { path = "../common/common-api" }
|
||||
plugin_proc = { path = "./plugin_proc"}
|
||||
plugin-api-derive = { path = "./plugin-api-derive"}
|
||||
serde = {version = "1.0.118", features = ["derive"]}
|
||||
bincode = "1.3.1"
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
[package]
|
||||
name = "plugin_proc"
|
||||
name = "plugin-api-derive"
|
||||
version = "0.1.0"
|
||||
authors = ["ccgauche <gaucheron.laurent@gmail.com>"]
|
||||
edition = "2018"
|
@ -1,13 +1,12 @@
|
||||
#![feature(const_fn)]
|
||||
|
||||
|
||||
pub extern crate plugin_proc;
|
||||
pub extern crate plugin_api_derive;
|
||||
pub extern crate common_api;
|
||||
|
||||
pub use common_api::*;
|
||||
|
||||
|
||||
pub use plugin_proc::*;
|
||||
pub use plugin_api_derive::*;
|
||||
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
|
Loading…
Reference in New Issue
Block a user