Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_asset/src/io/embedded/embedded_watcher.rs
6601 views
1
use crate::io::{
2
file::{get_asset_path, get_base_path, new_asset_event_debouncer, FilesystemEventHandler},
3
memory::Dir,
4
AssetSourceEvent, AssetWatcher,
5
};
6
use alloc::{boxed::Box, sync::Arc, vec::Vec};
7
use bevy_platform::collections::HashMap;
8
use core::time::Duration;
9
use notify_debouncer_full::{notify::RecommendedWatcher, Debouncer, RecommendedCache};
10
use parking_lot::RwLock;
11
use std::{
12
fs::File,
13
io::{BufReader, Read},
14
path::{Path, PathBuf},
15
};
16
use tracing::warn;
17
18
/// A watcher for assets stored in the `embedded` asset source. Embedded assets are assets whose
19
/// bytes have been embedded into the Rust binary using the [`embedded_asset`](crate::embedded_asset) macro.
20
/// This watcher will watch for changes to the "source files", read the contents of changed files from the file system
21
/// and overwrite the initial static bytes of the file embedded in the binary with the new dynamically loaded bytes.
22
pub struct EmbeddedWatcher {
23
_watcher: Debouncer<RecommendedWatcher, RecommendedCache>,
24
}
25
26
impl EmbeddedWatcher {
27
/// Creates a new `EmbeddedWatcher` that watches for changes to the embedded assets in the given `dir`.
28
pub fn new(
29
dir: Dir,
30
root_paths: Arc<RwLock<HashMap<Box<Path>, PathBuf>>>,
31
sender: crossbeam_channel::Sender<AssetSourceEvent>,
32
debounce_wait_time: Duration,
33
) -> Self {
34
let root = get_base_path();
35
let handler = EmbeddedEventHandler {
36
dir,
37
root: root.clone(),
38
sender,
39
root_paths,
40
last_event: None,
41
};
42
let watcher = new_asset_event_debouncer(root, debounce_wait_time, handler).unwrap();
43
Self { _watcher: watcher }
44
}
45
}
46
47
impl AssetWatcher for EmbeddedWatcher {}
48
49
/// A [`FilesystemEventHandler`] that uses [`EmbeddedAssetRegistry`](crate::io::embedded::EmbeddedAssetRegistry) to hot-reload
50
/// binary-embedded Rust source files. This will read the contents of changed files from the file system and overwrite
51
/// the initial static bytes from the file embedded in the binary.
52
pub(crate) struct EmbeddedEventHandler {
53
sender: crossbeam_channel::Sender<AssetSourceEvent>,
54
root_paths: Arc<RwLock<HashMap<Box<Path>, PathBuf>>>,
55
root: PathBuf,
56
dir: Dir,
57
last_event: Option<AssetSourceEvent>,
58
}
59
60
impl FilesystemEventHandler for EmbeddedEventHandler {
61
fn begin(&mut self) {
62
self.last_event = None;
63
}
64
65
fn get_path(&self, absolute_path: &Path) -> Option<(PathBuf, bool)> {
66
let (local_path, is_meta) = get_asset_path(&self.root, absolute_path);
67
let final_path = self.root_paths.read().get(local_path.as_path())?.clone();
68
if is_meta {
69
warn!("Meta file asset hot-reloading is not supported yet: {final_path:?}");
70
}
71
Some((final_path, false))
72
}
73
74
fn handle(&mut self, absolute_paths: &[PathBuf], event: AssetSourceEvent) {
75
if self.last_event.as_ref() != Some(&event) {
76
if let AssetSourceEvent::ModifiedAsset(path) = &event
77
&& let Ok(file) = File::open(&absolute_paths[0])
78
{
79
let mut reader = BufReader::new(file);
80
let mut buffer = Vec::new();
81
82
// Read file into vector.
83
if reader.read_to_end(&mut buffer).is_ok() {
84
self.dir.insert_asset(path, buffer);
85
}
86
}
87
self.last_event = Some(event.clone());
88
self.sender.send(event).unwrap();
89
}
90
}
91
}
92
93