pub struct Amx { /* private fields */ }Expand description
Wrapper over the raw *mut AMX and the exported function table.
Implementations§
Source§impl Amx
impl Amx
Sourcepub fn new(ptr: *mut AMX, fn_table: usize) -> Amx
pub fn new(ptr: *mut AMX, fn_table: usize) -> Amx
Builds the wrapper.
ptr is the pointer received in callbacks such as AmxLoad; fn_table
is the address resolved during plugin initialization (typically stored
in a global AtomicUsize read in Load() from
crate::consts::ServerData::AmxExports).
Sourcepub fn register(&self, natives: &[AMX_NATIVE_INFO]) -> Result<(), AmxError>
pub fn register(&self, natives: &[AMX_NATIVE_INFO]) -> Result<(), AmxError>
Registers plugin natives in the VM via amx_Register.
Generally called in AmxLoad — the #[native] macro + initialize_plugin!
build the list automatically; only call manually from raw code.
§Errors
Propagates any AmxError returned by amx_Register — typically
AmxError::NotFound if a listed native is not declared in the script,
or VM state errors if called outside the load cycle.
Sourcepub fn exec(&self, index: AmxExecIdx) -> Result<i32, AmxError>
pub fn exec(&self, index: AmxExecIdx) -> Result<i32, AmxError>
Executes the public function identified by index in the VM.
Returns the Pawn return value (i32). Arguments must have been pushed
via push (in reverse order) and Allocator (for strings/arrays)
before this call.
§Errors
Propagates any AmxError from script execution — notably
Exit/Assert (Pawn aborted), StackError/StackLow/HeapLow
(stack or heap overflow), Divide, Native (a called native
returned an error) or Index if index does not match a valid function.
Sourcepub fn find_native(&self, name: &str) -> Result<i32, AmxError>
pub fn find_native(&self, name: &str) -> Result<i32, AmxError>
Index of a native by name (resolved via amx_FindNative).
§Errors
AmxError::NotFound if name contains an interior NUL byte or if the
native is not registered in the VM.
Sourcepub fn call_native(&self, name: &str, params: &[i32]) -> Result<i32, AmxError>
pub fn call_native(&self, name: &str, params: &[i32]) -> Result<i32, AmxError>
Calls a native registered by another plugin in the same AMX.
SA-MP plugins inject their natives into every loaded AMX via
amx_Register, which writes a host function pointer into the
native’s entry inside the AMX_HEADER natives table. This helper
resolves the name through amx_FindNative, reads that function
pointer back, builds the params block in the AMX convention
(first cell = argc * sizeof(cell), then the arguments), and
invokes the native.
Integer arguments are passed as their i32 value. Floats are
passed bit-cast to i32 (use f32::to_bits then
i32::from_ne_bytes on to_ne_bytes, or f32::to_bits() as i32).
String and array arguments are AMX cell addresses returned by
Allocator::allot_string/Allocator::allot_buffer — same
marshalling as for exec_public.
§Example
// Calling Streamer_CreateDynamicObject from a Rust plugin
fn on_amx_load(&mut self, amx: &Amx) -> AmxResult<()> {
let model_id: i32 = 1337;
#[allow(clippy::cast_possible_wrap)]
let x = 100.0_f32.to_bits() as i32;
let y = 200.0_f32.to_bits() as i32;
let z = 10.0_f32.to_bits() as i32;
let object_id = amx.call_native(
"Streamer_CreateDynamicObject",
&[model_id, x, y, z, 0, 0, 0],
)?;
log::info!("created dynamic object id={object_id}");
Ok(())
}§Errors
AmxError::NotFoundifnamecontains an interior NUL byte, the native is not registered, or its address is still zero (registered name but no host pointer attached).AmxError::MemoryAccessif the AMX header cannot be read.AmxError::Indexif the resolved index is out of range for the natives table reported by the AMX header.- Any
AmxErrorpropagated from the called native viaamx.error(re-raised by the caller throughamx_try!).
Sourcepub fn find_public(&self, name: &str) -> Result<AmxExecIdx, AmxError>
pub fn find_public(&self, name: &str) -> Result<AmxExecIdx, AmxError>
Index of a public function by name — pass the result to exec.
use samp_sdk::amx::Amx;
use samp_sdk::error::AmxResult;
fn has_on_player_connect(amx: &Amx) -> AmxResult<bool> {
let idx = amx.find_public("OnPlayerConnect")?;
Ok(i32::from(idx) >= 0)
}§Errors
AmxError::NotFound if name contains an interior NUL byte or if the
public function is not declared in the Pawn script.
Sourcepub fn find_pubvar<T>(&self, name: &str) -> Result<Ref<'_, T>, AmxError>where
T: AmxPrimitive,
pub fn find_pubvar<T>(&self, name: &str) -> Result<Ref<'_, T>, AmxError>where
T: AmxPrimitive,
Ref<T> pointing to a public variable declared in the Pawn script.
let version = amx.find_pubvar::<f32>("my_plugin_version")?;
// outdated
if *version < 1.0 { }§Errors
AmxError::NotFound if name contains an interior NUL byte or if the
pubvar is not declared. AmxError::MemoryAccess if the address returned
by the VM is invalid.
Sourcepub fn allocator(&self) -> Allocator<'_>
pub fn allocator(&self) -> Allocator<'_>
Creates an Allocator bound to this Amx.
All memory allocated via Allocator::allot/Allocator::allot_buffer/
Allocator::allot_string is released automatically when the
Allocator goes out of scope (Drop). Keep it alive while using the
returned references.
Sourcepub fn amx(&self) -> Option<NonNull<AMX>>
pub fn amx(&self) -> Option<NonNull<AMX>>
Raw pointer to the AMX (non-null) or None if constructed with null.
Sourcepub fn header(&self) -> Option<NonNull<AMX_HEADER>>
pub fn header(&self) -> Option<NonNull<AMX_HEADER>>
Raw pointer to the AMX_HEADER of the loaded .amx.
Sourcepub fn cip(&self) -> Option<u32>
pub fn cip(&self) -> Option<u32>
Current instruction pointer (cip) — a code-segment offset in a debug
hook. Read as u32.
Sourcepub fn frame(&self) -> Option<i32>
pub fn frame(&self) -> Option<i32>
Current frame pointer (frm); local/argument symbols are addressed
relative to it.
Sourcepub fn stp(&self) -> Option<i32>
pub fn stp(&self) -> Option<i32>
Top of the stack (stp) — the upper bound of the data address space.
Sourcepub fn read_cell(&self, addr: i32) -> Option<i32>
pub fn read_cell(&self, addr: i32) -> Option<i32>
Reads a 32-bit cell from the data segment at addr, validating bounds
like amx_GetAddr. Returns None if the address is inaccessible.
Reads byte-wise (no alignment assumption). Usable from a debug hook.
Sourcepub fn write_cell(&self, addr: i32, value: i32) -> bool
pub fn write_cell(&self, addr: i32, value: i32) -> bool
Writes a 32-bit cell to the data segment at addr, validating bounds
like amx_GetAddr. Returns false if the address is inaccessible.
Writes byte-wise (no alignment assumption). Usable from a debug hook to edit a variable while the VM is paused.
Sourcepub fn install_debug_hook(&self, cb: extern "C" fn(*mut AMX) -> i32)
pub fn install_debug_hook(&self, cb: extern "C" fn(*mut AMX) -> i32)
Installs a debug hook callback into this VM (amx->debug = cb), the
equivalent of amx_SetDebugHook. The VM then calls cb on every line,
provided the .amx was compiled with -d2/-d3.
The callback runs on the VM thread and crosses the FFI boundary, so it must never unwind (no panics).
Sourcepub fn remove_debug_hook(&self)
pub fn remove_debug_hook(&self)
Removes a previously installed debug hook, restoring amx->debug to a
no-op callback that returns AMX_ERR_NONE.