Skip to content

Commit

Permalink
Update assets_manager to 0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
a1phyr committed May 27, 2022
1 parent 3ed91c4 commit 09e1533
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 84 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
104 changes: 34 additions & 70 deletions src/assets.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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())),
}
}

Expand All @@ -31,8 +25,8 @@ struct GgezValue<T>(T);

impl<T: Send + Sync + 'static> Storable for GgezValue<T> {}

fn default_load_fast<T: GgezAsset + Clone, S: Source + ?Sized>(
cache: &AssetCache<S>,
fn default_load_fast<T: GgezAsset + Clone>(
cache: AnyCache,
context: &mut ggez::Context,
id: &str,
) -> ggez::GameResult<T> {
Expand All @@ -45,8 +39,8 @@ fn default_load_fast<T: GgezAsset + Clone, S: Source + ?Sized>(
Ok(cache.get_or_insert(id, GgezValue(this)).cloned().0)
}

fn default_get_cached_fast<T: GgezAsset + Clone, S: Source + ?Sized>(
cache: &AssetCache<S>,
fn default_get_cached_fast<T: GgezAsset + Clone>(
cache: AnyCache,
_context: &mut ggez::Context,
id: &str,
) -> ggez::GameResult<T> {
Expand All @@ -56,10 +50,7 @@ fn default_get_cached_fast<T: GgezAsset + Clone, S: Source + ?Sized>(
Ok(handle.cloned().0)
}

fn default_contains_fast<T: GgezAsset + Clone, S: Source + ?Sized>(
cache: &AssetCache<S>,
id: &str,
) -> bool {
fn default_contains_fast<T: GgezAsset + Clone>(cache: AnyCache, id: &str) -> bool {
cache.contains::<GgezValue<T>>(id)
}

Expand All @@ -72,25 +63,17 @@ pub trait GgezAsset: Send + Sync + Sized + 'static {
Self::from_repr(context, &repr)
}

fn load<S: Source + ?Sized>(
cache: &AssetCache<S>,
context: &mut ggez::Context,
id: &str,
) -> ggez::GameResult<Self> {
fn load(cache: AnyCache, context: &mut ggez::Context, id: &str) -> ggez::GameResult<Self> {
let repr = cache.load::<Self::MidRepr>(id).map_err(convert_error)?;
Self::from_repr(context, &repr.read())
}

fn load_fast<S: Source + ?Sized>(
cache: &AssetCache<S>,
context: &mut ggez::Context,
id: &str,
) -> ggez::GameResult<Self> {
fn load_fast(cache: AnyCache, context: &mut ggez::Context, id: &str) -> ggez::GameResult<Self> {
Self::load(cache, context, id)
}

fn get_cached<S: Source + ?Sized>(
cache: &AssetCache<S>,
fn get_cached(
cache: AnyCache,
context: &mut ggez::Context,
id: &str,
) -> ggez::GameResult<Self> {
Expand All @@ -100,26 +83,23 @@ pub trait GgezAsset: Send + Sync + Sized + 'static {
Self::from_repr(context, &repr.read())
}

fn get_cached_fast<S: Source + ?Sized>(
cache: &AssetCache<S>,
fn get_cached_fast(
cache: AnyCache,
context: &mut ggez::Context,
id: &str,
) -> ggez::GameResult<Self> {
Self::get_cached(cache, context, id)
}

fn contains<S: Source + ?Sized>(cache: &AssetCache<S>, id: &str) -> bool {
fn contains(cache: AnyCache, id: &str) -> bool {
cache.contains::<Self::MidRepr>(id)
}

fn contains_fast<S: Source + ?Sized>(cache: &AssetCache<S>, id: &str) -> bool {
fn contains_fast(cache: AnyCache, id: &str) -> bool {
cache.contains::<Self::MidRepr>(id)
}

fn reload_watcher<'a, S: Source + ?Sized>(
cache: &'a AssetCache<S>,
id: &str,
) -> Option<ReloadWatcher<'a>> {
fn reload_watcher<'a>(cache: AnyCache<'a>, id: &str) -> Option<ReloadWatcher<'a>> {
let repr = cache.get_cached::<Self::MidRepr>(id)?;
Some(repr.reload_watcher())
}
Expand Down Expand Up @@ -157,24 +137,20 @@ impl GgezAsset for ggez::graphics::Image {
Self::from_rgba8(context, width, height, &image.0)
}

fn load_fast<S: Source + ?Sized>(
cache: &AssetCache<S>,
context: &mut ggez::Context,
id: &str,
) -> ggez::GameResult<Self> {
fn load_fast(cache: AnyCache, context: &mut ggez::Context, id: &str) -> ggez::GameResult<Self> {
default_load_fast(cache, context, id)
}

fn get_cached_fast<S: Source + ?Sized>(
cache: &AssetCache<S>,
fn get_cached_fast(
cache: AnyCache,
context: &mut ggez::Context,
id: &str,
) -> ggez::GameResult<Self> {
default_get_cached_fast(cache, context, id)
}

fn contains_fast<S: Source + ?Sized>(cache: &AssetCache<S>, id: &str) -> bool {
default_contains_fast::<Self, S>(cache, id)
fn contains_fast(cache: AnyCache, id: &str) -> bool {
default_contains_fast::<Self>(cache, id)
}
}

Expand Down Expand Up @@ -217,11 +193,7 @@ impl GgezAsset for ggez::graphics::Font {
Self::new_glyph_font_bytes(context, &font.0)
}

fn load<S: Source + ?Sized>(
cache: &AssetCache<S>,
context: &mut ggez::Context,
id: &str,
) -> ggez::GameResult<Self> {
fn load(cache: AnyCache, context: &mut ggez::Context, id: &str) -> ggez::GameResult<Self> {
let handle = cache.get_cached::<FontAsset>(id);

// `ggez` already caches fonts so we avoid calling `new_glyph_font_bytes`
Expand All @@ -242,11 +214,7 @@ impl GgezAsset for ggez::graphics::Font {
Ok(font)
}

fn load_fast<S: Source + ?Sized>(
cache: &AssetCache<S>,
context: &mut ggez::Context,
id: &str,
) -> ggez::GameResult<Self> {
fn load_fast(cache: AnyCache, context: &mut ggez::Context, id: &str) -> ggez::GameResult<Self> {
let handle = cache.get_cached::<GgezValue<Self>>(id);

if let Some(handle) = handle {
Expand All @@ -259,8 +227,8 @@ impl GgezAsset for ggez::graphics::Font {
Ok(font)
}

fn get_cached<S: Source + ?Sized>(
cache: &AssetCache<S>,
fn get_cached(
cache: AnyCache,
context: &mut ggez::Context,
id: &str,
) -> ggez::GameResult<Self> {
Expand All @@ -279,8 +247,8 @@ impl GgezAsset for ggez::graphics::Font {
Ok(font)
}

fn get_cached_fast<S: Source + ?Sized>(
cache: &AssetCache<S>,
fn get_cached_fast(
cache: AnyCache,
_context: &mut ggez::Context,
id: &str,
) -> ggez::GameResult<Self> {
Expand All @@ -290,11 +258,11 @@ impl GgezAsset for ggez::graphics::Font {
Ok(handle.copied().0)
}

fn contains<S: Source + ?Sized>(cache: &AssetCache<S>, id: &str) -> bool {
fn contains(cache: AnyCache, id: &str) -> bool {
cache.contains::<FontId>(id)
}

fn contains_fast<S: Source + ?Sized>(cache: &AssetCache<S>, id: &str) -> bool {
fn contains_fast(cache: AnyCache, id: &str) -> bool {
cache.contains::<GgezValue<Self>>(id)
}
}
Expand Down Expand Up @@ -324,24 +292,20 @@ impl GgezAsset for ggez::audio::SoundData {
Ok(sound.0)
}

fn load_fast<S: Source + ?Sized>(
cache: &AssetCache<S>,
context: &mut ggez::Context,
id: &str,
) -> ggez::GameResult<Self> {
fn load_fast(cache: AnyCache, context: &mut ggez::Context, id: &str) -> ggez::GameResult<Self> {
default_load_fast(cache, context, id)
}

fn get_cached_fast<S: Source + ?Sized>(
cache: &AssetCache<S>,
fn get_cached_fast(
cache: AnyCache,
context: &mut ggez::Context,
id: &str,
) -> ggez::GameResult<Self> {
default_get_cached_fast(cache, context, id)
}

fn contains_fast<S: Source + ?Sized>(cache: &AssetCache<S>, id: &str) -> bool {
default_contains_fast::<Self, _>(cache, id)
fn contains_fast(cache: AnyCache, id: &str) -> bool {
default_contains_fast::<Self>(cache, id)
}
}

Expand Down
66 changes: 57 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
mod assets;
mod source;

use ::assets_manager::AnyCache;
pub use ::assets_manager::{AssetCache, ReloadWatcher};
pub use source::GgezFileSystem;

Expand All @@ -20,7 +21,8 @@ pub mod assets_manager {
mod seal {
pub trait Sealed {}

impl<S: assets_manager::source::Source + ?Sized> Sealed for assets_manager::AssetCache<S> {}
impl<S: assets_manager::source::Source> Sealed for assets_manager::AssetCache<S> {}
impl<'a> Sealed for assets_manager::AnyCache<'a> {}
}

/// Creates a new `GgezAssetCache`.
Expand Down Expand Up @@ -76,15 +78,15 @@ pub trait AssetCacheExt: seal::Sealed {
T: GgezAsset;
}

impl<S: assets_manager::source::Source + ?Sized> AssetCacheExt for AssetCache<S> {
impl<S: assets_manager::source::Source> AssetCacheExt for AssetCache<S> {
fn ggez_load<T>(&self, context: &mut ggez::Context, id: &str) -> ggez::GameResult<T>
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)
}
}

Expand All @@ -93,9 +95,9 @@ impl<S: assets_manager::source::Source + ?Sized> AssetCacheExt for AssetCache<S>
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)
}
}

Expand All @@ -104,9 +106,9 @@ impl<S: assets_manager::source::Source + ?Sized> AssetCacheExt for AssetCache<S>
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)
}
}

Expand All @@ -115,7 +117,53 @@ impl<S: assets_manager::source::Source + ?Sized> AssetCacheExt for AssetCache<S>
T: GgezAsset,
{
if cfg!(feature = "hot-reloading") {
T::reload_watcher(self, id)
T::reload_watcher(self.as_any_cache(), id)
} else {
self.ggez_contains::<T>(id).then(ReloadWatcher::default)
}
}
}

impl<'a> AssetCacheExt for AnyCache<'a> {
fn ggez_load<T>(&self, context: &mut ggez::Context, id: &str) -> ggez::GameResult<T>
where
T: GgezAsset,
{
if cfg!(feature = "hot-reloading") {
T::load(*self, context, id)
} else {
T::load_fast(*self, context, id)
}
}

fn ggez_get_cached<T>(&self, context: &mut ggez::Context, id: &str) -> ggez::GameResult<T>
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<T>(&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<T>(&self, id: &str) -> Option<ReloadWatcher>
where
T: GgezAsset,
{
if cfg!(feature = "hot-reloading") {
T::reload_watcher(*self, id)
} else {
self.ggez_contains::<T>(id).then(ReloadWatcher::default)
}
Expand Down
4 changes: 2 additions & 2 deletions src/source.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -13,7 +13,7 @@ use assets_manager::{
#[derive(Debug, Clone)]
pub struct GgezFileSystem {
resources: Option<source::FileSystem>,
zip: Option<Arc<source::Zip<fs::File>>>,
zip: Option<Arc<source::Zip>>,
config: Option<source::FileSystem>,
}

Expand Down

0 comments on commit 09e1533

Please sign in to comment.