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
|
use std::ops::Deref;
use bevy::prelude::*;
use notify::{
event::{MetadataKind, ModifyKind, RenameMode},
EventKind,
};
use crate::{
cache::DirworldCache,
components::{DirworldEntity, Persist},
events::{
DirworldChangeRoot, DirworldEnterRoom, DirworldLeaveRoom, DirworldNavigationComplete,
},
preload::{load_entity, DirworldPreloadBegin, PreloadState, RoomAssets},
resources::{
DirworldCodecs, DirworldCurrentDir, DirworldObservers, DirworldRootDir, DirworldTasks,
},
utils::{despawn_entity_by_path, extract_entity_payload},
DirworldWatcherEvent,
};
/// On navigation from a room, insert modified payloads into the cache
pub fn navigate_from_room(
trigger: Trigger<DirworldLeaveRoom>,
entities: Query<(Entity, Ref<DirworldEntity>), Without<Persist>>,
mut cache: ResMut<DirworldCache>,
mut commands: Commands,
mut event_writer: EventWriter<DirworldLeaveRoom>,
) {
for (entity, dirworld_entity) in entities.iter() {
cache.cache_entity(&dirworld_entity);
commands.entity(entity).despawn_recursive();
}
event_writer.send(trigger.event().clone());
}
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,
mut event_writer: EventWriter<DirworldEnterRoom>,
mut preload_event_writer: EventWriter<DirworldPreloadBegin>,
mut current_dir: ResMut<DirworldCurrentDir>,
mut next_preload_state: ResMut<NextState<PreloadState>>,
mut room_assets: ResMut<RoomAssets>,
mut dirworld_tasks: ResMut<DirworldTasks>,
) {
let DirworldEnterRoom {
exited: old_path,
entered: path,
} = &trigger.event();
let room_payload = extract_entity_payload(&path.join(".door"), &codecs).0;
*current_dir = DirworldCurrentDir {
path: path.to_path_buf(),
payload: room_payload,
};
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| {
info!("Root: {root_dir:?}, Path: {path:?}");
if root_dir == *path {
None
} else {
Some(path.join(".."))
}
})
.into_iter()
.flatten(),
),
Err(e) => {
error!("Failed to read directory \"{}\", ({:?})", path.display(), e);
return;
}
};
dirworld_tasks.insert("Loading Room".into(), None);
for entry in entries {
load_entity(
&entry,
&mut cache,
&codecs,
&observers,
&mut commands,
&mut next_preload_state,
&mut room_assets,
);
}
preload_event_writer.send(DirworldPreloadBegin { old_path: old_path.clone(), path: path.clone() });
event_writer.send(trigger.event().clone());
}
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>,
mut event_writer: EventWriter<DirworldWatcherEvent>,
mut next_preload_state: ResMut<NextState<PreloadState>>,
mut room_assets: ResMut<RoomAssets>,
) {
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 {
load_entity(
&path,
&mut cache,
&codecs,
&observers,
&mut commands,
&mut next_preload_state,
&mut room_assets,
);
}
}
EventKind::Modify(ModifyKind::Name(RenameMode::Both)) => {
despawn_entity_by_path(&mut commands, &dirworld_entities, &event.paths[0]);
load_entity(
&event.paths[1],
&mut cache,
&codecs,
&observers,
&mut commands,
&mut next_preload_state,
&mut room_assets,
);
}
EventKind::Modify(ModifyKind::Metadata(MetadataKind::Any)) => {
despawn_entity_by_path(&mut commands, &dirworld_entities, &event.paths[1]);
load_entity(
&event.paths[0],
&mut cache,
&codecs,
&observers,
&mut commands,
&mut next_preload_state,
&mut room_assets,
);
}
_ => {
// warn!("Not Processed.")
}
}
event_writer.send(trigger.event().clone());
}
pub fn change_root(
trigger: Trigger<DirworldChangeRoot>,
mut root_dir: ResMut<DirworldRootDir>,
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.canonicalize().unwrap();
info!("Changing Root to {}", new_root.display());
**root_dir = Some(new_root.to_path_buf());
commands.trigger(DirworldEnterRoom {
exited: new_root.to_path_buf(),
entered: new_root.to_path_buf(),
});
}
|