diff --git a/Cargo.lock b/Cargo.lock index 084116a..6bb7bcf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1501,6 +1501,7 @@ dependencies = [ "log", "pollster", "rune", + "walkdir", "wgpu", "winit", ] diff --git a/Cargo.toml b/Cargo.toml index 4913249..60585f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,5 +11,6 @@ env_logger = "0.11.3" log = "0.4.21" pollster = "0.3.0" rune = "0.13.2" +walkdir = "2.5.0" wgpu = "0.19.3" winit = "0.29.14" diff --git a/src/game.rs b/src/game.rs index 9dc4d23..80ec82e 100644 --- a/src/game.rs +++ b/src/game.rs @@ -43,7 +43,7 @@ impl<'w> Game<'w> { .build(&event_loop)?; let context = pollster::block_on(Context::new(Arc::new(window), Limits::default()))?; - let runtime = scripting::Runtime::new(&["frame_counter"])?; + let runtime = scripting::Runtime::new(&config.source_dir)?; Ok(Self { event_loop, diff --git a/src/scripting/runtime.rs b/src/scripting/runtime.rs index 4a00aad..3c64a2e 100644 --- a/src/scripting/runtime.rs +++ b/src/scripting/runtime.rs @@ -1,36 +1,28 @@ -use std::sync::Arc; +use std::{path::PathBuf, sync::Arc}; use anyhow::Result; use rune::{ termcolor::{ColorChoice, StandardStream}, Context, Diagnostics, Source, Sources, Vm, }; +use walkdir::WalkDir; use super::api; pub struct Runtime { pub vm: Vm, - pub sources: Vec, + pub sources: Vec, } impl Runtime { - pub fn new(source_paths: &[&str]) -> Result { - let mut full_source_paths = vec![]; - - let source_dir = format!("{}/scripts", env!("CARGO_MANIFEST_DIR")); - let mut sources = Sources::new(); - for i in 0..source_paths.len() { - let path = format!("{source_dir}/{}.rn", source_paths[i]); - full_source_paths.push(path.clone()); - sources.insert(Source::from_path(path)?)?; - } - + pub fn new(source_dir: &str) -> Result { let mut context = Context::with_default_modules()?; context.install(api::log::module()?)?; let runtime = Arc::new(context.runtime()?); let mut diagnostics = Diagnostics::new(); + let (mut sources, source_paths) = Self::get_sources(source_dir)?; let unit = rune::prepare(&mut sources) .with_context(&context) .with_diagnostics(&mut diagnostics) @@ -45,7 +37,23 @@ impl Runtime { Ok(Self { vm, - sources: full_source_paths, + sources: source_paths, }) } + + fn get_sources(source_dir: &str) -> Result<(Sources, Vec)> { + let mut source_paths = vec![]; + let mut sources = Sources::new(); + for entry in WalkDir::new(source_dir).into_iter().filter_map(|e| e.ok()) { + let path = entry.path(); + if path.is_file() && path.extension().is_some_and(|e| e == "rn") { + sources.insert(Source::from_path(path)?)?; + source_paths.push(path.to_owned()); + } + } + + log::warn!("Source paths: {source_paths:?}"); + + Ok((sources, source_paths)) + } }