pub struct Host { /* private fields */ }Expand description
The host: Anvil’s table and this plugin’s handle. It is Copy: capture it
in callbacks, or get it anywhere with Host::current once the plugin is
loading.
Logging never fails. Every other call returns the Error
Anvil answered with.
Implementations§
Source§impl Host
impl Host
Sourcepub fn current() -> Option<Self>
pub fn current() -> Option<Self>
The host of this plugin; None before Anvil called anvil_plugin_load.
Sourcepub fn raw_plugin(&self) -> *mut AnvilPlugin
pub fn raw_plugin(&self) -> *mut AnvilPlugin
This plugin’s handle, the self of every C host call.
Sourcepub fn module(&self, name: &str) -> Result<Host>
pub fn module(&self, name: &str) -> Result<Host>
The host of module name, which plugin.toml declares in
[[modules]] (Anvil 1.22.0; ADR 0011). Every call on
the returned Host acts for the module: Anvil suspends its changes by
the module’s own affects_gameplay, allow_ironman and
allow_multiplayer, anvil.suspended reaches what it subscribes, and a
service that checks its caller sees the module. Settings, symbols,
folders and the names of services and events stay the plugin’s.
ErrorCode::Disabled when the player
turned the module off (its code should not run then),
ErrorCode::NotDeclared for a name
plugin.toml does not declare or when self is a module already, and
ErrorCode::Unsupported from an Anvil
without modules.
Sourcepub fn anvil_version(&self) -> &'static str
pub fn anvil_version(&self) -> &'static str
Anvil’s version (SemVer).
Sourcepub fn plugin_dir(&self) -> PathBuf
pub fn plugin_dir(&self) -> PathBuf
The plugin’s folder (read-only).
Sourcepub fn data_dir(&self) -> PathBuf
pub fn data_dir(&self) -> PathBuf
The plugin’s writable anvil/data/<id>/, created on the first call.
Sourcepub fn log(&self, level: LogLevel, message: impl Display)
pub fn log(&self, level: LogLevel, message: impl Display)
One line of anvil.log, under the plugin’s id: host.log(level, "text")
or host.log(level, format_args!(…)).
Sourcepub unsafe fn hook<F: FnPtr>(
&self,
hook: &'static FunctionHook<F>,
symbol: AnvilSymbol,
detour: F,
) -> Result<()>
pub unsafe fn hook<F: FnPtr>( &self, hook: &'static FunctionHook<F>, symbol: AnvilSymbol, detour: F, ) -> Result<()>
Hooks a function symbol: the game calls detour, which passes on
through hook.next(). With hook and detour of the symbol’s
generated type (sym::<Name>Fn), a detour of another shape does not
compile; but the symbol id is a plain u32, so nothing ties F to
symbol but the caller. hook_symbol is the safe
form, with the typed symbols of anvil gen --lang rust-sdk.
During load the change is staged and enabled with the others when
load returns; later it is enabled at once (or with its group).
§Safety
symbol is a function symbol whose declared signature F is: the
sym::<Name>Fn that anvil gen --lang rust writes for the same
symbol as sym::<NAME>. The game calls detour with the arguments
of that signature, and the hook’s next is the game’s function of it.
Sourcepub fn hook_symbol<F: FnPtr>(
&self,
hook: &'static FunctionHook<F>,
symbol: FnSymbol<F>,
detour: F,
) -> Result<()>
pub fn hook_symbol<F: FnPtr>( &self, hook: &'static FunctionHook<F>, symbol: FnSymbol<F>, detour: F, ) -> Result<()>
hook without unsafe: the symbol comes with its type,
sym::typed::<NAME> from anvil gen --lang rust-sdk, so hook and
detour must be of the symbol’s own sym::<Name>Fn and a detour of
another shape does not compile.
static SPEED_OBJECT: FunctionHook<sym::SpeedObjectFn> = FunctionHook::new();
host.hook_symbol(&SPEED_OBJECT, sym::typed::SPEED_OBJECT, on_speed_object)?;Sourcepub fn mid_hook<F>(&self, site: AnvilSymbol, callback: F) -> Result<Change>
pub fn mid_hook<F>(&self, site: AnvilSymbol, callback: F) -> Result<Change>
A mid hook: callback runs when the game reaches the site (a function
or site symbol with at least 5 declared bytes), before the code there.
It may run on several game threads at once. A panic in it is logged
once and the game carries on.
Sourcepub fn replace_block<F>(
&self,
block: AnvilSymbol,
callback: F,
) -> Result<Change>
pub fn replace_block<F>( &self, block: AnvilSymbol, callback: F, ) -> Result<Change>
A block replacement: the symbol’s declared bytes never run; callback
runs instead and the game resumes at the block’s resume or end
(unless the callback writes rip with Cpu::regs_mut).
Sourcepub unsafe fn redirect_call<F: FnPtr>(
&self,
hook: &'static FunctionHook<F>,
call_site: AnvilSymbol,
target: F,
) -> Result<()>
pub unsafe fn redirect_call<F: FnPtr>( &self, hook: &'static FunctionHook<F>, call_site: AnvilSymbol, target: F, ) -> Result<()>
A call redirect at a call rel32 site symbol: target gets the call,
hook.next() is the previous target.
§Safety
A call site declares no signature: F must be the type of the
function the site calls.
Sourcepub unsafe fn patch_bytes(
&self,
site: AnvilSymbol,
bytes: &[u8],
) -> Result<Change>
pub unsafe fn patch_bytes( &self, site: AnvilSymbol, bytes: &[u8], ) -> Result<Change>
A byte patch within the symbol’s declared bytes.
§Safety
The bytes become the game’s code or data at the site: they must be what the game can run or read there.
Sourcepub unsafe fn swap_pointer(
&self,
swap: &'static PointerSwap,
slot: AnvilSymbol,
value: *mut c_void,
) -> Result<()>
pub unsafe fn swap_pointer( &self, swap: &'static PointerSwap, slot: AnvilSymbol, value: *mut c_void, ) -> Result<()>
A pointer swap on a slot symbol (points_to): the slot holds value,
swap.previous() the rest of the chain.
§Safety
The game uses value as the slot’s pointer: it must be what the slot
points to (a function of the vtable entry’s type, an object), for as
long as the change lives.
Sourcepub unsafe fn replace_import<F: FnPtr>(
&self,
hook: &'static FunctionHook<F>,
module: &str,
function: &str,
replacement: F,
) -> Result<()>
pub unsafe fn replace_import<F: FnPtr>( &self, hook: &'static FunctionHook<F>, module: &str, function: &str, replacement: F, ) -> Result<()>
Replaces an import of eu4.exe, e.g. ("KERNEL32.dll", "GetTickCount")
(the module ignores case).
§Safety
F must be the type of the imported function.
Sourcepub unsafe fn hook_at<F: FnPtr>(
&self,
hook: &'static FunctionHook<F>,
address: NonNull<c_void>,
expected_prologue: &[u8],
detour: F,
) -> Result<()>
pub unsafe fn hook_at<F: FnPtr>( &self, hook: &'static FunctionHook<F>, address: NonNull<c_void>, expected_prologue: &[u8], detour: F, ) -> Result<()>
A function hook at a raw address outside the eu4.exe image (e.g. a
Direct3D 9 vtable entry); expected_prologue may be empty.
§Safety
address is the start of a function of type F that lives while the
change does.
Sourcepub unsafe fn mid_hook_at<F>(
&self,
address: NonNull<c_void>,
expected: &[u8],
callback: F,
) -> Result<Change>
pub unsafe fn mid_hook_at<F>( &self, address: NonNull<c_void>, expected: &[u8], callback: F, ) -> Result<Change>
A mid hook at a raw address outside the eu4.exe image; expected may
be empty.
§Safety
address is the start of an instruction that lives while the change does.
Sourcepub fn group<R>(&self, body: impl FnOnce() -> Result<R>) -> Result<R>
pub fn group<R>(&self, body: impl FnOnce() -> Result<R>) -> Result<R>
After load: the changes body requests are enabled together, or none
when it returns an error or panics (the panic goes on, logged as
panic: … unless an adapter of this crate catches it further out,
which logs it itself). Inside load
the load itself is the group: WrongPhase.
When the group’s changes are dropped (an error, a panic, or a commit
Anvil refuses), the FunctionHooks and PointerSwaps this thread
filled in body are free again: is_active is false and they take a
new request.
The group is the plugin’s, not this thread’s: a request from any of
the plugin’s threads while it is open (a panel click, a thread body
started) joins it, is enabled only with the commit and dropped with an
abort. Hook objects another thread filled keep their handles then:
remove() them before reusing them.
Sourcepub fn address(&self, symbol: AnvilSymbol) -> Option<NonNull<c_void>>
pub fn address(&self, symbol: AnvilSymbol) -> Option<NonNull<c_void>>
The verified address of a symbol; None for an unknown one, or one
symbols.toml declares absent on the running game build.
Sourcepub fn present(&self, symbol: AnvilSymbol) -> bool
pub fn present(&self, symbol: AnvilSymbol) -> bool
Whether the symbol is there on the running game build: check it before
using a symbol symbols.toml may declare absent.
Sourcepub unsafe fn function<F: FnPtr>(&self, symbol: AnvilSymbol) -> Option<F>
pub unsafe fn function<F: FnPtr>(&self, symbol: AnvilSymbol) -> Option<F>
The game function of a function symbol, to call.
§Safety
F is the symbol’s generated type (sym::<Name>Fn).
Sourcepub unsafe fn data<T>(&self, symbol: AnvilSymbol) -> Option<&'static T>
pub unsafe fn data<T>(&self, symbol: AnvilSymbol) -> Option<&'static T>
The game’s data at a data symbol, for the life of the process.
§Safety
The symbol’s memory holds a T, and nothing writes it while the
reference is used (data the game never changes, or read only on the
thread that writes it).
Sourcepub fn value(&self, symbol: AnvilSymbol, name: &str) -> Result<u64>
pub fn value(&self, symbol: AnvilSymbol, name: &str) -> Result<u64>
A value of symbols.toml values (a rip_relative value is an RVA).
Sourcepub fn setting_bool(&self, setting: AnvilSetting) -> bool
pub fn setting_bool(&self, setting: AnvilSetting) -> bool
A bool setting (false for an unknown one or one of another type).
Sourcepub fn setting_int(&self, setting: AnvilSetting) -> i64
pub fn setting_int(&self, setting: AnvilSetting) -> i64
An int, bool, enum index (<NAME>_<OPTION>) or key code.
Sourcepub fn setting_float(&self, setting: AnvilSetting) -> f64
pub fn setting_float(&self, setting: AnvilSetting) -> f64
A float or int setting.
Sourcepub fn setting_string(&self, setting: AnvilSetting) -> String
pub fn setting_string(&self, setting: AnvilSetting) -> String
A string, an enum option’s name or a key (“Ctrl+F12”).
Sourcepub fn setting_key(&self, setting: AnvilSetting) -> Key
pub fn setting_key(&self, setting: AnvilSetting) -> Key
A key setting.
Sourcepub fn subscribe_setting<F>(
&self,
setting: AnvilSetting,
handler: F,
) -> Result<()>
pub fn subscribe_setting<F>( &self, setting: AnvilSetting, handler: F, ) -> Result<()>
handler runs after a restart = false setting changes (in the
overlay), on the thread that changed it; kept for the process.
Sourcepub fn subscribe<P, F>(&self, event: Event<P>, handler: F) -> Result<()>
pub fn subscribe<P, F>(&self, event: Event<P>, handler: F) -> Result<()>
handler(&payload) runs for each event, synchronously on the
publishing thread; kept for the process.
Sourcepub fn subscribe_raw<F>(&self, event: &str, handler: F) -> Result<()>
pub fn subscribe_raw<F>(&self, event: &str, handler: F) -> Result<()>
handler(name, payload) for any event name, with the publisher’s raw
payload pointer (valid during the handler).
Sourcepub fn publish(&self, event: &str) -> Result<()>
pub fn publish(&self, event: &str) -> Result<()>
Publishes <plugin-id>.<name> with no payload (NULL), which every
Payload::read answers without reading.
Sourcepub unsafe fn publish_raw(
&self,
event: &str,
payload: *const c_void,
) -> Result<()>
pub unsafe fn publish_raw( &self, event: &str, payload: *const c_void, ) -> Result<()>
Publishes <plugin-id>.<name> with a payload its subscribers read
during the call: synchronously, on this thread, before it returns.
§Safety
payload is null or points to a value of the layout this event’s
subscribers read it as: the payload type its documentation (or C
header) names, which every Event of that name declares with
Event::new. For a type that starts with uint32_t size, the
size is the value’s own and at least that many bytes are readable.
It stays valid and unchanged until the call returns.
Sourcepub unsafe fn register_service<T: Sync>(
&self,
name: &str,
version: u32,
service: &'static T,
) -> Result<()>
pub unsafe fn register_service<T: Sync>( &self, name: &str, version: u32, service: &'static T, ) -> Result<()>
Registers a service <plugin-id>.<service> of major version.
§Safety
service is a #[repr(C)] struct whose first field is u32 size
(its size), followed by extern "C" functions that never unwind, as
the service’s C header describes it.
Sourcepub unsafe fn service<T>(&self, name: &CStr, version: u32) -> Option<Table<T>>
pub unsafe fn service<T>(&self, name: &CStr, version: u32) -> Option<Table<T>>
A service by name and major version, as a Table; the typed
wrappers (game::Game,
world::World,
panel::Panels,
benchmark::Benchmark) use it. Only the
table’s size is read here: a provider older than T has a shorter
table, and Table reads a field only when that size covers it.
§Safety
T is the service’s table type for that version (or a newer
release of the same version): a #[repr(C)] struct whose first field
is u32 size.