Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_app/src/hotpatch.rs
6849 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, message::MessageWriter, 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 message, and update change detection.
38
app.init_resource::<HotPatchChanges>()
39
.add_message::<HotPatched>()
40
.add_systems(
41
Last,
42
move |mut hot_patched_writer: MessageWriter<HotPatched>,
43
mut res: ResMut<HotPatchChanges>| {
44
if receiver.try_recv().is_ok() {
45
hot_patched_writer.write_default();
46
res.set_changed();
47
}
48
},
49
);
50
51
#[cfg(feature = "reflect_auto_register")]
52
app.add_systems(
53
crate::First,
54
(move |registry: bevy_ecs::system::Res<bevy_ecs::reflect::AppTypeRegistry>| {
55
registry.write().register_derived_types();
56
})
57
.run_if(bevy_ecs::schedule::common_conditions::on_message::<HotPatched>),
58
);
59
}
60
}
61
62