Skip to main content

GameAPI

Trait GameAPI 

Source
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 (in openstranded-plugin-api behind test-utils feature) 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§

Source

fn registry_domain( &self, name: &str, ) -> Result<Vec<RegistryEntry>, ServiceError>

Get all file entries for a domain.

Source

fn registry_file( &self, domain: &str, filename: &str, ) -> Result<Vec<u8>, ServiceError>

Get raw bytes of a specific file within a domain.

Source

fn register_service( &mut self, domain: &str, service: Box<dyn Service>, ) -> Result<(), ServiceError>

Register a service under the given domain name.

Source

fn get_service(&self, domain: &str) -> Result<&dyn Service, ServiceError>

Get a reference to a registered service.

Source

fn has_service(&self, domain: &str) -> bool

Check whether a service domain is registered.

Source

fn log(&self, level: LogLevel, message: &str)

Log a message at the given level.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§