Skip to content

Commit

Permalink
update plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
maniwani committed Sep 22, 2023
1 parent 9c1ac43 commit d760588
Show file tree
Hide file tree
Showing 61 changed files with 394 additions and 342 deletions.
80 changes: 42 additions & 38 deletions crates/bevy_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl Plugin for AssetPlugin {
watch_for_changes,
} => {
let source_reader = app
.world
.world_mut()
.resource_mut::<AssetProviders>()
.get_source_reader(source);
app.insert_resource(AssetServer::new(source_reader, *watch_for_changes));
Expand All @@ -156,7 +156,7 @@ impl Plugin for AssetPlugin {
watch_for_changes,
} => {
let destination_reader = app
.world
.world_mut()
.resource_mut::<AssetProviders>()
.get_destination_reader(destination);
app.insert_resource(AssetServer::new(destination_reader, *watch_for_changes));
Expand All @@ -166,7 +166,7 @@ impl Plugin for AssetPlugin {
destination,
watch_for_changes,
} => {
let mut asset_providers = app.world.resource_mut::<AssetProviders>();
let mut asset_providers = app.world_mut().resource_mut::<AssetProviders>();
let processor = AssetProcessor::new(&mut asset_providers, source, destination);
let destination_reader = asset_providers.get_destination_reader(source);
// the main asset server gates loads based on asset state
Expand All @@ -191,7 +191,7 @@ impl Plugin for AssetPlugin {
)
.add_systems(UpdateAssets, server::handle_internal_asset_events);

let mut order = app.world.resource_mut::<MainScheduleOrder>();
let mut order = app.world_mut().resource_mut::<MainScheduleOrder>();
order.insert_after(First, UpdateAssets);
order.insert_after(PostUpdate, AssetEvents);
}
Expand Down Expand Up @@ -276,20 +276,24 @@ pub trait AssetApp {

impl AssetApp for App {
fn register_asset_loader<L: AssetLoader>(&mut self, loader: L) -> &mut Self {
self.world.resource::<AssetServer>().register_loader(loader);
self.world()
.resource::<AssetServer>()
.register_loader(loader);
self
}

fn init_asset_loader<L: AssetLoader + FromWorld>(&mut self) -> &mut Self {
let loader = L::from_world(&mut self.world);
let loader = L::from_world(self.world_mut());
self.register_asset_loader(loader)
}

fn init_asset<A: Asset>(&mut self) -> &mut Self {
let assets = Assets::<A>::default();
self.world.resource::<AssetServer>().register_asset(&assets);
if self.world.contains_resource::<AssetProcessor>() {
let processor = self.world.resource::<AssetProcessor>();
self.world()
.resource::<AssetServer>()
.register_asset(&assets);
if self.world().contains_resource::<AssetProcessor>() {
let processor = self.world().resource::<AssetProcessor>();
// The processor should have its own handle provider separate from the Asset storage
// to ensure the id spaces are entirely separate. Not _strictly_ necessary, but
// desirable.
Expand All @@ -312,7 +316,7 @@ impl AssetApp for App {
where
A: Asset + Reflect + FromReflect + GetTypeRegistration,
{
let type_registry = self.world.resource::<AppTypeRegistry>();
let type_registry = self.world().resource::<AppTypeRegistry>();
{
let mut type_registry = type_registry.write();

Expand All @@ -326,21 +330,21 @@ impl AssetApp for App {
}

fn preregister_asset_loader<L: AssetLoader>(&mut self, extensions: &[&str]) -> &mut Self {
self.world
self.world_mut()
.resource_mut::<AssetServer>()
.preregister_loader::<L>(extensions);
self
}

fn register_asset_processor<P: Process>(&mut self, processor: P) -> &mut Self {
if let Some(asset_processor) = self.world.get_resource::<AssetProcessor>() {
if let Some(asset_processor) = self.world().get_resource::<AssetProcessor>() {
asset_processor.register_processor(processor);
}
self
}

fn set_default_asset_processor<P: Process>(&mut self, extension: &str) -> &mut Self {
if let Some(asset_processor) = self.world.get_resource::<AssetProcessor>() {
if let Some(asset_processor) = self.world().get_resource::<AssetProcessor>() {
asset_processor.set_default_processor::<P>(extension);
}
self
Expand All @@ -365,7 +369,7 @@ pub struct AssetEvents;
#[macro_export]
macro_rules! load_internal_asset {
($app: ident, $handle: expr, $path_str: expr, $loader: expr) => {{
let mut assets = $app.world.resource_mut::<$crate::Assets<_>>();
let mut assets = $app.world_mut().resource_mut::<$crate::Assets<_>>();
assets.insert($handle, ($loader)(
include_str!($path_str),
std::path::Path::new(file!())
Expand All @@ -377,7 +381,7 @@ macro_rules! load_internal_asset {
}};
// we can't support params without variadic arguments, so internal assets with additional params can't be hot-reloaded
($app: ident, $handle: ident, $path_str: expr, $loader: expr $(, $param:expr)+) => {{
let mut assets = $app.world.resource_mut::<$crate::Assets<_>>();
let mut assets = $app.world_mut().resource_mut::<$crate::Assets<_>>();
assets.insert($handle, ($loader)(
include_str!($path_str),
std::path::Path::new(file!())
Expand All @@ -394,7 +398,7 @@ macro_rules! load_internal_asset {
#[macro_export]
macro_rules! load_internal_binary_asset {
($app: ident, $handle: expr, $path_str: expr, $loader: expr) => {{
let mut assets = $app.world.resource_mut::<$crate::Assets<_>>();
let mut assets = $app.world_mut().resource_mut::<$crate::Assets<_>>();
assets.insert(
$handle,
($loader)(
Expand Down Expand Up @@ -526,7 +530,7 @@ mod tests {
fn run_app_until(app: &mut App, mut predicate: impl FnMut(&mut World) -> Option<()>) {
for _ in 0..LARGE_ITERATION_COUNT {
app.update();
if (predicate)(&mut app.world).is_some() {
if (predicate)(app.world_mut()).is_some() {
return;
}
}
Expand Down Expand Up @@ -612,13 +616,13 @@ mod tests {
.init_resource::<StoredEvents>()
.register_asset_loader(CoolTextLoader)
.add_systems(Update, store_asset_events);
let asset_server = app.world.resource::<AssetServer>().clone();
let asset_server = app.world().resource::<AssetServer>().clone();
let handle: Handle<CoolText> = asset_server.load(a_path);
let a_id = handle.id();
let entity = app.world.spawn(handle).id();
let entity = app.world_mut().spawn(handle).id();
app.update();
{
let a_text = get::<CoolText>(&app.world, a_id);
let a_text = get::<CoolText>(app.world(), a_id);
let (a_load, a_deps, a_rec_deps) = asset_server.get_load_states(a_id).unwrap();
assert!(a_text.is_none(), "a's asset should not exist yet");
assert_eq!(a_load, LoadState::Loading, "a should still be loading");
Expand Down Expand Up @@ -800,27 +804,27 @@ mod tests {
});

{
let mut texts = app.world.resource_mut::<Assets<CoolText>>();
let mut texts = app.world_mut().resource_mut::<Assets<CoolText>>();
let a = texts.get_mut(a_id).unwrap();
a.text = "Changed".to_string();
}

app.world.despawn(entity);
app.world_mut().despawn(entity);
app.update();
assert_eq!(
app.world.resource::<Assets<CoolText>>().len(),
app.world().resource::<Assets<CoolText>>().len(),
0,
"CoolText asset entities should be despawned when no more handles exist"
);
app.update();
// this requires a second update because the parent asset was freed in the previous app.update()
assert_eq!(
app.world.resource::<Assets<SubText>>().len(),
app.world().resource::<Assets<SubText>>().len(),
0,
"SubText asset entities should be despawned when no more handles exist"
);
let events = app.world.remove_resource::<StoredEvents>().unwrap();
let id_results = app.world.remove_resource::<IdResults>().unwrap();
let events = app.world_mut().remove_resource::<StoredEvents>().unwrap();
let id_results = app.world_mut().remove_resource::<IdResults>().unwrap();
let expected_events = vec![
AssetEvent::Added { id: a_id },
AssetEvent::LoadedWithDependencies {
Expand Down Expand Up @@ -910,7 +914,7 @@ mod tests {
let (mut app, gate_opener) = test_app(dir);
app.init_asset::<CoolText>()
.register_asset_loader(CoolTextLoader);
let asset_server = app.world.resource::<AssetServer>().clone();
let asset_server = app.world().resource::<AssetServer>().clone();
let handle: Handle<CoolText> = asset_server.load(a_path);
let a_id = handle.id();
{
Expand All @@ -926,7 +930,7 @@ mod tests {
);
}

app.world.spawn(handle);
app.world_mut().spawn(handle);
gate_opener.open(a_path);
gate_opener.open(b_path);
gate_opener.open(c_path);
Expand Down Expand Up @@ -1003,7 +1007,7 @@ mod tests {

let id = {
let handle = {
let mut texts = app.world.resource_mut::<Assets<CoolText>>();
let mut texts = app.world_mut().resource_mut::<Assets<CoolText>>();
texts.add(CoolText {
text: hello.clone(),
embedded: empty.clone(),
Expand All @@ -1016,7 +1020,7 @@ mod tests {

{
let text = app
.world
.world()
.resource::<Assets<CoolText>>()
.get(&handle)
.unwrap();
Expand All @@ -1027,36 +1031,36 @@ mod tests {
// handle is dropped
app.update();
assert!(
app.world.resource::<Assets<CoolText>>().get(id).is_none(),
app.world().resource::<Assets<CoolText>>().get(id).is_none(),
"asset has no handles, so it should have been dropped last update"
);
// remove event is emitted
app.update();
let events = std::mem::take(&mut app.world.resource_mut::<StoredEvents>().0);
let events = std::mem::take(&mut app.world_mut().resource_mut::<StoredEvents>().0);
let expected_events = vec![AssetEvent::Added { id }, AssetEvent::Removed { id }];
assert_eq!(events, expected_events);

let dep_handle = app.world.resource::<AssetServer>().load(dep_path);
let dep_handle = app.world().resource::<AssetServer>().load(dep_path);
let a = CoolText {
text: "a".to_string(),
embedded: empty,
// this dependency is behind a manual load gate, which should prevent 'a' from emitting a LoadedWithDependencies event
dependencies: vec![dep_handle.clone()],
sub_texts: Vec::new(),
};
let a_handle = app.world.resource::<AssetServer>().load_asset(a);
let a_handle = app.world().resource::<AssetServer>().load_asset(a);
app.update();
// TODO: ideally it doesn't take two updates for the added event to emit
app.update();

let events = std::mem::take(&mut app.world.resource_mut::<StoredEvents>().0);
let events = std::mem::take(&mut app.world_mut().resource_mut::<StoredEvents>().0);
let expected_events = vec![AssetEvent::Added { id: a_handle.id() }];
assert_eq!(events, expected_events);

gate_opener.open(dep_path);
loop {
app.update();
let events = std::mem::take(&mut app.world.resource_mut::<StoredEvents>().0);
let events = std::mem::take(&mut app.world_mut().resource_mut::<StoredEvents>().0);
if events.is_empty() {
continue;
}
Expand All @@ -1070,7 +1074,7 @@ mod tests {
break;
}
app.update();
let events = std::mem::take(&mut app.world.resource_mut::<StoredEvents>().0);
let events = std::mem::take(&mut app.world_mut().resource_mut::<StoredEvents>().0);
let expected_events = vec![AssetEvent::Added {
id: dep_handle.id(),
}];
Expand Down Expand Up @@ -1117,7 +1121,7 @@ mod tests {
app.init_asset::<CoolText>()
.init_asset::<SubText>()
.register_asset_loader(CoolTextLoader);
let asset_server = app.world.resource::<AssetServer>().clone();
let asset_server = app.world().resource::<AssetServer>().clone();
let handle: Handle<LoadedFolder> = asset_server.load_folder("text");
gate_opener.open(a_path);
gate_opener.open(b_path);
Expand Down
16 changes: 8 additions & 8 deletions crates/bevy_asset/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ mod tests {
.register_asset_reflect::<AssetType>();

let reflect_asset = {
let type_registry = app.world.resource::<AppTypeRegistry>();
let type_registry = app.world().resource::<AppTypeRegistry>();
let type_registry = type_registry.read();

type_registry
Expand All @@ -267,9 +267,9 @@ mod tests {
field: "test".into(),
};

let handle = reflect_asset.add(&mut app.world, &value);
let handle = reflect_asset.add(app.world_mut(), &value);
let strukt = match reflect_asset
.get_mut(&mut app.world, handle)
.get_mut(app.world_mut(), handle)
.unwrap()
.reflect_mut()
{
Expand All @@ -281,19 +281,19 @@ mod tests {
.unwrap()
.apply(&String::from("edited"));

assert_eq!(reflect_asset.len(&app.world), 1);
let ids: Vec<_> = reflect_asset.ids(&app.world).collect();
assert_eq!(reflect_asset.len(app.world()), 1);
let ids: Vec<_> = reflect_asset.ids(app.world()).collect();
assert_eq!(ids.len(), 1);

let fetched_handle = UntypedHandle::Weak(ids[0]);
let asset = reflect_asset
.get(&app.world, fetched_handle.clone_weak())
.get(app.world(), fetched_handle.clone_weak())
.unwrap();
assert_eq!(asset.downcast_ref::<AssetType>().unwrap().field, "edited");

reflect_asset
.remove(&mut app.world, fetched_handle)
.remove(app.world_mut(), fetched_handle)
.unwrap();
assert_eq!(reflect_asset.len(&app.world), 0);
assert_eq!(reflect_asset.len(app.world()), 0);
}
}
3 changes: 1 addition & 2 deletions crates/bevy_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use std::marker::PhantomData;
use std::ops::Range;
use std::path::{Path, PathBuf};

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(target_arch = "wasm32"))]
use bevy_tasks::tick_global_task_pools_on_main_thread;

Expand Down Expand Up @@ -206,7 +205,7 @@ mod tests {
));
app.update();

let frame_count = app.world.resource::<FrameCount>();
let frame_count = app.world().resource::<FrameCount>();
assert_eq!(1, frame_count.0);
}
}
3 changes: 1 addition & 2 deletions crates/bevy_core_pipeline/src/blit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ impl Plugin for BlitPlugin {
}

fn finish(&self, app: &mut App) {
let Ok(render_app) = app.get_sub_app_mut(RenderApp) else {
let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
return;
};

render_app
.init_resource::<BlitPipeline>()
.init_resource::<SpecializedRenderPipelines<BlitPipeline>>();
Expand Down
12 changes: 4 additions & 8 deletions crates/bevy_core_pipeline/src/bloom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ impl Plugin for BloomPlugin {
UniformComponentPlugin::<BloomUniforms>::default(),
));

let render_app = match app.get_sub_app_mut(RenderApp) {
Ok(render_app) => render_app,
Err(_) => return,
let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
return;
};

render_app
.init_resource::<SpecializedRenderPipelines<BloomDownsamplingPipeline>>()
.init_resource::<SpecializedRenderPipelines<BloomUpsamplingPipeline>>()
Expand Down Expand Up @@ -101,11 +99,9 @@ impl Plugin for BloomPlugin {
}

fn finish(&self, app: &mut App) {
let render_app = match app.get_sub_app_mut(RenderApp) {
Ok(render_app) => render_app,
Err(_) => return,
let Some(render_app) = app.get_sub_app_mut(RenderApp) else {
return;
};

render_app
.init_resource::<BloomDownsamplingPipeline>()
.init_resource::<BloomUpsamplingPipeline>();
Expand Down
Loading

0 comments on commit d760588

Please sign in to comment.