mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Force type-correct hooks, renamed proc macro
This commit is contained in:
parent
780f54ac90
commit
2743825181
@ -5,7 +5,7 @@ use quote::quote;
|
||||
use syn::{parse_macro_input, ItemFn};
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn export_function(_args: TokenStream, item: TokenStream) -> TokenStream {
|
||||
pub fn event_handler(_args: TokenStream, item: TokenStream) -> TokenStream {
|
||||
let parsed = parse_macro_input!(item as ItemFn);
|
||||
let fn_body = parsed.block; // function body
|
||||
let sig = parsed.sig; // function signature
|
||||
@ -16,11 +16,16 @@ pub fn export_function(_args: TokenStream, item: TokenStream) -> TokenStream {
|
||||
let out: proc_macro2::TokenStream = quote! {
|
||||
#[no_mangle]
|
||||
pub fn #fn_name(intern__ptr: i32, intern__len: u32) -> i32 {
|
||||
let input = plugin_rt::read_input(intern__ptr,intern__len).unwrap();
|
||||
let input = ::plugin_rt::read_input(intern__ptr,intern__len).unwrap();
|
||||
#[inline]
|
||||
fn inner(#fn_args) #fn_return {
|
||||
#fn_body
|
||||
}
|
||||
plugin_rt::write_output(&inner(input))
|
||||
// Artificially force the event handler to be type-correct
|
||||
fn force_event<E: ::plugin_rt::api::Event>(event: E, inner: fn(E) -> E::Response) -> E::Response {
|
||||
inner(event)
|
||||
}
|
||||
::plugin_rt::write_output(&force_event(input, inner))
|
||||
}
|
||||
};
|
||||
out.into()
|
||||
|
@ -3,13 +3,13 @@ pub extern crate plugin_rt;
|
||||
use plugin_rt::*;
|
||||
use plugin_api::{Action, events::*};
|
||||
|
||||
#[export_function]
|
||||
#[event_handler]
|
||||
pub fn on_load(load: PluginLoadEvent) -> () {
|
||||
send_actions(vec![Action::Print("This is a test".to_owned())]);
|
||||
println!("Hello world");
|
||||
}
|
||||
|
||||
#[export_function]
|
||||
#[event_handler]
|
||||
pub fn on_player_join(input: PlayerJoinEvent) -> PlayerJoinResult {
|
||||
send_actions(vec![Action::PlayerSendMessage(input.player_id,format!("Welcome {} on our server",input.player_name))]);
|
||||
if input.player_name == "Cheater123" {
|
||||
|
Loading…
Reference in New Issue
Block a user