Load all sources from source directory rather than manually specifying them

This commit is contained in:
Jarrod Doyle 2024-03-08 17:34:41 +00:00
parent 290edc26d3
commit 44ddda9736
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
4 changed files with 25 additions and 15 deletions

1
Cargo.lock generated
View File

@ -1501,6 +1501,7 @@ dependencies = [
"log", "log",
"pollster", "pollster",
"rune", "rune",
"walkdir",
"wgpu", "wgpu",
"winit", "winit",
] ]

View File

@ -11,5 +11,6 @@ env_logger = "0.11.3"
log = "0.4.21" log = "0.4.21"
pollster = "0.3.0" pollster = "0.3.0"
rune = "0.13.2" rune = "0.13.2"
walkdir = "2.5.0"
wgpu = "0.19.3" wgpu = "0.19.3"
winit = "0.29.14" winit = "0.29.14"

View File

@ -43,7 +43,7 @@ impl<'w> Game<'w> {
.build(&event_loop)?; .build(&event_loop)?;
let context = pollster::block_on(Context::new(Arc::new(window), Limits::default()))?; 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 { Ok(Self {
event_loop, event_loop,

View File

@ -1,36 +1,28 @@
use std::sync::Arc; use std::{path::PathBuf, sync::Arc};
use anyhow::Result; use anyhow::Result;
use rune::{ use rune::{
termcolor::{ColorChoice, StandardStream}, termcolor::{ColorChoice, StandardStream},
Context, Diagnostics, Source, Sources, Vm, Context, Diagnostics, Source, Sources, Vm,
}; };
use walkdir::WalkDir;
use super::api; use super::api;
pub struct Runtime { pub struct Runtime {
pub vm: Vm, pub vm: Vm,
pub sources: Vec<String>, pub sources: Vec<PathBuf>,
} }
impl Runtime { impl Runtime {
pub fn new(source_paths: &[&str]) -> Result<Self> { pub fn new(source_dir: &str) -> Result<Self> {
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)?)?;
}
let mut context = Context::with_default_modules()?; let mut context = Context::with_default_modules()?;
context.install(api::log::module()?)?; context.install(api::log::module()?)?;
let runtime = Arc::new(context.runtime()?); let runtime = Arc::new(context.runtime()?);
let mut diagnostics = Diagnostics::new(); let mut diagnostics = Diagnostics::new();
let (mut sources, source_paths) = Self::get_sources(source_dir)?;
let unit = rune::prepare(&mut sources) let unit = rune::prepare(&mut sources)
.with_context(&context) .with_context(&context)
.with_diagnostics(&mut diagnostics) .with_diagnostics(&mut diagnostics)
@ -45,7 +37,23 @@ impl Runtime {
Ok(Self { Ok(Self {
vm, vm,
sources: full_source_paths, sources: source_paths,
}) })
} }
fn get_sources(source_dir: &str) -> Result<(Sources, Vec<PathBuf>)> {
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))
}
} }