
# Objective This is a necessary precursor to #9122 (this was split from that PR to reduce the amount of code to review all at once). Moving `!Send` resource ownership to `App` will make it unambiguously `!Send`. `SubApp` must be `Send`, so it can't wrap `App`. ## Solution Refactor `App` and `SubApp` to not have a recursive relationship. Since `SubApp` no longer wraps `App`, once `!Send` resources are moved out of `World` and into `App`, `SubApp` will become unambiguously `Send`. There could be less code duplication between `App` and `SubApp`, but that would break `App` method chaining. ## Changelog - `SubApp` no longer wraps `App`. - `App` fields are no longer publicly accessible. - `App` can no longer be converted into a `SubApp`. - Various methods now return references to a `SubApp` instead of an `App`. ## Migration Guide - To construct a sub-app, use `SubApp::new()`. `App` can no longer convert into `SubApp`. - If you implemented a trait for `App`, you may want to implement it for `SubApp` as well. - If you're accessing `app.world` directly, you now have to use `app.world()` and `app.world_mut()`. - `App::sub_app` now returns `&SubApp`. - `App::sub_app_mut` now returns `&mut SubApp`. - `App::get_sub_app` now returns `Option<&SubApp>.` - `App::get_sub_app_mut` now returns `Option<&mut SubApp>.`
64 lines
2.1 KiB
Rust
64 lines
2.1 KiB
Rust
use std::marker::PhantomData;
|
|
|
|
use bevy_app::{App, Plugin};
|
|
use bevy_ecs::prelude::*;
|
|
pub use bevy_render_macros::ExtractResource;
|
|
|
|
use crate::{Extract, ExtractSchedule, RenderApp};
|
|
|
|
/// Describes how a resource gets extracted for rendering.
|
|
///
|
|
/// Therefore the resource is transferred from the "main world" into the "render world"
|
|
/// in the [`ExtractSchedule`] step.
|
|
pub trait ExtractResource: Resource {
|
|
type Source: Resource;
|
|
|
|
/// Defines how the resource is transferred into the "render world".
|
|
fn extract_resource(source: &Self::Source) -> Self;
|
|
}
|
|
|
|
/// This plugin extracts the resources into the "render world".
|
|
///
|
|
/// Therefore it sets up the[`ExtractSchedule`] step
|
|
/// for the specified [`Resource`].
|
|
pub struct ExtractResourcePlugin<R: ExtractResource>(PhantomData<R>);
|
|
|
|
impl<R: ExtractResource> Default for ExtractResourcePlugin<R> {
|
|
fn default() -> Self {
|
|
Self(PhantomData)
|
|
}
|
|
}
|
|
|
|
impl<R: ExtractResource> Plugin for ExtractResourcePlugin<R> {
|
|
fn build(&self, app: &mut App) {
|
|
if let Some(render_app) = app.get_sub_app_mut(RenderApp) {
|
|
render_app.add_systems(ExtractSchedule, extract_resource::<R>);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// This system extracts the resource of the corresponding [`Resource`] type
|
|
pub fn extract_resource<R: ExtractResource>(
|
|
mut commands: Commands,
|
|
main_resource: Extract<Option<Res<R::Source>>>,
|
|
target_resource: Option<ResMut<R>>,
|
|
) {
|
|
if let Some(main_resource) = main_resource.as_ref() {
|
|
if let Some(mut target_resource) = target_resource {
|
|
if main_resource.is_changed() {
|
|
*target_resource = R::extract_resource(main_resource);
|
|
}
|
|
} else {
|
|
#[cfg(debug_assertions)]
|
|
if !main_resource.is_added() {
|
|
bevy_utils::warn_once!(
|
|
"Removing resource {} from render world not expected, adding using `Commands`.
|
|
This may decrease performance",
|
|
std::any::type_name::<R>()
|
|
);
|
|
}
|
|
commands.insert_resource(R::extract_resource(main_resource));
|
|
}
|
|
}
|
|
}
|