Fix is_plugin_added::<Self>() being true during build (#13817)
# Objective Fixes #13815 ## Solution Move insertion of the plugin name to after build is called. ## Testing I added a regression test --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com> Co-authored-by: François Mockers <mockersf@gmail.com> Co-authored-by: François Mockers <francois.mockers@vleue.com>
This commit is contained in:
parent
01971f210e
commit
1395e3672c
@ -439,12 +439,7 @@ impl App {
|
|||||||
plugin: Box<dyn Plugin>,
|
plugin: Box<dyn Plugin>,
|
||||||
) -> Result<&mut Self, AppError> {
|
) -> Result<&mut Self, AppError> {
|
||||||
debug!("added plugin: {}", plugin.name());
|
debug!("added plugin: {}", plugin.name());
|
||||||
if plugin.is_unique()
|
if plugin.is_unique() && self.main_mut().plugin_names.contains(plugin.name()) {
|
||||||
&& !self
|
|
||||||
.main_mut()
|
|
||||||
.plugin_names
|
|
||||||
.insert(plugin.name().to_string())
|
|
||||||
{
|
|
||||||
Err(AppError::DuplicatePlugin {
|
Err(AppError::DuplicatePlugin {
|
||||||
plugin_name: plugin.name().to_string(),
|
plugin_name: plugin.name().to_string(),
|
||||||
})?;
|
})?;
|
||||||
@ -459,6 +454,9 @@ impl App {
|
|||||||
|
|
||||||
self.main_mut().plugin_build_depth += 1;
|
self.main_mut().plugin_build_depth += 1;
|
||||||
let result = catch_unwind(AssertUnwindSafe(|| plugin.build(self)));
|
let result = catch_unwind(AssertUnwindSafe(|| plugin.build(self)));
|
||||||
|
self.main_mut()
|
||||||
|
.plugin_names
|
||||||
|
.insert(plugin.name().to_string());
|
||||||
self.main_mut().plugin_build_depth -= 1;
|
self.main_mut().plugin_build_depth -= 1;
|
||||||
|
|
||||||
if let Err(payload) = result {
|
if let Err(payload) = result {
|
||||||
@ -1275,6 +1273,21 @@ mod tests {
|
|||||||
.init_resource::<TestResource>();
|
.init_resource::<TestResource>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
/// Plugin should not be considered inserted while it's being built
|
||||||
|
///
|
||||||
|
/// bug: <https://github.com/bevyengine/bevy/issues/13815>
|
||||||
|
fn plugin_should_not_be_added_during_build_time() {
|
||||||
|
pub struct Foo;
|
||||||
|
|
||||||
|
impl Plugin for Foo {
|
||||||
|
fn build(&self, app: &mut App) {
|
||||||
|
assert!(!app.is_plugin_added::<Self>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
App::new().add_plugins(Foo);
|
||||||
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn events_should_be_updated_once_per_update() {
|
fn events_should_be_updated_once_per_update() {
|
||||||
#[derive(Event, Clone)]
|
#[derive(Event, Clone)]
|
||||||
|
Loading…
Reference in New Issue
Block a user