Skip to content

Commit

Permalink
add path transform
Browse files Browse the repository at this point in the history
  • Loading branch information
MScottMcBee committed Aug 19, 2024
1 parent bd8faa7 commit 7bd1cfe
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
11 changes: 8 additions & 3 deletions crates/bevy_asset/src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,10 @@ impl AssetProcessor {
// TODO: this class of failure can be recovered via re-processing + smarter log validation that allows for duplicate transactions in the event of failures
self.log_begin_processing(asset_path).await;
if let Some(processor) = processor {
let mut writer = processed_writer.write(path).await.map_err(writer_err)?;
let mut writer = processed_writer
.write(processor.get_path_transformation(path).as_path())
.await
.map_err(writer_err)?;
let mut processed_meta = {
let mut context =
ProcessContext::new(self, asset_path, &asset_bytes, &mut new_processed_info);
Expand Down Expand Up @@ -858,7 +861,10 @@ impl AssetProcessor {
*processed_meta.processed_info_mut() = Some(new_processed_info.clone());
let meta_bytes = processed_meta.serialize();
processed_writer
.write_meta_bytes(path, &meta_bytes)
.write_meta_bytes(
processor.get_path_transformation(path).as_path(),
&meta_bytes,
)
.await
.map_err(writer_err)?;
} else {
Expand Down Expand Up @@ -1102,7 +1108,6 @@ pub(crate) struct ProcessorAssetInfo {
/// * when the processor is running in parallel with an app
/// * when processing assets in parallel, the processor might read an asset's `process_dependencies` when processing new versions of those dependencies
/// * this second scenario almost certainly isn't possible with the current implementation, but its worth protecting against
///
/// This lock defends against those scenarios by ensuring readers don't read while processed files are being written. And it ensures
/// Because this lock is shared across meta and asset bytes, readers can ensure they don't read "old" versions of metadata with "new" asset data.
pub(crate) file_transaction_lock: Arc<async_lock::RwLock<()>>,
Expand Down
20 changes: 20 additions & 0 deletions 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::saver;
use crate::{
io::{
AssetReaderError, AssetWriterError, MissingAssetWriterError,
Expand All @@ -14,6 +15,7 @@ use crate::{
use bevy_utils::{BoxedFuture, ConditionalSendFuture};
use serde::{Deserialize, Serialize};
use std::marker::PhantomData;
use std::path::{Path, PathBuf};
use thiserror::Error;

/// Asset "processor" logic that reads input asset bytes (stored on [`ProcessContext`]), processes the value in some way,
Expand All @@ -36,6 +38,10 @@ pub trait Process: Send + Sync + Sized + 'static {
) -> impl ConditionalSendFuture<
Output = Result<<Self::OutputLoader as AssetLoader>::Settings, ProcessError>,
>;

fn get_path_transformation(path: &Path) -> Box<PathBuf> {
Box::new(path.to_owned())
}
}

/// A flexible [`Process`] implementation that loads the source [`Asset`] using the `L` [`AssetLoader`], then transforms
Expand Down Expand Up @@ -211,6 +217,11 @@ where
.map_err(|error| ProcessError::AssetSaveError(error.into()))?;
Ok(output_settings)
}

fn get_path_transformation(path: &Path) -> Box<PathBuf> {
Saver::get_path_transformation(path)
}

}

impl<Loader: AssetLoader, Saver: AssetSaver<Asset = Loader::Asset>> Process
Expand Down Expand Up @@ -241,6 +252,10 @@ impl<Loader: AssetLoader, Saver: AssetSaver<Asset = Loader::Asset>> Process
.map_err(|error| ProcessError::AssetSaveError(error.into()))?;
Ok(output_settings)
}

fn get_path_transformation(path: &Path) -> Box<PathBuf> {
Saver::get_path_transformation(path)
}
}

/// A type-erased variant of [`Process`] that enables interacting with processor implementations without knowing
Expand All @@ -258,6 +273,7 @@ pub trait ErasedProcessor: Send + Sync {
fn deserialize_meta(&self, meta: &[u8]) -> Result<Box<dyn AssetMetaDyn>, DeserializeMetaError>;
/// Returns the default type-erased [`AssetMeta`] for the underlying [`Process`] impl.
fn default_meta(&self) -> Box<dyn AssetMetaDyn>;
fn get_path_transformation(&self, path: &Path) -> Box<PathBuf>;
}

impl<P: Process> ErasedProcessor for P {
Expand Down Expand Up @@ -292,6 +308,10 @@ impl<P: Process> ErasedProcessor for P {
settings: P::Settings::default(),
}))
}

fn get_path_transformation(&self, path: &Path) -> Box<PathBuf> {
<P as Process>::get_path_transformation(path)
}
}

/// Provides scoped data access to the [`AssetProcessor`].
Expand Down
11 changes: 11 additions & 0 deletions crates/bevy_asset/src/saver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{io::Writer, meta::Settings, Asset, ErasedLoadedAsset};
use crate::{AssetLoader, Handle, LabeledAsset, UntypedHandle};
use bevy_utils::{BoxedFuture, ConditionalSendFuture, CowArc, HashMap};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::{borrow::Borrow, hash::Hash, ops::Deref};

/// Saves an [`Asset`] of a given [`AssetSaver::Asset`] type. [`AssetSaver::OutputLoader`] will then be used to load the saved asset
Expand All @@ -27,6 +28,10 @@ pub trait AssetSaver: Send + Sync + 'static {
) -> impl ConditionalSendFuture<
Output = Result<<Self::OutputLoader as AssetLoader>::Settings, Self::Error>,
>;

fn get_path_transformation(path: &Path) -> Box<PathBuf> {
Box::new(path.to_owned())
}
}

/// A type-erased dynamic variant of [`AssetSaver`] that allows callers to save assets without knowing the actual type of the [`AssetSaver`].
Expand All @@ -42,6 +47,8 @@ pub trait ErasedAssetSaver: Send + Sync + 'static {

/// The type name of the [`AssetSaver`].
fn type_name(&self) -> &'static str;

fn get_path_transformation(&self, path: &Path) -> Box<PathBuf>;
}

impl<S: AssetSaver> ErasedAssetSaver for S {
Expand All @@ -65,6 +72,10 @@ impl<S: AssetSaver> ErasedAssetSaver for S {
fn type_name(&self) -> &'static str {
std::any::type_name::<S>()
}

fn get_path_transformation(&self, path: &Path) -> Box<PathBuf> {
S::get_path_transformation(path)
}
}

/// An [`Asset`] (and any labeled "sub assets") intended to be saved.
Expand Down

0 comments on commit 7bd1cfe

Please sign in to comment.