aboutsummaryrefslogtreecommitdiff
path: root/src/lua_api.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lua_api.rs')
-rw-r--r--src/lua_api.rs227
1 files changed, 128 insertions, 99 deletions
diff --git a/src/lua_api.rs b/src/lua_api.rs
index 53252a5..88d7193 100644
--- a/src/lua_api.rs
+++ b/src/lua_api.rs
@@ -1,37 +1,40 @@
-use std::{str::FromStr, sync::Mutex};
+use std::str::FromStr;
use bevy::prelude::*;
-use bevy_mod_scripting::api::providers::bevy_reflect::LuaVec3;
-use bevy_mod_scripting::{api::providers::bevy_ecs::LuaEntity, lua::tealr::mlu::mlua::Error as LuaError};
-use bevy_mod_scripting::lua::LuaEvent;
-use bevy_mod_scripting::prelude::*;
+use bevy_mod_scripting::core::{
+ bindings::function::{
+ from::Val,
+ namespace::{GlobalNamespace, NamespaceBuilder},
+ script_function::FunctionCallContext,
+ },
+ callback_labels,
+ error::InteropError,
+ event::ScriptCallbackEvent,
+};
use uuid::Uuid;
use crate::{components::DirworldEntity, conditionals::Condition};
-pub fn trigger_update(mut w: PriorityEventWriter<LuaEvent<()>>) {
- let event = LuaEvent::<()> {
- args: (),
- hook_name: "on_update".into(),
- recipients: Recipients::All,
- };
- w.send(event, 0);
+callback_labels!(OnUpdate => "on_update");
+
+pub fn trigger_update(mut w: EventWriter<ScriptCallbackEvent>) {
+ w.send(ScriptCallbackEvent::new_for_all(OnUpdate, vec![]));
}
// ACTUAL API STUFF BELOW THIS POINT {{{
macro_rules! register_fns {
- ($runtime:expr, $($function:expr),+) => {
+ ($world:expr, $($function:expr),+) => {
{
- let ctx = $runtime.get_mut().unwrap();
- $(ctx.globals().set(stringify!($function).to_string(), ctx.create_function($function).unwrap()).unwrap();)+
+ NamespaceBuilder::<GlobalNamespace>::new_unregistered($world)
+ $(.register(stringify!($function), $function))+;
}
};
}
-pub fn register(api: &mut Mutex<Lua>) {
+pub fn register(world: &mut World) {
register_fns!(
- api,
+ world,
translate,
rotate,
get_dirworld_id,
@@ -45,65 +48,57 @@ pub fn register(api: &mut Mutex<Lua>) {
)
}
-fn translate(ctx: &Lua, (entity, translation): (LuaEntity, LuaVec3)) -> Result<(), LuaError> {
- let world = ctx.get_world()?;
- let mut world = world.write();
- if let Some(mut transform) = world.entity_mut(entity.inner().unwrap()).get_mut::<Transform>() {
- transform.translation += translation.inner().unwrap();
- }
- Ok(())
+fn translate(ctx: FunctionCallContext, entity: Val<Entity>, translation: Val<Vec3>) {
+ let world = ctx.world().unwrap();
+ world.with_global_access(|world| {
+ if let Some(mut transform) = world.entity_mut(*entity).get_mut::<Transform>() {
+ transform.translation += *translation;
+ }
+ });
}
-fn rotate(ctx: &Lua, (entity, axis, angle): (LuaEntity, LuaVec3, f32)) -> Result<(), LuaError> {
- let world = ctx.get_world()?;
- let mut world = world.write();
- if let Some(mut transform) = world.entity_mut(entity.inner().unwrap()).get_mut::<Transform>() {
- transform.rotation *= Quat::from_axis_angle(axis.inner().unwrap(), angle);
- }
- Ok(())
+fn rotate(ctx: FunctionCallContext, entity: Val<Entity>, axis: Val<Vec3>, angle: f32) {
+ let world = ctx.world().unwrap();
+ world.with_global_access(|world| {
+ if let Some(mut transform) = world.entity_mut(*entity).get_mut::<Transform>() {
+ transform.rotation *= Quat::from_axis_angle(*axis, angle);
+ }
+ });
}
-fn get_dirworld_id(ctx: &Lua, (entity,): (LuaEntity,)) -> Result<String, LuaError> {
- let world = ctx.get_world()?;
- let world = world.read();
- if let Some(dirworld_entity) = world.entity(entity.inner().unwrap()).get::<DirworldEntity>() {
- dirworld_entity.payload.as_ref().map(|p| p.id.to_string()).ok_or(LuaError::runtime("Failed to get entity id from payload"))
- } else {
- Err(LuaError::runtime("Entity missing DirworldEntity component"))
- }
+fn get_dirworld_id(ctx: FunctionCallContext, entity: Val<Entity>) -> Result<String, InteropError> {
+ let world = ctx.world()?;
+ let mut result = Ok(Default::default());
+ world.with_global_access(|world| {
+ result = if let Some(dirworld_entity) = world.entity(*entity).get::<DirworldEntity>() {
+ Ok(dirworld_entity
+ .payload
+ .as_ref()
+ .map(|p| p.id.to_string())
+ .unwrap())
+ } else {
+ Err(InteropError::missing_entity(*entity))
+ };
+ });
+ result
}
// Conditionals
-pub struct ConditionalAPI;
-
-impl APIProvider for ConditionalAPI {
- type APITarget = Mutex<Lua>;
-
- type ScriptContext = Mutex<Lua>;
-
- type DocTarget = LuaDocFragment;
-
- fn attach_api(
- &mut self,
- api: &mut Self::APITarget,
- ) -> Result<(), bevy_mod_scripting::prelude::ScriptError> {
- register(api);
- Ok(())
- }
+fn condition_true(ctx: FunctionCallContext) -> Result<bool, InteropError> {
+ let world = ctx.world()?;
+ let mut result = Ok(Default::default());
+ world.with_global_access(|world| {
+ result = Ok(Condition::True.evaluate(world));
+ });
+ result
}
-
-
-fn condition_true(ctx: &Lua, _: ()) -> Result<bool, LuaError> {
- let world = ctx.get_world()?;
- let mut world = world.write();
- Ok(Condition::True.evaluate(&mut world))
-}
-
-fn condition_ancestor_of(ctx: &Lua, (ancestor, descendant): (String, String)) -> Result<bool, LuaError> {
- let world = ctx.get_world()?;
- let mut world = world.write();
+fn condition_ancestor_of(
+ ctx: FunctionCallContext,
+ ancestor: String,
+ descendant: String,
+) -> Result<bool, InteropError> {
let Ok(ancestor) = Uuid::from_str(&ancestor) else {
warn!("Provided ancestor is not a valid UUID");
return Ok(false);
@@ -112,15 +107,23 @@ fn condition_ancestor_of(ctx: &Lua, (ancestor, descendant): (String, String)) ->
warn!("Provided descendant is not a valid UUID");
return Ok(false);
};
- Ok(Condition::AncestorOf {
- ancestor,
- descendant,
- }.evaluate(&mut world))
+ let world = ctx.world()?;
+ let mut result = Ok(Default::default());
+ world.with_global_access(|world| {
+ result = Ok(Condition::AncestorOf {
+ ancestor,
+ descendant,
+ }
+ .evaluate(world));
+ });
+ result
}
-fn condition_descendant_of(ctx: &Lua, (descendant, ancestor): (String, String)) -> Result<bool, LuaError> {
- let world = ctx.get_world()?;
- let mut world = world.write();
+fn condition_descendant_of(
+ ctx: FunctionCallContext,
+ descendant: String,
+ ancestor: String,
+) -> Result<bool, InteropError> {
let Ok(ancestor) = Uuid::from_str(&ancestor) else {
warn!("Provided ancestor is not a valid UUID");
return Ok(false);
@@ -129,15 +132,23 @@ fn condition_descendant_of(ctx: &Lua, (descendant, ancestor): (String, String))
warn!("Provided descendant is not a valid UUID");
return Ok(false);
};
- Ok(Condition::DescendantOf {
- ancestor,
- descendant,
- }.evaluate(&mut world))
+ let world = ctx.world()?;
+ let mut result = Ok(Default::default());
+ world.with_global_access(|world| {
+ result = Ok(Condition::DescendantOf {
+ ancestor,
+ descendant,
+ }
+ .evaluate(world));
+ });
+ result
}
-fn condition_parent_of(ctx: &Lua, (parent, child): (String, String)) -> Result<bool, LuaError> {
- let world = ctx.get_world()?;
- let mut world = world.write();
+fn condition_parent_of(
+ ctx: FunctionCallContext,
+ parent: String,
+ child: String,
+) -> Result<bool, InteropError> {
let Ok(parent) = Uuid::from_str(&parent) else {
warn!("Provided ancestor is not a valid UUID");
return Ok(false);
@@ -146,15 +157,19 @@ fn condition_parent_of(ctx: &Lua, (parent, child): (String, String)) -> Result<b
warn!("Provided descendant is not a valid UUID");
return Ok(false);
};
- Ok(Condition::ParentOf {
- parent,
- child,
- }.evaluate(&mut world))
+ let world = ctx.world()?;
+ let mut result = Ok(Default::default());
+ world.with_global_access(|world| {
+ result = Ok(Condition::ParentOf { parent, child }.evaluate(world));
+ });
+ result
}
-fn condition_child_of(ctx: &Lua, (child, parent): (String, String)) -> Result<bool, LuaError> {
- let world = ctx.get_world()?;
- let mut world = world.write();
+fn condition_child_of(
+ ctx: FunctionCallContext,
+ child: String,
+ parent: String,
+) -> Result<bool, InteropError> {
let Ok(parent) = Uuid::from_str(&parent) else {
warn!("Provided ancestor is not a valid UUID");
return Ok(false);
@@ -163,30 +178,44 @@ fn condition_child_of(ctx: &Lua, (child, parent): (String, String)) -> Result<bo
warn!("Provided descendant is not a valid UUID");
return Ok(false);
};
- Ok(Condition::ChildOf {
- parent,
- child,
- }.evaluate(&mut world))
+ let world = ctx.world()?;
+ let mut result = Ok(Default::default());
+ world.with_global_access(|world| {
+ result = Ok(Condition::ChildOf { parent, child }.evaluate(world));
+ });
+ result
}
-fn condition_in_room(ctx: &Lua, (room,): (String,)) -> Result<bool, LuaError> {
- let world = ctx.get_world()?;
- let mut world = world.write();
+fn condition_in_room(
+ ctx: FunctionCallContext,
+ room: String,
+) -> Result<bool, InteropError> {
let Ok(room) = Uuid::from_str(&room) else {
warn!("Provided room is not a valid UUID");
return Ok(false);
};
- Ok(Condition::InRoom(room).evaluate(&mut world))
+ let world = ctx.world()?;
+ let mut result = Ok(Default::default());
+ world.with_global_access(|world| {
+ result = Ok(Condition::InRoom(room).evaluate(world));
+ });
+ result
}
-fn condition_object_in_room(ctx: &Lua, (object,): (String,)) -> Result<bool, LuaError> {
- let world = ctx.get_world()?;
- let mut world = world.write();
+fn condition_object_in_room(
+ ctx: FunctionCallContext,
+ object: String
+) -> Result<bool, InteropError> {
let Ok(object) = Uuid::from_str(&object) else {
warn!("Provided object is not a valid UUID");
return Ok(false);
};
- Ok(Condition::ObjectInRoom(object).evaluate(&mut world))
+ let world = ctx.world()?;
+ let mut result = Ok(Default::default());
+ world.with_global_access(|world| {
+ result = Ok(Condition::ObjectInRoom(object).evaluate(world));
+ });
+ result
}
// }}}