-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Internal Asset Hot Reloading (#3966)
Adds "hot reloading" of internal assets, which is normally not possible because they are loaded using `include_str` / direct Asset collection access. This is accomplished via the following: * Add a new `debug_asset_server` feature flag * When that feature flag is enabled, create a second App with a second AssetServer that points to a configured location (by default the `crates` folder). Plugins that want to add hot reloading support for their assets can call the new `app.add_debug_asset::<T>()` and `app.init_debug_asset_loader::<T>()` functions. * Load "internal" assets using the new `load_internal_asset` macro. By default this is identical to the current "include_str + register in asset collection" approach. But if the `debug_asset_server` feature flag is enabled, it will also load the asset dynamically in the debug asset server using the file path. It will then set up a correlation between the "debug asset" and the "actual asset" by listening for asset change events. This is an alternative to #3673. The goal was to keep the boilerplate and features flags to a minimum for bevy plugin authors, and allow them to home their shaders near relevant code. This is a draft because I haven't done _any_ quality control on this yet. I'll probably rename things and remove a bunch of unwraps. I just got it working and wanted to use it to start a conversation. Fixes #3660
- Loading branch information
Showing
17 changed files
with
271 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
use bevy_app::{App, Events, Plugin}; | ||
use bevy_ecs::{ | ||
schedule::SystemLabel, | ||
system::{NonSendMut, Res, ResMut, SystemState}, | ||
}; | ||
use bevy_tasks::{IoTaskPool, TaskPoolBuilder}; | ||
use bevy_utils::HashMap; | ||
use std::{ | ||
ops::{Deref, DerefMut}, | ||
path::Path, | ||
}; | ||
|
||
use crate::{ | ||
Asset, AssetEvent, AssetPlugin, AssetServer, AssetServerSettings, Assets, FileAssetIo, Handle, | ||
HandleUntyped, | ||
}; | ||
|
||
/// A "debug asset app", whose sole responsibility is hot reloading assets that are | ||
/// "internal" / compiled-in to Bevy Plugins. | ||
pub struct DebugAssetApp(App); | ||
|
||
impl Deref for DebugAssetApp { | ||
type Target = App; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl DerefMut for DebugAssetApp { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.0 | ||
} | ||
} | ||
|
||
#[derive(SystemLabel, Debug, Clone, PartialEq, Eq, Hash)] | ||
pub struct DebugAssetAppRun; | ||
|
||
/// Facilitates the creation of a "debug asset app", whose sole responsibility is hot reloading | ||
/// assets that are "internal" / compiled-in to Bevy Plugins. | ||
/// Pair with [`load_internal_asset`](crate::load_internal_asset) to load "hot reloadable" assets | ||
/// The `debug_asset_server` feature flag must also be enabled for hot reloading to work. | ||
/// Currently only hot reloads assets stored in the `crates` folder. | ||
#[derive(Default)] | ||
pub struct DebugAssetServerPlugin; | ||
pub struct HandleMap<T: Asset> { | ||
pub handles: HashMap<Handle<T>, Handle<T>>, | ||
} | ||
|
||
impl<T: Asset> Default for HandleMap<T> { | ||
fn default() -> Self { | ||
Self { | ||
handles: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
impl Plugin for DebugAssetServerPlugin { | ||
fn build(&self, app: &mut bevy_app::App) { | ||
let mut debug_asset_app = App::new(); | ||
debug_asset_app | ||
.insert_resource(IoTaskPool( | ||
TaskPoolBuilder::default() | ||
.num_threads(2) | ||
.thread_name("Debug Asset Server IO Task Pool".to_string()) | ||
.build(), | ||
)) | ||
.insert_resource(AssetServerSettings { | ||
asset_folder: "crates".to_string(), | ||
watch_for_changes: true, | ||
}) | ||
.add_plugin(AssetPlugin); | ||
app.insert_non_send_resource(DebugAssetApp(debug_asset_app)); | ||
app.add_system(run_debug_asset_app); | ||
} | ||
} | ||
|
||
fn run_debug_asset_app(mut debug_asset_app: NonSendMut<DebugAssetApp>) { | ||
debug_asset_app.0.update(); | ||
} | ||
|
||
pub(crate) fn sync_debug_assets<T: Asset + Clone>( | ||
mut debug_asset_app: NonSendMut<DebugAssetApp>, | ||
mut assets: ResMut<Assets<T>>, | ||
) { | ||
let world = &mut debug_asset_app.0.world; | ||
let mut state = SystemState::<( | ||
Res<Events<AssetEvent<T>>>, | ||
Res<HandleMap<T>>, | ||
Res<Assets<T>>, | ||
)>::new(world); | ||
let (changed_shaders, handle_map, debug_assets) = state.get_mut(world); | ||
for changed in changed_shaders.iter_current_update_events() { | ||
let debug_handle = match changed { | ||
AssetEvent::Created { handle } => handle, | ||
AssetEvent::Modified { handle } => handle, | ||
AssetEvent::Removed { .. } => continue, | ||
}; | ||
if let Some(handle) = handle_map.handles.get(debug_handle) { | ||
if let Some(debug_asset) = debug_assets.get(debug_handle) { | ||
assets.set_untracked(handle, debug_asset.clone()); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Uses the return type of the given loader to register the given handle with the appropriate type | ||
/// and load the asset with the given `path` and parent `file_path`. | ||
/// If this feels a bit odd ... thats because it is. This was built to improve the UX of the | ||
/// `load_internal_asset` macro. | ||
pub fn register_handle_with_loader<A: Asset>( | ||
_loader: fn(&'static str) -> A, | ||
app: &mut DebugAssetApp, | ||
handle: HandleUntyped, | ||
file_path: &str, | ||
path: &'static str, | ||
) { | ||
let mut state = SystemState::<(ResMut<HandleMap<A>>, Res<AssetServer>)>::new(&mut app.world); | ||
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap(); | ||
let manifest_dir_path = Path::new(&manifest_dir); | ||
let (mut handle_map, asset_server) = state.get_mut(&mut app.world); | ||
let asset_io = asset_server | ||
.asset_io() | ||
.downcast_ref::<FileAssetIo>() | ||
.expect("The debug AssetServer only works with FileAssetIo-backed AssetServers"); | ||
let absolute_file_path = manifest_dir_path.join( | ||
Path::new(file_path) | ||
.parent() | ||
.expect("file path must have a parent"), | ||
); | ||
let asset_folder_relative_path = absolute_file_path | ||
.strip_prefix(asset_io.root_path()) | ||
.expect("The AssetIo root path should be a prefix of the absolute file path"); | ||
handle_map.handles.insert( | ||
asset_server.load(asset_folder_relative_path.join(path)), | ||
handle.clone_weak().typed::<A>(), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.