From da39ae3aab567e4cfa3fa6ac150e6c2335176f45 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Fri, 21 Feb 2025 23:42:54 +0200 Subject: [PATCH] fix search paths --- crates/vestal/src/main.rs | 74 ++++++++++++++++++++++++++++----------- 1 file changed, 53 insertions(+), 21 deletions(-) diff --git a/crates/vestal/src/main.rs b/crates/vestal/src/main.rs index b14e145..c9083d1 100644 --- a/crates/vestal/src/main.rs +++ b/crates/vestal/src/main.rs @@ -6,12 +6,14 @@ mod link; mod bang; pub(crate) use self::util::*; -#[derive(Debug)] +#[derive(Debug, Default)] struct Rebuilder { - /// Paths visited - paths: BTreeSet>, + /// Search paths + paths: BTreeSet>, /// All DLLs in scope - dlls: BTreeMap, Arc>, + dlls: BTreeMap, Arc>, + /// Paths visited + visited: BTreeSet>, } #[derive(Debug)] @@ -53,13 +55,40 @@ struct Call { } impl Rebuilder { - fn new (path: &impl AsRef) -> Usually { - let mut build = Self { paths: Default::default(), dlls: Default::default() }; + fn new (paths: &[impl AsRef]) -> Self { + Self { + paths: paths.iter().map(|x|Arc::new(x.as_ref().into())).collect(), + ..Default::default() + } + } + fn load (&mut self, path: &impl AsRef, recurse: bool) -> Usually<()> { let path: Arc = Arc::from(PathBuf::from(path.as_ref())); - build.paths.insert(path.clone()); - let dll = Dll::new(&Arc::new(PathBuf::from(path.as_ref())), true)?; - build.dlls.insert(dll.name.clone(), dll); - Ok(build) + self.visited.insert(path.clone()); + let dll = Arc::new(Dll::new(&Arc::new(PathBuf::from(path.as_ref())), true)?); + self.dlls.insert(dll.name.clone(), dll.clone()); + if recurse { + for dep in dll.deps_by_library.keys() { + if let Some(dep) = self.find(dep, true)? { + println!("{dep:?}"); + } else { + panic!("not found: {dep:?}"); + } + } + } + Ok(()) + } + fn find (&self, name: &str, verbose: bool) -> Usually> { + for base in self.paths.iter() { + let mut path = base.as_ref().clone(); + path.push(name.to_lowercase()); + if verbose { + println!("looking for {name} at {path:?}"); + } + if std::fs::exists(&path)? { + return Ok(Some(canonicalize(&path)?)) + } + } + Ok(None) } } @@ -67,7 +96,7 @@ impl Dll { fn new ( path: &Arc, verbose: bool - ) -> Usually> { + ) -> Usually { println!("\n(load {BOLD}{path:?}{RESET})"); let name = path.file_name().expect("no file name"); let name: Arc = name.to_str().map(Arc::from).expect("non-unicode filename"); @@ -97,7 +126,7 @@ impl Dll { &mut calls_by_target, true )?; - Ok(Arc::new(Self { + Ok(Self { name: name.clone(), path: path.clone(), bang, @@ -107,7 +136,7 @@ impl Dll { deps_by_address: deps_by_address.clone(), calls_by_source, calls_by_target, - })) + }) } fn deps ( pe: &VecPE, @@ -315,18 +344,21 @@ fn main () -> Usually<()> { let matches = command!() .arg(arg!([path] "Path to VST DLL").value_parser(value_parser!(PathBuf))) .arg(arg!(-i --inspect "Show info, don't run").required(false)) - //.arg(arg!(-s --stub "Provide stub import").required(false)) + //.arg(arg!(-s --stub "Provide a stub import").required(false)) .get_matches(); if let Some(path) = matches.get_one::("path") { - let mut vestal = Vestal::default(); - vestal.search_paths.push(std::env::current_dir()?); - vestal.search_paths.push("/home/user/Lab/Cosmo/wineprefix/drive_c/windows/system32".into()); - for path in vestal.search_paths.iter() { + let mut rebuilder = Rebuilder::new(&[ + std::env::current_dir()?, + canonicalize(path.clone().parent().expect("invalid parent path"))?, + "/home/user/Lab/Cosmo/wineprefix/drive_c/windows/system32".into(), + ]); + println!(); + for path in rebuilder.paths.iter() { println!("(search {path:?})") } - if let Some(path) = vestal.resolve(path.to_str().expect("path must be unicode"))? { - let rebuilder = Rebuilder::new(&path)?; - //vestal.enter(&path)?; + if let Some(path) = rebuilder.find(path.to_str().expect("path must be unicode"), false)? { + let mut rebuilder = Rebuilder::default(); + rebuilder.load(&path, true)?; } else { panic!("Could not find: {path:?}") }