2024-01-06 20:52:12 +00:00
|
|
|
use std::collections::HashMap;
|
2024-01-05 01:52:47 +00:00
|
|
|
|
2024-01-12 19:11:33 +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")]
|
2024-02-17 19:27:16 +00:00
|
|
|
MluaError(elua::Error),
|
2024-01-05 01:52:47 +00:00
|
|
|
|
|
|
|
#[error("{0}")]
|
|
|
|
Other(anyhow::Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "lua")]
|
2024-02-17 19:27:16 +00:00
|
|
|
impl From<elua::Error> for ScriptError {
|
|
|
|
fn from(value: elua::Error) -> Self {
|
2024-01-05 01:52:47 +00:00
|
|
|
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 {
|
2024-01-07 05:57:19 +00:00
|
|
|
/// The script id
|
2024-01-05 01:52:47 +00:00
|
|
|
pub script_id: u64,
|
2024-01-07 05:57:19 +00:00
|
|
|
/// The name of the script
|
2024-01-05 01:52:47 +00:00
|
|
|
pub name: String,
|
2024-01-07 05:57:19 +00:00
|
|
|
/// 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;
|
|
|
|
|
2024-01-12 19:11:33 +00:00
|
|
|
/// Prepare the ECS world for this api. Things like registering types with the type registry happen here.
|
2024-01-16 04:22:21 +00:00
|
|
|
fn prepare_world(&mut self, world: &mut World) {
|
|
|
|
let _ = world; // remove compiler warning
|
|
|
|
}
|
2024-01-12 19:11:33 +00:00
|
|
|
|
2024-01-05 01:52:47 +00:00
|
|
|
/// Exposes an API in the provided script context.
|
2024-01-17 04:04:17 +00:00
|
|
|
fn expose_api(&mut self, data: &ScriptData, ctx: &mut Self::ScriptContext) -> Result<(), ScriptError>;
|
2024-01-05 01:52:47 +00:00
|
|
|
|
2024-01-07 05:57:19 +00:00
|
|
|
/// 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.
|
2024-01-07 05:57:19 +00:00
|
|
|
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.
|
2024-01-07 05:57:19 +00:00
|
|
|
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
|
|
|
|
2024-02-24 16:16:09 +00:00
|
|
|
/// Calls a event function in the script.
|
2024-01-07 05:57:19 +00:00
|
|
|
fn call_script(&mut self, world: ScriptWorldPtr, script_data: &crate::ScriptData,
|
|
|
|
ctx: &mut Self::ScriptContext, providers: &mut crate::ScriptApiProviders<Self>,
|
2024-02-24 16:16:09 +00:00
|
|
|
event_fn_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)
|
|
|
|
}
|
2024-01-16 04:22:21 +00:00
|
|
|
|
|
|
|
pub fn remove_context(&mut self, script_id: u64) -> Option<T> {
|
|
|
|
self.contexts.remove(&script_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.contexts.len()
|
|
|
|
}
|
2024-01-05 01:52:47 +00:00
|
|
|
}
|