use super::AssetCache;
use indicatrix::renderer::env_map::EnvironmentMap;
use indicatrix_net::{messages::ContentHash, scene::HdrEnvironment};
use std::sync::{Arc, Mutex, PoisonError};
pub struct HeldAsset {
hdr: HdrEnvironment,
cache: Arc<AssetCache>,
bytes: Mutex<Option<Arc<Vec<u8>>>>,
map: Option<Arc<EnvironmentMap>>,
}
impl HeldAsset {
#[must_use]
pub const fn new(
hdr: HdrEnvironment,
cache: Arc<AssetCache>,
bytes: Option<Arc<Vec<u8>>>,
map: Option<Arc<EnvironmentMap>>,
) -> Self {
Self {
hdr,
cache,
bytes: Mutex::new(bytes),
map,
}
}
#[must_use]
pub const fn hdr(&self) -> &HdrEnvironment {
&self.hdr
}
#[must_use]
pub const fn content_hash(&self) -> ContentHash {
self.hdr.content_hash
}
#[must_use]
pub const fn map(&self) -> Option<&Arc<EnvironmentMap>> {
self.map.as_ref()
}
#[must_use]
pub fn bytes(&self) -> Option<Arc<Vec<u8>>> {
let mut held = self.bytes.lock().unwrap_or_else(PoisonError::into_inner);
if held.is_none() {
*held = self.cache.get(&self.hdr.content_hash).map(Arc::new);
}
let bytes = held.clone();
drop(held);
bytes
}
}