Skip to content

Commit

Permalink
fix: makes bones asset path representation more consistent. (#106)
Browse files Browse the repository at this point in the history
Previously the normalize method on a bones path would remove the leading
`/` to make it support Bevy paths, which can't start with a `/`, but
this was not consistent with the way that the handle was serialized.

Now, the bones path representations always maintain the leading `/` to
indicate a root path, and the leading `/` is removed when converting to
a Bevy handle.

This fixes issues run into when trying to read serialized bones handles
during map saving in Jumpy.
  • Loading branch information
zicklag committed Mar 1, 2023
1 parent 29fd36e commit 632ef4e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
38 changes: 23 additions & 15 deletions crates/bones_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,23 @@ pub struct AssetPath {
pub label: Option<Arc<str>>,
}

#[cfg(feature = "bevy")]
impl AssetPath {
/// Get the equivalent [`bevy_asset::AssetPath`] from this Bones [`AssetPath`].
pub fn get_bevy_asset_path(&self) -> bevy_asset::AssetPath<'static> {
bevy_asset::AssetPath::new(
// Bones convention is that `/` is relative to root, but Bevy doesn't like the
// leading /.
if let Ok(path) = self.path.strip_prefix("/") {
path.to_path_buf()
} else {
self.path.to_path_buf()
},
self.label.as_ref().map(|x| x.to_string()),
)
}
}

impl AssetPath {
/// Create a new asset path.
pub fn new<P: Into<PathBuf>>(path: P, label: Option<String>) -> Self {
Expand Down Expand Up @@ -274,7 +291,7 @@ impl AssetPath {
let base = base_path.parent().unwrap_or_else(|| Path::new(""));
base.join(&self.path)
} else {
self.path.strip_prefix("/").unwrap().to_owned()
self.path.to_path_buf()
};

self.path = Arc::from(normalize_path(&path));
Expand Down Expand Up @@ -400,7 +417,7 @@ impl serde::Serialize for UntypedHandle {
S: serde::Serializer,
{
serializer.serialize_str(&format!(
"/{}{}",
"{}{}",
self.path.path.to_str().expect("Non-unicode path"),
self.path
.label
Expand All @@ -417,7 +434,7 @@ impl<T: TypeUlid> serde::Serialize for Handle<T> {
S: serde::Serializer,
{
serializer.serialize_str(&format!(
"/{}{}",
"{}{}",
self.path.path.to_str().expect("Non-unicode path"),
self.path
.label
Expand Down Expand Up @@ -469,31 +486,22 @@ mod bevy {
impl<T: Asset + TypeUlid> super::Handle<T> {
/// Get a Bevy weak [`Handle`] from from this bones asset handle.
pub fn get_bevy_handle(&self) -> Handle<T> {
let asset_path = AssetPath::new(
self.path.path.to_path_buf(),
self.path.label.as_ref().map(|x| x.to_string()),
);
let asset_path = self.path.get_bevy_asset_path();
Handle::weak(asset_path.into())
}
}

impl<T: TypeUlid> super::Handle<T> {
/// Get a Bevy weak [`HandleUntyped`] from this bones asset handle.
pub fn get_bevy_handle_untyped(&self) -> HandleUntyped {
let asset_path = AssetPath::new(
self.path.path.to_path_buf(),
self.path.label.as_ref().map(|x| x.to_string()),
);
let asset_path = self.path.get_bevy_asset_path();
HandleUntyped::weak(asset_path.into())
}
}
impl super::UntypedHandle {
/// Get a Bevy weak [`HandleUntyped`] from this bones asset handle.
pub fn get_bevy_handle(&self) -> HandleUntyped {
let asset_path = AssetPath::new(
self.path.path.to_path_buf(),
self.path.label.as_ref().map(|x| x.to_string()),
);
let asset_path = self.path.get_bevy_asset_path();
HandleUntyped::weak(asset_path.into())
}
}
Expand Down
5 changes: 1 addition & 4 deletions crates/bones_bevy_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ impl<T: TypeUlid> BonesBevyAssetLoad for bones::Handle<T> {
self.path.normalize_relative_to(load_context.path());

// Create a bevy asset path from this bones handle
let asset_path = bevy_asset::AssetPath::new(
self.path.path.to_path_buf(),
self.path.clone().label.map(|x| x.to_string()),
);
let asset_path = self.path.get_bevy_asset_path();
let path_id = asset_path.get_id();
dependencies.push(asset_path);

Expand Down

0 comments on commit 632ef4e

Please sign in to comment.