summaryrefslogtreecommitdiff
path: root/rust/src
diff options
context:
space:
mode:
authorMason Reed <mason@vector35.com>2026-02-22 22:42:35 -0800
committerMason Reed <35282038+emesare@users.noreply.github.com>2026-02-23 00:09:44 -0800
commitd0eec62d6df0edbbb76dfeac58879797e90a8594 (patch)
tree03cf8d04eb7d33ac0785f2eede92ecbd552a48b0 /rust/src
parentf3e0bf4ab35d50b11b2dbde41a699ab7497208d1 (diff)
Add global plugin command type
Register plugins which are available outside the context of a binary view
Diffstat (limited to 'rust/src')
-rw-r--r--rust/src/command.rs42
1 files changed, 37 insertions, 5 deletions
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<C: GlobalCommand>(name: &str, desc: &str, command: C) {
+ extern "C" fn cb_action<C>(ctxt: *mut c_void)
+ where
+ C: GlobalCommand,
+ {
+ let cmd = unsafe { &*(ctxt as *const C) };
+ cmd.action();
+ }
+
+ extern "C" fn cb_valid<C>(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::<C>),
+ Some(cb_valid::<C>),
+ 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);