Skip to content

Commit

Permalink
Add read_process_bytes function
Browse files Browse the repository at this point in the history
  • Loading branch information
BeastLe9enD committed Jul 20, 2024
1 parent d708036 commit df4fb34
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion crates/bevy_asset/src/processor/process.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::io::SliceReader;
use crate::meta::get_asset_hash;
use crate::{
io::{
AssetReaderError, AssetWriterError, MissingAssetWriterError,
Expand All @@ -9,7 +10,7 @@ use crate::{
saver::{AssetSaver, SavedAsset},
transformer::{AssetTransformer, TransformedAsset},
AssetLoadError, AssetLoader, AssetPath, DeserializeMetaError, ErasedLoadedAsset,
MissingAssetLoaderForExtensionError, MissingAssetLoaderForTypeNameError,
MissingAssetLoaderForExtensionError, MissingAssetLoaderForTypeNameError, ReadAssetBytesError,
};
use bevy_utils::{BoxedFuture, ConditionalSendFuture};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -368,6 +369,42 @@ impl<'a> ProcessContext<'a> {
Ok(loaded_asset)
}

/// Reads the asset at the given path and returns its bytes
pub async fn read_asset_bytes<'b, 'c>(
&'b mut self,
path: impl Into<AssetPath<'c>>,
) -> Result<Vec<u8>, ReadAssetBytesError> {
let path = path.into();
let server = &self.processor.server;
let source = server.get_source(path.source())?;
let asset_reader = source.reader();

let mut reader = asset_reader.read(path.path()).await?;
let full_hash = {
// NOTE: ensure meta is read while the asset bytes reader is still active to ensure transactionality
// See `ProcessorGatedReader` for more info
let meta_bytes = asset_reader.read_meta_bytes(path.path()).await?;
get_asset_hash(&meta_bytes, self.asset_bytes)
};

let mut bytes = Vec::new();
reader
.read_to_end(&mut bytes)
.await
.map_err(|source| ReadAssetBytesError::Io {
path: path.path().to_path_buf(),
source,
})?;

self.new_processed_info
.process_dependencies
.push(ProcessDependencyInfo {
full_hash,
path: path.clone_owned(),
});
Ok(bytes)
}

/// The path of the asset being processed.
#[inline]
pub fn path(&self) -> &AssetPath<'static> {
Expand Down

0 comments on commit df4fb34

Please sign in to comment.