From 9bf63533e1cc888de56767055b0c6bfbd700ebb2 Mon Sep 17 00:00:00 2001 From: eater <=@eater.me> Date: Fri, 28 Feb 2020 01:16:41 +0100 Subject: [PATCH] unpack null's correctly --- tes3mp-plugin/src/plugin/mod.rs | 157 +++++++++++++------------------- 1 file changed, 65 insertions(+), 92 deletions(-) diff --git a/tes3mp-plugin/src/plugin/mod.rs b/tes3mp-plugin/src/plugin/mod.rs index d615f17..7b93694 100644 --- a/tes3mp-plugin/src/plugin/mod.rs +++ b/tes3mp-plugin/src/plugin/mod.rs @@ -76,6 +76,20 @@ macro_rules! call_instance { }; } +/// Returns Some(string) if $argument is not null else returns None +#[macro_export] +macro_rules! maybe_string { + ($argument:tt) => { + unsafe { + if $argument.is_null() { + None + } else { + Some(CStr::from_ptr($argument).to_str().unwrap_or_default()) + } + } + }; +} + /// /// create and bind C symbols to given struct, should implement Events /// @@ -114,97 +128,78 @@ macro_rules! use_events { #[no_mangle] #[allow(non_snake_case)] pub fn OnActorAI(player_id: c_ushort, description: *const i8) { - call_instance!(on_actor_ai, player_id, unsafe { - CStr::from_ptr(description).to_str().unwrap_or_default() - }); + call_instance!(on_actor_ai, player_id, maybe_string!(description)); } #[no_mangle] #[allow(non_snake_case)] pub fn OnActorCellChange(player_id: c_ushort, description: *const i8) { - call_instance!(on_actor_cell_change, player_id, unsafe { - CStr::from_ptr(description).to_str().unwrap_or_default() - }); + call_instance!(on_actor_cell_change, player_id, maybe_string!(description)); } #[no_mangle] #[allow(non_snake_case)] pub fn OnActorDeath(player_id: c_ushort, description: *const i8) { - call_instance!(on_actor_death, player_id, unsafe { - CStr::from_ptr(description).to_str().unwrap_or_default() - }); + call_instance!(on_actor_death, player_id, maybe_string!(description)); } #[no_mangle] #[allow(non_snake_case)] pub fn OnActorEquipment(player_id: c_ushort, description: *const i8) { - call_instance!(on_actor_equipment, player_id, unsafe { - CStr::from_ptr(description).to_str().unwrap_or_default() - }); + call_instance!(on_actor_equipment, player_id, maybe_string!(description)); } #[no_mangle] #[allow(non_snake_case)] pub fn OnActorList(player_id: c_ushort, description: *const i8) { - call_instance!(on_actor_list, player_id, unsafe { - CStr::from_ptr(description).to_str().unwrap_or_default() - }); + call_instance!(on_actor_list, player_id, maybe_string!(description)); } #[no_mangle] #[allow(non_snake_case)] pub fn OnActorTest(player_id: c_ushort, description: *const i8) { - call_instance!(on_actor_test, player_id, unsafe { - CStr::from_ptr(description).to_str().unwrap_or_default() - }); + call_instance!(on_actor_test, player_id, maybe_string!(description)); } #[no_mangle] #[allow(non_snake_case)] pub fn OnCellDeletion(description: *const i8) { - call_instance!(on_cell_deletion, unsafe { - CStr::from_ptr(description).to_str().unwrap_or_default() - }); + call_instance!(on_cell_deletion, maybe_string!(description)); } #[no_mangle] #[allow(non_snake_case)] pub fn OnCellLoad(description: *const i8) { - call_instance!(on_cell_load, unsafe { - CStr::from_ptr(description).to_str().unwrap_or_default() - }); + call_instance!(on_cell_load, maybe_string!(description)); } #[no_mangle] #[allow(non_snake_case)] pub fn OnCellUnload(description: *const i8) { - call_instance!(on_cell_unload, unsafe { - CStr::from_ptr(description).to_str().unwrap_or_default() - }); + call_instance!(on_cell_unload, maybe_string!(description)); } #[no_mangle] #[allow(non_snake_case)] pub fn OnContainer(player_id: c_ushort, description: *const i8) { - call_instance!(on_container, player_id, unsafe { - CStr::from_ptr(description).to_str().unwrap_or_default() - }); + call_instance!(on_container, player_id, maybe_string!(description)); } #[no_mangle] #[allow(non_snake_case)] pub fn OnDoorState(player_id: c_ushort, description: *const i8) { - call_instance!(on_door_state, player_id, unsafe { - CStr::from_ptr(description).to_str().unwrap_or_default() - }); + call_instance!(on_door_state, player_id, maybe_string!(description)); } #[no_mangle] #[allow(non_snake_case)] pub fn OnGUIAction(player_id: c_ushort, message_box_id: c_int, data: *const i8) { - call_instance!(on_gui_action, player_id, message_box_id, unsafe { - CStr::from_ptr(data).to_str().unwrap_or_default() - }); + call_instance!( + on_gui_action, + player_id, + message_box_id, + maybe_string!(data) + ); } #[no_mangle] @@ -216,65 +211,49 @@ macro_rules! use_events { #[no_mangle] #[allow(non_snake_case)] pub fn OnObjectActivate(player_id: c_ushort, description: *const i8) { - call_instance!(on_object_activate, player_id, unsafe { - CStr::from_ptr(description).to_str().unwrap_or_default() - }); + call_instance!(on_object_activate, player_id, maybe_string!(description)); } #[no_mangle] #[allow(non_snake_case)] pub fn OnObjectDelete(player_id: c_ushort, description: *const i8) { - call_instance!(on_object_delete, player_id, unsafe { - CStr::from_ptr(description).to_str().unwrap_or_default() - }); + call_instance!(on_object_delete, player_id, maybe_string!(description)); } #[no_mangle] #[allow(non_snake_case)] pub fn OnObjectLock(player_id: c_ushort, description: *const i8) { - call_instance!(on_object_lock, player_id, unsafe { - CStr::from_ptr(description).to_str().unwrap_or_default() - }); + call_instance!(on_object_lock, player_id, maybe_string!(description)); } #[no_mangle] #[allow(non_snake_case)] pub fn OnObjectPlace(player_id: c_ushort, description: *const i8) { - call_instance!(on_object_place, player_id, unsafe { - CStr::from_ptr(description).to_str().unwrap_or_default() - }); + call_instance!(on_object_place, player_id, maybe_string!(description)); } #[no_mangle] #[allow(non_snake_case)] pub fn OnObjectScale(player_id: c_ushort, description: *const i8) { - call_instance!(on_object_scale, player_id, unsafe { - CStr::from_ptr(description).to_str().unwrap_or_default() - }); + call_instance!(on_object_scale, player_id, maybe_string!(description)); } #[no_mangle] #[allow(non_snake_case)] pub fn OnObjectSpawn(player_id: c_ushort, description: *const i8) { - call_instance!(on_object_spawn, player_id, unsafe { - CStr::from_ptr(description).to_str().unwrap_or_default() - }); + call_instance!(on_object_spawn, player_id, maybe_string!(description)); } #[no_mangle] #[allow(non_snake_case)] pub fn OnObjectState(player_id: c_ushort, description: *const i8) { - call_instance!(on_object_state, player_id, unsafe { - CStr::from_ptr(description).to_str().unwrap_or_default() - }); + call_instance!(on_object_state, player_id, maybe_string!(description)); } #[no_mangle] #[allow(non_snake_case)] pub fn OnObjectTrap(player_id: c_ushort, description: *const i8) { - call_instance!(on_object_trap, player_id, unsafe { - CStr::from_ptr(description).to_str().unwrap_or_default() - }); + call_instance!(on_object_trap, player_id, maybe_string!(description)); } #[no_mangle] @@ -406,9 +385,7 @@ macro_rules! use_events { #[no_mangle] #[allow(non_snake_case)] pub fn OnPlayerSendMessage(player_id: c_ushort, message: *const i8) { - call_instance!(on_player_send_message, player_id, unsafe { - CStr::from_ptr(message).to_str().unwrap_or_default() - }); + call_instance!(on_player_send_message, player_id, maybe_string!(message)); } #[no_mangle] @@ -450,17 +427,13 @@ macro_rules! use_events { #[no_mangle] #[allow(non_snake_case)] pub fn OnServerScriptCrash(error: *const i8) { - call_instance!(on_server_script_crash, unsafe { - CStr::from_ptr(error).to_str().unwrap_or_default() - }); + call_instance!(on_server_script_crash, maybe_string!(error)); } #[no_mangle] #[allow(non_snake_case)] pub fn OnVideoPlay(player_id: c_ushort, description: *const i8) { - call_instance!(on_video_play, player_id, unsafe { - CStr::from_ptr(description).to_str().unwrap_or_default() - }); + call_instance!(on_video_play, player_id, maybe_string!(description)); } #[no_mangle] @@ -488,32 +461,32 @@ pub trait Events: Sized { fn new() -> Self; fn on_any(&mut self, event_name: &str) {} - fn on_actor_ai(&mut self, player_id: c_ushort, description: &str) {} - fn on_actor_cell_change(&mut self, player_id: c_ushort, description: &str) {} - fn on_actor_death(&mut self, player_id: c_ushort, description: &str) {} - fn on_actor_equipment(&mut self, player_id: c_ushort, description: &str) {} - fn on_actor_list(&mut self, player_id: c_ushort, description: &str) {} - fn on_actor_test(&mut self, player_id: c_ushort, description: &str) {} + fn on_actor_ai(&mut self, player_id: c_ushort, description: Option<&str>) {} + fn on_actor_cell_change(&mut self, player_id: c_ushort, description: Option<&str>) {} + fn on_actor_death(&mut self, player_id: c_ushort, description: Option<&str>) {} + fn on_actor_equipment(&mut self, player_id: c_ushort, description: Option<&str>) {} + fn on_actor_list(&mut self, player_id: c_ushort, description: Option<&str>) {} + fn on_actor_test(&mut self, player_id: c_ushort, description: Option<&str>) {} - fn on_cell_deletion(&mut self, description: &str) {} - fn on_cell_load(&mut self, description: &str) {} - fn on_cell_unload(&mut self, description: &str) {} + fn on_cell_deletion(&mut self, description: Option<&str>) {} + fn on_cell_load(&mut self, description: Option<&str>) {} + fn on_cell_unload(&mut self, description: Option<&str>) {} - fn on_container(&mut self, player_id: c_ushort, description: &str) {} - fn on_door_state(&mut self, player_id: c_ushort, description: &str) {} + fn on_container(&mut self, player_id: c_ushort, description: Option<&str>) {} + fn on_door_state(&mut self, player_id: c_ushort, description: Option<&str>) {} - fn on_gui_action(&mut self, player_id: c_ushort, message_box_id: c_int, data: &str) {} + fn on_gui_action(&mut self, player_id: c_ushort, message_box_id: c_int, data: Option<&str>) {} fn on_mp_num_increment(&mut self, current_mp_num: c_int) {} - fn on_object_activate(&mut self, player_id: c_ushort, description: &str) {} - fn on_object_delete(&mut self, player_id: c_ushort, description: &str) {} - fn on_object_lock(&mut self, player_id: c_ushort, description: &str) {} - fn on_object_place(&mut self, player_id: c_ushort, description: &str) {} - fn on_object_scale(&mut self, player_id: c_ushort, description: &str) {} - fn on_object_spawn(&mut self, player_id: c_ushort, description: &str) {} - fn on_object_state(&mut self, player_id: c_ushort, description: &str) {} - fn on_object_trap(&mut self, player_id: c_ushort, description: &str) {} + fn on_object_activate(&mut self, player_id: c_ushort, description: Option<&str>) {} + fn on_object_delete(&mut self, player_id: c_ushort, description: Option<&str>) {} + fn on_object_lock(&mut self, player_id: c_ushort, description: Option<&str>) {} + fn on_object_place(&mut self, player_id: c_ushort, description: Option<&str>) {} + fn on_object_scale(&mut self, player_id: c_ushort, description: Option<&str>) {} + fn on_object_spawn(&mut self, player_id: c_ushort, description: Option<&str>) {} + fn on_object_state(&mut self, player_id: c_ushort, description: Option<&str>) {} + fn on_object_trap(&mut self, player_id: c_ushort, description: Option<&str>) {} fn on_player_attribute(&mut self, player_id: c_ushort) {} fn on_player_book(&mut self, player_id: c_ushort) {} @@ -536,7 +509,7 @@ pub trait Events: Sized { fn on_player_reputation(&mut self, player_id: c_ushort) {} fn on_player_rest(&mut self, player_id: c_ushort) {} fn on_player_resurrect(&mut self, player_id: c_ushort) {} - fn on_player_send_message(&mut self, player_id: c_ushort, message: &str) {} + fn on_player_send_message(&mut self, player_id: c_ushort, message: Option<&str>) {} fn on_player_shapeshift(&mut self, player_id: c_ushort) {} fn on_player_skill(&mut self, player_id: c_ushort) {} fn on_player_spellbook(&mut self, player_id: c_ushort) {} @@ -549,9 +522,9 @@ pub trait Events: Sized { fn on_server_exit(&mut self, is_error: bool) {} fn on_server_init(&mut self) {} fn on_server_post_init(&mut self) {} - fn on_server_script_crash(&mut self, error: &str) {} + fn on_server_script_crash(&mut self, error: Option<&str>) {} - fn on_video_play(&mut self, player_id: c_ushort, description: &str) {} + fn on_video_play(&mut self, player_id: c_ushort, description: Option<&str>) {} fn on_world_kill_count(&mut self, player_id: c_ushort) {} fn on_world_map(&mut self, player_id: c_ushort) {}