use std::collections::HashMap;
use crate::{
app::SystemStage,
assets::{
deps::Dependencies,
storage::{Assets, ProcessedAssets, RawAssetHandle},
upload::Asset,
},
ecs::{
plugin::Plugin,
resources::Resources,
system::{Local, Res, ResMut},
},
};
const STUCK_AFTER_TICKS: u32 = 300;
fn should_warn_stuck(ticks: u32) -> bool {
ticks >= STUCK_AFTER_TICKS && ticks.is_multiple_of(STUCK_AFTER_TICKS)
}
pub struct AssetPlugin<B, T: Asset<B>> {
_marker: std::marker::PhantomData<(B, T)>,
}
impl<B, T: Asset<B>> AssetPlugin<B, T> {
pub fn new() -> Self {
Self {
_marker: std::marker::PhantomData,
}
}
}
impl<B, T> Plugin for AssetPlugin<B, T>
where
B: 'static + Send + Sync,
T: Asset<B>,
{
fn build(&self, app: &mut crate::app::App) {
app.try_insert_resource(Assets::<T::Source>::new());
app.try_insert_resource(ProcessedAssets::<T>::new());
app.add_system(SystemStage::AssetSync, sync_assets::<B, T>);
app.provides::<ProcessedAssets<T>>();
}
}
fn sync_assets<B, T>(
mut cpu: ResMut<Assets<T::Source>>,
mut processed: ResMut<ProcessedAssets<T>>,
backend: Option<Res<B>>,
mut blocked_ticks: Local<u32>,
mut pending_ticks: Local<HashMap<RawAssetHandle, u32>>,
world: &hecs::World,
resources: &Resources,
) where
B: 'static + Send + Sync,
T: Asset<B>,
{
let Some(backend) = backend else {
log_waiting::<B, T>(&cpu, "backend", &mut blocked_ticks);
return;
};
let Some(deps) = T::Deps::try_gather(world, resources) else {
log_waiting::<B, T>(&cpu, "dependencies", &mut blocked_ticks);
return;
};
*blocked_ticks = 0;
for handle in cpu.take_removed() {
processed.remove(handle);
pending_ticks.remove(&handle);
}
let mut still_pending = Vec::new();
for handle in cpu.take_dirty() {
let Some(source) = cpu.get_quiet(handle) else {
tracing::debug!(
"{}: handle {:?} was in the dirty queue but the source asset is already gone \
(inserted and removed in the same tick?)",
std::any::type_name::<T>(),
handle
);
pending_ticks.remove(&handle);
continue;
};
match T::upload(source, &backend, &deps) {
Some(value) => {
if let Some(name) = cpu.name_for_handle(handle) {
processed.names.insert(name.to_string(), handle);
}
tracing::debug!(
"{}: uploaded {:?}{}",
std::any::type_name::<T>(),
handle,
cpu.name_for_handle(handle)
.map(|n| format!(" ({n})"))
.unwrap_or_default()
);
processed.insert(handle, value);
pending_ticks.remove(&handle);
}
None => {
let ticks = pending_ticks.entry(handle).or_insert(0);
*ticks += 1;
if should_warn_stuck(*ticks) {
tracing::warn!(
"{}: {:?}{} has not uploaded after {} ticks — upload() may be \
unconditionally returning None, or a Deps resource it needs is never \
actually going to appear. Still retrying every tick.",
std::any::type_name::<T>(),
handle,
cpu.name_for_handle(handle)
.map(|n| format!(" ({n})"))
.unwrap_or_default(),
*ticks
);
} else {
tracing::debug!(
"{}: {:?} upload returned None — a required dependency is not yet ready, \
requeued for next tick",
std::any::type_name::<T>(),
handle
);
}
still_pending.push(handle);
}
}
}
if !still_pending.is_empty() {
tracing::debug!(
"{}: {} handle(s) still pending upload (waiting on dependencies)",
std::any::type_name::<T>(),
still_pending.len()
);
}
cpu.requeue(still_pending);
}
fn log_waiting<D, T>(cpu: &Assets<T::Source>, what: &str, blocked_ticks: &mut u32)
where
D: 'static + Send + Sync,
T: Asset<D>,
{
if cpu.dirty_is_empty() {
*blocked_ticks = 0;
return;
}
*blocked_ticks += 1;
if should_warn_stuck(*blocked_ticks) {
tracing::warn!(
"{}: {} asset(s) have been queued for {} ticks, still waiting on {what} before \
upload can begin — if {what} is never going to appear, this pipeline will wait \
forever.",
std::any::type_name::<T>(),
cpu.dirty_len(),
*blocked_ticks,
);
} else {
tracing::debug!(
"{}: {} asset(s) queued but waiting on {what} before upload can begin",
std::any::type_name::<T>(),
cpu.dirty_len()
);
}
}