From 16c1574e400d73198713336e18975ff37ab78290 Mon Sep 17 00:00:00 2001 From: Silas Bartha Date: Fri, 11 Oct 2024 16:02:07 -0400 Subject: Way too many changes (0.2) --- src/lib.rs | 158 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 144 insertions(+), 14 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index f4bd783..2bfbc57 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,12 +1,19 @@ -#![warn(missing_docs)] +// #![warn(missing_docs)] //! Plugin for bevy engine enabling interaction with and representation of the file system in the world. -use std::path::PathBuf; +use std::{ffi::OsStr, path::PathBuf}; -use bevy::{asset::io::AssetSource, prelude::*}; -use events::DirworldNavigationEvent; -use resources::{Dirworld, DirworldConfig}; +use bevy::{ecs::system::IntoObserverSystem, prelude::*}; +use bevy_scriptum::{runtimes::lua::LuaRuntime, BuildScriptingRuntime, ScriptingRuntimeBuilder}; +use events::{DirworldNavigationEvent, DirworldSpawn}; +use occule::Codec; +use resources::{ + DirworldCodecs, DirworldCurrentDir, DirworldObservers, DirworldRootDir, DirworldTasks, + EntryType, +}; +pub use watcher::DirworldWatcherEvent; +pub use watcher::DirworldWatcherSet; /// Components used by this plugin pub mod components; @@ -17,21 +24,144 @@ pub mod events; /// Resources used by this plugin pub mod resources; +mod watcher; + +/// Commands for this plugin +pub mod commands; + +mod systems; + +/// Payload for dirworld entities +pub mod payload; + +/// Actor component +pub mod actor; + +mod lua_api; + /// Plugin which enables high-level interaction +#[derive(Default)] pub struct DirworldPlugin { - /// Root path of world - pub path: PathBuf, + pub register_custom_lua_api: + Option) + Send + Sync>>, } impl Plugin for DirworldPlugin { fn build(&self, app: &mut App) { - info!("building"); - let path_string = self.path.to_string_lossy().to_string(); - app.insert_resource(DirworldConfig::new(self.path.clone())) - .register_asset_source("dirworld", AssetSource::build() - .with_reader(AssetSource::get_default_reader(path_string.clone())) - .with_watcher(|_| None)) + info!("building"); + app.add_systems(Startup, watcher::setup) + .add_systems( + Update, + (systems::remove_completed_tasks, lua_api::trigger_update), + ) + .add_systems(PostUpdate, watcher::update) + .add_systems( + PreUpdate, + watcher::handle_changes, + ) + .add_scripting::(|runtime| { + let runtime = lua_api::register(runtime); + if let Some(register_custom) = &self.register_custom_lua_api { + (register_custom)(runtime); + } + }) .add_event::() - .init_resource::(); + .init_resource::() + .init_resource::() + .init_resource::() + .init_resource::() + .init_resource::() + .add_event::(); + } +} + +pub trait Extensions { + fn extensions(&self) -> Option; + + fn file_stem_no_extensions(&self) -> Option; + + fn no_extensions(&self) -> PathBuf; +} + +impl Extensions for PathBuf { + fn extensions(&self) -> Option { + let mut temp_path = self.clone(); + let mut extensions = Vec::::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 { + 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( + &mut self, + extensions: Vec, + observer: impl IntoObserverSystem, + ) -> &mut Self; + + fn register_dirworld_entry_codec( + &mut self, + extensions: Vec, + codec: C, + ) -> &mut Self; +} + +impl DirworldApp for App { + fn register_dirworld_entry_callback( + &mut self, + extensions: Vec, + observer: impl IntoObserverSystem, + ) -> &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::() + .insert_many(extensions, observer_entity_id); + self + } + + fn register_dirworld_entry_codec( + &mut self, + extensions: Vec, + codec: C, + ) -> &mut Self { + self.world_mut() + .resource_mut::() + .insert_many(extensions, Box::new(codec)); + self } } -- cgit v1.2.3