Skip to content

Commit a2c427a

Browse files
committed
test: Prep tests to run.
1 parent d6ea7a7 commit a2c427a

File tree

8 files changed

+92
-49
lines changed

8 files changed

+92
-49
lines changed

crates/bevy_mod_scripting_core/src/bindings/script_system.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use super::{
1010
WorldGuard,
1111
};
1212
use crate::{
13+
ScriptAsset,
1314
bindings::pretty_print::DisplayWithWorld,
1415
context::ContextLoadingSettings,
1516
error::{InteropError, ScriptError},
@@ -21,6 +22,7 @@ use crate::{
2122
IntoScriptPluginParams,
2223
};
2324
use bevy::{
25+
asset::Handle,
2426
prelude::AssetServer,
2527
ecs::{
2628
archetype::{ArchetypeComponentId, ArchetypeGeneration},
@@ -257,13 +259,13 @@ impl<'w, P: IntoScriptPluginParams> DynamicHandlerContext<'w, P> {
257259
pub fn call_dynamic_label(
258260
&self,
259261
label: &CallbackLabel,
260-
script_id: &ScriptId,
262+
script_id: &Handle<ScriptAsset>,
261263
entity: Entity,
262264
payload: Vec<ScriptValue>,
263265
guard: WorldGuard<'_>,
264266
) -> Result<ScriptValue, ScriptError> {
265267
// find script
266-
let script = match self.scripts.scripts.get(script_id) {
268+
let script = match self.scripts.scripts.get(&script_id.id()) {
267269
Some(script) => script,
268270
None => return Err(InteropError::missing_script(script_id.clone()).into()),
269271
};
@@ -281,7 +283,7 @@ impl<'w, P: IntoScriptPluginParams> DynamicHandlerContext<'w, P> {
281283
handler,
282284
payload,
283285
entity,
284-
script_id,
286+
&script_id.id(),
285287
label,
286288
&mut context,
287289
pre_handling_initializers,
@@ -425,8 +427,7 @@ impl<P: IntoScriptPluginParams> System for DynamicScriptSystem<P> {
425427
let mut payload = Vec::with_capacity(state.system_params.len());
426428
let script_id = {
427429
let asset_server = world.world().resource::<AssetServer>();
428-
let handle = asset_server.load(&*self.target_script);
429-
handle.id()
430+
asset_server.load(&*self.target_script)
430431
};
431432
let guard = if self.exclusive {
432433
// safety: we are an exclusive system, therefore the cell allows us to do this

crates/bevy_mod_scripting_core/src/commands.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ impl<P: IntoScriptPluginParams> CreateOrUpdateScript<P> {
107107

108108
(ContextBuilder::<P>::reload)(
109109
handler_ctxt.context_loading_settings.loader.reload,
110-
&self.id,
110+
// &self.id,
111+
&Handle::Weak(self.id),
111112
&self.content,
112113
&mut context,
113114
&handler_ctxt.context_loading_settings.context_initializers,
@@ -128,7 +129,8 @@ impl<P: IntoScriptPluginParams> CreateOrUpdateScript<P> {
128129
) -> Result<(), ScriptError> {
129130
let context = (ContextBuilder::<P>::load)(
130131
handler_ctxt.context_loading_settings.loader.load,
131-
&self.id,
132+
// &self.id,
133+
&Handle::Weak(self.id),
132134
&self.content,
133135
&handler_ctxt.context_loading_settings.context_initializers,
134136
&handler_ctxt

crates/bevy_mod_scripting_core/src/context.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
//! Traits and types for managing script contexts.
22
33
use crate::{
4+
ScriptAsset,
45
bindings::{ThreadWorldContainer, WorldContainer, WorldGuard},
56
error::{InteropError, ScriptError},
67
script::ScriptId,
78
IntoScriptPluginParams,
89
};
9-
use bevy::ecs::{entity::Entity, system::Resource};
10+
use bevy::{
11+
ecs::{entity::Entity, system::Resource},
12+
asset::Handle,
13+
};
1014

1115
/// A trait that all script contexts must implement.
1216
///
@@ -17,11 +21,11 @@ impl<T: 'static + Send> Context for T {}
1721

1822
/// Initializer run once after creating a context but before executing it for the first time as well as after re-loading the script
1923
pub type ContextInitializer<P> =
20-
fn(&ScriptId, &mut <P as IntoScriptPluginParams>::C) -> Result<(), ScriptError>;
24+
fn(&Handle<ScriptAsset>, &mut <P as IntoScriptPluginParams>::C) -> Result<(), ScriptError>;
2125

2226
/// Initializer run every time before executing or loading/re-loading a script
2327
pub type ContextPreHandlingInitializer<P> =
24-
fn(&ScriptId, Entity, &mut <P as IntoScriptPluginParams>::C) -> Result<(), ScriptError>;
28+
fn(&Handle<ScriptAsset>, Entity, &mut <P as IntoScriptPluginParams>::C) -> Result<(), ScriptError>;
2529

2630
/// Settings concerning the creation and assignment of script contexts as well as their initialization.
2731
#[derive(Resource)]
@@ -59,7 +63,7 @@ impl<T: IntoScriptPluginParams> Clone for ContextLoadingSettings<T> {
5963
}
6064
/// A strategy for loading contexts
6165
pub type ContextLoadFn<P> = fn(
62-
script_id: &ScriptId,
66+
script_id: &Handle<ScriptAsset>,
6367
content: &[u8],
6468
context_initializers: &[ContextInitializer<P>],
6569
pre_handling_initializers: &[ContextPreHandlingInitializer<P>],
@@ -68,7 +72,7 @@ pub type ContextLoadFn<P> = fn(
6872

6973
/// A strategy for reloading contexts
7074
pub type ContextReloadFn<P> = fn(
71-
script_id: &ScriptId,
75+
script_id: &Handle<ScriptAsset>,
7276
content: &[u8],
7377
previous_context: &mut <P as IntoScriptPluginParams>::C,
7478
context_initializers: &[ContextInitializer<P>],
@@ -99,7 +103,7 @@ impl<P: IntoScriptPluginParams> ContextBuilder<P> {
99103
/// load a context
100104
pub fn load(
101105
loader: ContextLoadFn<P>,
102-
script: &ScriptId,
106+
script: &Handle<ScriptAsset>,
103107
content: &[u8],
104108
context_initializers: &[ContextInitializer<P>],
105109
pre_handling_initializers: &[ContextPreHandlingInitializer<P>],
@@ -121,7 +125,7 @@ impl<P: IntoScriptPluginParams> ContextBuilder<P> {
121125
/// reload a context
122126
pub fn reload(
123127
reloader: ContextReloadFn<P>,
124-
script: &ScriptId,
128+
script: &Handle<ScriptAsset>,
125129
content: &[u8],
126130
previous_context: &mut P::C,
127131
context_initializers: &[ContextInitializer<P>],

crates/bevy_mod_scripting_core/src/error.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Errors that can occur when interacting with the scripting system
22
33
use crate::{
4+
ScriptAsset,
45
bindings::{
56
access_map::{DisplayCodeLocation, ReflectAccessId},
67
function::namespace::Namespace,
@@ -11,6 +12,7 @@ use crate::{
1112
script::ScriptId,
1213
};
1314
use bevy::{
15+
asset::Handle,
1416
ecs::{
1517
component::ComponentId,
1618
schedule::{ScheduleBuildError, ScheduleNotInitialized},
@@ -592,7 +594,7 @@ impl InteropError {
592594
}
593595

594596
/// Thrown if a script could not be found when trying to call a synchronous callback or otherwise
595-
pub fn missing_script(script_id: impl Into<ScriptId>) -> Self {
597+
pub fn missing_script(script_id: impl Into<Handle<ScriptAsset>>) -> Self {
596598
Self(Arc::new(InteropErrorInner::MissingScript {
597599
script_id: script_id.into(),
598600
}))
@@ -628,7 +630,7 @@ pub enum InteropErrorInner {
628630
/// Thrown if a script could not be found when trying to call a synchronous callback.
629631
MissingScript {
630632
/// The script id that was not found.
631-
script_id: ScriptId,
633+
script_id: Handle<ScriptAsset>,
632634
},
633635
/// Thrown if a base type is not registered with the reflection system
634636
UnregisteredBase {
@@ -1253,10 +1255,17 @@ macro_rules! unregistered_component_or_resource_type {
12531255

12541256
macro_rules! missing_script_for_callback {
12551257
($script_id:expr) => {
1256-
format!(
1257-
"Could not find script with id: {}. Is the script loaded?",
1258-
$script_id
1259-
)
1258+
if let Some(path) = $script_id.path() {
1259+
format!(
1260+
"Could not find script with path: {}. Is the script loaded?",
1261+
path
1262+
)
1263+
} else {
1264+
format!(
1265+
"Could not find script with id: {}. Is the script loaded?",
1266+
$script_id.id()
1267+
)
1268+
}
12601269
};
12611270
}
12621271

crates/bevy_mod_scripting_core/src/extractors.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
#![allow(deprecated)]
55
use std::ops::{Deref, DerefMut};
66

7-
use bevy::ecs::{
8-
component::ComponentId,
9-
entity::Entity,
10-
event::{Event, EventCursor, EventIterator, Events},
11-
query::{Access, AccessConflicts},
12-
storage::SparseSetIndex,
13-
system::{Local, Resource, SystemParam, SystemState},
14-
world::World,
7+
use bevy::{
8+
asset::Handle,
9+
ecs::{
10+
component::ComponentId,
11+
entity::Entity,
12+
event::{Event, EventCursor, EventIterator, Events},
13+
query::{Access, AccessConflicts},
14+
storage::SparseSetIndex,
15+
system::{Local, Resource, SystemParam, SystemState},
16+
world::World,
17+
}
1518
};
1619
use fixedbitset::FixedBitSet;
1720

@@ -213,7 +216,8 @@ impl<P: IntoScriptPluginParams> HandlerContext<'_, P> {
213216
// find script
214217
let script = match self.scripts.scripts.get(script_id) {
215218
Some(script) => script,
216-
None => return Err(InteropError::missing_script(script_id.clone()).into()),
219+
// NOTE: It'd be nice to use a handle here because then we have the path.
220+
None => return Err(InteropError::missing_script(Handle::Weak(script_id.clone())).into()),
217221
};
218222

219223
// call the script

crates/bevy_mod_scripting_core/src/handler.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Contains the logic for handling script callback events
22
use crate::{
3+
ScriptAsset,
34
bindings::{
45
pretty_print::DisplayWithWorld, script_value::ScriptValue, ThreadWorldContainer,
56
WorldContainer, WorldGuard,
@@ -15,6 +16,7 @@ use crate::{
1516
IntoScriptPluginParams,
1617
};
1718
use bevy::{
19+
asset::Handle,
1820
ecs::{
1921
entity::Entity,
2022
query::QueryState,
@@ -219,11 +221,19 @@ pub(crate) fn event_handler_inner<P: IntoScriptPluginParams>(
219221
Err(e) => {
220222
match e.downcast_interop_inner() {
221223
Some(InteropErrorInner::MissingScript { script_id }) => {
222-
trace_once!(
223-
"{}: Script `{}` on entity `{:?}` is either still loading, doesn't exist, or is for another language, ignoring until the corresponding script is loaded.",
224-
P::LANGUAGE,
225-
script_id, entity
226-
);
224+
if let Some(path) = script_id.path() {
225+
trace_once!(
226+
"{}: Script path `{}` on entity `{:?}` is either still loading, doesn't exist, or is for another language, ignoring until the corresponding script is loaded.",
227+
P::LANGUAGE,
228+
path, entity
229+
);
230+
} else {
231+
trace_once!(
232+
"{}: Script id `{}` on entity `{:?}` is either still loading, doesn't exist, or is for another language, ignoring until the corresponding script is loaded.",
233+
P::LANGUAGE,
234+
script_id.id(), entity
235+
);
236+
}
227237
continue;
228238
}
229239
Some(InteropErrorInner::MissingContext { .. }) => {

crates/languages/bevy_mod_scripting_lua/src/lib.rs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
//! Lua integration for the bevy_mod_scripting system.
22
use bevy::{
33
app::Plugin,
4+
asset::Handle,
45
ecs::{entity::Entity, world::World},
56
};
67
use bevy_mod_scripting_core::{
7-
asset::Language,
8+
asset::{ScriptAsset, Language},
89
bindings::{
910
function::namespace::Namespace, globals::AppScriptGlobalsRegistry,
1011
script_value::ScriptValue, ThreadWorldContainer, WorldContainer,
@@ -124,9 +125,10 @@ impl Default for LuaScriptingPlugin {
124125
LuaReflectReference(<Entity>::allocate(Box::new(entity), world)),
125126
)
126127
.map_err(ScriptError::from_mlua_error)?;
128+
let path = script_id.path().map(|p| p.to_string()).unwrap_or_else(|| script_id.id().to_string());
127129
context
128130
.globals()
129-
.set("script_id", script_id.to_string())
131+
.set("script_id", path)
130132
.map_err(ScriptError::from_mlua_error)?;
131133
Ok(())
132134
}],
@@ -149,7 +151,7 @@ impl Plugin for LuaScriptingPlugin {
149151

150152
fn load_lua_content_into_context(
151153
context: &mut Lua,
152-
script_id: &ScriptId,
154+
script_id: &Handle<ScriptAsset>,
153155
content: &[u8],
154156
initializers: &[ContextInitializer<LuaScriptingPlugin>],
155157
pre_handling_initializers: &[ContextPreHandlingInitializer<LuaScriptingPlugin>],
@@ -173,7 +175,7 @@ fn load_lua_content_into_context(
173175
#[profiling::function]
174176
/// Load a lua context from a script
175177
pub fn lua_context_load(
176-
script_id: &ScriptId,
178+
script_id: &Handle<ScriptAsset>,
177179
content: &[u8],
178180
initializers: &[ContextInitializer<LuaScriptingPlugin>],
179181
pre_handling_initializers: &[ContextPreHandlingInitializer<LuaScriptingPlugin>],
@@ -197,7 +199,7 @@ pub fn lua_context_load(
197199
#[profiling::function]
198200
/// Reload a lua context from a script
199201
pub fn lua_context_reload(
200-
script: &ScriptId,
202+
script: &Handle<ScriptAsset>,
201203
content: &[u8],
202204
old_ctxt: &mut Lua,
203205
initializers: &[ContextInitializer<LuaScriptingPlugin>],
@@ -220,7 +222,7 @@ pub fn lua_context_reload(
220222
pub fn lua_handler(
221223
args: Vec<ScriptValue>,
222224
entity: bevy::ecs::entity::Entity,
223-
script_id: &ScriptId,
225+
script_id: &Handle<ScriptAsset>,
224226
callback_label: &CallbackLabel,
225227
context: &mut Lua,
226228
pre_handling_initializers: &[ContextPreHandlingInitializer<LuaScriptingPlugin>],
@@ -234,11 +236,22 @@ pub fn lua_handler(
234236
Ok(handler) => handler,
235237
// not subscribed to this event type
236238
Err(_) => {
237-
bevy::log::trace!(
238-
"Script {} is not subscribed to callback {}",
239-
script_id,
240-
callback_label.as_ref()
241-
);
239+
match script_id.path() {
240+
Some(path) => {
241+
bevy::log::trace!(
242+
"Script path {} is not subscribed to callback {}",
243+
path,
244+
callback_label.as_ref()
245+
);
246+
}
247+
None => {
248+
bevy::log::trace!(
249+
"Script id {} is not subscribed to callback {}",
250+
script_id.id(),
251+
callback_label.as_ref()
252+
);
253+
}
254+
}
242255
return Ok(ScriptValue::Unit);
243256
}
244257
};

crates/testing_crates/test_utils/src/test_data.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,10 @@ pub fn setup_integration_test<F: FnOnce(&mut World, &mut TypeRegistry)>(init: F)
348348
AssetPlugin::default(),
349349
HierarchyPlugin,
350350
DiagnosticsPlugin,
351-
LogPlugin {
352-
filter: log_level,
353-
..Default::default()
354-
},
351+
// LogPlugin {
352+
// filter: log_level,
353+
// ..Default::default()
354+
// },
355355
));
356356
app
357357
}

0 commit comments

Comments
 (0)