-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Handle Serialization and Deserialization #2306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1ac0833
3e09b8c
2d8f849
2c4ce2e
e71c300
bc96a93
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| use std::cell::Cell; | ||
|
|
||
| use bevy_reflect::Uuid; | ||
| use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
|
|
||
| use crate::{Asset, AssetServer, Handle, HandleId, HandleUntyped}; | ||
|
|
||
| /////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| thread_local! { | ||
| static ASSET_SERVER: Cell<Option<AssetServer>> = Cell::new(None); | ||
lassade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| #[derive(Serialize, Deserialize)] | ||
| enum AssetRef { | ||
| Default, | ||
| /// Used for static handles like the `PBR_PIPELINE_HANDLE` or a embedded assets | ||
| Local(Uuid, u64), | ||
| /// Loads from a file | ||
| External(String), | ||
| } | ||
|
|
||
| impl<T: Asset> Serialize for Handle<T> { | ||
lassade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
| where | ||
| S: Serializer, | ||
| { | ||
| let path = ASSET_SERVER | ||
| .with(|cell| { | ||
| let server = cell.replace(None); | ||
| let path = server.as_ref().and_then(|server| { | ||
| // TODO: `get_handle_path` does absolutely nothing issue #1290 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has been resolved. |
||
| server.get_handle_path(self).map(|asset_path| { | ||
| let mut path = asset_path.path().to_string_lossy().to_string(); | ||
| if let Some(label) = asset_path.label() { | ||
| path.push('#'); | ||
| path.push_str(label); | ||
| } | ||
| AssetRef::External(path) | ||
| }) | ||
| }); | ||
| cell.replace(server); | ||
| path | ||
| }) | ||
| .unwrap_or_else(|| match &self.id { | ||
| HandleId::Id(type_uuid, id) => AssetRef::Local(*type_uuid, *id), | ||
| HandleId::AssetPathId(_) => AssetRef::Default, | ||
| }); | ||
|
|
||
| path.serialize(serializer) | ||
| } | ||
| } | ||
|
|
||
| impl<'de, T: Asset> Deserialize<'de> for Handle<T> { | ||
| fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
| where | ||
| D: Deserializer<'de>, | ||
| { | ||
| match AssetRef::deserialize(deserializer)? { | ||
| AssetRef::Default => Ok(Handle::default()), | ||
| AssetRef::Local(type_uuid, id) => { | ||
| Ok(HandleUntyped::weak_from_u64(type_uuid, id).typed()) | ||
| } | ||
| AssetRef::External(path) => ASSET_SERVER.with(|cell| { | ||
| let server = cell.replace(None); | ||
| let handle = server | ||
| .as_ref() | ||
| .map(|server| server.load(path.as_str())) | ||
| .unwrap_or_default(); | ||
| cell.replace(server); | ||
| Ok(handle) | ||
| }), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl AssetServer { | ||
| /// Enables asset references to be serialized or deserialized | ||
| pub fn with_asset_refs_serialization<F, T>(&self, f: F) -> T | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you give
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something like |
||
| where | ||
| F: FnOnce() -> T, | ||
| { | ||
| ASSET_SERVER.with(|key| { | ||
| key.replace(Some(self.clone())); | ||
| let result = (f)(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you have an example use case of how this API is better than the previous one?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the last API didn't allow to
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, that makes sense. |
||
| key.replace(None); | ||
| result | ||
| }) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.