lyra-engine/lyra-scripting/src/host.rs

113 lines
3.5 KiB
Rust

use std::collections::HashMap;
use lyra_ecs::ResourceObject;
use crate::ScriptWorldPtr;
#[derive(Debug, thiserror::Error)]
pub enum ScriptError {
#[error("{0}")]
#[cfg(feature = "lua")]
MluaError(mlua::Error),
#[error("{0}")]
Other(anyhow::Error),
}
#[cfg(feature = "lua")]
impl From<mlua::Error> for ScriptError {
fn from(value: mlua::Error) -> Self {
ScriptError::MluaError(value)
}
}
impl From<anyhow::Error> for ScriptError {
fn from(value: anyhow::Error) -> Self {
ScriptError::Other(value)
}
}
#[derive(Clone, Hash, PartialEq, Eq)]
pub struct ScriptData {
pub script_id: u64,
pub name: String,
}
/// Provides an API to a scripting context.
pub trait ScriptApiProvider {
/// The type used as the script's context.
type ScriptContext;
/// Exposes an API in the provided script context.
fn expose_api(&mut self, ctx: &mut Self::ScriptContext) -> Result<(), ScriptError>;
/// Create a script in the script host.
///
/// This only creates the script for the host, it does not setup the script for execution. See [`ScriptHostProvider::setup_script`].
fn setup_script(&mut self, data: &ScriptData, ctx: &mut Self::ScriptContext) -> Result<(), ScriptError>;
/// A function that is used to update a script's environment.
///
/// This is used to update stuff like the world pointer in the script context.
fn update_script_environment(&mut self, world: ScriptWorldPtr, data: &ScriptData, ctx: &mut Self::ScriptContext) -> Result<(), ScriptError>;
}
/// A storage for a [`ScriptHost`]'s api providers
#[derive(Default)]
pub struct ScriptApiProviders<T: ScriptHost> {
pub apis: Vec<Box<dyn ScriptApiProvider<ScriptContext = T::ScriptContext>>>,
}
impl<T: ScriptHost> ScriptApiProviders<T> {
pub fn add_provider<P>(&mut self, provider: P)
where
P: ScriptApiProvider<ScriptContext = T::ScriptContext> + 'static
{
self.apis.push(Box::new(provider));
}
}
pub trait ScriptHost: Default + ResourceObject {
/// The type used as the script's context.
type ScriptContext;
/// Loads a script and creates a context for it.
///
/// Before the script is executed, the API providers are exposed to the script.
fn load_script(&mut self, script: &[u8], script_data: &ScriptData, providers: &mut crate::ScriptApiProviders<Self>) -> Result<Self::ScriptContext, ScriptError>;
/// Setup a script for execution.
fn setup_script(&mut self, script_data: &ScriptData, ctx: &mut Self::ScriptContext, providers: &mut ScriptApiProviders<Self>) -> Result<(), ScriptError>;
/// Executes the update step for the script.
fn update_script(&mut self, world: ScriptWorldPtr, script_data: &crate::ScriptData, ctx: &mut Self::ScriptContext, providers: &mut crate::ScriptApiProviders<Self>) -> Result<(), ScriptError>;
}
#[derive(Default)]
pub struct ScriptContexts<T> {
contexts: HashMap<u64, T>,
}
impl<T> ScriptContexts<T> {
pub fn new(contexts: HashMap<u64, T>) -> Self {
Self {
contexts,
}
}
pub fn add_context(&mut self, script_id: u64, context: T) {
self.contexts.insert(script_id, context);
}
pub fn get_context(&self, script_id: u64) -> Option<&T> {
self.contexts.get(&script_id)
}
pub fn get_context_mut(&mut self, script_id: u64) -> Option<&mut T> {
self.contexts.get_mut(&script_id)
}
pub fn has_context(&self, script_id: u64) -> bool {
self.contexts.contains_key(&script_id)
}
}