diff --git a/Cargo.toml b/Cargo.toml index 983b27c..3af2ca1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,12 +21,12 @@ ahash = ["assets_manager/ahash"] hot-reloading = ["assets_manager/hot-reloading"] [dependencies] -assets_manager = {version = "0.7", default-features = false, features = ["parking_lot", "zip", "image"]} +assets_manager = {version = "0.8", default-features = false, features = ["parking_lot", "zip", "image"]} ggez = "0.7" directories = "3.0" -image = {version = "0.23", default-features = false} -parking_lot = "0.11" +image = {version = "0.24", default-features = false, features = ["png", "bmp", "webp"]} +parking_lot = "0.12" [dev-dependencies] env_logger = "0.9" diff --git a/src/assets.rs b/src/assets.rs index 99c989a..106c7d6 100644 --- a/src/assets.rs +++ b/src/assets.rs @@ -1,8 +1,6 @@ use assets_manager::{ asset::{NotHotReloaded, Storable}, - loader, - source::Source, - Asset, AssetCache, BoxedError, ReloadWatcher, + loader, AnyCache, Asset, BoxedError, ReloadWatcher, }; use parking_lot::Mutex; use std::{borrow::Cow, io}; @@ -13,11 +11,7 @@ fn convert_error(err: assets_manager::Error) -> ggez::GameError { Some(io_err) if io_err.kind() == io::ErrorKind::NotFound => { ggez::GameError::ResourceNotFound(err.id().to_owned(), Vec::new()) } - _ => ggez::GameError::ResourceLoadError(format!( - "Asset loading error for \"{}\" : {:?}", - err.id(), - err.reason() - )), + _ => ggez::GameError::ResourceLoadError(format!("\"{}\": {:?}", err.id(), err.reason())), } } @@ -31,8 +25,8 @@ struct GgezValue(T); impl Storable for GgezValue {} -fn default_load_fast( - cache: &AssetCache, +fn default_load_fast( + cache: AnyCache, context: &mut ggez::Context, id: &str, ) -> ggez::GameResult { @@ -45,8 +39,8 @@ fn default_load_fast( Ok(cache.get_or_insert(id, GgezValue(this)).cloned().0) } -fn default_get_cached_fast( - cache: &AssetCache, +fn default_get_cached_fast( + cache: AnyCache, _context: &mut ggez::Context, id: &str, ) -> ggez::GameResult { @@ -56,10 +50,7 @@ fn default_get_cached_fast( Ok(handle.cloned().0) } -fn default_contains_fast( - cache: &AssetCache, - id: &str, -) -> bool { +fn default_contains_fast(cache: AnyCache, id: &str) -> bool { cache.contains::>(id) } @@ -72,25 +63,17 @@ pub trait GgezAsset: Send + Sync + Sized + 'static { Self::from_repr(context, &repr) } - fn load( - cache: &AssetCache, - context: &mut ggez::Context, - id: &str, - ) -> ggez::GameResult { + fn load(cache: AnyCache, context: &mut ggez::Context, id: &str) -> ggez::GameResult { let repr = cache.load::(id).map_err(convert_error)?; Self::from_repr(context, &repr.read()) } - fn load_fast( - cache: &AssetCache, - context: &mut ggez::Context, - id: &str, - ) -> ggez::GameResult { + fn load_fast(cache: AnyCache, context: &mut ggez::Context, id: &str) -> ggez::GameResult { Self::load(cache, context, id) } - fn get_cached( - cache: &AssetCache, + fn get_cached( + cache: AnyCache, context: &mut ggez::Context, id: &str, ) -> ggez::GameResult { @@ -100,26 +83,23 @@ pub trait GgezAsset: Send + Sync + Sized + 'static { Self::from_repr(context, &repr.read()) } - fn get_cached_fast( - cache: &AssetCache, + fn get_cached_fast( + cache: AnyCache, context: &mut ggez::Context, id: &str, ) -> ggez::GameResult { Self::get_cached(cache, context, id) } - fn contains(cache: &AssetCache, id: &str) -> bool { + fn contains(cache: AnyCache, id: &str) -> bool { cache.contains::(id) } - fn contains_fast(cache: &AssetCache, id: &str) -> bool { + fn contains_fast(cache: AnyCache, id: &str) -> bool { cache.contains::(id) } - fn reload_watcher<'a, S: Source + ?Sized>( - cache: &'a AssetCache, - id: &str, - ) -> Option> { + fn reload_watcher<'a>(cache: AnyCache<'a>, id: &str) -> Option> { let repr = cache.get_cached::(id)?; Some(repr.reload_watcher()) } @@ -157,24 +137,20 @@ impl GgezAsset for ggez::graphics::Image { Self::from_rgba8(context, width, height, &image.0) } - fn load_fast( - cache: &AssetCache, - context: &mut ggez::Context, - id: &str, - ) -> ggez::GameResult { + fn load_fast(cache: AnyCache, context: &mut ggez::Context, id: &str) -> ggez::GameResult { default_load_fast(cache, context, id) } - fn get_cached_fast( - cache: &AssetCache, + fn get_cached_fast( + cache: AnyCache, context: &mut ggez::Context, id: &str, ) -> ggez::GameResult { default_get_cached_fast(cache, context, id) } - fn contains_fast(cache: &AssetCache, id: &str) -> bool { - default_contains_fast::(cache, id) + fn contains_fast(cache: AnyCache, id: &str) -> bool { + default_contains_fast::(cache, id) } } @@ -217,11 +193,7 @@ impl GgezAsset for ggez::graphics::Font { Self::new_glyph_font_bytes(context, &font.0) } - fn load( - cache: &AssetCache, - context: &mut ggez::Context, - id: &str, - ) -> ggez::GameResult { + fn load(cache: AnyCache, context: &mut ggez::Context, id: &str) -> ggez::GameResult { let handle = cache.get_cached::(id); // `ggez` already caches fonts so we avoid calling `new_glyph_font_bytes` @@ -242,11 +214,7 @@ impl GgezAsset for ggez::graphics::Font { Ok(font) } - fn load_fast( - cache: &AssetCache, - context: &mut ggez::Context, - id: &str, - ) -> ggez::GameResult { + fn load_fast(cache: AnyCache, context: &mut ggez::Context, id: &str) -> ggez::GameResult { let handle = cache.get_cached::>(id); if let Some(handle) = handle { @@ -259,8 +227,8 @@ impl GgezAsset for ggez::graphics::Font { Ok(font) } - fn get_cached( - cache: &AssetCache, + fn get_cached( + cache: AnyCache, context: &mut ggez::Context, id: &str, ) -> ggez::GameResult { @@ -279,8 +247,8 @@ impl GgezAsset for ggez::graphics::Font { Ok(font) } - fn get_cached_fast( - cache: &AssetCache, + fn get_cached_fast( + cache: AnyCache, _context: &mut ggez::Context, id: &str, ) -> ggez::GameResult { @@ -290,11 +258,11 @@ impl GgezAsset for ggez::graphics::Font { Ok(handle.copied().0) } - fn contains(cache: &AssetCache, id: &str) -> bool { + fn contains(cache: AnyCache, id: &str) -> bool { cache.contains::(id) } - fn contains_fast(cache: &AssetCache, id: &str) -> bool { + fn contains_fast(cache: AnyCache, id: &str) -> bool { cache.contains::>(id) } } @@ -324,24 +292,20 @@ impl GgezAsset for ggez::audio::SoundData { Ok(sound.0) } - fn load_fast( - cache: &AssetCache, - context: &mut ggez::Context, - id: &str, - ) -> ggez::GameResult { + fn load_fast(cache: AnyCache, context: &mut ggez::Context, id: &str) -> ggez::GameResult { default_load_fast(cache, context, id) } - fn get_cached_fast( - cache: &AssetCache, + fn get_cached_fast( + cache: AnyCache, context: &mut ggez::Context, id: &str, ) -> ggez::GameResult { default_get_cached_fast(cache, context, id) } - fn contains_fast(cache: &AssetCache, id: &str) -> bool { - default_contains_fast::(cache, id) + fn contains_fast(cache: AnyCache, id: &str) -> bool { + default_contains_fast::(cache, id) } } diff --git a/src/lib.rs b/src/lib.rs index cce1560..d46d163 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ mod assets; mod source; +use ::assets_manager::AnyCache; pub use ::assets_manager::{AssetCache, ReloadWatcher}; pub use source::GgezFileSystem; @@ -20,7 +21,8 @@ pub mod assets_manager { mod seal { pub trait Sealed {} - impl Sealed for assets_manager::AssetCache {} + impl Sealed for assets_manager::AssetCache {} + impl<'a> Sealed for assets_manager::AnyCache<'a> {} } /// Creates a new `GgezAssetCache`. @@ -76,15 +78,15 @@ pub trait AssetCacheExt: seal::Sealed { T: GgezAsset; } -impl AssetCacheExt for AssetCache { +impl AssetCacheExt for AssetCache { fn ggez_load(&self, context: &mut ggez::Context, id: &str) -> ggez::GameResult where T: GgezAsset, { if cfg!(feature = "hot-reloading") { - T::load(self, context, id) + T::load(self.as_any_cache(), context, id) } else { - T::load_fast(self, context, id) + T::load_fast(self.as_any_cache(), context, id) } } @@ -93,9 +95,9 @@ impl AssetCacheExt for AssetCache T: GgezAsset, { if cfg!(feature = "hot-reloading") { - T::get_cached(self, context, id) + T::get_cached(self.as_any_cache(), context, id) } else { - T::get_cached_fast(self, context, id) + T::get_cached_fast(self.as_any_cache(), context, id) } } @@ -104,9 +106,9 @@ impl AssetCacheExt for AssetCache T: GgezAsset, { if cfg!(feature = "hot-reloading") { - T::contains(self, id) + T::contains(self.as_any_cache(), id) } else { - T::contains_fast(self, id) + T::contains_fast(self.as_any_cache(), id) } } @@ -115,7 +117,53 @@ impl AssetCacheExt for AssetCache T: GgezAsset, { if cfg!(feature = "hot-reloading") { - T::reload_watcher(self, id) + T::reload_watcher(self.as_any_cache(), id) + } else { + self.ggez_contains::(id).then(ReloadWatcher::default) + } + } +} + +impl<'a> AssetCacheExt for AnyCache<'a> { + fn ggez_load(&self, context: &mut ggez::Context, id: &str) -> ggez::GameResult + where + T: GgezAsset, + { + if cfg!(feature = "hot-reloading") { + T::load(*self, context, id) + } else { + T::load_fast(*self, context, id) + } + } + + fn ggez_get_cached(&self, context: &mut ggez::Context, id: &str) -> ggez::GameResult + where + T: GgezAsset, + { + if cfg!(feature = "hot-reloading") { + T::get_cached(*self, context, id) + } else { + T::get_cached_fast(*self, context, id) + } + } + + fn ggez_contains(&self, id: &str) -> bool + where + T: GgezAsset, + { + if cfg!(feature = "hot-reloading") { + T::contains(*self, id) + } else { + T::contains_fast(*self, id) + } + } + + fn ggez_reload_watcher(&self, id: &str) -> Option + where + T: GgezAsset, + { + if cfg!(feature = "hot-reloading") { + T::reload_watcher(*self, id) } else { self.ggez_contains::(id).then(ReloadWatcher::default) } diff --git a/src/source.rs b/src/source.rs index 2c88f37..1577551 100644 --- a/src/source.rs +++ b/src/source.rs @@ -1,4 +1,4 @@ -use std::{borrow::Cow, fs, io, sync::Arc}; +use std::{borrow::Cow, io, sync::Arc}; use assets_manager::{ hot_reloading::{DynUpdateSender, EventSender, FsWatcherBuilder}, @@ -13,7 +13,7 @@ use assets_manager::{ #[derive(Debug, Clone)] pub struct GgezFileSystem { resources: Option, - zip: Option>>, + zip: Option>, config: Option, }