use concinnity_core::ecs::World;
use concinnity_core::render::backend::{GpuProfile, RenderBackend};
use concinnity_host::store::paths::StateTree;
use crate::animation::AnimationSystem;
use crate::app::budget::{MemoryBudget, ThreadBudget};
use crate::app::mem_drift::MemoryDrift;
use crate::ecs::ActiveRenderBackend;
use crate::gfx::streaming::system::{StreamingPressure, StreamingState, StreamingStats};
pub fn renders(world: &World) -> bool {
world
.resource::<crate::ecs::render_mode::RenderMode>()
.is_some_and(|m| m.renders())
|| world.systems().iter().any(|s| {
s.downcast_ref::<crate::gfx::system::GraphicsSystem>()
.is_some()
})
}
pub fn streaming_stats(world: &World) -> Option<StreamingStats> {
world
.resource::<StreamingState>()
.map(|s| s.streaming_stats())
}
pub fn streaming_pressure(world: &World) -> Option<StreamingPressure> {
world.resource::<StreamingPressure>().copied()
}
pub fn memory_drift(world: &World) -> Option<MemoryDrift> {
world.resource::<MemoryDrift>().copied()
}
pub fn gpu_profile(world: &World) -> Option<GpuProfile> {
world.resource::<GpuProfile>().copied()
}
pub fn state_tree(world: &World) -> Option<&StateTree> {
world.resource::<StateTree>()
}
pub fn thread_budget(world: &World) -> Option<ThreadBudget> {
world.resource::<ThreadBudget>().copied()
}
pub fn memory_budget(world: &World) -> Option<MemoryBudget> {
world.resource::<MemoryBudget>().copied()
}
pub fn take_render_backend(world: &mut World) -> Option<Box<dyn RenderBackend>> {
world
.resource_mut::<ActiveRenderBackend>()
.and_then(|slot| slot.0.take())
}
pub fn animation_system_mut(world: &mut World) -> Option<&mut AnimationSystem> {
world
.systems_mut()
.iter_mut()
.find_map(|system| system.downcast_mut::<AnimationSystem>())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_published_run_mode_is_what_renders_reports() {
use crate::ecs::render_mode::RenderMode;
let mut world = World::new();
assert!(!renders(&world), "an unresolved world renders nothing");
world.insert_resource(RenderMode::Headless);
assert!(!renders(&world));
world.insert_resource(RenderMode::Rendered);
assert!(renders(&world));
}
#[test]
fn streaming_readouts_are_absent_before_graphics_init() {
let world = World::new();
assert!(streaming_stats(&world).is_none());
assert!(streaming_pressure(&world).is_none());
}
#[test]
fn world_accessors_without_their_systems() {
let mut world = World::new();
assert!(take_render_backend(&mut world).is_none());
assert!(animation_system_mut(&mut world).is_none());
}
}