-
-
Notifications
You must be signed in to change notification settings - Fork 49
refactor: add world-local static plugin config, remove ContextLoadingSettings resource
#470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9d7c131
refactor: add world local plugin config
makspll 09eeba0
remove test consumer crate
makspll 292a1ac
remove bevy_tasks
makspll cb75011
remove space
makspll 5bdb5e2
Update crates/bevy_mod_scripting_core/src/config.rs
makspll 2eee454
Update crates/bevy_mod_scripting_core/src/bindings/world.rs
makspll File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| //! Init only configuration and relevant types. | ||
| use bevy_ecs::world::WorldId; | ||
|
|
||
| use crate::{ | ||
| IntoScriptPluginParams, | ||
| context::{ContextInitializer, ContextPreHandlingInitializer}, | ||
| }; | ||
|
|
||
| /// A set of global* configs keyed by the plugin params type. | ||
| /// | ||
| /// Configuration is immutable after initialization. | ||
| /// | ||
| /// Configs contained here should be | ||
| /// | ||
| /// *global meaning stored in thread-locals, i.e. not annoyingly global, but pretty global. | ||
| #[derive(Debug, Default)] | ||
| pub struct ScriptingPluginConfiguration<P: IntoScriptPluginParams + ?Sized> { | ||
| /// callbacks executed before a handler callback is executed every time | ||
| pub pre_handling_callbacks: &'static [ContextPreHandlingInitializer<P>], | ||
| /// callbacks executed once after creating a context but before executing it for the first time | ||
| pub context_initialization_callbacks: &'static [ContextInitializer<P>], | ||
| /// Whether to emit responses from the core callbacks like `on_script_loaded`. | ||
| pub emit_responses: bool, | ||
| } | ||
|
|
||
| impl<P: IntoScriptPluginParams + ?Sized> Clone for ScriptingPluginConfiguration<P> { | ||
| fn clone(&self) -> Self { | ||
| *self | ||
| } | ||
| } | ||
|
|
||
| impl<P: IntoScriptPluginParams + ?Sized> Copy for ScriptingPluginConfiguration<P> {} | ||
|
|
||
| /// A utility trait for accessing the readonly configuration for types that provide some. | ||
| /// | ||
| /// This is typically implemented using the `make_plugin_config_static!` macro. | ||
| /// | ||
| /// The default implementation will allow you to statically retrieve the configuration for a given world id. | ||
| /// | ||
| /// I.e. this config is not quite thread-local but world-local, meaning it should play nice with tests. | ||
| pub trait GetPluginThreadConfig<P: IntoScriptPluginParams + ?Sized> { | ||
| /// Get a reference to the readonly configuration. | ||
| fn readonly_configuration(world: WorldId) -> ScriptingPluginConfiguration<P>; | ||
|
|
||
| /// Set the configuration or overwrites it if already set. | ||
| fn set_thread_config(world: WorldId, config: ScriptingPluginConfiguration<P>); | ||
| } | ||
|
|
||
| #[macro_export] | ||
| /// A macro to implement `WithReadonlyConfiguration` for a given plugin type using thread-local storage. | ||
| macro_rules! make_plugin_config_static { | ||
| ($ty:ty) => { | ||
| // thread_local! { | ||
| static CONFIG: std::sync::RwLock< | ||
| bevy_platform::prelude::Vec< | ||
| // bevy_ecs::world::WorldId, | ||
| Option<ScriptingPluginConfiguration<$ty>>, | ||
| >, | ||
| > = std::sync::RwLock::new(bevy_platform::prelude::Vec::new()); | ||
| // } | ||
makspll marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| impl GetPluginThreadConfig<$ty> for $ty { | ||
| fn readonly_configuration( | ||
| world: bevy_ecs::world::WorldId, | ||
| ) -> ScriptingPluginConfiguration<$ty> { | ||
| CONFIG | ||
| .read() | ||
| .unwrap() | ||
| .get(<bevy_ecs::world::WorldId as bevy_ecs::storage::SparseSetIndex>::sparse_set_index(&world)) | ||
| .and_then(|c| *c) | ||
| .unwrap_or_else(|| | ||
| panic!( | ||
| "Configuration for plugin {} not set for world {:?}. Did you add the plugin to the app?", | ||
| stringify!($ty), | ||
| world | ||
| ), | ||
| ) | ||
| } | ||
|
|
||
| fn set_thread_config( | ||
| world: bevy_ecs::world::WorldId, | ||
| config: ScriptingPluginConfiguration<$ty>, | ||
| ) { | ||
| let mut guard = CONFIG.write().unwrap(); | ||
| let index = <bevy_ecs::world::WorldId as bevy_ecs::storage::SparseSetIndex>::sparse_set_index(&world) as usize; | ||
| if index >= guard.len() { | ||
| guard.resize_with(index + 1, || None); | ||
| } | ||
| guard[index] = Some(config); | ||
| } | ||
| } | ||
| }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.