added basic retreive

This commit is contained in:
ccgauche
2021-02-15 17:38:55 +01:00
committed by Marcel Märtens
parent 72bd0f42fc
commit a067a20b69
8 changed files with 89 additions and 12 deletions

View File

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

View File

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