Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 52 additions & 6 deletions crates/bevy_asset/src/asset_changed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ use bevy_ecs::{
change_detection::Tick,
component::ComponentId,
prelude::{Entity, Resource, World},
query::{FilteredAccess, QueryData, QueryFilter, ReadFetch, WorldQuery},
query::{FilteredAccess, FilteredAccessSet, QueryData, QueryFilter, ReadFetch, WorldQuery},
resource::IS_RESOURCE,
storage::{Table, TableRow},
world::unsafe_world_cell::UnsafeWorldCell,
};
use bevy_platform::collections::HashMap;
use bevy_utils::prelude::DebugName;
use core::marker::PhantomData;
use disqualified::ShortName;
use tracing::error;
Expand Down Expand Up @@ -168,9 +170,8 @@ unsafe impl<A: AsAssetId> WorldQuery for AssetChanged<A> {
// SAFETY:
// - `state.resource_id` was obtained from `world.init_resource::<AssetChanges<A::Asset>>()`,
// so the untyped pointer returned by `get_resource_by_id` can safely be dereferenced into that type.
// - `update_component_access` declares a read on `state.resource_id`, so it is safe to
// read that resource here (see trait-level safety comments on `WorldQuery`, regarding
// readonly resource access in `init_fetch`)
// - `init_nested_access` declares a read on `state.resource_id`, so it is safe to
// read that resource here (see trait-level safety comments on `WorldQuery`)
let Some(changes) = (unsafe {
world
.get_resource_by_id(state.resource_id)
Expand Down Expand Up @@ -235,7 +236,38 @@ unsafe impl<A: AsAssetId> WorldQuery for AssetChanged<A> {
#[inline]
fn update_component_access(state: &Self::State, access: &mut FilteredAccess) {
<&A>::update_component_access(&state.asset_id, access);
access.add_resource_read(state.resource_id);
}

// ChangedAsset accesses both the asset and the AssetChanges<A> resource.
// In order to access two different entities we implement init_nested_access.
fn init_nested_access(
state: &Self::State,
system_name: Option<&str>,
component_access_set: &mut FilteredAccessSet,
_world: UnsafeWorldCell,
) {
let combined_access = component_access_set.combined_access();
assert!(
!combined_access.has_resource_write(state.resource_id),
Comment thread
Trashtalk217 marked this conversation as resolved.
"error[B0002]: AssetChanged<{}> in system {:?} conflicts with a previous system param. Consider removing the duplicate access. See: https://bevy.org/learn/errors/b0002",
DebugName::type_name::<A>(),
system_name,
);

let mut filter = FilteredAccess::default();
filter.add_component_read(state.resource_id);
filter.add_resource_read(state.resource_id);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need both a component read and resource read?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ensures that this correctly conflicts with FilteredResources, which only uses resource accesses, and Query<&mut MyResource>, which only uses component accesses. This is fixed in #22910, which comes right after this PR.

filter.and_with(IS_RESOURCE);

assert!(component_access_set
.get_conflicts_single(&filter)
.is_empty(),
"error[B0002]: AssetChanged<{}> in system {:?} conflicts with a previous system param. Consider removing the duplicate access. See: https://bevy.org/learn/errors/b0002",
DebugName::type_name::<A>(),
system_name,
);

component_access_set.add(filter);
}

fn init_state(world: &mut World) -> AssetChangedState<A> {
Expand Down Expand Up @@ -304,7 +336,7 @@ mod tests {
component::Component,
message::MessageWriter,
resource::Resource,
system::{Commands, IntoSystem, Local, Query, Res, ResMut},
system::{assert_is_system, Commands, IntoSystem, Local, Query, Res, ResMut},
};
use bevy_reflect::TypePath;

Expand All @@ -324,6 +356,20 @@ mod tests {
}
}

#[test]
#[should_panic]
fn should_conflict() {
#[derive(Component)]
struct Foo;

fn system(
_: Query<&Foo, AssetChanged<MyComponent>>,
_: Query<&mut AssetChanges<MyAsset>, bevy_ecs::query::Without<Foo>>,
) {
}
assert_is_system(system);
}

fn run_app<Marker>(system: impl IntoSystem<(), (), Marker>) {
let mut app = create_app().0;
app.init_asset::<MyAsset>().add_systems(Update, system);
Expand Down