pub trait GameAPI {
// Required methods
fn registry_domain(
&self,
name: &str,
) -> Result<Vec<RegistryEntry>, ServiceError>;
fn registry_file(
&self,
domain: &str,
filename: &str,
) -> Result<Vec<u8>, ServiceError>;
fn register_service(
&mut self,
domain: &str,
service: Box<dyn Service>,
) -> Result<(), ServiceError>;
fn get_service(&self, domain: &str) -> Result<&dyn Service, ServiceError>;
fn has_service(&self, domain: &str) -> bool;
fn log(&self, level: LogLevel, message: &str);
}Expand description
Host-side API surface provided to WASM plugins.
During the build/ready/finish lifecycle phases, the plugin receives
a &mut dyn GameAPI to interact with the engine.
§Implementations
- Host (engine): real implementation backed by ECS + wasmtime
- Test:
MockGameAPI(inopenstranded-plugin-apibehindtest-utilsfeature) for unit-testing plugins natively
§Example
use openstranded_common_wasmcontract::{GameAPI, RegistryEntry, Service, ServiceError, Value, LogLevel};
fn plugin_build(api: &mut dyn GameAPI) {
let entries = api.registry_domain("items").unwrap();
for entry in &entries {
api.log(LogLevel::Info, &format!("Loaded {}", entry.filename));
}
}Required Methods§
Sourcefn registry_domain(
&self,
name: &str,
) -> Result<Vec<RegistryEntry>, ServiceError>
fn registry_domain( &self, name: &str, ) -> Result<Vec<RegistryEntry>, ServiceError>
Get all file entries for a domain.
Sourcefn registry_file(
&self,
domain: &str,
filename: &str,
) -> Result<Vec<u8>, ServiceError>
fn registry_file( &self, domain: &str, filename: &str, ) -> Result<Vec<u8>, ServiceError>
Get raw bytes of a specific file within a domain.
Sourcefn register_service(
&mut self,
domain: &str,
service: Box<dyn Service>,
) -> Result<(), ServiceError>
fn register_service( &mut self, domain: &str, service: Box<dyn Service>, ) -> Result<(), ServiceError>
Register a service under the given domain name.
Sourcefn get_service(&self, domain: &str) -> Result<&dyn Service, ServiceError>
fn get_service(&self, domain: &str) -> Result<&dyn Service, ServiceError>
Get a reference to a registered service.
Sourcefn has_service(&self, domain: &str) -> bool
fn has_service(&self, domain: &str) -> bool
Check whether a service domain is registered.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".