From f7d5aaf420bc67b45ead8ebaf3438efba0ab0685 Mon Sep 17 00:00:00 2001 From: makspll Date: Wed, 12 Mar 2025 17:58:14 +0000 Subject: [PATCH 1/4] feat: improve extension configuration options for end users, and improve docs --- docs/src/ScriptSystems/introduction.md | 45 +++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/docs/src/ScriptSystems/introduction.md b/docs/src/ScriptSystems/introduction.md index 8f58c2bbde..fe53c76dfc 100644 --- a/docs/src/ScriptSystems/introduction.md +++ b/docs/src/ScriptSystems/introduction.md @@ -4,9 +4,11 @@ Script systems are an experimental feature -It's possible within BMS to inject new systems from scripts themselves, although the support is currently limited. +It's possible within BMS to inject new systems from within scripts themselves. -Systems introduced by scripts cannot run in parallel to other systems, but can be freely inserted between any other rust system (not script systems at the moment) and into any schedule. +Systems introduced by scripts *can* run in parallel to other systems, and can be freely inserted between any other system, including other script systems. + +BMS also provides utilities for visualising schedules using dot graphs, allowing low-effort modding frameworks for game authors. ## Schedules @@ -27,6 +29,7 @@ To insert a system you will need to use the `system_builder` global function lik ```lua local system = system_builder("my_system", script_id) + :exclusive() :after(some_other_system) :before(another_system) ``` @@ -43,9 +46,43 @@ If your event handler running the script is running in a certain schedule, that -## Dynamic system +## Parameters + +The system builder allows script authors to parameterise systems, using `resource` and `query` functions. +The order in which those functions are called, will define the order in which arguments will be provided to the specified script callback. + +For example: +```lua +system_builder("my_system") + :query( + world.query() + :component(ComponentA) + :component(ComponentB) + :with(ComponentC) + :without(ComponentD) + ) + :resource(ResourceA) +``` + +will create a system which calls the specified callback `my_system` with 2 arguments: +- The `ScriptQueryResult` for the first query + - With `components` access to ComponentA and ComponentB +- The `ReflectReference` to `ResourceA` + +## Exclusive systems + +An exclusive system can be created using the `exclusive` function call on the system builder. + +This allows the system to access everything as in a normal event handler. + +Non-exclusive systems, will only be able to access the set of components and resources as parameterized when building the system. This is why we can run the system in parallel to other non-overlapping systems. + +Exclusive systems on the other hand, cannot run in parallel. + + +## Callback -The system injected will be similar to an event handler, however it will only trigger the specified script, and without any entity, in the above example you'd see the following lua callback: +The system injected will be similar to an event handler, however it will only trigger the specified script, and without any entity, in the first example you'd see the following lua callback: ```lua function my_system() From defec17bf8cc7764efdab02e202e9a4830a7a605 Mon Sep 17 00:00:00 2001 From: makspll Date: Wed, 12 Mar 2025 17:58:21 +0000 Subject: [PATCH 2/4] improve docs --- crates/bevy_mod_scripting_core/src/asset.rs | 68 ++++++------------ crates/bevy_mod_scripting_core/src/lib.rs | 46 +++++++----- .../bevy_mod_scripting_lua/src/lib.rs | 2 +- .../bevy_mod_scripting_rhai/src/lib.rs | 2 +- docs/src/SUMMARY.md | 1 + .../constructing-arbitrary-types.md | 72 +++++++++++++++++++ docs/src/ScriptingReference/introduction.md | 2 +- .../Summary/controlling-script-bindings.md | 26 +++++++ docs/src/Summary/managing-scripts.md | 18 ++++- docs/src/Summary/running-scripts.md | 4 ++ docs/src/Summary/script-id-mapping.md | 6 +- 11 files changed, 176 insertions(+), 71 deletions(-) create mode 100644 docs/src/ScriptingReference/constructing-arbitrary-types.md diff --git a/crates/bevy_mod_scripting_core/src/asset.rs b/crates/bevy_mod_scripting_core/src/asset.rs index a41e911237..3b7018dd5f 100644 --- a/crates/bevy_mod_scripting_core/src/asset.rs +++ b/crates/bevy_mod_scripting_core/src/asset.rs @@ -16,12 +16,12 @@ use bevy::{ ResMut, }, reflect::TypePath, - utils::HashMap, + utils::hashbrown::HashMap, }; use std::borrow::Cow; /// Represents a scripting language. Languages which compile into another language should use the target language as their language. -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Default)] pub enum Language { /// The Rhai scripting language Rhai, @@ -32,6 +32,7 @@ pub enum Language { /// An external scripting language External(Cow<'static, str>), /// Set if none of the asset path to language mappers match + #[default] Unknown, } @@ -110,8 +111,8 @@ impl AssetLoader for ScriptAssetLoader { pub struct ScriptAssetSettings { /// Strategy for mapping asset paths to script ids, by default this is the identity function pub script_id_mapper: AssetPathToScriptIdMapper, - /// Strategies for mapping asset paths to languages - pub script_language_mappers: Vec, + /// Mapping from extension to script language + pub extension_to_language_map: HashMap<&'static str, Language>, /// The currently supported asset extensions /// Should be updated by each scripting plugin to include the extensions it supports. @@ -123,15 +124,11 @@ pub struct ScriptAssetSettings { impl ScriptAssetSettings { /// Selects the language for a given asset path pub fn select_script_language(&self, path: &AssetPath) -> Language { - for mapper in &self.script_language_mappers { - let language = (mapper.map)(path); - match language { - Language::Unknown => continue, - _ => return language, - } - } - - Language::Unknown + let extension = path.path().extension().and_then(|ext| ext.to_str()).unwrap_or_default(); + self.extension_to_language_map + .get(extension) + .cloned() + .unwrap_or_default() } } @@ -141,7 +138,12 @@ impl Default for ScriptAssetSettings { script_id_mapper: AssetPathToScriptIdMapper { map: (|path: &AssetPath| path.path().to_string_lossy().into_owned().into()), }, - script_language_mappers: vec![], + extension_to_language_map: HashMap::from_iter(vec![ + ("lua", Language::Lua), + ("luau", Language::Lua), + ("rhai", Language::Rhai), + ("rn", Language::Rune), + ]), supported_extensions: &[], } } @@ -154,20 +156,6 @@ pub struct AssetPathToScriptIdMapper { pub map: fn(&AssetPath) -> ScriptId, } -#[derive(Clone, Copy)] -/// Strategy for mapping asset paths to languages -pub struct AssetPathToLanguageMapper { - /// The mapping function - pub map: fn(&AssetPath) -> Language, -} - -impl Default for AssetPathToLanguageMapper { - fn default() -> Self { - Self { - map: |_| Language::Unknown, - } - } -} /// A cache of asset id's to their script id's. Necessary since when we drop an asset we won't have the ability to get the path from the asset. #[derive(Default, Debug, Resource)] @@ -393,26 +381,10 @@ mod tests { script_id_mapper: AssetPathToScriptIdMapper { map: |path| path.path().to_string_lossy().into_owned().into(), }, - script_language_mappers: vec![ - AssetPathToLanguageMapper { - map: |path| { - if path.path().extension().unwrap() == "lua" { - Language::Lua - } else { - Language::Unknown - } - }, - }, - AssetPathToLanguageMapper { - map: |path| { - if path.path().extension().unwrap() == "rhai" { - Language::Rhai - } else { - Language::Unknown - } - }, - }, - ], + extension_to_language_map: HashMap::from_iter(vec![ + ("lua", Language::Lua), + ("rhai", Language::Rhai), + ]), } } diff --git a/crates/bevy_mod_scripting_core/src/lib.rs b/crates/bevy_mod_scripting_core/src/lib.rs index 571413c8a8..a08f14adcd 100644 --- a/crates/bevy_mod_scripting_core/src/lib.rs +++ b/crates/bevy_mod_scripting_core/src/lib.rs @@ -4,7 +4,7 @@ use crate::event::ScriptErrorEvent; use asset::{ - configure_asset_systems, configure_asset_systems_for_plugin, AssetPathToLanguageMapper, + configure_asset_systems, configure_asset_systems_for_plugin, Language, ScriptAsset, ScriptAssetLoader, ScriptAssetSettings, }; use bevy::prelude::*; @@ -87,16 +87,17 @@ pub struct ScriptingPlugin { /// The strategy for assigning contexts to scripts pub context_assignment_strategy: ContextAssignmentStrategy, - /// The asset path to language mapper for the plugin - pub language_mapper: AssetPathToLanguageMapper, + /// The language this plugin declares + pub language: Language, + /// Supported extensions to be added to the asset settings without the dot + /// By default BMS populates a set of extensions for the languages it supports. + pub additional_supported_extensions: &'static [&'static str], /// initializers for the contexts, run when loading the script pub context_initializers: Vec>, /// initializers for the contexts run every time before handling events pub context_pre_handling_initializers: Vec>, - /// Supported extensions to be added to the asset settings without the dot - pub supported_extensions: &'static [&'static str], } impl Default for ScriptingPlugin

{ @@ -106,10 +107,10 @@ impl Default for ScriptingPlugin

{ callback_handler: CallbackSettings::

::default().callback_handler, context_builder: Default::default(), context_assignment_strategy: Default::default(), - language_mapper: Default::default(), + language: Default::default(), context_initializers: Default::default(), context_pre_handling_initializers: Default::default(), - supported_extensions: Default::default(), + additional_supported_extensions: Default::default(), } } } @@ -136,13 +137,9 @@ impl Plugin for ScriptingPlugin

{ // add extension for the language to the asset loader once_per_app_init(app); - app.add_supported_script_extensions(self.supported_extensions); - - app.world_mut() - .resource_mut::() - .as_mut() - .script_language_mappers - .push(self.language_mapper); + if !self.additional_supported_extensions.is_empty() { + app.add_supported_script_extensions(self.additional_supported_extensions, self.language.clone()); + } register_types(app); } @@ -203,6 +200,11 @@ pub trait ConfigureScriptPlugin { /// This means that all scripts will share the same context. This is useful for when you want to share data between scripts easilly. /// Be careful however as this also means that scripts can interfere with each other in unexpected ways! Including overwriting each other's handlers. fn enable_context_sharing(self) -> Self; + + /// Set the set of extensions to be added for the plugin's language. + /// + /// This is useful for adding extensions that are not supported by default by BMS. + fn set_additional_supported_extensions(self, extensions: &'static [&'static str]) -> Self; } impl>> ConfigureScriptPlugin for P { @@ -231,6 +233,13 @@ impl>> ConfigureScriptPlugi self.as_mut().context_assignment_strategy = ContextAssignmentStrategy::Global; self } + + fn set_additional_supported_extensions(mut self, extensions: &'static [&'static str]) -> Self { + self.as_mut().additional_supported_extensions = extensions; + self + } + + } fn once_per_app_finalize(app: &mut App) { @@ -386,11 +395,13 @@ impl ManageStaticScripts for App { /// Any changes to the asset settings after that will not be reflected in the asset loader. pub trait ConfigureScriptAssetSettings { /// Adds a supported extension to the asset settings - fn add_supported_script_extensions(&mut self, extensions: &[&'static str]) -> &mut Self; + /// + /// This is only valid to call in the plugin building phase, as the asset loader will be created in the `finalize` phase. + fn add_supported_script_extensions(&mut self, extensions: &[&'static str], language: Language) -> &mut Self; } impl ConfigureScriptAssetSettings for App { - fn add_supported_script_extensions(&mut self, extensions: &[&'static str]) -> &mut Self { + fn add_supported_script_extensions(&mut self, extensions: &[&'static str], language: Language) -> &mut Self { let mut asset_settings = self .world_mut() .get_resource_or_init::(); @@ -402,6 +413,9 @@ impl ConfigureScriptAssetSettings for App { let new_arr_static = Vec::leak(new_arr); asset_settings.supported_extensions = new_arr_static; + for extension in extensions { + asset_settings.extension_to_language_map.insert(*extension, language.clone()); + } self } diff --git a/crates/languages/bevy_mod_scripting_lua/src/lib.rs b/crates/languages/bevy_mod_scripting_lua/src/lib.rs index 5ad3b18aea..a8c10d46c0 100644 --- a/crates/languages/bevy_mod_scripting_lua/src/lib.rs +++ b/crates/languages/bevy_mod_scripting_lua/src/lib.rs @@ -134,7 +134,7 @@ impl Default for LuaScriptingPlugin { .map_err(ScriptError::from_mlua_error)?; Ok(()) }], - supported_extensions: &["lua"], + additional_supported_extensions: &["lua"], }, } } diff --git a/crates/languages/bevy_mod_scripting_rhai/src/lib.rs b/crates/languages/bevy_mod_scripting_rhai/src/lib.rs index fee3189de0..45c1a5aa5d 100644 --- a/crates/languages/bevy_mod_scripting_rhai/src/lib.rs +++ b/crates/languages/bevy_mod_scripting_rhai/src/lib.rs @@ -159,7 +159,7 @@ impl Default for RhaiScriptingPlugin { context.scope.set_or_push("script_id", script.to_owned()); Ok(()) }], - supported_extensions: &["rhai"], + additional_supported_extensions: &["rhai"], }, } } diff --git a/docs/src/SUMMARY.md b/docs/src/SUMMARY.md index 770dda6d42..601eacc155 100644 --- a/docs/src/SUMMARY.md +++ b/docs/src/SUMMARY.md @@ -16,6 +16,7 @@ # Scripting Reference - [Introduction](./ScriptingReference/introduction.md) +- [Constructing Arbitrary Types](./ScriptingReference/constructing-arbitrary-types.md) - [Core Bindings](./ScriptingReference/core-api.md) - [World](./ScriptingReference/world.md) - [ReflectReference](./ScriptingReference/reflect-reference.md) diff --git a/docs/src/ScriptingReference/constructing-arbitrary-types.md b/docs/src/ScriptingReference/constructing-arbitrary-types.md new file mode 100644 index 0000000000..2ffd65725a --- /dev/null +++ b/docs/src/ScriptingReference/constructing-arbitrary-types.md @@ -0,0 +1,72 @@ +# Constructing Arbitrary Types + +When interfacing with bevy, we do this via reflection. +While the generated bindings do not cover constructors for every single type that bevy or other libraries provide, reflection allows us to construct some (not all types implement `FromReflect`) types from dynamic structs. + +BMS exposes this ability to all script writers via the `construct` global function. + + +## Structs + +The following struct: +```rust,ignore +pub struct MyStruct { + pub my_field: String +} +``` + +can be constructed from lua like so: +```lua +local MyStruct = world.get_type_by_name("MyStruct") +local concrete_my_struct = construct(MyStruct, { + my_field = "hello" +}) +``` + +## Tuple Structs +The following tuple struct: +```rust,ignore + +pub struct MyTupleStruct(pub String); +``` + +can be constructed like so: +```lua + +local MyTupleStruct = world.get_type_by_name("MyTupleStruct") +local concrete_my_tuple_struct = construct(MyTupleStruct, { + _1 = "hello" +}) +``` + +## Enums +The following enum: +```rust,ignore +pub enum MyEnum { + VariantA { + field: String + }, + VariantB +} +``` + +can be constructed like so: +```lua + +local MyEnum = world.get_type_by_name("MyEnum") +local variantA = construct(MyEnum, { + variant = "VariantA", + field = "hello" +}) +local variantB = construct(MyEnum, { + variant = "VariantB" +}) +``` + +When working with enums you can also figure out the variant at runtime using `variant_name`: + +```lua +if my_enum:variant_name() == "VariantA" then + print(my_enum.field) +end +``` \ No newline at end of file diff --git a/docs/src/ScriptingReference/introduction.md b/docs/src/ScriptingReference/introduction.md index 301f2c22e2..fbaf0967fb 100644 --- a/docs/src/ScriptingReference/introduction.md +++ b/docs/src/ScriptingReference/introduction.md @@ -8,5 +8,5 @@ If you are a modder, welcome! 👋, apologies for the rust-centricity of this gu Scripts will have access to a few global variables in most callbacks: - `world`: a static reference to the world, with all sorts of functions available -- `entity`: the entity the script is attached to, not available on load/unload callbacks +- `entity`: the entity the script is attached to, not available on load/unload callbacks, and in dynamic system callbacks. - `script_id`: the ID of the current script diff --git a/docs/src/Summary/controlling-script-bindings.md b/docs/src/Summary/controlling-script-bindings.md index 0a4d9fe13c..1b706a35d9 100644 --- a/docs/src/Summary/controlling-script-bindings.md +++ b/docs/src/Summary/controlling-script-bindings.md @@ -59,6 +59,32 @@ hello_world2("hi from global!"); Note the `new_unregistered` call instead of `new`, this is because `GlobalNamespace` is not a `Reflect` type, and the `new` call also automatically registers the type in the reflection registry. +## Macros +The above is a bit tedious, so instead you can use the `script_bindings` macro, which applies to impl blocks like so: + +```rust,ignore +#[script_bindings("test_fn")] +impl TestStruct { + /// My docs !! + /// + /// Arguments: + /// * `_self` - the first argument + /// * `arg1` - the second argument + /// Returns: + /// * `return` - nothing + fn test_fn(_self: Ref, mut arg1: usize) {} +} + + +pub fn main() { + let mut app = App::new(); + register_test_fn(app.world_mut()) +} +``` + +Note the documentation will automatically be picked up and stored for the purposes of reflection and documentation generation, including argument/return type specific docs. + + ## Context Arguments Each script function call always receives an additional context argument: `FunctionCallContext`. diff --git a/docs/src/Summary/managing-scripts.md b/docs/src/Summary/managing-scripts.md index 43d4d57abb..e2f93aecbc 100644 --- a/docs/src/Summary/managing-scripts.md +++ b/docs/src/Summary/managing-scripts.md @@ -30,7 +30,19 @@ To enable hot-loading of assets, you need to enable the necessary bevy features Assuming that hot-reloading is enabled for your app, any changes to script assets will automatically be picked up and the scripts re-loaded. -## Manually (re)loading scripts +## File Extensions +Normally the set of supported extensions is pre-decided by each language plugin. + +I.e. Lua supports ".lua" extensions and Rhai supports ".rhai" extensions. + +Scripts are mapped to the corresponding language plugin based on these and so it's important to use them correctly. + +If you would like to add more extensions you need to populate them via `app.add_supported_script_extensions`. + +## Advanced +Normally not necessary, but knowing these exist could be useful for more advanced use cases. + +### Manually (re)loading scripts In order to manually re-load or load a script you can issue the `CreateOrUpdateScript` command: ```rust,ignore @@ -39,7 +51,7 @@ CreateOrUpdateScript::::new("my_script.lua".into(), "print(\ replace `LuaScriptingPlugin` with the scripting plugin you are using. -## Manually Deleting scripts +### Manually Deleting scripts In order to delete a previously loaded script, you will need to issue a `DeleteScript` command like so: ```rust,ignore @@ -48,5 +60,5 @@ DeleteScript::::new("my_script.lua".into()) replace `LuaScriptingPlugin` with the scripting plugin you are using. -## Loading/Unloading timeframe +### Loading/Unloading timeframe Scripts asset events are processed within the same frame they are issued. This means the moment an asset is loaded, it should be loaded and ready to use in the `Update` schedule. Similarly, the moment an asset is deleted, it should be unloaded and no longer usable in the `Update` schedule. \ No newline at end of file diff --git a/docs/src/Summary/running-scripts.md b/docs/src/Summary/running-scripts.md index c912ffa197..239ab42ef0 100644 --- a/docs/src/Summary/running-scripts.md +++ b/docs/src/Summary/running-scripts.md @@ -19,6 +19,10 @@ In order to attach a script and make it runnable simply add a `ScriptComponent` When this script is run the `entity` global will represent the entity the script is attached to. This allows you to interact with the entity in your script easilly. +

+Be wary of path separators, by default script ID's are derived from asset paths, which are platform dependent. Make sure to use `std::path::PathBuf` if you are targetting multiple platforms. +
+ ## Making static scripts runnable Some scripts do not require attaching to an entity. You can run these scripts by loading them first as you would with any other script, then either adding them at app level via `add_static_script` or by issuing a `AddStaticScript` command like so: diff --git a/docs/src/Summary/script-id-mapping.md b/docs/src/Summary/script-id-mapping.md index 0dccd85de5..fe8027b0f7 100644 --- a/docs/src/Summary/script-id-mapping.md +++ b/docs/src/Summary/script-id-mapping.md @@ -4,4 +4,8 @@ Every script is currently identified by a unique ID. ID's are derived from the script asset path for scripts loaded via the asset system. -By default this is an identity mapping, but you can override this by modifying the `AssetPathToScriptIdMapper` inside the `ScriptAssetSettings` resource before loading the script. \ No newline at end of file +By default this is an identity mapping, but you can override this by modifying the `AssetPathToScriptIdMapper` inside the `ScriptAssetSettings` resource before loading the script. + +
+Be wary of path separators, by default script ID's are derived from asset paths, which are platform dependent. Make sure to use `std::path::PathBuf` if you are targetting multiple platforms. +
\ No newline at end of file From b533c4f07a09f172f4fea23d00389c9023f15d93 Mon Sep 17 00:00:00 2001 From: makspll Date: Wed, 12 Mar 2025 18:03:47 +0000 Subject: [PATCH 3/4] clean up language implementors --- .../languages/bevy_mod_scripting_lua/src/lib.rs | 17 ++++------------- .../bevy_mod_scripting_rhai/src/lib.rs | 17 ++++------------- 2 files changed, 8 insertions(+), 26 deletions(-) diff --git a/crates/languages/bevy_mod_scripting_lua/src/lib.rs b/crates/languages/bevy_mod_scripting_lua/src/lib.rs index a8c10d46c0..8e2932f8c8 100644 --- a/crates/languages/bevy_mod_scripting_lua/src/lib.rs +++ b/crates/languages/bevy_mod_scripting_lua/src/lib.rs @@ -1,11 +1,10 @@ //! Lua integration for the bevy_mod_scripting system. use bevy::{ app::Plugin, - asset::AssetPath, ecs::{entity::Entity, world::World}, }; use bevy_mod_scripting_core::{ - asset::{AssetPathToLanguageMapper, Language}, + asset::Language, bindings::{ function::namespace::Namespace, globals::AppScriptGlobalsRegistry, script_value::ScriptValue, ThreadWorldContainer, WorldContainer, @@ -60,9 +59,6 @@ impl Default for LuaScriptingPlugin { load: lua_context_load, reload: lua_context_reload, }, - language_mapper: AssetPathToLanguageMapper { - map: lua_language_mapper, - }, context_initializers: vec![ |_script_id, context| { // set the world global @@ -134,18 +130,13 @@ impl Default for LuaScriptingPlugin { .map_err(ScriptError::from_mlua_error)?; Ok(()) }], - additional_supported_extensions: &["lua"], + additional_supported_extensions: &[], + language: Language::Lua, }, } } } -#[profiling::function] -fn lua_language_mapper(path: &AssetPath) -> Language { - match path.path().extension().and_then(|ext| ext.to_str()) { - Some("lua") => Language::Lua, - _ => Language::Unknown, - } -} + impl Plugin for LuaScriptingPlugin { fn build(&self, app: &mut bevy::prelude::App) { diff --git a/crates/languages/bevy_mod_scripting_rhai/src/lib.rs b/crates/languages/bevy_mod_scripting_rhai/src/lib.rs index 45c1a5aa5d..857c5b5ca3 100644 --- a/crates/languages/bevy_mod_scripting_rhai/src/lib.rs +++ b/crates/languages/bevy_mod_scripting_rhai/src/lib.rs @@ -2,11 +2,10 @@ use bevy::{ app::Plugin, - asset::AssetPath, ecs::{entity::Entity, world::World}, }; use bevy_mod_scripting_core::{ - asset::{AssetPathToLanguageMapper, Language}, + asset::Language, bindings::{ function::namespace::Namespace, globals::AppScriptGlobalsRegistry, script_value::ScriptValue, ThreadWorldContainer, WorldContainer, @@ -84,9 +83,6 @@ impl Default for RhaiScriptingPlugin { load: rhai_context_load, reload: rhai_context_reload, }, - language_mapper: AssetPathToLanguageMapper { - map: rhai_language_mapper, - }, context_initializers: vec![ |_, context: &mut RhaiScriptContext| { context.scope.set_or_push( @@ -159,19 +155,14 @@ impl Default for RhaiScriptingPlugin { context.scope.set_or_push("script_id", script.to_owned()); Ok(()) }], - additional_supported_extensions: &["rhai"], + // already supported by BMS core + additional_supported_extensions: &[], + language: Language::Rhai, }, } } } -fn rhai_language_mapper(path: &AssetPath) -> Language { - match path.path().extension().and_then(|ext| ext.to_str()) { - Some("rhai") => Language::Rhai, - _ => Language::Unknown, - } -} - impl Plugin for RhaiScriptingPlugin { fn build(&self, app: &mut bevy::prelude::App) { self.scripting_plugin.build(app); From cb68731e762495277eb50f643cad41995a46945d Mon Sep 17 00:00:00 2001 From: makspll Date: Wed, 12 Mar 2025 18:32:30 +0000 Subject: [PATCH 4/4] formatting --- crates/bevy_mod_scripting_core/src/asset.rs | 9 ++++-- crates/bevy_mod_scripting_core/src/lib.rs | 32 ++++++++++++------- .../bevy_mod_scripting_lua/src/lib.rs | 1 - 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/crates/bevy_mod_scripting_core/src/asset.rs b/crates/bevy_mod_scripting_core/src/asset.rs index 3b7018dd5f..5343ea5abc 100644 --- a/crates/bevy_mod_scripting_core/src/asset.rs +++ b/crates/bevy_mod_scripting_core/src/asset.rs @@ -124,11 +124,15 @@ pub struct ScriptAssetSettings { impl ScriptAssetSettings { /// Selects the language for a given asset path pub fn select_script_language(&self, path: &AssetPath) -> Language { - let extension = path.path().extension().and_then(|ext| ext.to_str()).unwrap_or_default(); + let extension = path + .path() + .extension() + .and_then(|ext| ext.to_str()) + .unwrap_or_default(); self.extension_to_language_map .get(extension) .cloned() - .unwrap_or_default() + .unwrap_or_default() } } @@ -156,7 +160,6 @@ pub struct AssetPathToScriptIdMapper { pub map: fn(&AssetPath) -> ScriptId, } - /// A cache of asset id's to their script id's. Necessary since when we drop an asset we won't have the ability to get the path from the asset. #[derive(Default, Debug, Resource)] pub struct ScriptMetadataStore { diff --git a/crates/bevy_mod_scripting_core/src/lib.rs b/crates/bevy_mod_scripting_core/src/lib.rs index a08f14adcd..690cc18b98 100644 --- a/crates/bevy_mod_scripting_core/src/lib.rs +++ b/crates/bevy_mod_scripting_core/src/lib.rs @@ -4,8 +4,8 @@ use crate::event::ScriptErrorEvent; use asset::{ - configure_asset_systems, configure_asset_systems_for_plugin, - Language, ScriptAsset, ScriptAssetLoader, ScriptAssetSettings, + configure_asset_systems, configure_asset_systems_for_plugin, Language, ScriptAsset, + ScriptAssetLoader, ScriptAssetSettings, }; use bevy::prelude::*; use bindings::{ @@ -97,7 +97,6 @@ pub struct ScriptingPlugin { pub context_initializers: Vec>, /// initializers for the contexts run every time before handling events pub context_pre_handling_initializers: Vec>, - } impl Default for ScriptingPlugin

{ @@ -138,7 +137,10 @@ impl Plugin for ScriptingPlugin

{ once_per_app_init(app); if !self.additional_supported_extensions.is_empty() { - app.add_supported_script_extensions(self.additional_supported_extensions, self.language.clone()); + app.add_supported_script_extensions( + self.additional_supported_extensions, + self.language.clone(), + ); } register_types(app); @@ -202,7 +204,7 @@ pub trait ConfigureScriptPlugin { fn enable_context_sharing(self) -> Self; /// Set the set of extensions to be added for the plugin's language. - /// + /// /// This is useful for adding extensions that are not supported by default by BMS. fn set_additional_supported_extensions(self, extensions: &'static [&'static str]) -> Self; } @@ -238,8 +240,6 @@ impl>> ConfigureScriptPlugi self.as_mut().additional_supported_extensions = extensions; self } - - } fn once_per_app_finalize(app: &mut App) { @@ -395,13 +395,21 @@ impl ManageStaticScripts for App { /// Any changes to the asset settings after that will not be reflected in the asset loader. pub trait ConfigureScriptAssetSettings { /// Adds a supported extension to the asset settings - /// + /// /// This is only valid to call in the plugin building phase, as the asset loader will be created in the `finalize` phase. - fn add_supported_script_extensions(&mut self, extensions: &[&'static str], language: Language) -> &mut Self; + fn add_supported_script_extensions( + &mut self, + extensions: &[&'static str], + language: Language, + ) -> &mut Self; } impl ConfigureScriptAssetSettings for App { - fn add_supported_script_extensions(&mut self, extensions: &[&'static str], language: Language) -> &mut Self { + fn add_supported_script_extensions( + &mut self, + extensions: &[&'static str], + language: Language, + ) -> &mut Self { let mut asset_settings = self .world_mut() .get_resource_or_init::(); @@ -414,7 +422,9 @@ impl ConfigureScriptAssetSettings for App { asset_settings.supported_extensions = new_arr_static; for extension in extensions { - asset_settings.extension_to_language_map.insert(*extension, language.clone()); + asset_settings + .extension_to_language_map + .insert(*extension, language.clone()); } self diff --git a/crates/languages/bevy_mod_scripting_lua/src/lib.rs b/crates/languages/bevy_mod_scripting_lua/src/lib.rs index 8e2932f8c8..473c98d24c 100644 --- a/crates/languages/bevy_mod_scripting_lua/src/lib.rs +++ b/crates/languages/bevy_mod_scripting_lua/src/lib.rs @@ -137,7 +137,6 @@ impl Default for LuaScriptingPlugin { } } - impl Plugin for LuaScriptingPlugin { fn build(&self, app: &mut bevy::prelude::App) { self.scripting_plugin.build(app);