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
|
use std::ops::Deref;
use bevy::prelude::*;
use notify::{
event::{MetadataKind, ModifyKind, RenameMode},
EventKind,
};
use crate::{
components::{DirworldEntity, Persist},
events::{DirworldChangeRoot, DirworldEnterRoom, DirworldLeaveRoom},
resources::{
DirworldCache, DirworldCodecs, DirworldCurrentDir, DirworldObservers, DirworldRootDir,
},
utils::{despawn_entity_by_path, spawn_entity},
DirworldWatcherEvent,
};
/// On navigation from a room, insert modified payloads into the cache
pub fn navigate_from_room(
_: Trigger<DirworldLeaveRoom>,
entities: Query<(Entity, Ref<DirworldEntity>), Without<Persist>>,
mut cache: ResMut<DirworldCache>,
mut commands: Commands,
) {
for (entity, dirworld_entity) in entities.iter() {
if let Some(payload) = &dirworld_entity.payload {
info!("Caching {entity:?}");
cache.insert(dirworld_entity.path.clone(), payload.clone());
}
commands.entity(entity).despawn_recursive();
}
}
pub fn navigate_to_room(
trigger: Trigger<DirworldEnterRoom>,
root_dir: Res<DirworldRootDir>,
mut cache: ResMut<DirworldCache>,
observers: Res<DirworldObservers>,
codecs: Res<DirworldCodecs>,
mut commands: Commands,
) {
let path = &trigger.event().0;
let entries = match path.read_dir() {
Ok(entries) => entries
.flatten()
.map(|entry| entry.path().canonicalize())
.flatten()
.filter(|entry| {
!entry
.file_name()
.is_some_and(|file_name| file_name.to_string_lossy().starts_with("."))
})
.chain(
root_dir
.clone()
.map(|root_dir| {
if root_dir == *path {
None
} else {
Some(path.join(".."))
}
})
.into_iter()
.flatten(),
),
Err(e) => {
error!("Failed to read directory \"{}\", ({:?})", path.display(), e);
return;
}
};
for entry in entries {
spawn_entity(&entry, &mut cache, &codecs, &observers, &mut commands);
}
}
pub fn handle_changes(
trigger: Trigger<DirworldWatcherEvent>,
mut commands: Commands,
dirworld_entities: Query<(Entity, &DirworldEntity)>,
observers: Res<DirworldObservers>,
codecs: Res<DirworldCodecs>,
mut cache: ResMut<DirworldCache>,
) {
let event = &trigger.event().0;
info!("Watcher Event: {event:?}");
match event.kind {
EventKind::Remove(_) | EventKind::Modify(ModifyKind::Name(RenameMode::From)) => {
for path in &event.paths {
despawn_entity_by_path(&mut commands, &dirworld_entities, path);
}
}
EventKind::Create(_) | EventKind::Modify(ModifyKind::Name(RenameMode::To)) => {
for path in &event.paths {
spawn_entity(path, &mut cache, &codecs, &observers, &mut commands);
}
}
EventKind::Modify(ModifyKind::Name(RenameMode::Both)) => {
despawn_entity_by_path(&mut commands, &dirworld_entities, &event.paths[0]);
spawn_entity(
&event.paths[1],
&mut cache,
&codecs,
&observers,
&mut commands,
);
}
EventKind::Modify(ModifyKind::Metadata(MetadataKind::Any)) => {
despawn_entity_by_path(&mut commands, &dirworld_entities, &event.paths[1]);
spawn_entity(
&event.paths[0],
&mut cache,
&codecs,
&observers,
&mut commands,
);
}
_ => {
// warn!("Not Processed.")
}
}
}
pub fn change_root(
trigger: Trigger<DirworldChangeRoot>,
mut root_dir: ResMut<DirworldRootDir>,
mut current_dir: ResMut<DirworldCurrentDir>,
mut commands: Commands,
) {
if let DirworldRootDir(Some(old_dir)) = root_dir.deref() {
commands.trigger(DirworldLeaveRoom(old_dir.to_path_buf()));
};
let new_root = &trigger.event().0;
info!("Changing Root to {}", new_root.display());
**root_dir = Some(new_root.to_path_buf());
**current_dir = Some(new_root.to_path_buf());
commands.trigger(DirworldEnterRoom(new_root.to_path_buf()));
}
|