|
1 | 1 | use bevy::{
|
2 |
| - app::{App, Plugin}, |
| 2 | + app::Plugin, |
3 | 3 | ecs::{entity::Entity, world::World},
|
4 | 4 | };
|
5 | 5 | use bevy_mod_scripting_core::{
|
6 | 6 | asset::{AssetPathToLanguageMapper, Language},
|
7 | 7 | bindings::{
|
8 |
| - script_value::ScriptValue, ThreadWorldContainer, WorldCallbackAccess, WorldContainer, |
| 8 | + function::namespace::Namespace, script_value::ScriptValue, ThreadWorldContainer, |
| 9 | + WorldCallbackAccess, WorldContainer, |
9 | 10 | },
|
10 | 11 | context::{ContextBuilder, ContextInitializer, ContextPreHandlingInitializer},
|
11 | 12 | error::ScriptError,
|
12 | 13 | event::CallbackLabel,
|
13 | 14 | reflection_extensions::PartialReflectExt,
|
14 | 15 | script::ScriptId,
|
15 |
| - AddContextInitializer, IntoScriptPluginParams, ScriptingPlugin, |
| 16 | + IntoScriptPluginParams, ScriptingPlugin, |
16 | 17 | };
|
17 | 18 | use bindings::{
|
18 | 19 | reference::{LuaReflectReference, LuaStaticReflectReference},
|
@@ -48,16 +49,62 @@ impl Default for LuaScriptingPlugin {
|
48 | 49 | language_mapper: Some(AssetPathToLanguageMapper {
|
49 | 50 | map: lua_language_mapper,
|
50 | 51 | }),
|
51 |
| - context_initializers: vec![|_script_id, context| { |
52 |
| - context |
53 |
| - .globals() |
54 |
| - .set( |
55 |
| - "world", |
56 |
| - LuaStaticReflectReference(std::any::TypeId::of::<World>()), |
57 |
| - ) |
58 |
| - .map_err(ScriptError::from_mlua_error)?; |
59 |
| - Ok(()) |
60 |
| - }], |
| 52 | + context_initializers: vec![ |
| 53 | + |_script_id, context| { |
| 54 | + // set the world global |
| 55 | + context |
| 56 | + .globals() |
| 57 | + .set( |
| 58 | + "world", |
| 59 | + LuaStaticReflectReference(std::any::TypeId::of::<World>()), |
| 60 | + ) |
| 61 | + .map_err(ScriptError::from_mlua_error)?; |
| 62 | + Ok(()) |
| 63 | + }, |
| 64 | + |_script_id, context: &mut Lua| { |
| 65 | + // set static globals |
| 66 | + let world = ThreadWorldContainer.get_world(); |
| 67 | + let type_registry = world.type_registry(); |
| 68 | + let type_registry = type_registry.read(); |
| 69 | + |
| 70 | + for registration in type_registry.iter() { |
| 71 | + // only do this for non generic types |
| 72 | + // we don't want to see `Vec<Entity>:function()` in lua |
| 73 | + if !registration.type_info().generics().is_empty() { |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + if let Some(global_name) = |
| 78 | + registration.type_info().type_path_table().ident() |
| 79 | + { |
| 80 | + let ref_ = LuaStaticReflectReference(registration.type_id()); |
| 81 | + context |
| 82 | + .globals() |
| 83 | + .set(global_name, ref_) |
| 84 | + .map_err(ScriptError::from_mlua_error)?; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // go through functions in the global namespace and add them to the lua context |
| 89 | + let script_function_registry = world.script_function_registry(); |
| 90 | + let script_function_registry = script_function_registry.read(); |
| 91 | + |
| 92 | + for (key, function) in script_function_registry |
| 93 | + .iter_all() |
| 94 | + .filter(|(k, _)| k.namespace == Namespace::Global) |
| 95 | + { |
| 96 | + context |
| 97 | + .globals() |
| 98 | + .set( |
| 99 | + key.name.to_string(), |
| 100 | + LuaScriptValue::from(ScriptValue::Function(function.clone())), |
| 101 | + ) |
| 102 | + .map_err(ScriptError::from_mlua_error)?; |
| 103 | + } |
| 104 | + |
| 105 | + Ok(()) |
| 106 | + }, |
| 107 | + ], |
61 | 108 | context_pre_handling_initializers: vec![|script_id, entity, context| {
|
62 | 109 | let world = ThreadWorldContainer.get_world();
|
63 | 110 | context
|
@@ -89,33 +136,6 @@ impl Plugin for LuaScriptingPlugin {
|
89 | 136 | fn build(&self, app: &mut bevy::prelude::App) {
|
90 | 137 | self.scripting_plugin.build(app);
|
91 | 138 | }
|
92 |
| - |
93 |
| - fn cleanup(&self, app: &mut App) { |
94 |
| - // find all registered types, and insert dummy for calls |
95 |
| - |
96 |
| - app.add_context_initializer::<LuaScriptingPlugin>(|_script_id, context: &mut Lua| { |
97 |
| - let world = ThreadWorldContainer.get_world(); |
98 |
| - let type_registry = world.type_registry(); |
99 |
| - let type_registry = type_registry.read(); |
100 |
| - |
101 |
| - for registration in type_registry.iter() { |
102 |
| - // only do this for non generic types |
103 |
| - // we don't want to see `Vec<Entity>:function()` in lua |
104 |
| - if !registration.type_info().generics().is_empty() { |
105 |
| - continue; |
106 |
| - } |
107 |
| - |
108 |
| - if let Some(global_name) = registration.type_info().type_path_table().ident() { |
109 |
| - let ref_ = LuaStaticReflectReference(registration.type_id()); |
110 |
| - context |
111 |
| - .globals() |
112 |
| - .set(global_name, ref_) |
113 |
| - .map_err(ScriptError::from_mlua_error)?; |
114 |
| - } |
115 |
| - } |
116 |
| - Ok(()) |
117 |
| - }); |
118 |
| - } |
119 | 139 | }
|
120 | 140 |
|
121 | 141 | pub fn lua_context_load(
|
|
0 commit comments