Skip to main content

Host

Struct Host 

Source
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

Source

pub fn current() -> Option<Self>

The host of this plugin; None before Anvil called anvil_plugin_load.

Source

pub fn c_api(&self) -> Table<AnvilHost>

Anvil’s C table, for what this crate does not wrap.

Source

pub fn raw_plugin(&self) -> *mut AnvilPlugin

This plugin’s handle, the self of every C host call.

Source

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.

Source

pub fn anvil_version(&self) -> &'static str

Anvil’s version (SemVer).

Source

pub fn game(&self) -> GameInfo

The running game executable.

Source

pub fn plugin_dir(&self) -> PathBuf

The plugin’s folder (read-only).

Source

pub fn data_dir(&self) -> PathBuf

The plugin’s writable anvil/data/<id>/, created on the first call.

Source

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!(…)).

Source

pub fn debug(&self, message: impl Display)

A debug line.

Source

pub fn info(&self, message: impl Display)

An information line.

Source

pub fn warn(&self, message: impl Display)

A warning.

Source

pub fn error(&self, message: impl Display)

An error line.

Source

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.

Source

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)?;
Source

pub fn mid_hook<F>(&self, site: AnvilSymbol, callback: F) -> Result<Change>
where F: Fn(&mut Cpu) + Send + Sync + 'static,

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.

Source

pub fn replace_block<F>( &self, block: AnvilSymbol, callback: F, ) -> Result<Change>
where F: Fn(&mut Cpu) + Send + Sync + 'static,

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).

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub unsafe fn mid_hook_at<F>( &self, address: NonNull<c_void>, expected: &[u8], callback: F, ) -> Result<Change>
where F: Fn(&mut Cpu) + Send + Sync + 'static,

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.

Source

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.

Source

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.

Source

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.

Source

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).

Source

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).

Source

pub fn value(&self, symbol: AnvilSymbol, name: &str) -> Result<u64>

A value of symbols.toml values (a rip_relative value is an RVA).

Source

pub fn setting_bool(&self, setting: AnvilSetting) -> bool

A bool setting (false for an unknown one or one of another type).

Source

pub fn setting_int(&self, setting: AnvilSetting) -> i64

An int, bool, enum index (<NAME>_<OPTION>) or key code.

Source

pub fn setting_float(&self, setting: AnvilSetting) -> f64

A float or int setting.

Source

pub fn setting_string(&self, setting: AnvilSetting) -> String

A string, an enum option’s name or a key (“Ctrl+F12”).

Source

pub fn setting_key(&self, setting: AnvilSetting) -> Key

A key setting.

Source

pub fn subscribe_setting<F>( &self, setting: AnvilSetting, handler: F, ) -> Result<()>
where F: Fn() + Send + Sync + 'static,

handler runs after a restart = false setting changes (in the overlay), on the thread that changed it; kept for the process.

Source

pub fn subscribe<P, F>(&self, event: Event<P>, handler: F) -> Result<()>
where P: Payload, F: Fn(&P) + Send + Sync + 'static,

handler(&payload) runs for each event, synchronously on the publishing thread; kept for the process.

Source

pub fn subscribe_raw<F>(&self, event: &str, handler: F) -> Result<()>
where F: Fn(&str, *const c_void) + Send + Sync + 'static,

handler(name, payload) for any event name, with the publisher’s raw payload pointer (valid during the handler).

Source

pub fn publish(&self, event: &str) -> Result<()>

Publishes <plugin-id>.<name> with no payload (NULL), which every Payload::read answers without reading.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for Host

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Host

Source§

impl Debug for Host

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Send for Host

Source§

impl Sync for Host

Auto Trait Implementations§

§

impl Forget for Host

§

impl Freeze for Host

§

impl Move for Host

§

impl RefUnwindSafe for Host

§

impl Unpin for Host

§

impl UnsafeUnpin for Host

§

impl UnwindSafe for Host

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.