From d0eec62d6df0edbbb76dfeac58879797e90a8594 Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Sun, 22 Feb 2026 22:42:35 -0800 Subject: Add global plugin command type Register plugins which are available outside the context of a binary view --- rust/src/command.rs | 42 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) (limited to 'rust/src') diff --git a/rust/src/command.rs b/rust/src/command.rs index 9f585633..ac9870fa 100644 --- a/rust/src/command.rs +++ b/rust/src/command.rs @@ -32,11 +32,7 @@ //! //! The return value of these functions should indicate whether they successfully initialized themselves. -use binaryninjacore_sys::{ - BNBinaryView, BNFunction, BNProject, BNRegisterPluginCommand, - BNRegisterPluginCommandForAddress, BNRegisterPluginCommandForFunction, - BNRegisterPluginCommandForProject, BNRegisterPluginCommandForRange, -}; +use binaryninjacore_sys::*; use crate::binary_view::BinaryView; use crate::function::Function; @@ -46,6 +42,42 @@ use std::ops::Range; use std::os::raw::c_void; use std::ptr::NonNull; +pub trait GlobalCommand: 'static + Sync { + fn action(&self); + fn valid(&self) -> bool; +} + +pub fn register_global_command(name: &str, desc: &str, command: C) { + extern "C" fn cb_action(ctxt: *mut c_void) + where + C: GlobalCommand, + { + let cmd = unsafe { &*(ctxt as *const C) }; + cmd.action(); + } + + extern "C" fn cb_valid(ctxt: *mut c_void) -> bool + where + C: GlobalCommand, + { + let cmd = unsafe { &*(ctxt as *const C) }; + cmd.valid() + } + + let name = name.to_cstr(); + let desc = desc.to_cstr(); + let ctxt = Box::into_raw(Box::new(command)); + unsafe { + BNRegisterPluginCommandGlobal( + name.as_ptr(), + desc.as_ptr(), + Some(cb_action::), + Some(cb_valid::), + ctxt as *mut _, + ); + } +} + /// The trait required for generic commands. See [register_command] for example usage. pub trait Command: 'static + Sync { fn action(&self, view: &BinaryView); -- cgit v1.3.1