From 790e5a4725157dcea8d0404c3563f2fcf96ab18e Mon Sep 17 00:00:00 2001 From: Shane Celis Date: Sun, 17 Aug 2025 01:22:35 -0400 Subject: [PATCH 1/2] refactor: Remove asset_path from ScriptAsset. --- .../handle_asset_path_when_script_loaded.lua | 6 +++++- crates/bevy_mod_scripting_core/src/asset.rs | 6 +----- .../bevy_mod_scripting_functions/src/core.rs | 21 +++++++++++-------- .../src/scenario.rs | 1 - 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/assets/tests/handle/handle_asset_path_when_script_loaded.lua b/assets/tests/handle/handle_asset_path_when_script_loaded.lua index f0c5f301aa..dd3a289895 100644 --- a/assets/tests/handle/handle_asset_path_when_script_loaded.lua +++ b/assets/tests/handle/handle_asset_path_when_script_loaded.lua @@ -1,6 +1,10 @@ local expected_asset_path = "tests/handle/handle_asset_path_when_script_loaded.lua" function normalize_path(path) - return string.gsub(tostring(path), "\\", "/") + if path then + return string.gsub(path, "\\", "/") + else + return nil + end end local normalized_gotten_asset_path = normalize_path(script_asset:asset_path()) diff --git a/crates/bevy_mod_scripting_core/src/asset.rs b/crates/bevy_mod_scripting_core/src/asset.rs index 4e5f25f074..5c09fec0cf 100644 --- a/crates/bevy_mod_scripting_core/src/asset.rs +++ b/crates/bevy_mod_scripting_core/src/asset.rs @@ -12,7 +12,7 @@ use crate::{ }; use bevy::{ app::{App, Last}, - asset::{Asset, AssetEvent, AssetLoader, AssetPath, Assets, LoadState}, + asset::{Asset, AssetEvent, AssetLoader, Assets, LoadState}, log::{error, trace, warn, warn_once}, prelude::{ AssetServer, Commands, Entity, EventReader, EventWriter, IntoScheduleConfigs, Local, Query, @@ -59,8 +59,6 @@ pub struct ScriptAsset { pub content: Box<[u8]>, // Any chance a Cow<'static, ?> could work here? /// The language of the script pub language: Language, - /// The asset path of the script. - pub asset_path: AssetPath<'static>, } impl From for ScriptAsset { @@ -68,7 +66,6 @@ impl From for ScriptAsset { ScriptAsset { content: s.into_bytes().into_boxed_slice(), language: Language::default(), - asset_path: AssetPath::default(), } } } @@ -168,7 +165,6 @@ impl AssetLoader for ScriptAssetLoader { let asset = ScriptAsset { content: content.into_boxed_slice(), language, - asset_path: load_context.asset_path().clone(), }; Ok(asset) } diff --git a/crates/bevy_mod_scripting_functions/src/core.rs b/crates/bevy_mod_scripting_functions/src/core.rs index 9c0dfe2611..2b4ef9dd47 100644 --- a/crates/bevy_mod_scripting_functions/src/core.rs +++ b/crates/bevy_mod_scripting_functions/src/core.rs @@ -1257,16 +1257,19 @@ impl Handle { /// * `path`: The asset path of the script asset. fn asset_path(ctxt: FunctionCallContext, handle: Ref>) -> Option { profiling::function_scope!("path"); - ctxt.world().ok().and_then(|w| { - w.with_resource(|assets: &Assets| { - // debug - assets - .get(&*handle) - .map(|asset| asset.asset_path.to_string()) + handle.path() + .map(|p| p.to_string()) + .or_else(|| { + ctxt.world().ok().and_then(|w| { + w.with_resource(|asset_server: &AssetServer| { + // debug + asset_server.get_path(&*handle) + .map(|p| p.to_string()) + }) + .ok() + .flatten() + }) }) - .ok() - .flatten() - }) } } diff --git a/crates/testing_crates/script_integration_test_harness/src/scenario.rs b/crates/testing_crates/script_integration_test_harness/src/scenario.rs index 1c5fa8a71f..c24a4e2b8e 100644 --- a/crates/testing_crates/script_integration_test_harness/src/scenario.rs +++ b/crates/testing_crates/script_integration_test_harness/src/scenario.rs @@ -700,7 +700,6 @@ impl ScenarioStep { let boxed_byte_arr = content.into_bytes().into_boxed_slice(); *existing = ScriptAsset { content: boxed_byte_arr, - asset_path: path.into(), language: existing.language.clone(), }; } else { From 66a8d25a3f6550c31e66a34d2ad226e1c0a2de8c Mon Sep 17 00:00:00 2001 From: Shane Celis Date: Sun, 17 Aug 2025 02:13:36 -0400 Subject: [PATCH 2/2] style: Reformat. --- .../bevy_mod_scripting_functions/src/core.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/crates/bevy_mod_scripting_functions/src/core.rs b/crates/bevy_mod_scripting_functions/src/core.rs index 2b4ef9dd47..3904897bb6 100644 --- a/crates/bevy_mod_scripting_functions/src/core.rs +++ b/crates/bevy_mod_scripting_functions/src/core.rs @@ -1257,19 +1257,15 @@ impl Handle { /// * `path`: The asset path of the script asset. fn asset_path(ctxt: FunctionCallContext, handle: Ref>) -> Option { profiling::function_scope!("path"); - handle.path() - .map(|p| p.to_string()) - .or_else(|| { - ctxt.world().ok().and_then(|w| { - w.with_resource(|asset_server: &AssetServer| { - // debug - asset_server.get_path(&*handle) - .map(|p| p.to_string()) - }) - .ok() - .flatten() + handle.path().map(|p| p.to_string()).or_else(|| { + ctxt.world().ok().and_then(|w| { + w.with_resource(|asset_server: &AssetServer| { + asset_server.get_path(&*handle).map(|p| p.to_string()) }) + .ok() + .flatten() }) + }) } }