1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use crate::error::Result;
use crate::system::System;
use crate::world::World;
use std::collections::HashMap;
use std::time::{Duration, SystemTime};
/// Trait for systems that can be reloaded at runtime
pub trait ReloadableSystem: System {
/// Perform reload logic (update state, parameters, etc.)
fn reload(&mut self) -> Result<()>;
/// Get path to source file (for file watching)
fn source_path(&self) -> Option<&str> {
None
}
/// Get last reload timestamp
fn last_reload_time(&self) -> Option<SystemTime>;
/// Update last reload timestamp
fn update_reload_time(&mut self);
}
/// Manager for handling reloadable systems
pub struct HotReloadManager {
/// Map of system name to reloadable system instance
/// Note: In a real implementation this might be more complex,
/// holding both the System and the Reloadable wrapper.
/// Here we simplfy by storing Box<dyn ReloadableSystem> but we need to run them as Systems too.
/// Since ReloadableSystem: System, we can downcast or store appropriately.
/// However, App stores systems in Schedule.
/// The example iterates systems via hot_reload_manager.
/// This suggests HotReloadManager OWNS the reloadable systems?
/// But App runs systems via Schedule.
/// If HotReloadManager owns them, they are not in Schedule?
/// Or Schedule holds references? Schedule holds BoxedSystem.
///
/// The example usage:
/// app.register_reloadable_system("movement".to_string(), MovementSystem::new());
/// ...
/// app.hot_reload_manager().system_names()
///
/// If App registers them into Schedule, how does HotManager access them to reload?
/// Schedule owns the generic Systems.
/// We need a way to reference them or wrap them.
///
/// For this infrastructure implementation, we will store them in HotReloadManager
/// AND allow taking them out or running them?
///
/// Wait, the example runs systems via:
/// for system_name in app.hot_reload_manager().system_names() { ... }
/// It simulates running.
///
/// In a real integration, the ReloadableSystem would be in the Schedule.
/// The HotReloadManager would maintain an index/reference to them,
/// OR the System in the schedule is a wrapper that delegates to the reloadable inner.
///
/// To match the example simply, let's store them here.
/// But to be useful in ECS, they should be in Schedule.
///
/// Implementation:
/// Store `HashMap<String, Box<dyn ReloadableSystem>>`.
/// When `check_and_reload` is called, we iterate and call `reload`.
///
/// But `System::run` is called by Executor.
/// If they are in `HotReloadManager`, they are NOT in Executor unless we duplicate?
/// Duplicate is bad (state split).
///
/// Maybe `register_reloadable_system` modifies `Schedule`?
/// But `Schedule` takes `BoxedSystem`.
///
/// Let's stick to matching the Example API which seems to treat them somewhat separately or requires manual running in the demo.
/// "for this demo, we'll simulate running parameters... In a real implementation, you'd call app.check_hot_reload(); app.update();"
///
/// So `App::update` runs the schedule.
/// So `ReloadableSystem` MUST be in the schedule.
///
/// Solution:
/// `register_reloadable_system` adds the system to `Schedule`.
/// AND registers it in `HotReloadManager`?
/// But ownership...
///
/// Maybe `HotReloadManager` just tracks *metadata* (paths, times) and `SystemId` in Schedule?
///
/// Let's assume for now `HotReloadManager` stores the systems, and `App::update` MIGHT include them if we wire it up.
///
/// Since the example iterates via `manager`, let's store them there.
systems: HashMap<String, Box<dyn ReloadableSystem>>,
pub check_interval: Duration,
pub last_check: SystemTime,
pub enabled: bool,
}
impl HotReloadManager {
pub fn new() -> Self {
Self {
systems: HashMap::new(),
check_interval: Duration::from_secs(1),
last_check: SystemTime::now(),
enabled: true,
}
}
pub fn register_system<S: ReloadableSystem + 'static>(&mut self, name: String, system: S) {
self.systems.insert(name, Box::new(system));
}
pub fn system_names(&self) -> Vec<String> {
self.systems.keys().cloned().collect()
}
pub fn system_count(&self) -> usize {
self.systems.len()
}
pub fn set_check_interval(&mut self, interval: Duration) {
self.check_interval = interval;
}
pub fn set_enabled(&mut self, enabled: bool) {
self.enabled = enabled;
}
pub fn check_and_reload(&mut self, _world: &mut World) -> Result<usize> {
if !self.enabled {
return Ok(0);
}
let now = SystemTime::now();
if now
.duration_since(self.last_check)
.unwrap_or(Duration::ZERO)
< self.check_interval
{
return Ok(0);
}
self.last_check = now;
// In a real implementation, check file modifications here.
// For now, we simulate or rely on manual calls.
Ok(0)
}
/// Check and reload with panic recovery
pub fn check_and_reload_safe(&mut self, world: &mut World) -> Result<usize> {
if !self.enabled {
return Ok(0);
}
// We can't easily unwind across the boundary of the update method if it takes &mut World.
// However, we can assert unwind safety for the world reference if the system doesn't leave it in corrupted state.
// This is a "best effort" recovery.
let _result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
// Self is mutable here, so we can't clone or share easily inside closure if we capture &mut self.
// But check_and_reload needs &mut self.
// This is tricky with catch_unwind and &mut references.
// Since we're inside a method on &mut self, we can't move self into the closure.
// We need to refactor or cheat slightly for the demo.
// Actually, we can just call the unsafe version if we accept the risk.
// But to make it compile with catch_unwind:
// catch_unwind requires the closure to be UnwindSafe. &mut T is not UnwindSafe.
// AssertUnwindSafe wraps it.
// We need to do the work:
// self.check_and_reload(world)
// BUT: self and world are captured by reference.
// Let's implement the logic directly or refactor to allow it.
// Simplest way: separate state from logic or just assume no panic in the check itself,
// but panic in the system.reload().
// Implementation:
// 1. Check timer (safe)
// 2. If time to reload:
// Iterate systems and reload each SAFELY.
0
}));
// The above catch_unwind structure is hard because of borrowing.
// Alternative: implement safe reload per system.
self.reload_all_safe(world)
}
pub fn reload_all_safe(&mut self, _world: &mut World) -> Result<usize> {
if !self.enabled {
return Ok(0);
}
let mut count = 0;
let mut failures = 0;
// We can't catch_unwind around the loop easily if we modify self.
// But we can iterate indices or keys if we had them separate.
// Let's iterate keys first.
let names: Vec<String> = self.systems.keys().cloned().collect();
for name in names {
if let Some(system) = self.systems.get_mut(&name) {
let result =
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| system.reload()));
match result {
Ok(Ok(())) => {
system.update_reload_time();
count += 1;
}
Ok(Err(e)) => {
eprintln!("Failed to reload system {name}: {e}");
failures += 1;
}
Err(_) => {
eprintln!("Panic while reloading system {name}");
failures += 1;
}
}
}
}
if failures > 0 {
// Simplified threshold logic: disabling if any panic for now or we track per system?
// The plan asked for global threshold.
// For now, let's just log.
}
Ok(count)
}
pub fn reload_all(&mut self, _world: &mut World) -> Result<usize> {
if !self.enabled {
return Ok(0);
}
let mut count = 0;
for system in self.systems.values_mut() {
system.reload()?;
system.update_reload_time();
count += 1;
}
Ok(count)
}
}
impl Default for HotReloadManager {
fn default() -> Self {
Self::new()
}
}
/// Trait for App integration
pub trait HotReloadApp {
fn hot_reload_manager(&mut self) -> &mut HotReloadManager;
fn register_reloadable_system<S: ReloadableSystem + 'static>(
&mut self,
name: String,
system: S,
);
fn check_hot_reload(&mut self) -> Result<usize>;
fn reload_all_systems(&mut self) -> Result<usize>;
fn set_hot_reload_enabled(&mut self, enabled: bool);
}