mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
added basic retreive
This commit is contained in:
@ -20,6 +20,8 @@ pub fn on_command_testplugin(command: ChatCommandEvent) -> Result<Vec<String>, S
|
||||
)])
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[event_handler]
|
||||
pub fn on_player_join(input: PlayerJoinEvent) -> PlayerJoinResult {
|
||||
emit_action(Action::PlayerSendMessage(
|
||||
|
@ -2,6 +2,10 @@
|
||||
|
||||
pub extern crate plugin_derive;
|
||||
|
||||
pub mod retreive;
|
||||
|
||||
pub use retreive::*;
|
||||
|
||||
pub use plugin_api as api;
|
||||
pub use plugin_derive::*;
|
||||
|
||||
@ -10,6 +14,21 @@ use serde::{de::DeserializeOwned, Serialize};
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
extern "C" {
|
||||
fn raw_emit_actions(ptr: *const u8, len: usize);
|
||||
fn raw_retreive_action(ptr: *const u8, len: usize) -> (i32, u32);
|
||||
}
|
||||
|
||||
pub fn retreive_action<T: DeserializeOwned>(_actions: &api::Retreive) -> Result<T,bincode::Error> {
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
{
|
||||
let ret = bincode::serialize(&actions).expect("Can't serialize action in emit");
|
||||
unsafe {
|
||||
let (ptr,len) = raw_retreive_action(ret.as_ptr(), ret.len());
|
||||
let a = ::std::slice::from_raw_parts(ptr as _, len as _);
|
||||
bincode::deserialize(&a)
|
||||
}
|
||||
}
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
pub fn emit_action(action: api::Action) { emit_actions(vec![action]) }
|
||||
|
12
plugin/rt/src/retreive.rs
Normal file
12
plugin/rt/src/retreive.rs
Normal file
@ -0,0 +1,12 @@
|
||||
use crate::api::Retreive;
|
||||
|
||||
trait GetEntityName {
|
||||
fn get_entity_name(&self) -> String;
|
||||
}
|
||||
|
||||
impl GetEntityName for crate::api::event::Player {
|
||||
|
||||
fn get_entity_name(&self) -> String {
|
||||
crate::retreive_action(&Retreive::GetEntityName(self.id)).expect("Can't get entity name")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user