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

123 lines
3.7 KiB
Rust
Raw Normal View History

2024-01-06 20:52:12 +00:00
use std::collections::HashMap;
2024-01-05 01:52:47 +00:00
use lyra_ecs::{ResourceObject, Entity, World};
2024-01-05 01:52:47 +00:00
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 {
/// The script id
2024-01-05 01:52:47 +00:00
pub script_id: u64,
/// The name of the script
2024-01-05 01:52:47 +00:00
pub name: String,
/// The entity that this script exists on
pub entity: Entity,
2024-01-05 01:52:47 +00:00
}
/// Provides an API to a scripting context.
pub trait ScriptApiProvider {
/// The type used as the script's context.
type ScriptContext;
/// Prepare the ECS world for this api. Things like registering types with the type registry happen here.
fn prepare_world(&mut self, world: &mut World) {}
2024-01-05 01:52:47 +00:00
/// Exposes an API in the provided script context.
fn expose_api(&mut self, ctx: &mut Self::ScriptContext) -> Result<(), ScriptError>;
/// Setup a script right before its 'init' method is called.
2024-01-05 01:52:47 +00:00
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>;
2024-01-05 01:52:47 +00:00
/// Setup a script for execution.
fn setup_script(&mut self, script_data: &ScriptData, ctx: &mut Self::ScriptContext,
providers: &mut ScriptApiProviders<Self>) -> Result<(), ScriptError>;
2024-01-05 01:52:47 +00:00
/// Executes the update step for the script.
fn call_script(&mut self, world: ScriptWorldPtr, script_data: &crate::ScriptData,
ctx: &mut Self::ScriptContext, providers: &mut crate::ScriptApiProviders<Self>,
function_name: &str) -> Result<(), ScriptError>;
2024-01-05 01:52:47 +00:00
}
#[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)
}
}