Skip to content

Commit cf4dbf4

Browse files
committed
hack: Compile with lua54 feature.
1 parent 3430b92 commit cf4dbf4

File tree

5 files changed

+47
-25
lines changed

5 files changed

+47
-25
lines changed

crates/bevy_mod_scripting_core/src/asset.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,17 @@ impl AssetLoader for ScriptAssetLoader {
9595
if let Some(processor) = &self.preprocessor {
9696
processor(&mut content)?;
9797
}
98+
let language = match load_context.path().extension().and_then(|e| e.to_str()).unwrap_or_default() {
99+
"lua" => Language::Lua,
100+
"rhai" => Language::Rhai,
101+
x => {
102+
warn!("Unknown language for {:?}", load_context.path().display());
103+
Language::Unknown
104+
}
105+
};
98106
let asset = ScriptAsset {
99107
content: content.into_boxed_slice(),
100-
language: Language::Lua,
108+
language,
101109
};
102110
Ok(asset)
103111
}

crates/bevy_mod_scripting_core/src/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ impl<T: 'static + Send> Context for T {}
1717

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

2222
/// Initializer run every time before executing or loading/re-loading a script
2323
pub type ContextPreHandlingInitializer<P> =
24-
fn(&str, Entity, &mut <P as IntoScriptPluginParams>::C) -> Result<(), ScriptError>;
24+
fn(&ScriptId, Entity, &mut <P as IntoScriptPluginParams>::C) -> Result<(), ScriptError>;
2525

2626
/// Settings concerning the creation and assignment of script contexts as well as their initialization.
2727
#[derive(Resource)]

crates/languages/bevy_mod_scripting_lua/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl Default for LuaScriptingPlugin {
126126
.map_err(ScriptError::from_mlua_error)?;
127127
context
128128
.globals()
129-
.set("script_id", script_id)
129+
.set("script_id", script_id.to_string())
130130
.map_err(ScriptError::from_mlua_error)?;
131131
Ok(())
132132
}],

crates/testing_crates/script_integration_test_harness/src/lib.rs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88

99
use bevy::{
1010
app::{Last, Plugin, PostUpdate, Startup, Update},
11-
asset::{AssetServer, Handle},
11+
asset::{AssetServer, Handle, AssetPath, AssetId},
1212
ecs::{
1313
component::Component,
1414
event::{Event, Events},
@@ -55,13 +55,15 @@ struct TestCallbackBuilder<P: IntoScriptPluginParams, L: IntoCallbackLabel> {
5555
}
5656

5757
impl<L: IntoCallbackLabel, P: IntoScriptPluginParams> TestCallbackBuilder<P, L> {
58-
fn build(script_id: impl Into<ScriptId>, expect_response: bool) -> SystemConfigs {
59-
let script_id = script_id.into();
58+
fn build<'a>(script_path: impl Into<AssetPath<'a>>, expect_response: bool) -> SystemConfigs {
59+
let script_path = script_path.into().into_owned();
6060
IntoSystem::into_system(
6161
move |world: &mut World,
6262
system_state: &mut SystemState<WithWorldGuard<HandlerContext<P>>>| {
63+
let script_id = world.resource::<AssetServer>().load(script_path.clone()).id();
64+
6365
let with_guard = system_state.get_mut(world);
64-
let _ = run_test_callback::<P, L>(&script_id.clone(), with_guard, expect_response);
66+
let _ = run_test_callback::<P, L>(&script_id, with_guard, expect_response);
6567

6668
system_state.apply(world);
6769
},
@@ -296,18 +298,18 @@ pub fn execute_integration_test<
296298
}
297299

298300
fn run_test_callback<P: IntoScriptPluginParams, C: IntoCallbackLabel>(
299-
script_id: &str,
301+
script_id: &ScriptId,
300302
mut with_guard: WithWorldGuard<'_, '_, HandlerContext<'_, P>>,
301303
expect_response: bool,
302304
) -> Result<ScriptValue, ScriptError> {
303305
let (guard, handler_ctxt) = with_guard.get_mut();
304306

305-
if !handler_ctxt.is_script_fully_loaded(script_id.to_string().into()) {
307+
if !handler_ctxt.is_script_fully_loaded(*script_id) {
306308
return Ok(ScriptValue::Unit);
307309
}
308310

309311
let res = handler_ctxt.call::<C>(
310-
&script_id.to_string().into(),
312+
&script_id,
311313
Entity::from_raw(0),
312314
vec![],
313315
guard.clone(),
@@ -406,9 +408,9 @@ pub fn run_rhai_benchmark<M: criterion::measurement::Measurement>(
406408
)
407409
}
408410

409-
pub fn run_plugin_benchmark<P, F, M: criterion::measurement::Measurement>(
411+
pub fn run_plugin_benchmark<'a, P, F, M: criterion::measurement::Measurement>(
410412
plugin: P,
411-
script_id: &str,
413+
script_path: impl Into<AssetPath<'a>>,
412414
label: &str,
413415
criterion: &mut criterion::BenchmarkGroup<M>,
414416
bench_fn: F,
@@ -425,14 +427,22 @@ where
425427

426428
install_test_plugin(&mut app, plugin, true);
427429

428-
let script_id = script_id.to_owned();
429-
let script_id_clone = script_id.clone();
430-
app.add_systems(
431-
Startup,
432-
move |server: Res<AssetServer>, mut handle: Local<Handle<ScriptAsset>>| {
433-
*handle = server.load(script_id_clone.to_owned());
434-
},
435-
);
430+
// let script_id = script_id.to_owned();
431+
// let script_id_clone = script_id.clone();
432+
433+
434+
let script_path = script_path.into();
435+
let script_handle = app.world().resource::<AssetServer>().load(script_path);
436+
let script_id = script_handle.id();
437+
438+
439+
// app.add_systems(
440+
// Startup,
441+
// move |server: Res<AssetServer>, mut handle: Local<Handle<ScriptAsset>>| {
442+
// *handle = server.load(script_id_clone.to_owned());
443+
// handle.id()
444+
// },
445+
// );
436446

437447
// finalize
438448
app.cleanup();
@@ -482,7 +492,7 @@ pub fn run_plugin_script_load_benchmark<
482492
benchmark_id: &str,
483493
content: &str,
484494
criterion: &mut criterion::BenchmarkGroup<M>,
485-
script_id_generator: impl Fn(u64) -> String,
495+
script_id_generator: impl Fn(u64) -> AssetId<ScriptAsset>,
486496
reload_probability: f32,
487497
) {
488498
let mut app = setup_integration_test(|_, _| {});
@@ -502,7 +512,7 @@ pub fn run_plugin_script_load_benchmark<
502512
let content = content.to_string().into_boxed_str();
503513
(
504514
CreateOrUpdateScript::<P>::new(
505-
random_script_id.into(),
515+
random_script_id,
506516
content.clone().into(),
507517
None,
508518
),

examples/game_of_life.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ fn run_script_cmd(
5555
mut log: ConsoleCommand<GameOfLifeCommand>,
5656
mut commands: Commands,
5757
mut loaded_scripts: ResMut<LoadedScripts>,
58+
asset_server: Res<AssetServer>,
59+
mut script_handle: Local<Option<Handle<ScriptAsset>>>,
5860
) {
5961
if let Some(Ok(command)) = log.take() {
6062
match command {
@@ -69,12 +71,14 @@ fn run_script_cmd(
6971
);
7072

7173
let script_path = format!("scripts/game_of_life.{}", language);
74+
*script_handle = Some(asset_server.load(script_path));
75+
let script_id = script_handle.as_ref().unwrap().id();
7276
if !use_static_script {
7377
bevy::log::info!("Spawning an entity with ScriptComponent");
74-
commands.spawn(ScriptComponent::new(vec![script_path]));
78+
commands.spawn(ScriptComponent::new(vec![script_id]));
7579
} else {
7680
bevy::log::info!("Using static script instead of spawning an entity");
77-
commands.queue(AddStaticScript::new(script_path))
81+
commands.queue(AddStaticScript::new(script_id))
7882
}
7983
}
8084
GameOfLifeCommand::Stop => {

0 commit comments

Comments
 (0)