Skip to content

Commit

Permalink
shorten code, fix logger exports
Browse files Browse the repository at this point in the history
  • Loading branch information
ezpuzz committed Jan 17, 2021
1 parent fca5929 commit cea1cf9
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 121 deletions.
238 changes: 121 additions & 117 deletions amethyst_assets/src/prefab/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,142 +211,146 @@ impl PrefabProcessingQueue {
)
}

/// Process asset data into assets
/// cook prefabs with changed dependencies
pub fn process(
&mut self,
storage: &mut AssetStorage<Prefab>,
component_registry: &ComponentRegistry,
loader: &impl Loader,
) {
{
{
// cook prefabs with changed dependencies
// FIXME: deal with cyclic and diamond dependencies correctly
let mut visited = FnvHashSet::default();
while let Some(dependee) = self.changed.pop() {
let updates: Vec<(WeakHandle, legion_prefab::CookedPrefab)> = storage
.get_for_load_handle(dependee)
.iter()
.flat_map(|p| p.dependers.iter())
.flat_map(|weak_handle| {
storage
.get_asset_with_version(weak_handle)
.into_iter()
.map(move |(prefab, _)| (weak_handle, prefab))
})
.map(|(weak_handle, prefab)| {
let cooked_prefab =
Self::cook_prefab(prefab, storage, component_registry);
if visited.insert(weak_handle.load_handle()) {
self.changed.push(weak_handle.load_handle());
}
// FIXME: Add Clone to WeakHandle
(WeakHandle::new(weak_handle.load_handle()), cooked_prefab)
})
.collect();
use crate::storage::MutateAssetInStorage;
for (handle, cooked_prefab) in updates.into_iter() {
storage.mutate_asset_in_storage(&handle, move |asset| {
asset.prefab = Some(cooked_prefab);
asset.version += 1;
});
// FIXME: deal with cyclic and diamond dependencies correctly
let mut visited = FnvHashSet::default();

while let Some(dependee) = self.changed.pop() {
let updates: Vec<(WeakHandle, legion_prefab::CookedPrefab)> = storage
.get_for_load_handle(dependee)
.iter()
.flat_map(|p| p.dependers.iter())
.flat_map(|weak_handle| {
storage
.get_asset_with_version(weak_handle)
.map(move |(prefab, _)| (weak_handle, prefab))
})
.map(|(weak_handle, prefab)| {
if visited.insert(weak_handle.load_handle()) {
self.changed.push(weak_handle.load_handle());
}
}
(
weak_handle.clone(),
Self::cook_prefab(prefab, storage, component_registry),
)
})
.collect();

use crate::storage::MutateAssetInStorage;
for (handle, cooked_prefab) in updates.into_iter() {
storage.mutate_asset_in_storage(&handle, move |asset| {
asset.prefab = Some(cooked_prefab);
asset.version += 1;
});
}
}

let requeue = self
.requeue
.get_mut()
.expect("The mutex of `requeue` in `AssetStorage` was poisoned");

let requeue = self
.requeue
.get_mut()
.expect("The mutex of `requeue` in `AssetStorage` was poisoned");
while let Some(processed) = self.processed.pop() {
let Processed {
data,
handle,
load_notifier,
version,
commit,
} = processed;
log::debug!("processing load_handle {:?}", handle);
let mut prefab = match data.map(
|Prefab {
prefab,
raw_prefab,
mut dependencies,
dependers,
version,
}| {
log::debug!("AssetUuid: {:x?}", raw_prefab.prefab_id());
let deps = dependencies.get_or_insert_with(|| {
raw_prefab
.prefab_meta
.prefab_refs
.iter()
.map(|(other_prefab_id, _)| {
loader.load_asset(AssetUuid(*other_prefab_id))
})
.collect()
});

if deps
while let Some(Processed {
data,
handle,
load_notifier,
version,
commit,
}) = self.processed.pop()
{
log::debug!("processing load_handle {:?}", handle);
let mut prefab = match data.map(
|Prefab {
prefab,
raw_prefab,
mut dependencies,
dependers,
version,
}| {
log::debug!("AssetUuid: {:x?}", raw_prefab.prefab_id());

let deps = dependencies.get_or_insert_with(|| {
raw_prefab
.prefab_meta
.prefab_refs
.iter()
.all(|handle| storage.contains(handle.load_handle()))
{
ProcessingState::Loaded(Prefab {
prefab,
raw_prefab,
dependencies,
dependers,
version,
.map(|(other_prefab_id, _)| {
loader.load_asset(AssetUuid(*other_prefab_id))
})
} else {
ProcessingState::Loading(Prefab {
prefab,
raw_prefab,
dependencies,
dependers,
version,
})
}
},
) {
Ok(ProcessingState::Loaded(raw)) => {
load_notifier.complete();
raw
}
Ok(ProcessingState::Loading(x)) => {
requeue.push(Processed {
data: Ok(x),
handle,
load_notifier,
.collect()
});

if deps
.iter()
.all(|handle| storage.contains(handle.load_handle()))
{
ProcessingState::Loaded(Prefab {
prefab,
raw_prefab,
dependencies,
dependers,
version,
commit,
});
continue;
}
Err(e) => {
load_notifier.error(e);
continue;
})
} else {
ProcessingState::Loading(Prefab {
prefab,
raw_prefab,
dependencies,
dependers,
version,
})
}
};

let cooked_prefab = Self::cook_prefab(&prefab, storage, component_registry);
let cook_version = storage
.get_for_load_handle(handle)
.map_or(1, |Prefab { version, .. }| *version + 1);
prefab.prefab = Some(cooked_prefab);
prefab.version = cook_version;
storage.update_asset(handle, prefab, version);
if commit {
storage.commit_asset(handle, version);
},
) {
Ok(ProcessingState::Loaded(raw)) => {
load_notifier.complete();
raw
}
}
Ok(ProcessingState::Loading(x)) => {
requeue.push(Processed {
data: Ok(x),
handle,
load_notifier,
version,
commit,
});
continue;
}
Err(e) => {
load_notifier.error(e);
continue;
}
};

prefab.prefab = Some(PrefabProcessingQueue::cook_prefab(
&prefab,
storage,
component_registry,
));

prefab.version = storage
.get_for_load_handle(handle)
.map_or(1, |Prefab { version, .. }| *version + 1);

for p in requeue.drain(..) {
self.processed.push(p);
storage.update_asset(handle, prefab, version);

if commit {
storage.commit_asset(handle, version);
}
}

for p in requeue.drain(..) {
self.processed.push(p);
}
}
}

#[derive(Default)]
struct PrefabAssetProcessor;

Expand Down
7 changes: 4 additions & 3 deletions amethyst_assets/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ impl<A> AssetStorage<A> {
}

pub(crate) fn update_asset(&mut self, handle: LoadHandle, asset: A, version: u32) {
if let Some(data) = self.uncommitted.remove(&handle) {
if let Some(data) = self
.uncommitted
.insert(handle, AssetState { version, asset })
{
self.to_drop.push(data.asset);
}
self.uncommitted
.insert(handle, AssetState { version, asset });
}

pub(crate) fn remove_asset(&mut self, handle: LoadHandle, version: u32) {
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub use winit;

pub use self::{
app::{Application, ApplicationBuilder, CoreApplication},
core::logger::{start_logger, LevelFilter as LogLevelFilter, Logger, LoggerConfig, StdoutLog},
error::Error,
game_data::{DataDispose, DataInit, GameData},
state::{
Expand All @@ -104,7 +105,7 @@ pub use self::{
},
state_event::{StateEvent, StateEventReader},
};
pub use crate::core::{ecs, shrev, logger};
pub use crate::core::{ecs, shrev};
#[doc(hidden)]
pub use crate::derive::*;

Expand Down

0 comments on commit cea1cf9

Please sign in to comment.