bevy_mod_scripting_lua 0.20.0

Necessary functionality for Lua support with bevy_mod_scripting
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! Lua integration for the bevy_mod_scripting system.
use std::{
    ops::{Deref, DerefMut},
    sync::Arc,
};

use ::{
    bevy_app::Plugin,
    bevy_asset::Handle,
    bevy_ecs::{entity::Entity, world::World},
};
use bevy_app::App;
use bevy_asset::AssetPath;
use bevy_ecs::world::{Mut, WorldId};
use bevy_log::trace;
use bevy_mod_scripting_asset::{Language, ScriptAsset};
use bevy_mod_scripting_bindings::{
    InteropError, PartialReflectExt, WorldExtensions, function::namespace::Namespace,
    globals::AppScriptGlobalsRegistry, script_value::ScriptValue,
};
use bevy_mod_scripting_core::{
    IntoScriptPluginParams, ScriptingPlugin,
    callbacks::ScriptCallbacks,
    config::{GetPluginThreadConfig, ScriptingPluginConfiguration},
    event::CallbackLabel,
    make_plugin_config_static,
    script::ContextPolicy,
};
use bevy_mod_scripting_script::ScriptAttachment;
use bevy_mod_scripting_world::ThreadWorldContainer;
use bindings::{
    reference::{LuaReflectReference, LuaStaticReflectReference},
    script_value::LuaScriptValue,
};
pub use mlua;
use mlua::{Function, IntoLua, Lua, MultiValue, Variadic};

/// Bindings for lua.
pub mod bindings;

make_plugin_config_static!(LuaScriptingPlugin);

/// A newtype around a lua context.
#[derive(Debug, Clone)]
pub struct LuaContext(Lua);

impl Deref for LuaContext {
    type Target = Lua;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for LuaContext {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl IntoScriptPluginParams for LuaScriptingPlugin {
    type C = LuaContext;
    type R = ();
    const LANGUAGE: Language = Language::Lua;

    fn build_runtime() -> Self::R {}

    fn handler() -> bevy_mod_scripting_core::handler::HandlerFn<Self> {
        lua_handler
    }

    fn context_loader() -> bevy_mod_scripting_core::context::ContextLoadFn<Self> {
        lua_context_load
    }

    fn context_reloader() -> bevy_mod_scripting_core::context::ContextReloadFn<Self> {
        lua_context_reload
    }
}

// necessary for automatic config goodies
impl AsMut<ScriptingPlugin<Self>> for LuaScriptingPlugin {
    fn as_mut(&mut self) -> &mut ScriptingPlugin<LuaScriptingPlugin> {
        &mut self.scripting_plugin
    }
}

/// The lua scripting plugin. Used to add lua scripting to a bevy app within the context of the BMS framework.
pub struct LuaScriptingPlugin {
    /// The internal scripting plugin
    pub scripting_plugin: ScriptingPlugin<Self>,
}

fn register_plugin_globals(lua: &mut Lua) -> Result<(), mlua::Error> {
    lua.globals().set(
        "register_callback",
        lua.create_function(|_lua: &Lua, (callback, func): (String, Function)| {
            let thread_ctxt = ThreadWorldContainer
                .try_get_context()
                .map_err(IntoMluaError::to_lua_error)?;
            let world = thread_ctxt.world;
            let attachment = world.current_attachment().0.ok_or_else(|| {
                mlua::Error::external(
                    "Cannot register callback, missing script attachment context.",
                )
            })?;

            world
                .with_resource_mut(|res: Mut<ScriptCallbacks<LuaScriptingPlugin>>| {
                    let mut callbacks = res.callbacks.write();
                    callbacks.insert(
                        (attachment.clone(), callback),
                        Arc::new(
                            move |args: Vec<ScriptValue>,
                                  lua: &mut LuaContext,
                                  world_id: WorldId| {
                                let pre_handling_callbacks =
                                    LuaScriptingPlugin::readonly_configuration(world_id)
                                        .pre_handling_callbacks;

                                pre_handling_callbacks
                                    .iter()
                                    .try_for_each(|init| init(&attachment, lua))?;

                                let args = args
                                    .into_iter()
                                    .map(LuaScriptValue)
                                    .collect::<Variadic<_>>();

                                func.call::<LuaScriptValue>(args)
                                    .map_err(IntoInteropError::to_bms_error)
                                    .map(ScriptValue::from)
                            },
                        ),
                    )
                })
                .map_err(mlua::Error::external)?;
            Ok(())
        })?,
    )?;
    Ok(())
}

impl Default for LuaScriptingPlugin {
    fn default() -> Self {
        LuaScriptingPlugin {
            scripting_plugin: ScriptingPlugin {
                runtime_initializers: Vec::default(),
                supported_extensions: vec!["lua", "luau"],
                context_initializers: vec![
                    |_script_id, context| {
                        // set the world global
                        let globals = context.globals();

                        globals
                            .set(
                                "world",
                                LuaStaticReflectReference(std::any::TypeId::of::<World>()),
                            )
                            .map_err(IntoInteropError::to_bms_error)?;

                        register_plugin_globals(context).map_err(IntoInteropError::to_bms_error)?;

                        Ok(())
                    },
                    |_script_id, context| {
                        // set static globals
                        let world = ThreadWorldContainer.try_get_context()?.world;
                        let globals_registry =
                            world.with_resource(|r: &AppScriptGlobalsRegistry| r.clone())?;
                        let globals_registry = globals_registry.read();

                        for (key, global) in globals_registry.iter() {
                            match &global.maker {
                                Some(maker) => {
                                    // non-static global
                                    let global = (maker)(world.clone())?;
                                    context
                                        .globals()
                                        .set(key.to_string(), LuaScriptValue::from(global))
                                        .map_err(IntoInteropError::to_bms_error)?
                                }
                                None => {
                                    let ref_ = LuaStaticReflectReference(global.type_id);
                                    context
                                        .globals()
                                        .set(key.to_string(), ref_)
                                        .map_err(IntoInteropError::to_bms_error)?
                                }
                            }
                        }

                        // go through functions in the global namespace and add them to the lua context
                        let script_function_registry = world.script_function_registry();
                        let script_function_registry = script_function_registry.read();

                        for (key, function) in script_function_registry
                            .iter_all()
                            .filter(|(k, _)| k.namespace == Namespace::Global)
                        {
                            context
                                .globals()
                                .set(
                                    key.name.to_string(),
                                    LuaScriptValue::from(ScriptValue::Function(function.clone())),
                                )
                                .map_err(IntoInteropError::to_bms_error)?;
                        }

                        Ok(())
                    },
                ],
                context_pre_handling_initializers: vec![|context_key, context| {
                    // TODO: convert these to functions
                    let world = ThreadWorldContainer.try_get_context()?.world;
                    if let Some(entity) = context_key.entity() {
                        context
                            .globals()
                            .set(
                                "entity",
                                LuaReflectReference(<Entity>::allocate(
                                    Box::new(entity),
                                    world.clone(),
                                )),
                            )
                            .map_err(IntoInteropError::to_bms_error)?;
                    }
                    context
                        .globals()
                        .set(
                            "script_asset",
                            LuaReflectReference(<Handle<ScriptAsset>>::allocate(
                                Box::new(context_key.script()),
                                world,
                            )),
                        )
                        .map_err(IntoInteropError::to_bms_error)?;

                    Ok(())
                }],
                language: Language::Lua,
                context_policy: ContextPolicy::default(),
                emit_responses: false,
                processing_pipeline_plugin: Default::default(),
            },
        }
    }
}

impl Plugin for LuaScriptingPlugin {
    fn build(&self, app: &mut App) {
        self.scripting_plugin.build(app);
    }

    fn finish(&self, app: &mut App) {
        self.scripting_plugin.finish(app);
    }
}

fn load_lua_content_into_context(
    context: &mut LuaContext,
    context_key: &ScriptAttachment,
    content: &[u8],
    world_id: WorldId,
) -> Result<(), InteropError> {
    let config = LuaScriptingPlugin::readonly_configuration(world_id);
    let initializers = config.context_initialization_callbacks;
    let pre_handling_initializers = config.pre_handling_callbacks;
    initializers
        .iter()
        .try_for_each(|init| init(context_key, context))?;

    pre_handling_initializers
        .iter()
        .try_for_each(|init| init(context_key, context))?;

    context
        .load(content)
        .exec()
        .map_err(IntoInteropError::to_bms_error)?;

    Ok(())
}

/// App data which can be retrieved via [`mlua::Lua::app_data_ref`], containing some metadata about scripts present
#[derive(Default, Debug)]
pub struct LuaContextAppData {
    /// the asset path of the script loaded last if this is a shared context, or the only script if it's not.
    pub last_loaded_script_name: Option<AssetPath<'static>>,
}

#[profiling::function]
/// Load a lua context from a script
pub fn lua_context_load(
    context_key: &ScriptAttachment,
    content: &[u8],
    world_id: WorldId,
) -> Result<LuaContext, InteropError> {
    #[cfg(feature = "unsafe_lua_modules")]
    let mut context = LuaContext(unsafe { Lua::unsafe_new() });
    #[cfg(not(feature = "unsafe_lua_modules"))]
    let mut context = LuaContext(Lua::new());

    context.set_app_data(LuaContextAppData {
        last_loaded_script_name: context_key.script().path().cloned(),
    });

    load_lua_content_into_context(&mut context, context_key, content, world_id)?;
    Ok(context)
}

#[profiling::function]
/// Reload a lua context from a script
pub fn lua_context_reload(
    context_key: &ScriptAttachment,
    content: &[u8],
    old_ctxt: &mut LuaContext,
    world_id: WorldId,
) -> Result<(), InteropError> {
    load_lua_content_into_context(old_ctxt, context_key, content, world_id)?;
    Ok(())
}

#[allow(clippy::too_many_arguments)]
#[profiling::function]
/// The lua handler for events
pub fn lua_handler(
    args: Vec<ScriptValue>,
    context_key: &ScriptAttachment,
    callback_label: &CallbackLabel,
    context: &mut LuaContext,
    world_id: WorldId,
) -> Result<ScriptValue, bevy_mod_scripting_bindings::InteropError> {
    let config = LuaScriptingPlugin::readonly_configuration(world_id);

    config
        .pre_handling_callbacks
        .iter()
        .try_for_each(|init| init(context_key, context))?;

    let handler: Function = match context.globals().raw_get(callback_label.as_ref()) {
        Ok(handler) => handler,
        // not subscribed to this event type
        Err(_) => {
            trace!(
                "Context {} is not subscribed to callback {}",
                context_key,
                callback_label.as_ref()
            );
            return Ok(ScriptValue::Unit);
        }
    };

    let input = MultiValue::from_vec(
        args.into_iter()
            .map(|arg| LuaScriptValue::from(arg).into_lua(context))
            .collect::<Result<_, _>>()
            .map_err(IntoInteropError::to_bms_error)?,
    );

    let out = handler
        .call::<LuaScriptValue>(input)
        .map_err(IntoInteropError::to_bms_error)?;

    Ok(out.into())
}

/// A trait to convert between mlua::Error and InteropError
pub trait IntoInteropError {
    /// Convert into InteropError
    fn to_bms_error(self) -> InteropError;
}

impl IntoInteropError for mlua::Error {
    fn to_bms_error(self) -> InteropError {
        match self {
            mlua::Error::CallbackError { traceback, cause }
                if matches!(cause.as_ref(), mlua::Error::ExternalError(_)) =>
            {
                let inner = cause.deref().clone();
                inner.to_bms_error().with_context(traceback)
            }
            e => {
                if let Some(inner) = e.downcast_ref::<InteropError>() {
                    inner.clone()
                } else {
                    InteropError::external(e)
                }
            }
        }
    }
}

/// A trait to convert between InteropError and mlua::Error
pub trait IntoMluaError {
    /// Convert into mlua::Error
    fn to_lua_error(self) -> mlua::Error;
}

impl<T: Into<InteropError>> IntoMluaError for T {
    fn to_lua_error(self) -> mlua::Error {
        let error: InteropError = self.into();
        mlua::Error::external(error)
    }
}

#[cfg(test)]
mod test {
    use ::bevy_asset::Handle;
    use bevy_mod_scripting_asset::LanguageExtensions;
    use mlua::Value;

    use super::*;

    #[test]
    fn test_reload_doesnt_overwrite_old_context() {
        let lua = Lua::new();
        let mut old_ctxt = LuaContext(lua.clone());
        let handle = Handle::default();
        let context_key = ScriptAttachment::EntityScript(Entity::from_raw_u32(1).unwrap(), handle);
        let world_id = WorldId::new().unwrap();
        LuaScriptingPlugin::set_world_local_config(
            world_id,
            ScriptingPluginConfiguration {
                pre_handling_callbacks: &[],
                context_initialization_callbacks: &[],
                emit_responses: false,
                runtime: &(),
                language_extensions: Box::leak(Box::new(LanguageExtensions::default())),
            },
        );
        lua_context_load(
            &context_key,
            "function hello_world_from_first_load()

            end"
            .as_bytes(),
            world_id,
        )
        .unwrap();

        lua_context_reload(
            &context_key,
            "function hello_world_from_second_load()

            end"
            .as_bytes(),
            &mut old_ctxt,
            world_id,
        )
        .unwrap();

        // assert both functions exist in globals
        let globals = lua.globals();
        assert!(globals.get::<Value>("hello_world_from_first_load").is_ok());
        assert!(globals.get::<Value>("hello_world_from_second_load").is_ok());
    }
}