Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_app/src/hotpatch.rs
6595 views
1
//! Utilities for hotpatching code.
2
extern crate alloc;
3
4
use alloc::sync::Arc;
5
6
#[cfg(feature = "reflect_auto_register")]
7
use bevy_ecs::schedule::IntoScheduleConfigs;
8
use bevy_ecs::{
9
change_detection::DetectChangesMut, event::EventWriter, system::ResMut, HotPatchChanges,
10
HotPatched,
11
};
12
#[cfg(not(target_family = "wasm"))]
13
use dioxus_devtools::connect_subsecond;
14
use dioxus_devtools::subsecond;
15
16
pub use dioxus_devtools::subsecond::{call, HotFunction};
17
18
use crate::{Last, Plugin};
19
20
/// Plugin connecting to Dioxus CLI to enable hot patching.
21
#[derive(Default)]
22
pub struct HotPatchPlugin;
23
24
impl Plugin for HotPatchPlugin {
25
fn build(&self, app: &mut crate::App) {
26
let (sender, receiver) = crossbeam_channel::bounded::<HotPatched>(1);
27
28
// Connects to the dioxus CLI that will handle rebuilds
29
// This will open a connection to the dioxus CLI to receive updated jump tables
30
// Sends a `HotPatched` message through the channel when the jump table is updated
31
#[cfg(not(target_family = "wasm"))]
32
connect_subsecond();
33
subsecond::register_handler(Arc::new(move || {
34
sender.send(HotPatched).unwrap();
35
}));
36
37
// Adds a system that will read the channel for new `HotPatched` messages, send the event, and update change detection.
38
app.init_resource::<HotPatchChanges>()
39
.add_event::<HotPatched>()
40
.add_systems(
41
Last,
42
move |mut events: EventWriter<HotPatched>, mut res: ResMut<HotPatchChanges>| {
43
if receiver.try_recv().is_ok() {
44
events.write_default();
45
res.set_changed();
46
}
47
},
48
);
49
50
#[cfg(feature = "reflect_auto_register")]
51
app.add_systems(
52
crate::First,
53
(move |registry: bevy_ecs::system::Res<bevy_ecs::reflect::AppTypeRegistry>| {
54
registry.write().register_derived_types();
55
})
56
.run_if(bevy_ecs::schedule::common_conditions::on_event::<HotPatched>),
57
);
58
}
59
}
60
61