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
|
// #![warn(missing_docs)]
//! Plugin for bevy engine enabling interaction with and representation of the file system in the world.
use std::{ffi::OsStr, path::PathBuf};
use actor::ActorPlugin;
use bevy::render::mesh::ExtrusionBuilder;
use bevy::{ecs::system::IntoObserverSystem, prelude::*};
use bevy_scriptum::{runtimes::lua::LuaRuntime, BuildScriptingRuntime, ScriptingRuntimeBuilder};
use events::{
DirworldChangeRoot, DirworldEnterRoom, DirworldLeaveRoom, DirworldNavigationEvent,
DirworldSpawn,
};
use occule::Codec;
use resources::DirworldCache;
use resources::{
DirworldCodecs, DirworldCurrentDir, DirworldObservers, DirworldRootDir, DirworldTasks,
EntryType,
};
pub use watcher::DirworldWatcherEvent;
pub use watcher::DirworldWatcherSet;
use yarnspinner::core::Library;
/// Components used by this plugin
pub mod components;
/// Events used by this plugin
pub mod events;
/// Resources used by this plugin
pub mod resources;
mod watcher;
/// Commands for this plugin
pub mod commands;
mod systems;
mod observers;
pub mod utils;
/// Payload for dirworld entities
pub mod payload;
/// Actor component
pub mod actor;
mod lua_api;
pub mod conditionals;
pub mod yarnspinner_api;
pub mod room_generation;
/// Plugin which enables high-level interaction
#[derive(Default)]
pub struct DirworldPlugin {
pub register_custom_lua_api:
Option<Box<dyn Fn(ScriptingRuntimeBuilder<LuaRuntime>) + Send + Sync>>,
}
impl Plugin for DirworldPlugin {
fn build(&self, app: &mut App) {
info!("building");
app.add_plugins(ActorPlugin {
custom_function_registration: Some(yarnspinner_api::setup_yarnspinner_functions),
})
.add_systems(Startup, watcher::setup)
.add_systems(
Update,
(
systems::remove_completed_tasks,
lua_api::trigger_update,
yarnspinner_api::process_commands,
),
)
.add_systems(
PostUpdate,
watcher::update,
)
.add_scripting::<LuaRuntime>(|runtime| {
let runtime = lua_api::register(runtime);
if let Some(register_custom) = &self.register_custom_lua_api {
(register_custom)(runtime);
}
})
.init_resource::<DirworldCache>()
.init_resource::<DirworldRootDir>()
.init_resource::<DirworldCurrentDir>()
.init_resource::<DirworldTasks>()
.init_resource::<DirworldObservers>()
.init_resource::<DirworldCodecs>()
.add_event::<DirworldEnterRoom>()
.add_event::<DirworldLeaveRoom>()
.add_event::<DirworldChangeRoot>()
.add_event::<DirworldWatcherEvent>()
.observe(observers::navigate_to_room)
.observe(observers::handle_changes)
.observe(observers::change_root)
.observe(observers::navigate_from_room);
}
}
pub trait Extensions {
fn extensions(&self) -> Option<String>;
fn file_stem_no_extensions(&self) -> Option<String>;
fn no_extensions(&self) -> PathBuf;
}
impl Extensions for PathBuf {
fn extensions(&self) -> Option<String> {
let mut temp_path = self.clone();
let mut extensions = Vec::<String>::new();
while let Some(extension) = temp_path.extension() {
extensions.insert(0, extension.to_string_lossy().into());
temp_path.set_extension("");
}
if extensions.is_empty() {
None
} else {
Some(extensions.join("."))
}
}
fn file_stem_no_extensions(&self) -> Option<String> {
let mut temp_path = self.clone();
while let Some(_) = temp_path.extension() {
temp_path.set_extension("");
}
temp_path
.file_stem()
.and_then(OsStr::to_str)
.map(str::to_string)
}
fn no_extensions(&self) -> PathBuf {
let mut temp_path = self.clone();
while let Some(_) = temp_path.extension() {
temp_path.set_extension("");
}
temp_path
}
}
pub trait DirworldApp {
fn register_dirworld_entry_callback<B: Bundle, M>(
&mut self,
extensions: Vec<EntryType>,
observer: impl IntoObserverSystem<DirworldSpawn, B, M>,
) -> &mut Self;
fn register_dirworld_entry_codec<C: Codec + Send + Sync + 'static>(
&mut self,
extensions: Vec<String>,
codec: C,
) -> &mut Self;
}
impl DirworldApp for App {
fn register_dirworld_entry_callback<B: Bundle, M>(
&mut self,
extensions: Vec<EntryType>,
observer: impl IntoObserverSystem<DirworldSpawn, B, M>,
) -> &mut Self {
let world = self.world_mut();
let observer_entity_id;
{
let mut observer_entity = world.spawn_empty();
observer_entity_id = observer_entity.id();
observer_entity.insert(Observer::new(observer).with_entity(observer_entity_id));
}
world.flush();
world
.resource_mut::<DirworldObservers>()
.insert_many(extensions, observer_entity_id);
self
}
fn register_dirworld_entry_codec<C: Codec + Send + Sync + 'static>(
&mut self,
extensions: Vec<String>,
codec: C,
) -> &mut Self {
self.world_mut()
.resource_mut::<DirworldCodecs>()
.insert_many(extensions, Box::new(codec));
self
}
}
|