Skip to content

Commit

Permalink
Move loader example to top level bevy crate
Browse files Browse the repository at this point in the history
  • Loading branch information
cart committed Sep 7, 2023
1 parent 341e506 commit ce714f5
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 44 deletions.
12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,18 @@ description = "Demonstrates automatic reloading of assets when modified on disk"
category = "Assets"
wasm = true

[[example]]
name = "asset_processing"
path = "examples/asset/processing/main.rs"
doc-scrape-examples = true
required-features = ["filesystem_watcher"]

[package.metadata.example.asset_processing]
name = "Asset Processing"
description = "Demonstrates how to process and load custom assets"
category = "Assets"
wasm = false

# Async Tasks
[[example]]
name = "async_compute"
Expand Down
7 changes: 1 addition & 6 deletions crates/bevy_asset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,4 @@ wasm-bindgen-futures = "0.4"
js-sys = "0.3"

[dev-dependencies]
bevy_core = { path = "../bevy_core", version = "0.12.0-dev" }

[[example]]
name = "loader"
path = "examples/loader.rs"
required-features = ["multi-threaded"]
bevy_core = { path = "../bevy_core", version = "0.12.0-dev" }
20 changes: 19 additions & 1 deletion crates/bevy_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub use anyhow;

use crate::{
io::{processor_gated::ProcessorGatedReader, AssetProvider, AssetProviders},
processor::AssetProcessor,
processor::{AssetProcessor, Process},
};
use bevy_app::{App, First, MainScheduleOrder, Plugin, PostUpdate, Startup};
use bevy_ecs::{
Expand Down Expand Up @@ -250,6 +250,10 @@ impl VisitAssetDependencies for Vec<UntypedHandle> {
pub trait AssetApp {
/// Registers the given `loader` in the [`App`]'s [`AssetServer`].
fn register_asset_loader<L: AssetLoader>(&mut self, loader: L) -> &mut Self;
/// Registers the given `processor` in the [`App`]'s [`AssetProcessor`].
fn register_asset_processor<P: Process>(&mut self, processor: P) -> &mut Self;
/// Sets the default asset processor for the given `extension`.
fn set_default_asset_processor<P: Process>(&mut self, extension: &str) -> &mut Self;
/// Initializes the given loader in the [`App`]'s [`AssetServer`].
fn init_asset_loader<L: AssetLoader + FromWorld>(&mut self) -> &mut Self;
/// Initializes the given [`Asset`] in the [`App`] by:
Expand Down Expand Up @@ -326,6 +330,20 @@ impl AssetApp for App {
.preregister_loader::<L>(extensions);
self
}

fn register_asset_processor<P: Process>(&mut self, processor: P) -> &mut Self {
if let Some(asset_processor) = self.world.get_resource::<AssetProcessor>() {
asset_processor.register_processor(processor);
}
self
}

fn set_default_asset_processor<P: Process>(&mut self, extension: &str) -> &mut Self {
if let Some(asset_processor) = self.world.get_resource::<AssetProcessor>() {
asset_processor.set_default_processor::<P>(extension);
}
self
}
}

/// A system set that holds all "track asset" operations.
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(
meta_format_version: "1.0",
asset: Process(
processor: "bevy_asset::processor::process::LoadAndSave<loader::CoolTextLoader, loader::CoolTextSaver>",
processor: "bevy_asset::processor::process::LoadAndSave<asset_processing::CoolTextLoader, asset_processing::CoolTextSaver>",
settings: (
loader_settings: (),
saver_settings: (
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(
meta_format_version: "1.0",
asset: Process(
processor: "bevy_asset::processor::process::LoadAndSave<loader::CoolTextLoader, loader::CoolTextSaver>",
processor: "bevy_asset::processor::process::LoadAndSave<asset_processing::CoolTextLoader, asset_processing::CoolTextSaver>",
settings: (
loader_settings: (),
saver_settings: (
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(
meta_format_version: "1.0",
asset: Process(
processor: "bevy_asset::processor::process::LoadAndSave<loader::CoolTextLoader, loader::CoolTextSaver>",
processor: "bevy_asset::processor::process::LoadAndSave<asset_processing::CoolTextLoader, asset_processing::CoolTextSaver>",
settings: (
loader_settings: (),
saver_settings: (
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(
meta_format_version: "1.0",
asset: Process(
processor: "bevy_asset::processor::process::LoadAndSave<loader::CoolTextLoader, loader::CoolTextSaver>",
processor: "bevy_asset::processor::process::LoadAndSave<asset_processing::CoolTextLoader, asset_processing::CoolTextSaver>",
settings: (
loader_settings: (),
saver_settings: (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
use bevy_app::{App, Plugin, ScheduleRunnerPlugin, Startup, Update};
use bevy_asset::{
io::{Reader, Writer},
processor::{AssetProcessor, LoadAndSave},
saver::{AssetSaver, SavedAsset},
Asset, AssetApp, AssetLoader, AssetPlugin, AssetServer, Assets, Handle, LoadContext,
use bevy::{
asset::{
io::{Reader, Writer},
processor::LoadAndSave,
saver::{AssetSaver, SavedAsset},
AssetLoader, AsyncReadExt, AsyncWriteExt, LoadContext,
},
prelude::*,
reflect::TypePath,
utils::BoxedFuture,
};
use bevy_core::TaskPoolPlugin;
use bevy_ecs::prelude::*;
use bevy_log::{Level, LogPlugin};
use bevy_reflect::TypePath;
use bevy_utils::Duration;
use futures_lite::{AsyncReadExt, AsyncWriteExt};
use bevy_internal::asset::io::AssetProviders;
use serde::{Deserialize, Serialize};

fn main() {
App::new()
.add_plugins((
TaskPoolPlugin::default(),
LogPlugin {
level: Level::TRACE,
..Default::default()
},
ScheduleRunnerPlugin::run_loop(Duration::from_secs_f32(2.0)),
AssetPlugin::processed_dev(),
TextPlugin,
))
.insert_resource(
// This is just overriding the default paths to scope this to the correct example folder
// You can generally skip this in your own projects
AssetProviders::default()
.with_default_file_source("examples/asset/processing/assets".to_string())
.with_default_file_destination(
"examples/asset/processing/imported_assets".to_string(),
),
)
.add_plugins((DefaultPlugins.set(AssetPlugin::processed_dev()), TextPlugin))
.add_systems(Startup, setup)
.add_systems(Update, print_text)
.run();
Expand All @@ -37,15 +36,11 @@ impl Plugin for TextPlugin {
app.init_asset::<Text>()
.init_asset::<CoolText>()
.register_asset_loader(TextLoader)
.register_asset_loader(CoolTextLoader);

if let Some(processor) = app.world.get_resource::<AssetProcessor>() {
processor.register_processor::<LoadAndSave<CoolTextLoader, CoolTextSaver>>(
.register_asset_loader(CoolTextLoader)
.register_asset_processor::<LoadAndSave<CoolTextLoader, CoolTextSaver>>(
CoolTextSaver.into(),
);
processor
.set_default_processor::<LoadAndSave<CoolTextLoader, CoolTextSaver>>("cool.ron");
}
)
.set_default_asset_processor::<LoadAndSave<CoolTextLoader, CoolTextSaver>>("cool.ron");
}
}

Expand All @@ -68,7 +63,7 @@ impl AssetLoader for TextLoader {
reader: &'a mut Reader,
settings: &'a TextSettings,
_load_context: &'a mut LoadContext,
) -> bevy_utils::BoxedFuture<'a, Result<Text, anyhow::Error>> {
) -> BoxedFuture<'a, Result<Text, anyhow::Error>> {
Box::pin(async move {
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
Expand Down Expand Up @@ -113,7 +108,7 @@ impl AssetLoader for CoolTextLoader {
reader: &'a mut Reader,
_settings: &'a Self::Settings,
load_context: &'a mut LoadContext,
) -> bevy_utils::BoxedFuture<'a, Result<CoolText, anyhow::Error>> {
) -> BoxedFuture<'a, Result<CoolText, anyhow::Error>> {
Box::pin(async move {
let mut bytes = Vec::new();
reader.read_to_end(&mut bytes).await?;
Expand Down Expand Up @@ -157,7 +152,7 @@ impl AssetSaver for CoolTextSaver {
writer: &'a mut Writer,
asset: SavedAsset<'a, Self::Asset>,
settings: &'a Self::Settings,
) -> bevy_utils::BoxedFuture<'a, Result<TextSettings, anyhow::Error>> {
) -> BoxedFuture<'a, Result<TextSettings, anyhow::Error>> {
Box::pin(async move {
let text = format!("{}{}", asset.text.clone(), settings.appended);
writer.write_all(text.as_bytes()).await?;
Expand Down

0 comments on commit ce714f5

Please sign in to comment.