Skip to content

XP event5x XPListening, games, ratings, comments and chat earn five times the XP

leftLeaderboard

juicewrld-desktopsrc-taurisrcpluginsscan.rs

1.4.2Released Nov 21, 2026

scan.rs

1,961 lines97.5 KBRust
//! Finding the plugins installed on this PC, in the folders folders.rs lists (the standard VST 3 places, the folders//! where VST 3 bundles also end up, FL Studio's, and the user's own) as the user has them ticked. A module is a `.vst3`//! bundle (a folder with Contents\x86_64-win\<name>.vst3 inside) or a single `.vst3` file (the older layout most//! vendors still ship).//!//! Scanning is gentle, because loading a copy-protected plugin (PACE/iLok and the like) can open its licence or//! activation window on the user's screen, and one with an expired licence opens it every time it loads://!//! - First the folders are only listed and files read from disk, which runs no plugin code (`listing`): every module//!   is compared with the cache by path, size and modification time; a bundle with Contents\Resources\moduleinfo.json is//!   read from that file and its DLL never loads; a module built for another kind of PC (32-bit, ARM) and an old-format//!   `.dll` (one exporting `VSTPluginMain` or `main`) are told apart by their file's headers (pe.rs), listed greyed and//!   never loaded.//! - Then anything new or changed that's left loads in a short-lived child process (`juicewrld-plugins --scan`), ONE AT//!   A TIME, never two at once. A child that hasn't answered within its time limit (usually because it's showing a//!   licence window) is killed, which closes that window, and the module is listed as "Needs activation, or didn't//!   load".//! - Every result is cached, failures included, and saved after each module: a later scan loads only modules that are//!   new or changed. A module that failed stays failed, unloaded, until its file changes or the user presses Try again.//!//! One module can hold many plugins (Waves' WaveShell holds hundreds), and one plugin can be installed twice (the same//! class ID in two modules): it's listed once, the newer version, with the other copies noted.use super::pe;use serde::{Deserialize, Serialize};use std::collections::{BTreeMap, HashMap, HashSet};use std::path::{Path, PathBuf};use std::sync::atomic::{AtomicBool, Ordering};use std::sync::Mutex;use std::time::{Duration, Instant, UNIX_EPOCH};/// 2: failures are remembered with what went wrong (`problem`) and never retried on their own. (Files checked from/// disk, `files`, came later as an optional part of the same format, so a cache from before them loses nothing.)pub const CACHE_VERSION: u32 = 2;/// Folders deeper than this under a plugin folder aren't looked into (vendors nest one or two levels).const MAX_DEPTH: usize = 6;/// No walk looks into more folders than this, so a whole drive added as a plugin folder can't keep it going for long.const MAX_DIRS: usize = 50_000;/// One class a module's factory offers, as the factory (or moduleinfo.json) describes it.#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default)]#[serde(rename_all = "camelCase", default)]pub struct Class {    pub cid: String,    pub category: String,    pub name: String,    pub vendor: String,    pub version: String,    pub sdk_version: String,    pub sub_categories: Vec<String>,    pub class_flags: u32,    pub cardinality: i32,}#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Default)]pub struct Stamp {    /// Milliseconds since 1970 of the newest of the module's files.    pub modified: u64,    pub size: u64,}/// Why a module couldn't be read.#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]#[serde(rename_all = "camelCase")]pub enum Problem {    /// It didn't answer within its time limit: almost always a licence or activation window waiting for the user.    Timeout,    /// It crashed as it loaded.    Crashed,    /// It loaded but said no (no factory, not a VST 3 module, a missing DLL of its own).    Failed,    /// It opened a window as it loaded and was ended a moment later (a licence or activation window, almost always).    Window,}#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default)]#[serde(rename_all = "camelCase")]pub struct Module {    pub stamp: Stamp,    /// "moduleinfo" (read from its moduleinfo.json), "loaded" (in a scan child), or "copy" (an older copy of a module    /// in another folder: not read, see `copy_of`).    pub source: String,    pub vendor: String,    pub classes: Vec<Class>,    /// What went wrong, in words, when it couldn't be read.    pub error: Option<String>,    pub problem: Option<Problem>,    /// For an older copy: the newer module of the same file name, the one read instead.    #[serde(default, skip_serializing_if = "Option::is_none")]    pub copy_of: Option<String>,}/// A file as its headers describe it, read from disk without loading it.#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]#[serde(rename_all = "camelCase")]pub struct FileCheck {    pub stamp: Stamp,    /// The machine it was built for (pe::X64 runs here; pe::X86 is 32-bit); 0 when it isn't a Windows module.    pub machine: u16,    /// "vst2" (a `.dll` exporting VSTPluginMain or main), "vst3" (a VST 3 module that can't run here), or "" (a `.dll`    /// that isn't a plugin: a helper library).    pub format: String,}#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default)]pub struct Cache {    pub version: u32,    pub modules: BTreeMap<String, Module>,    /// Every `.dll` in the folders and every module that can't run here, as checked from its file, by path.    #[serde(default)]    pub files: BTreeMap<String, FileCheck>,}/// What one scan did.#[derive(Serialize, Debug, Clone, Default, PartialEq)]#[serde(rename_all = "camelCase")]pub struct Stats {    pub modules: usize,    pub plugins: usize,    pub effects: usize,    pub instruments: usize,    /// Modules read this time (the rest came from the cache), and how many of those had to be loaded.    pub read: usize,    pub loaded: usize,    /// Modules that couldn't be read (this time or remembered from before).    pub failed: usize,    /// Plugins found that can't run here (old format, 32-bit, ARM): listed greyed, never loaded.    pub unsupported: usize,    /// The user stopped it before the end (what was read so far is kept).    pub stopped: bool,    pub millis: u64,    /// When it ended, milliseconds since 1970.    pub at: u64,}/// A plugin as the page sees it. Never a path: the page names plugins by `id` and nothing else.#[derive(Serialize, Debug, Clone, PartialEq)]#[serde(rename_all = "camelCase")]pub struct Plugin {    /// The class ID, 32 hex digits as moduleinfo.json writes it on Windows. The same plugin has the same ID on every    /// PC and in every version (VST 3 keeps it stable), so a mix saved with it opens wherever the plugin is installed.    pub id: String,    pub name: String,    pub vendor: String,    pub version: String,    /// "effect" (category Fx, what the studio's mixer slots take), "instrument", or "other".    pub kind: String,    pub categories: Vec<String>,    /// Made for surround (5.1 and up): a stereo mixer wants the plugin's stereo version instead.    pub surround: bool,    /// The module's file name, for telling two copies apart.    pub module: String,    pub sdk_version: String,}/// A plugin and where it comes from, for the app only.#[derive(Debug, Clone, PartialEq)]pub struct Found {    pub plugin: Plugin,    pub module: String,    pub cid: String,    /// The other modules with the same class ID (an older copy, the same plugin installed twice): never opened for it.    pub others: Vec<String>,}/// A module that couldn't be read, for the app's menu ("Needs activation, or didn't load", with Try again).#[derive(Debug, Clone, PartialEq)]pub struct Unread {    pub path: String,    /// The bundle or file name.    pub name: String,    pub problem: Problem,    pub error: String,}/// A plugin in the folders that can't run here, listed greyed so the user knows why it's missing. Never loaded.#[derive(Serialize, Debug, Clone, PartialEq)]#[serde(rename_all = "camelCase")]pub struct Unsupported {    /// For the app only: the page gets file names, never folders.    #[serde(skip)]    pub path: String,    /// The file's name without its extension, as the user knows the plugin.    pub name: String,    pub file: String,    /// "vst2" or "vst3".    pub format: String,    /// "x64", "x86" (32-bit), "arm64" or "arm".    pub arch: String,    /// "vst2" (the old format), "32-bit" or "arm".    pub reason: String,    /// A VST 3 version of it (a 64-bit one, for a 32-bit VST 3) is in the folders too.    pub replacement: bool,    /// Why, in words, as the menu shows it.    pub why: String,}/// Folders never looked in: FL Studio's own Fruity plugins (they're FL Studio's format and run only inside it) and/// Avid's AAX plugins (another format altogether).pub fn never_scanned(p: &Path) -> bool {    let n = format!("{}\\", p.to_string_lossy().replace('/', "\\").to_lowercase());    n.contains("\\plugins\\fruity\\") || n.contains("\\common files\\avid\\")}fn extension(p: &Path) -> String {    p.extension().map(|e| e.to_string_lossy().to_lowercase()).unwrap_or_default()}/// What a walk of the folders found. Only directory listings: nothing is opened, let alone loaded.#[derive(Debug, Default)]pub struct Walk {    /// Every `.vst3` module, each once, in a stable order.    pub modules: Vec<PathBuf>,    /// Every `.dll` (an old-format plugin, or a helper library: the listing tells them apart).    pub dlls: Vec<PathBuf>,    /// Folders looked into.    pub dirs: usize,    /// The walk stopped at MAX_DIRS folders.    pub capped: bool,}/// Walks the folders: into subfolders (vendors nest their bundles) down to MAX_DEPTH, never through a junction or a/// link to a folder (a loop can't trap it, nor lead it off the folders), never into a `.vst3` bundle (only its/// Contents are read later, for moduleinfo.json and its binary), an AAX bundle or a never-scanned folder.pub fn walk(roots: &[PathBuf]) -> Walk {    let mut modules = BTreeMap::new();    let mut dlls = BTreeMap::new();    let mut w = Walk::default();    let mut seen = HashSet::new();    for root in roots {        let key = root.to_string_lossy().replace('/', "\\").trim_end_matches('\\').to_lowercase();        if never_scanned(root) || !seen.insert(key) {            continue;        }        walk_dir(root, 0, &mut modules, &mut dlls, &mut w);    }    w.modules = modules.into_values().collect();    w.dlls = dlls.into_values().collect();    w}fn walk_dir(dir: &Path, depth: usize, modules: &mut BTreeMap<String, PathBuf>, dlls: &mut BTreeMap<String, PathBuf>, w: &mut Walk) {    if w.dirs >= MAX_DIRS {        w.capped = true;        return;    }    w.dirs += 1;    let Ok(entries) = std::fs::read_dir(dir) else { return };    for e in entries.flatten() {        let path = e.path();        let Ok(kind) = e.file_type() else { continue };        let ext = extension(&path);        if ext == "vst3" {            // A bundle (folder) or a single file: either is one module, and nothing inside a bundle is another. A link            // to one counts too: only its own Contents are read, so it can't lead the walk anywhere.            modules.insert(path.to_string_lossy().to_lowercase(), path);        } else if kind.is_symlink() {            continue;        } else if kind.is_dir() {            if depth < MAX_DEPTH && ext != "aaxplugin" && !never_scanned(&path) {                walk_dir(&path, depth + 1, modules, dlls, w);            }        } else if ext == "dll" {            dlls.insert(path.to_string_lossy().to_lowercase(), path);        }    }}/// Whether `path` is one of `roots` or inside one (compared without case, either kind of slash).pub fn under(path: &str, roots: &[PathBuf]) -> bool {    let norm = |p: &str| p.replace('/', "\\").trim_end_matches('\\').to_lowercase();    let p = norm(path);    roots.iter().any(|r| {        let r = norm(&r.to_string_lossy());        !r.is_empty() && (p == r || (p.len() > r.len() && p.starts_with(&r) && p.as_bytes()[r.len()] == b'\\'))    })}/// The part of a cache under `roots`: what the lists show while those are the folders ticked. The rest stays in the/// cache itself (a folder unticked for now, a drive unplugged): ticked again, its modules aren't loaded again, a/// module that needed activation least of all.pub fn only_under(cache: &Cache, roots: &[PathBuf]) -> Cache {    Cache {        version: cache.version,        modules: cache.modules.iter().filter(|(k, _)| under(k, roots)).map(|(k, m)| (k.clone(), m.clone())).collect(),        files: cache.files.iter().filter(|(k, _)| under(k, roots)).map(|(k, f)| (k.clone(), f.clone())).collect(),    }}/// Every module under the folders, each once, in a stable order (`walk`'s modules). For the tests; the app walks.#[allow(dead_code)]pub fn find_modules(roots: &[PathBuf]) -> Vec<PathBuf> {    walk(roots).modules}/// The DLL a module loads from.pub fn binary(module: &Path) -> PathBuf {    if module.is_dir() {        let name = module.file_name().unwrap_or_default();        module.join("Contents").join("x86_64-win").join(name)    } else {        module.to_path_buf()    }}pub fn moduleinfo_path(module: &Path) -> PathBuf {    module.join("Contents").join("Resources").join("moduleinfo.json")}fn modified_ms(m: &std::fs::Metadata) -> u64 {    m.modified().ok().and_then(|t| t.duration_since(UNIX_EPOCH).ok()).map_or(0, |d| d.as_millis() as u64)}fn now_ms() -> u64 {    std::time::SystemTime::now().duration_since(UNIX_EPOCH).map_or(0, |d| d.as_millis() as u64)}/// When the module last changed and how big its DLL is. None when there's no DLL for this PC (an ARM-only bundle).pub fn stamp(module: &Path) -> Option<Stamp> {    let bin = std::fs::metadata(binary(module)).ok()?;    if !bin.is_file() {        return None;    }    let mut modified = modified_ms(&bin);    if module.is_dir() {        for p in [module.to_path_buf(), moduleinfo_path(module)] {            if let Ok(m) = std::fs::metadata(&p) {                modified = modified.max(modified_ms(&m));            }        }    }    Some(Stamp { modified, size: bin.len() })}/// A single file's stamp: when it changed and its size.pub fn file_stamp(path: &Path) -> Option<Stamp> {    let m = std::fs::metadata(path).ok().filter(|m| m.is_file())?;    Some(Stamp { modified: modified_ms(&m), size: m.len() })}/// A bundle's binary for another kind of PC, when it has none for this one, with the machine its folder names:/// Contents\x86-win (32-bit) or an ARM folder. (An ARM64EC binary calls itself x64 in its header, so the folder says.)pub fn other_binary(module: &Path) -> Option<(PathBuf, u16)> {    if !module.is_dir() {        return None;    }    let name = module.file_name()?;    [("x86-win", pe::X86), ("arm64x-win", pe::ARM64), ("arm64ec-win", pe::ARM64), ("arm64-win", pe::ARM64), ("arm-win", pe::ARM)]        .into_iter()        .map(|(dir, machine)| (module.join("Contents").join(dir).join(name), machine))        .find(|(b, _)| b.is_file())}/// moduleinfo.json is JSON5 as the SDK writes it: trailing commas, sometimes comments. Strips both (outside strings)/// so a strict JSON parser takes it.pub fn relaxed_json(text: &str) -> String {    let chars: Vec<char> = text.trim_start_matches('\u{feff}').chars().collect();    let mut out = String::with_capacity(chars.len());    let mut i = 0;    let mut in_string = false;    while i < chars.len() {        let c = chars[i];        if in_string {            out.push(c);            if c == '\\' && i + 1 < chars.len() {                out.push(chars[i + 1]);                i += 2;                continue;            }            if c == '"' {                in_string = false;            }            i += 1;            continue;        }        match c {            '"' => {                in_string = true;                out.push(c);            }            '/' if chars.get(i + 1) == Some(&'/') => {                while i < chars.len() && chars[i] != '\n' {                    i += 1;                }                continue;            }            '/' if chars.get(i + 1) == Some(&'*') => {                i += 2;                while i + 1 < chars.len() && !(chars[i] == '*' && chars[i + 1] == '/') {                    i += 1;                }                i += 2;                continue;            }            ',' => {                // A comma with only whitespace (or comments) before the closing bracket goes.                let mut j = i + 1;                while j < chars.len() && chars[j].is_whitespace() {                    j += 1;                }                if !matches!(chars.get(j), Some('}') | Some(']')) {                    out.push(c);                }            }            _ => out.push(c),        }        i += 1;    }    out}/// A module's vendor and classes from its moduleinfo.json.pub fn parse_moduleinfo(text: &str) -> Result<(String, Vec<Class>), String> {    let v: serde_json::Value = serde_json::from_str(&relaxed_json(text)).map_err(|e| format!("moduleinfo.json: {e}"))?;    let s = |v: &serde_json::Value, k: &str| v.get(k).and_then(|x| x.as_str()).unwrap_or_default().to_string();    let vendor = v.get("Factory Info").map(|f| s(f, "Vendor")).unwrap_or_default();    let classes = v        .get("Classes")        .and_then(|c| c.as_array())        .ok_or("moduleinfo.json has no classes")?        .iter()        .map(|c| Class {            cid: s(c, "CID").to_ascii_uppercase(),            category: s(c, "Category"),            name: s(c, "Name"),            vendor: s(c, "Vendor"),            version: s(c, "Version"),            sdk_version: s(c, "SDKVersion"),            sub_categories: c                .get("Sub Categories")                .and_then(|x| x.as_array())                .map(|a| a.iter().filter_map(|x| x.as_str()).map(str::to_string).collect())                .unwrap_or_default(),            class_flags: c.get("Class Flags").and_then(|x| x.as_u64()).unwrap_or(0) as u32,            cardinality: c.get("Cardinality").and_then(|x| x.as_i64()).unwrap_or(0) as i32,        })        .collect();    Ok((vendor, classes))}/// A scan child's answer (host/main.rs `scan`).pub fn parse_loaded(text: &str) -> Result<(String, Vec<Class>), String> {    let line = text.lines().rev().find(|l| l.trim_start().starts_with('{')).ok_or("the scan printed nothing")?;    let v: serde_json::Value = serde_json::from_str(line).map_err(|e| format!("the scan's answer: {e}"))?;    if let Some(e) = v.get("error").and_then(|e| e.as_str()) {        return Err(e.to_string());    }    let vendor = v.get("vendor").and_then(|x| x.as_str()).unwrap_or_default().to_string();    let classes = v        .get("classes")        .and_then(|c| c.as_array())        .ok_or("the scan's answer has no classes")?        .iter()        .map(|c| {            let s = |k: &str| c.get(k).and_then(|x| x.as_str()).unwrap_or_default().to_string();            Class {                cid: s("cid").to_ascii_uppercase(),                category: s("category"),                name: s("name"),                vendor: s("vendor"),                version: s("version"),                sdk_version: s("sdkVersion"),                sub_categories: s("subCategories").split('|').filter(|x| !x.is_empty()).map(str::to_string).collect(),                class_flags: c.get("classFlags").and_then(|x| x.as_u64()).unwrap_or(0) as u32,                cardinality: c.get("cardinality").and_then(|x| x.as_i64()).unwrap_or(0) as i32,            }        })        .collect();    Ok((vendor, classes))}/// How loading a module in a child went wrong.#[derive(Debug, Clone, PartialEq)]pub struct LoadError {    pub problem: Problem,    pub message: String,}impl LoadError {    fn new(problem: Problem, message: impl Into<String>) -> LoadError {        LoadError { problem, message: message.into() }    }}pub type Loaded = Result<(String, Vec<Class>), LoadError>;/// A scan child's own job object: the child and every process it starts (a licence checker that shows its window from/// a process of its own, say). Ending the job ends them all, so a window any of them showed goes with them; and the job/// ends when it's dropped, so no helper the plugin started outlives its scan. (It sits inside the app's own job, which/// ends everything when the app goes.)struct ChildJob(windows::Win32::Foundation::HANDLE);impl ChildJob {    fn new(child: &std::process::Child) -> Option<ChildJob> {        use std::os::windows::io::AsRawHandle;        use windows::Win32::Foundation::{CloseHandle, HANDLE};        use windows::Win32::System::JobObjects::{            AssignProcessToJobObject, CreateJobObjectW, JobObjectExtendedLimitInformation, SetInformationJobObject, JOBOBJECT_EXTENDED_LIMIT_INFORMATION,            JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE,        };        // SAFETY: a job of our own for a child we just started; the handle is closed when the ChildJob goes.        unsafe {            let job = CreateJobObjectW(None, None).ok()?;            let mut info = JOBOBJECT_EXTENDED_LIMIT_INFORMATION::default();            info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;            let _ = SetInformationJobObject(                job,                JobObjectExtendedLimitInformation,                &info as *const _ as *const std::ffi::c_void,                std::mem::size_of::<JOBOBJECT_EXTENDED_LIMIT_INFORMATION>() as u32,            );            if AssignProcessToJobObject(job, HANDLE(child.as_raw_handle())).is_err() {                let _ = CloseHandle(job);                return None;            }            Some(ChildJob(job))        }    }    /// The processes in the job now (the child and what it started).    fn pids(&self) -> Vec<u32> {        use windows::Win32::System::JobObjects::{JobObjectBasicProcessIdList, QueryInformationJobObject};        #[repr(C)]        struct List {            assigned: u32,            listed: u32,            ids: [usize; 64],        }        let mut l = List { assigned: 0, listed: 0, ids: [0; 64] };        // SAFETY: a buffer laid out as JOBOBJECT_BASIC_PROCESS_ID_LIST with room for 64 ids, its size given.        let ok = unsafe {            QueryInformationJobObject(Some(self.0), JobObjectBasicProcessIdList, &mut l as *mut _ as *mut std::ffi::c_void, std::mem::size_of::<List>() as u32, None)        };        if ok.is_err() {            return Vec::new();        }        l.ids[..(l.listed as usize).min(64)].iter().map(|&p| p as u32).collect()    }    /// The title of a visible top-level window of a process in the job, if there is one.    fn window(&self) -> Option<String> {        use windows::core::BOOL;        use windows::Win32::Foundation::{HWND, LPARAM};        use windows::Win32::UI::WindowsAndMessaging::{EnumWindows, GetWindowTextW, GetWindowThreadProcessId, IsWindowVisible};        struct Find {            pids: Vec<u32>,            title: Option<String>,        }        unsafe extern "system" fn each(hwnd: HWND, lparam: LPARAM) -> BOOL {            // SAFETY: lparam is the Find below, alive for the whole EnumWindows call; the rest are plain queries about a            // window EnumWindows handed us.            unsafe {                let f = &mut *(lparam.0 as *mut Find);                if IsWindowVisible(hwnd).as_bool() {                    let mut pid = 0u32;                    GetWindowThreadProcessId(hwnd, Some(&mut pid));                    if f.pids.contains(&pid) {                        let mut buf = [0u16; 256];                        let n = GetWindowTextW(hwnd, &mut buf).max(0) as usize;                        f.title = Some(String::from_utf16_lossy(&buf[..n]));                        return false.into();                    }                }            }            true.into()        }        let mut f = Find { pids: self.pids(), title: None };        if f.pids.is_empty() {            return None;        }        // SAFETY: the callback only reads the windows and writes into `f`, which outlives the call.        unsafe {            let _ = EnumWindows(Some(each), LPARAM(&mut f as *mut Find as isize));        }        f.title    }    /// Ends every process in the job at once.    fn end(&self) {        // SAFETY: our own job's handle.        unsafe {            let _ = windows::Win32::System::JobObjects::TerminateJobObject(self.0, 1);        }    }}impl Drop for ChildJob {    fn drop(&mut self) {        // SAFETY: our own handle, closed once; KILL_ON_JOB_CLOSE ends any helper the plugin left running.        unsafe {            let _ = windows::Win32::Foundation::CloseHandle(self.0);        }    }}/// Loads a module in a scan child as long as `limit` allows: `load_in_child_with` without the window rule (the user's/// Try again, which leaves them the time to deal with a plugin's window; and the tests of the limit itself).#[allow(dead_code)]pub fn load_in_child(exe: &Path, module: &Path, limit: Duration, stop: &AtomicBool) -> Loaded {    load_in_child_with(exe, module, limit, None, stop)}/// Loads a module in a scan child (`exe --scan <module>`, in a job object of its own) and waits for its answer. The/// child, and every process it started, is ended if it takes longer than `limit` (a licence window waiting for the user,/// usually: ending them closes it), as soon as `stop` is set, or, with `window`, that long after a window of theirs/// shows and stays (a licence or activation window, almost always: a scan the user didn't aim at this one module/// doesn't leave it on the screen). Logs the module's path as it starts, and the child's own lines.pub fn load_in_child_with(exe: &Path, module: &Path, limit: Duration, window: Option<Duration>, stop: &AtomicBool) -> Loaded {    use std::io::Read;    use std::os::windows::process::CommandExt;    use std::process::{Command, Stdio};    const CREATE_NO_WINDOW: u32 = 0x0800_0000;    let t0 = Instant::now();    let mut cmd = Command::new(exe);    cmd.arg("--scan").arg(module).stdin(Stdio::null()).stdout(Stdio::piped()).stderr(Stdio::piped()).creation_flags(CREATE_NO_WINDOW);    #[cfg(not(test))]    super::child_env(&mut cmd); // a debug test run's folders are the only ones the child may load from    let mut child = cmd.spawn().map_err(|e| LoadError::new(Problem::Failed, format!("couldn't start the scan: {e}")))?;    #[cfg(not(test))]    super::host::adopt(&child); // ends with the app, whatever happens to it    let job = ChildJob::new(&child);    eprintln!("[plugins] scan child {} loading {}", child.id(), module.display());    let out = child.stdout.take().ok_or_else(|| LoadError::new(Problem::Failed, "no output"))?;    let err = child.stderr.take().ok_or_else(|| LoadError::new(Problem::Failed, "no output"))?;    // Read as it comes, never waiting on the pipe's end: a helper process the plugin started can hold it open for good.    let pump = |mut from: Box<dyn Read + Send>| {        let text = std::sync::Arc::new(Mutex::new(Vec::new()));        let into = text.clone();        let thread = std::thread::spawn(move || {            let mut buf = [0u8; 8192];            while let Ok(n) = from.read(&mut buf) {                if n == 0 {                    break;                }                into.lock().unwrap_or_else(|e| e.into_inner()).extend_from_slice(&buf[..n]);            }        });        (text, thread)    };    let (out_text, out_thread) = pump(Box::new(out));    let (err_text, err_thread) = pump(Box::new(err));    let mut stopped = false;    // A window seen (when, its title), looked for every 100 ms; the load is ended for it once it has stayed `window`.    let mut seen: Option<(Instant, String)> = None;    let mut looked = Instant::now();    let mut window_ended: Option<String> = None;    let status = loop {        if let Ok(Some(status)) = child.try_wait() {            break Some(status);        }        stopped = stop.load(Ordering::SeqCst);        if let (Some(grace), Some(job)) = (window, &job) {            if looked.elapsed() >= Duration::from_millis(100) {                looked = Instant::now();                match (job.window(), &seen) {                    (Some(title), None) => {                        eprintln!("[plugins] {} opened a window as it loaded: {title:?}", module.display());                        seen = Some((Instant::now(), title));                    }                    (Some(_), Some((at, title))) if at.elapsed() >= grace => window_ended = Some(title.clone()),                    // It went (a splash screen, say): the load goes on.                    (None, Some(_)) => seen = None,                    _ => {}                }            }        }        if stopped || t0.elapsed() > limit || window_ended.is_some() {            // Ends the child, what it started and whatever window they showed; nothing waits on them beyond a moment.            if let Some(job) = &job {                job.end();            }            let _ = child.kill();            let _ = child.wait();            break None;        }        std::thread::sleep(Duration::from_millis(15));    };    let drained = Instant::now();    while !(out_thread.is_finished() && err_thread.is_finished()) && drained.elapsed() < Duration::from_millis(1500) {        std::thread::sleep(Duration::from_millis(10));    }    let text = String::from_utf8_lossy(&out_text.lock().unwrap_or_else(|e| e.into_inner())).into_owned();    let stderr = String::from_utf8_lossy(&err_text.lock().unwrap_or_else(|e| e.into_inner())).into_owned();    // The child's steps ("[stage] ...", host/main.rs `scan`): only the last one it started counts.    let stage = stderr.lines().rev().find_map(|l| l.strip_prefix("[stage] ")).map(str::to_string);    for line in stderr.lines().filter(|l| !l.trim().is_empty() && !l.starts_with("[stage] ")).take(20) {        eprintln!("[plugins]   child: {line}");    }    if let Some(s) = &stage {        eprintln!("[plugins]   child's last step: {s}");    }    let during = stage.as_deref().map(stage_words).unwrap_or_default();    drop(job);    let Some(status) = status else {        if let Some(title) = window_ended {            eprintln!("[plugins] ended {} at its window, {} ms in; its child was ended", module.display(), t0.elapsed().as_millis());            let title = if title.trim().is_empty() { String::new() } else { format!(" (\"{}\")", title.trim()) };            return Err(LoadError::new(Problem::Window, format!("it opened a window as it loaded{title}: a licence or activation window, usually")));        }        if stopped {            eprintln!("[plugins] scan stopped; ended the child loading {}", module.display());            return Err(LoadError::new(Problem::Timeout, "the scan was stopped while it loaded"));        }        eprintln!("[plugins] gave up on {} after {} s; its child was ended", module.display(), limit.as_secs());        let during = if during.is_empty() { String::new() } else { format!(", {during}") };        return Err(LoadError::new(            Problem::Timeout,            format!("it didn't answer within {} s (a licence or activation window, usually{during})", limit.as_secs()),        ));    };    let r = match parse_loaded(&text) {        // A whole answer stands whatever happened after it (a plugin's own thread ending the process, say).        Ok(r) => Ok(r),        Err(e) => Err(ended(&stderr, &text, status.code(), &during, e)),    };    match &r {        Ok((_, classes)) => eprintln!("[plugins] read {} ({} classes) in {} ms", module.display(), classes.len(), t0.elapsed().as_millis()),        Err(e) => eprintln!("[plugins] couldn't read {}: {}", module.display(), e.message),    }    r}/// A scan child's last step (host/main.rs `scan` and the shim's jw_module_load log each as it starts) in words.fn stage_words(stage: &str) -> String {    match stage.split_whitespace().collect::<Vec<_>>().as_slice() {        ["load"] => "while its DLL started (its start-up code, InitDll, GetPluginFactory)".into(),        ["context"] => "while it was given the host's context".into(),        ["class", i, "of", n] => format!("while listing its plugins, at {i} of {n}"),        ["done"] | ["answer"] => "after it listed its plugins".into(),        _ => format!("at its step \"{stage}\""),    }}fn file_name(path: &str) -> String {    path.rsplit(['\\', '/']).next().unwrap_or(path).to_string()}/// Why a scan child that ended without a whole answer didn't read its module: the error it printed, if it printed one;/// else a crash (its "[crash]" report, or an exit code that is an NTSTATUS error, a stack overflow's say); else the/// module ending the process itself (exit(), ExitProcess or TerminateProcess: the child never ends with a code but 0),/// with the modules the exit report found on the stack (the shim's, for ExitProcess) and the step it was at.fn ended(stderr: &str, text: &str, code: Option<i32>, during: &str, parse_error: String) -> LoadError {    if text.lines().any(|l| l.trim_start().starts_with('{')) {        return LoadError::new(Problem::Failed, parse_error);    }    let code = code.unwrap_or(0) as u32;    let during = if during.is_empty() { String::new() } else { format!(", {during}") };    if let Some(crash) = stderr.lines().find_map(|l| l.strip_prefix("[crash] ")) {        let v = serde_json::from_str::<serde_json::Value>(crash).ok();        let at = v.as_ref().and_then(|v| v["code"].as_str().map(str::to_string)).unwrap_or_else(|| "unknown".into());        let module = v.as_ref().and_then(|v| v["module"].as_str()).map(file_name).filter(|m| !m.is_empty());        let module = module.map(|m| format!(", in {m}")).unwrap_or_default();        return LoadError::new(Problem::Crashed, format!("it crashed as it loaded ({at}{module}{during})"));    }    if code == 0 {        return LoadError::new(Problem::Failed, parse_error);    }    let exit = stderr.lines().find_map(|l| l.strip_prefix("[exit] ")).and_then(|j| serde_json::from_str::<serde_json::Value>(j).ok());    if exit.is_none() && code & 0xF000_0000 == 0xC000_0000 {        return LoadError::new(Problem::Crashed, format!("it crashed as it loaded (exit code 0x{code:08X}{during})"));    }    let by = match &exit {        Some(v) => {            let modules: Vec<String> = v["modules"].as_array().into_iter().flatten().filter_map(|m| m.as_str()).map(file_name).take(3).collect();            match modules.split_first() {                Some((first, [])) => format!(", by {first}"),                Some((first, rest)) => format!(", by {first} (called from {})", rest.join(", ")),                None => ", through ExitProcess".to_string(),            }        }        None => String::new(),    };    LoadError::new(Problem::Failed, format!("it ended its own process as it loaded (exit code 0x{code:08X}{by}{during})"))}/// Where a scan is: `done` of `total` modules to load, the one loading now.#[derive(Serialize, Debug, Clone, Default, PartialEq)]#[serde(rename_all = "camelCase")]pub struct Progress {    pub done: usize,    pub total: usize,    /// The module's file name (never its folder).    pub module: String,}/// The first part of a scan, which loads nothing: the folders listed, each module compared with the cache, new and/// changed ones read from their moduleinfo.json where they have one, files checked from their headers. What's left to/// load is `to_load`.#[derive(Debug, Default)]pub struct Listing {    pub walk: Walk,    /// Modules unchanged since the last scan (failures included), and changed ones whose every plugin is hidden, by    /// path.    pub kept: BTreeMap<String, Module>,    /// New or changed modules read from their moduleinfo.json just now.    pub read: BTreeMap<String, Module>,    /// Older copies of modules in other folders (the same file name, an older file): noted, never loaded.    pub copies: BTreeMap<String, Module>,    /// New or changed modules (and those the user asked to try again) to load in a scan child, one at a time, in order.    pub to_load: Vec<(String, PathBuf, Stamp)>,    /// The last scan's entries for the modules in `to_load`, kept until each is read again, so a scan that stops (or an    /// app that closes) half way forgets nothing it knew, a module that needs activation least of all.    pub pending: BTreeMap<String, Module>,    /// Every `.dll` and every module that can't run here, as checked from its file.    pub files: BTreeMap<String, FileCheck>,    /// Files whose headers were read this time (the rest came from the cache).    pub checked: usize,    pub millis: u64,}/// What the user hid from the studio's list: plugins by class ID, vendors by name (compared without case). A module/// whose every plugin is hidden isn't loaded again when its file changes (the user hid it, often for the licence window/// it shows); its last entry stands until they show one of its plugins again.#[derive(Debug, Clone, Default)]pub struct Hidden {    pub plugins: Vec<String>,    pub vendors: Vec<String>,}impl Hidden {    /// Whether every plugin the module was last read with is hidden (false for a module with none, or never read).    fn all_of(&self, m: &Module) -> bool {        let mut audio = m.classes.iter().filter(|c| c.category == "Audio Module Class").peekable();        audio.peek().is_some()            && audio.all(|c| {                let vendor = if c.vendor.is_empty() { &m.vendor } else { &c.vendor };                self.plugins.iter().any(|id| id.eq_ignore_ascii_case(&c.cid)) || self.vendors.iter().any(|v| v.eq_ignore_ascii_case(vendor))            })    }}/// A path as the cache compares it: Windows paths are the same file whatever their case or slashes, so a folder spelt/// another way (the folder picker's "D:\VST", FL Studio's "d:\vst") finds what was learnt under the first spelling,/// and a remembered failure isn't loaded again for it.pub fn same_path(p: &str) -> String {    p.replace('/', "\\").to_lowercase()}/// Lists the folders and reads what can be read without loading anything (see `Listing`). `retry` names failed/// modules (paths, as the cache keys them) to load again although they haven't changed: the user pressed Try again./// (The app scans through `scan_saving`; this is the tests' and the dry run's way in.)#[allow(dead_code)]pub fn listing(roots: &[PathBuf], old: &Cache, retry: &HashSet<String>) -> Listing {    listing_hiding(roots, old, retry, &Hidden::default())}/// `listing`, leaving unloaded the changed modules whose every plugin the user hid (`hidden`).pub fn listing_hiding(roots: &[PathBuf], old: &Cache, retry: &HashSet<String>, hidden: &Hidden) -> Listing {    let t0 = Instant::now();    let mut l = Listing { walk: walk(roots), ..Default::default() };    let same_format = old.version == CACHE_VERSION;    let mut checked = 0;    // The last scan's entries by path compared without case (one spelt two ways keeps the one with a failure in it).    let mut old_modules: HashMap<String, &Module> = HashMap::new();    for (k, m) in &old.modules {        let slot = old_modules.entry(same_path(k)).or_insert(m);        if m.error.is_some() && slot.error.is_none() {            *slot = m;        }    }    let old_files: HashMap<String, &FileCheck> = old.files.iter().map(|(k, f)| (same_path(k), f)).collect();    let retry: HashSet<String> = retry.iter().map(|r| same_path(r)).collect();    let old_module = |key: &str| old_modules.get(&same_path(key)).copied();    let asked = |key: &str| retry.contains(&same_path(key));    // First what each module is, from its files alone: one built for another kind of PC (32-bit, ARM) is listed greyed    // and never loaded; the rest can run here.    let mut runnable: Vec<(String, PathBuf, Stamp)> = Vec::new();    for path in &l.walk.modules {        let key = path.to_string_lossy().to_string();        let Some(stamp) = stamp(path) else {            // No binary for this PC: a 32-bit or ARM-only bundle is listed greyed; anything else (a bundle for another            // system only) isn't a plugin here at all.            if let Some((bin, machine)) = other_binary(path) {                if let Some(s) = file_stamp(&bin) {                    l.files.insert(key, FileCheck { stamp: s, machine, format: "vst3".into() });                }            }            continue;        };        // Read, tried or noted as a copy before, as it is now: it runs here (it passed this check then). Otherwise its        // header says (read once per file version).        let known = old_module(&key).is_some_and(|m| m.stamp == stamp);        if !known {            let machine = match old_files.get(&same_path(&key)) {                Some(f) if f.stamp == stamp => Some(f.machine),                _ => {                    checked += 1;                    pe::machine(&binary(path)).ok()                }            };            if let Some(m) = machine.filter(|&m| m != pe::X64) {                l.files.insert(key, FileCheck { stamp, machine: m, format: "vst3".into() });                continue;            }        }        runnable.push((key, path.clone(), stamp));    }    // The same module file in two places (a vendor's older version left in another folder): only the newest is read;    // the others are noted as copies of it and never loaded, unless the newest one crashed or wouldn't load (the older    // may work), or the user asked for that copy (Try again).    let name_of = |p: &Path| p.file_name().map(|n| n.to_string_lossy().to_lowercase()).unwrap_or_default();    let mut newest: HashMap<String, usize> = HashMap::new();    for (i, (_, path, stamp)) in runnable.iter().enumerate() {        match newest.get(&name_of(path)) {            Some(&j) if runnable[j].2.modified >= stamp.modified => {}            _ => {                newest.insert(name_of(path), i);            }        }    }    for (i, (key, path, stamp)) in runnable.iter().enumerate() {        let n = newest[&name_of(path)];        if n != i && !asked(key) {            let (newer, _, newer_stamp) = &runnable[n];            let newer_broken = old_module(newer).is_some_and(|m| m.stamp == *newer_stamp && matches!(m.problem, Some(Problem::Crashed) | Some(Problem::Failed)));            if !newer_broken {                let mut copy = Module { stamp: *stamp, source: "copy".into(), copy_of: Some(newer.clone()), ..Default::default() };                // What was learnt of this very file before stays with it: a failure above all (a licence window), so it                // isn't loaded again the day the newer copy goes; and what a load read, so that day nothing loads.                if let Some(m) = old_module(key).filter(|m| m.stamp == *stamp) {                    if m.error.is_some() {                        (copy.error, copy.problem) = (m.error.clone(), m.problem);                    } else if m.source == "loaded" || (m.source == "copy" && !m.classes.is_empty()) {                        (copy.vendor, copy.classes) = (m.vendor.clone(), m.classes.clone());                    }                }                l.copies.insert(key.clone(), copy);                continue;            }        }        let (key, stamp) = (key.clone(), *stamp);        // Unchanged: as it was, failures included. A failure is kept even from an older cache format: it's never loaded        // again on its own, whatever changed in the app. An older copy that's the newest again (the newer one went)        // keeps what it remembered of itself: a failure, or what a load read.        if let Some(m) = old_module(&key) {            let remembered = m.copy_of.is_none() || m.error.is_some() || !m.classes.is_empty();            if m.stamp == stamp && remembered && (same_format || m.error.is_some()) && !(m.error.is_some() && asked(&key)) {                let mut m = m.clone();                if m.copy_of.take().is_some() {                    m.source = "loaded".into();                }                l.kept.insert(key, m);                continue;            }        }        // New or changed. Its moduleinfo.json, when it has a good one: read now, its DLL never loads.        if path.is_dir() {            if let Ok((vendor, classes)) = std::fs::read_to_string(moduleinfo_path(path)).map_err(|e| e.to_string()).and_then(|t| parse_moduleinfo(&t)) {                l.read.insert(key, Module { stamp, source: "moduleinfo".into(), vendor, classes, ..Default::default() });                continue;            }        }        // Changed, but every plugin in it is hidden (and the user didn't ask for it): not loaded, its last entry kept        // as it was (so the next check finds it changed again, and loads it once a plugin of it is shown again).        if let Some(m) = old_module(&key).filter(|m| m.copy_of.is_none() && !asked(&key) && hidden.all_of(m)) {            eprintln!("[plugins] not loading {}: every plugin in it is hidden", path.display());            l.kept.insert(key, m.clone());            continue;        }        if let Some(m) = old_module(&key).filter(|m| m.copy_of.is_none()) {            l.pending.insert(key.clone(), m.clone());        }        l.to_load.push((key, path.clone(), stamp));    }    for dll in &l.walk.dlls {        let key = dll.to_string_lossy().to_string();        let Some(stamp) = file_stamp(dll) else { continue };        let check = match old_files.get(&same_path(&key)) {            Some(f) if f.stamp == stamp => (*f).clone(),            _ => {                checked += 1;                match pe::read(dll) {                    Ok(info) => FileCheck { stamp, machine: info.machine, format: if info.exports_any(pe::VST2_ENTRIES) { "vst2".into() } else { String::new() } },                    Err(_) => FileCheck { stamp, machine: 0, format: String::new() },                }            }        };        l.files.insert(key, check);    }    l.checked = checked;    l.millis = t0.elapsed().as_millis() as u64;    l}/// Scans the folders, saving nothing (`scan_saving` without its checkpoints): the tests' way in.#[allow(dead_code)]pub fn scan(    roots: &[PathBuf],    old: &Cache,    retry: &HashSet<String>,    stop: &AtomicBool,    load: &mut dyn FnMut(&Path) -> Loaded,    progress: &mut dyn FnMut(&Progress),) -> (Cache, Stats) {    scan_saving(roots, old, retry, &Hidden::default(), stop, load, progress, &mut |_| {})}/// Scans the folders: `listing` first (nothing loaded), then the modules left to load, one at a time, in order./// `checkpoint` gets the cache as it stands after the listing and after each module loaded, to be saved, so what was/// learnt (a module that needs activation above all) is remembered even if the app closes half way. `stop` ends the/// scan between modules (and `load` ends the module under way); what was read so far is kept, and what wasn't is left/// for the next scan. A changed module whose every plugin is in `hidden` isn't loaded (see `Hidden`).#[allow(clippy::too_many_arguments)]pub fn scan_saving(    roots: &[PathBuf],    old: &Cache,    retry: &HashSet<String>,    hidden: &Hidden,    stop: &AtomicBool,    load: &mut dyn FnMut(&Path) -> Loaded,    progress: &mut dyn FnMut(&Progress),    checkpoint: &mut dyn FnMut(&Cache),) -> (Cache, Stats) {    let t0 = Instant::now();    let l = listing_hiding(roots, old, retry, hidden);    let mut cache = Cache { version: CACHE_VERSION, modules: l.kept, files: l.files };    // What's known of modules outside the folders scanned now (one unticked, a drive not plugged in) is kept as it is:    // listed again, unread, the day the folder is back. Inside them, the walk decides: a file that went drops out.    for (k, m) in old.modules.iter().filter(|(k, _)| !under(k, roots)) {        cache.modules.insert(k.clone(), m.clone());    }    for (k, f) in old.files.iter().filter(|(k, _)| !under(k, roots)) {        cache.files.insert(k.clone(), f.clone());    }    let read_now = l.read.len();    cache.modules.extend(l.read);    cache.modules.extend(l.copies);    let mut pending = l.pending;    let with_pending = |cache: &Cache, pending: &BTreeMap<String, Module>| {        let mut c = cache.clone();        for (k, m) in pending {            c.modules.entry(k.clone()).or_insert_with(|| m.clone());        }        c    };    checkpoint(&with_pending(&cache, &pending));    let total = l.to_load.len();    if total > 0 {        eprintln!(            "[plugins] scan: {total} modules to load, one at a time ({} unchanged, {read_now} read from moduleinfo.json, listed in {} ms)",            cache.modules.len() - read_now,            l.millis        );    }    let mut loaded = 0;    let mut stopped = false;    for (i, (key, path, stamp)) in l.to_load.iter().enumerate() {        if stop.load(Ordering::SeqCst) {            stopped = true;            break;        }        let name = path.file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_default();        progress(&Progress { done: i, total, module: name });        let m = match load(path) {            Ok((vendor, classes)) => Module { stamp: *stamp, source: "loaded".into(), vendor, classes, ..Default::default() },            Err(e) => Module { stamp: *stamp, source: "loaded".into(), error: Some(e.message), problem: Some(e.problem), ..Default::default() },        };        // A module the user stopped half way isn't judged: it's read again next time.        if stop.load(Ordering::SeqCst) && m.error.is_some() {            stopped = true;            break;        }        loaded += 1;        pending.remove(key);        cache.modules.insert(key.clone(), m);        checkpoint(&with_pending(&cache, &pending));    }    progress(&Progress { done: total, total, module: String::new() });    // Stopped: what wasn't read keeps what the last scan knew of it.    let cache = with_pending(&cache, &pending);    let found = plugins(&only_under(&cache, roots));    let stats = Stats {        modules: cache.modules.keys().filter(|k| under(k, roots)).count(),        plugins: found.len(),        effects: found.iter().filter(|f| f.plugin.kind == "effect").count(),        instruments: found.iter().filter(|f| f.plugin.kind == "instrument").count(),        read: read_now + loaded,        loaded,        failed: cache.modules.iter().filter(|(k, m)| m.error.is_some() && m.copy_of.is_none() && under(k, roots)).count(),        unsupported: unsupported(&only_under(&cache, roots)).len(),        stopped,        millis: t0.elapsed().as_millis() as u64,        at: now_ms(),    };    (cache, stats)}/// The modules that couldn't be read, by name. (Not an older copy that failed once: the newer one is used, and the/// copy's failure is only remembered.)pub fn unread(cache: &Cache) -> Vec<Unread> {    let mut out: Vec<Unread> = cache        .modules        .iter()        .filter(|(_, m)| m.copy_of.is_none())        .filter_map(|(path, m)| {            let error = m.error.clone()?;            Some(Unread {                path: path.clone(),                name: Path::new(path).file_name().map(|f| f.to_string_lossy().to_string()).unwrap_or_default(),                problem: m.problem.unwrap_or(Problem::Failed),                error,            })        })        .collect();    out.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase()));    out}/// Last words of a name naming a surround layout, as Waves and others name their variants ("C1 comp 5.1").fn surround_name(name: &str) -> bool {    let last = name.rsplit(' ').next().unwrap_or_default().to_ascii_lowercase();    last.split('/').any(|part| {        matches!(part, "quad" | "lcr" | "lcrs")            || (part.len() >= 3 && part.as_bytes()[0].is_ascii_digit() && part.as_bytes()[0] >= b'4' && part.contains('.') && part.chars().all(|c| c.is_ascii_digit() || c == '.'))    })}fn version_key(v: &str) -> Vec<u64> {    v.split(|c: char| !c.is_ascii_digit()).filter(|x| !x.is_empty()).map(|x| x.parse().unwrap_or(0)).collect()}/// The plugins in a scan: audio module classes, each ID once (two copies of a plugin: the newer version, then the/// newer file; the others noted), effects first then instruments, by vendor and name.pub fn plugins(cache: &Cache) -> Vec<Found> {    let mut best: HashMap<String, (Found, Vec<u64>, u64)> = HashMap::new();    let mut copies: HashMap<String, Vec<String>> = HashMap::new();    // An older copy's classes (remembered from a load before it became one) are never listed: it isn't opened.    for (path, m) in cache.modules.iter().filter(|(_, m)| m.copy_of.is_none()) {        let file = Path::new(path).file_name().map(|f| f.to_string_lossy().to_string()).unwrap_or_default();        for c in &m.classes {            if c.category != "Audio Module Class" || c.cid.len() != 32 || !c.cid.bytes().all(|b| b.is_ascii_hexdigit()) {                continue;            }            let fx = c.sub_categories.iter().any(|s| s.eq_ignore_ascii_case("Fx"));            let instrument = c.sub_categories.iter().any(|s| s.eq_ignore_ascii_case("Instrument"));            let kind = if instrument { "instrument" } else if fx { "effect" } else { "other" };            let surround_tags = c.sub_categories.iter().any(|s| s.eq_ignore_ascii_case("Surround"))                && !c.sub_categories.iter().any(|s| s.eq_ignore_ascii_case("Stereo") || s.eq_ignore_ascii_case("Mono"));            let vendor = if c.vendor.is_empty() { m.vendor.clone() } else { c.vendor.clone() };            let found = Found {                plugin: Plugin {                    id: c.cid.clone(),                    name: c.name.clone(),                    vendor,                    version: c.version.clone(),                    kind: kind.into(),                    categories: c.sub_categories.clone(),                    surround: surround_tags || surround_name(&c.name),                    module: file.clone(),                    sdk_version: c.sdk_version.clone(),                },                module: path.clone(),                cid: c.cid.clone(),                others: Vec::new(),            };            let in_module = copies.entry(c.cid.clone()).or_default();            if !in_module.contains(path) {                in_module.push(path.clone());            }            let key = (version_key(&c.version), m.stamp.modified);            match best.get(&c.cid) {                Some((_, v, t)) if (v.clone(), *t) >= key => {}                _ => {                    best.insert(c.cid.clone(), (found, key.0, key.1));                }            }        }    }    let mut out: Vec<Found> = best        .into_values()        .map(|(mut f, _, _)| {            f.others = copies.remove(&f.cid).unwrap_or_default().into_iter().filter(|p| *p != f.module).collect();            // Older copies of its module, noted without being read.            for (path, m) in &cache.modules {                if m.copy_of.as_deref() == Some(f.module.as_str()) && !f.others.contains(path) {                    f.others.push(path.clone());                }            }            f        })        .collect();    let rank = |k: &str| match k {        "effect" => 0,        "instrument" => 1,        _ => 2,    };    out.sort_by(|a, b| {        (rank(&a.plugin.kind), a.plugin.vendor.to_lowercase(), a.plugin.name.to_lowercase())            .cmp(&(rank(&b.plugin.kind), b.plugin.vendor.to_lowercase(), b.plugin.name.to_lowercase()))    });    out}/// A plugin's name as compared with another's: its words in lower case, run together, less trailing words that only/// say which variant it is ("FabFilter Pro-C 3 (Mono)", "Delay_x64" and "Delay VST" all match "delay"'s VST 3 version).pub fn name_key(name: &str) -> String {    const VARIANT: &[&str] = &["mono", "stereo", "x64", "x86", "64", "32", "64bit", "32bit", "vst", "vst2", "vst3", "vsti"];    let mut words: Vec<String> = name.split(|c: char| !c.is_alphanumeric()).filter(|w| !w.is_empty()).map(str::to_lowercase).collect();    while words.len() > 1 && words.last().is_some_and(|w| VARIANT.contains(&w.as_str())) {        words.pop();    }    words.concat()}fn stem(path: &str) -> String {    Path::new(path).file_stem().map(|s| s.to_string_lossy().to_string()).unwrap_or_default()}/// The plugins in the folders that can't run here, by name: old-format `.dll`s ("VST 2, not supported: use its VST 3/// version", noting when one is installed), 32-bit and ARM modules. Read from their files; none is ever loaded.pub fn unsupported(cache: &Cache) -> Vec<Unsupported> {    // The names a VST 3 version could go by: every module's file name (read or not: one that needs activation is    // installed all the same) and every plugin's name, with its vendor's in front and without.    let mut vst3_names: HashSet<String> = HashSet::new();    for (path, m) in &cache.modules {        vst3_names.insert(name_key(&stem(path)));        for c in m.classes.iter().filter(|c| c.category == "Audio Module Class") {            let vendor = if c.vendor.is_empty() { &m.vendor } else { &c.vendor };            vst3_names.insert(name_key(&c.name));            vst3_names.insert(name_key(&format!("{vendor} {}", c.name)));        }    }    vst3_names.remove("");    let mut out: Vec<Unsupported> = cache        .files        .iter()        .filter(|(_, f)| f.format == "vst2" || (f.format == "vst3" && f.machine != pe::X64))        .map(|(path, f)| {            let name = stem(path);            let arch = match f.machine {                pe::X64 => "x64",                pe::X86 => "x86",                pe::ARM64 => "arm64",                _ => "arm",            };            let reason = match f.machine {                pe::X64 => "vst2",                pe::X86 => "32-bit",                _ => "arm",            };            let replacement = vst3_names.contains(&name_key(&name));            let mut why = match (reason, f.format.as_str()) {                ("vst2", _) => "VST 2, not supported: use its VST 3 version".to_string(),                ("32-bit", "vst2") => "32-bit VST 2, not supported".to_string(),                ("32-bit", _) => "32-bit, not supported".to_string(),                _ => "Built for ARM, not supported on this PC".to_string(),            };            if replacement {                why.push_str(if f.format == "vst2" { " (VST 3 version installed)" } else { " (64-bit version installed)" });            }            Unsupported {                path: path.clone(),                file: Path::new(path).file_name().map(|s| s.to_string_lossy().to_string()).unwrap_or_default(),                name,                format: f.format.clone(),                arch: arch.into(),                reason: reason.into(),                replacement,                why,            }        })        .collect();    out.sort_by(|a, b| (a.name.to_lowercase(), a.path.to_lowercase()).cmp(&(b.name.to_lowercase(), b.path.to_lowercase())));    out}/// A plugin the user hid, by itself or with its whole vendor (compared without case).pub fn is_hidden(p: &Plugin, plugins: &[String], vendors: &[String]) -> bool {    plugins.iter().any(|id| id.eq_ignore_ascii_case(&p.id)) || vendors.iter().any(|v| v.eq_ignore_ascii_case(&p.vendor))}#[cfg(test)]mod tests {    use super::*;    use std::sync::atomic::AtomicU32;    /// A moduleinfo.json in the form the SDK's moduleinfotool writes it (trailing commas and all), for a made-up module.    const INFO: &str = r#"{  "Name": "Example Width",  "Version": "2.1.1",  "Factory Info": {    "Vendor": "Example Audio",    "URL": "www.example.com",    "E-Mail": "[email protected]",    "Flags": {      "Unicode": true,      "Classes Discardable": false,      "Component Non Discardable": false,    },  },  "Classes": [    {      "CID": "ABCDEF019182FAEB0000000000000001",      "Category": "Audio Module Class",      "Name": "Example Width",      "Vendor": "Example Audio",      "Version": "2.1.1",      "SDKVersion": "VST 3.7.12",      "Sub Categories": [        "Fx",        "Mono",        "Spatial",        "Stereo",        "Surround",        "Tools",        "Up-Downmix",      ],      "Class Flags": 0,      "Cardinality": 2147483647,      "Snapshots": [      ],    },    {      "CID": "ABCDEF011234ABCD0000000000000002",      "Category": "Component Controller Class",      "Name": "Example Width",      "Vendor": "Example Audio",      "Version": "2.1.1",      "SDKVersion": "VST 3.7.12",      "Class Flags": 0,      "Cardinality": 2147483647,    },  ],}"#;    /// A folder of the tests' own, under JW_TEST_DIR when set (test runs on a PC with plugins keep everything in one    /// scratch folder), else the system's temp folder. Only fake modules go in it here: nothing is loaded.    fn tmp(name: &str) -> PathBuf {        let base = std::env::var_os("JW_TEST_DIR").map(PathBuf::from).unwrap_or_else(std::env::temp_dir);        let d = base.join(format!("jw-scan-{name}-{}", std::process::id()));        let _ = std::fs::remove_dir_all(&d);        std::fs::create_dir_all(&d).unwrap();        d    }    fn bundle(dir: &Path, name: &str, info: Option<&str>) -> PathBuf {        let b = dir.join(format!("{name}.vst3"));        std::fs::create_dir_all(b.join("Contents").join("x86_64-win")).unwrap();        std::fs::write(b.join("Contents").join("x86_64-win").join(format!("{name}.vst3")), b"MZ fake").unwrap();        if let Some(info) = info {            std::fs::create_dir_all(b.join("Contents").join("Resources")).unwrap();            std::fs::write(moduleinfo_path(&b), info).unwrap();        }        b    }    fn class(cid: &str, name: &str, subs: &[&str], version: &str) -> Class {        Class {            cid: cid.into(),            category: "Audio Module Class".into(),            name: name.into(),            vendor: "V".into(),            version: version.into(),            sub_categories: subs.iter().map(|s| s.to_string()).collect(),            ..Default::default()        }    }    fn no_progress(_: &Progress) {}    #[test]    fn moduleinfo_reads_as_the_sdk_writes_it() {        let (vendor, classes) = parse_moduleinfo(INFO).unwrap();        assert_eq!(vendor, "Example Audio");        assert_eq!(classes.len(), 2);        assert_eq!(classes[0].cid, "ABCDEF019182FAEB0000000000000001");        assert_eq!(classes[0].sub_categories, ["Fx", "Mono", "Spatial", "Stereo", "Surround", "Tools", "Up-Downmix"]);        assert_eq!(classes[0].cardinality, 2147483647);        assert_eq!(classes[1].category, "Component Controller Class");        // Comments, a byte order mark and commas inside strings are handled too.        let text = "\u{feff}{ // vendor\n \"Factory Info\": {\"Vendor\": \"a, b\",}, /* x */ \"Classes\": [],}";        assert_eq!(parse_moduleinfo(text).unwrap().0, "a, b");        assert!(parse_moduleinfo("{\"Classes\": 3}").is_err());        assert!(parse_moduleinfo("not json").is_err());        assert_eq!(relaxed_json(r#"{"a": "x\",]", "b": [1,2,],}"#), r#"{"a": "x\",]", "b": [1,2]}"#);    }    #[test]    fn scan_childrens_answers_parse_and_failures_say_why() {        let text = r#"noise a plugin printed{"classes":[{"cid":"0123456789abcdef0123456789abcdef","category":"Audio Module Class","name":"Example EQ","vendor":"Example Audio","version":"4.1.0.0","sdkVersion":"VST 3.7.9","subCategories":"Fx|EQ","classFlags":1,"cardinality":2147483647}],"vendor":"Example Audio"}"#;        let (vendor, classes) = parse_loaded(text).unwrap();        assert_eq!(vendor, "Example Audio");        assert_eq!(classes[0].cid, "0123456789ABCDEF0123456789ABCDEF");        assert_eq!(classes[0].sub_categories, ["Fx", "EQ"]);        assert_eq!(parse_loaded(r#"{"error":"LoadLibraryW failed"}"#).unwrap_err(), "LoadLibraryW failed");        assert!(parse_loaded("").is_err());    }    #[test]    fn modules_are_found_bundles_and_files_alike_and_nothing_inside_a_bundle() {        let d = tmp("find");        bundle(&d, "Example Width", Some(INFO));        let vendor = d.join("Example Audio");        std::fs::create_dir_all(&vendor).unwrap();        std::fs::write(vendor.join("Example EQ.vst3"), b"MZ").unwrap();        std::fs::write(vendor.join("readme.txt"), b"x").unwrap();        std::fs::write(d.join("Shared Library.dll"), b"MZ").unwrap();        // A bundle with a second module tucked inside it counts once.        let b = bundle(&d, "Outer", None);        std::fs::write(b.join("Contents").join("Inner.vst3"), b"MZ").unwrap();        // An ARM-only bundle has nothing to load here.        std::fs::create_dir_all(d.join("Arm.vst3").join("Contents").join("arm64-win")).unwrap();        let found = find_modules(&[d.clone(), d.join("missing")]);        let names: Vec<_> = found.iter().map(|p| p.file_name().unwrap().to_string_lossy().to_string()).collect();        assert_eq!(names, ["Arm.vst3", "Example EQ.vst3", "Example Width.vst3", "Outer.vst3"]);        assert!(stamp(&d.join("Arm.vst3")).is_none());        assert_eq!(stamp(&vendor.join("Example EQ.vst3")).unwrap().size, 2);        // The same folder given twice (or in another case) is scanned once.        assert_eq!(find_modules(&[d.clone(), PathBuf::from(d.to_string_lossy().to_uppercase())]).len(), 4);        let _ = std::fs::remove_dir_all(&d);    }    /// The scan's memory: unchanged modules are never read again, failures included, until their file changes or the    /// user asks for that one module again; and modules load one at a time.    #[test]    fn the_cache_spares_modules_and_failures_stay_failed_until_asked() {        let d = tmp("cache");        let info = bundle(&d, "Example Width", Some(INFO));        let single = d.join("Single.vst3");        std::fs::write(&single, b"MZ one").unwrap();        let locked = d.join("Locked.vst3");        std::fs::write(&locked, b"MZ").unwrap();        let loads = AtomicU32::new(0);        let at_once = AtomicU32::new(0);        let most = AtomicU32::new(0);        let stop = AtomicBool::new(false);        let mut load = |p: &Path| -> Loaded {            loads.fetch_add(1, Ordering::SeqCst);            let n = at_once.fetch_add(1, Ordering::SeqCst) + 1;            most.fetch_max(n, Ordering::SeqCst);            std::thread::sleep(Duration::from_millis(5));            at_once.fetch_sub(1, Ordering::SeqCst);            if p.ends_with("Locked.vst3") {                return Err(LoadError::new(Problem::Timeout, "it didn't answer within 30 s"));            }            Ok(("Me".into(), vec![class("0123456789ABCDEF0123456789ABCDEF", "Single", &["Fx"], "1.0")]))        };        let mut seen = Vec::new();        let (cache, stats) = scan(&[d.clone()], &Cache::default(), &HashSet::new(), &stop, &mut load, &mut |p| seen.push(p.clone()));        assert_eq!((stats.modules, stats.read, stats.loaded, stats.failed), (3, 3, 2, 1));        assert_eq!(loads.load(Ordering::SeqCst), 2, "the moduleinfo.json module wasn't loaded");        assert_eq!(most.load(Ordering::SeqCst), 1, "one module at a time");        assert_eq!(stats.plugins, 2);        // Progress counts the modules to load: the moduleinfo.json one never is.        assert_eq!(seen.first().map(|p| (p.done, p.total)), Some((0, 2)));        assert_eq!(seen.last().map(|p| (p.done, p.total)), Some((2, 2)));        let key = |p: &Path| p.to_string_lossy().to_string();        assert_eq!(cache.modules[&key(&locked)].problem, Some(Problem::Timeout));        assert_eq!(cache.modules[&key(&info)].source, "moduleinfo");        let u = unread(&cache);        assert_eq!((u.len(), u[0].name.as_str(), u[0].problem), (1, "Locked.vst3", Problem::Timeout));        // Nothing changed: nothing is loaded again, not even the module that failed.        let (again, stats) = scan(&[d.clone()], &cache, &HashSet::new(), &stop, &mut load, &mut no_progress);        assert_eq!((stats.read, stats.failed, loads.load(Ordering::SeqCst)), (0, 1, 2));        assert_eq!(again.modules, cache.modules);        // Try again on that one module: it alone is loaded.        let retry: HashSet<String> = [key(&locked)].into();        let (_, stats) = scan(&[d.clone()], &cache, &retry, &stop, &mut load, &mut no_progress);        assert_eq!((stats.read, loads.load(Ordering::SeqCst)), (1, 3));        // Try again on a module that's fine does nothing.        let (_, stats) = scan(&[d.clone()], &cache, &[key(&single)].into(), &stop, &mut load, &mut no_progress);        assert_eq!((stats.read, loads.load(Ordering::SeqCst)), (0, 3));        // A module that changed is read again (the failed one too: an update may have fixed it); one that went is gone.        std::fs::write(&single, b"MZ two, longer").unwrap();        std::fs::write(&locked, b"MZ updated").unwrap();        let (cache2, stats) = scan(&[d.clone()], &cache, &HashSet::new(), &stop, &mut load, &mut no_progress);        assert_eq!((stats.read, loads.load(Ordering::SeqCst)), (2, 5));        std::fs::remove_file(&locked).unwrap();        let (cache3, stats) = scan(&[d.clone()], &cache2, &HashSet::new(), &stop, &mut load, &mut no_progress);        assert_eq!((stats.read, stats.modules, stats.failed), (0, 2, 0));        // The cache survives a trip to disk and back, and an old format is ignored (read afresh).        let text = serde_json::to_string(&cache3).unwrap();        assert_eq!(serde_json::from_str::<Cache>(&text).unwrap(), cache3);        let mut old = cache3.clone();        old.version = 1;        let (_, stats) = scan(&[d.clone()], &old, &HashSet::new(), &stop, &mut load, &mut no_progress);        assert_eq!(stats.read, 2);        let _ = std::fs::remove_dir_all(&d);    }    #[test]    fn a_stopped_scan_keeps_what_it_read_and_judges_nothing_it_didnt_finish() {        let d = tmp("stop");        for n in ["A", "B", "C"] {            std::fs::write(d.join(format!("{n}.vst3")), b"MZ").unwrap();        }        let stop = AtomicBool::new(false);        let mut n = 0;        let mut load = |p: &Path| -> Loaded {            n += 1;            if p.ends_with("B.vst3") {                // The user stops the scan while B is loading: B's child is ended.                stop.store(true, Ordering::SeqCst);                return Err(LoadError::new(Problem::Timeout, "the scan was stopped while it loaded"));            }            Ok(("Me".into(), vec![]))        };        let (cache, stats) = scan(&[d.clone()], &Cache::default(), &HashSet::new(), &stop, &mut load, &mut no_progress);        assert!(stats.stopped);        assert_eq!(cache.modules.len(), 1, "A only: B wasn't judged, C wasn't reached");        assert!(cache.modules.keys().next().unwrap().ends_with("A.vst3"));        let _ = std::fs::remove_dir_all(&d);    }    #[test]    fn plugins_are_listed_once_effects_first_instruments_marked() {        let mut cache = Cache { version: CACHE_VERSION, ..Default::default() };        let m = |classes: Vec<Class>, modified: u64| Module {            stamp: Stamp { modified, size: 1 },            source: "loaded".into(),            vendor: "Factory Vendor".into(),            classes,            ..Default::default()        };        let a = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";        cache.modules.insert(            r"D:\Plugins\Old\Tune.vst3".into(),            m(vec![class(a, "Tune", &["Fx", "Pitch Shift"], "9.1.0"), class("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB", "Keys", &["Instrument", "Synth"], "1")], 5),        );        cache.modules.insert(r"D:\Plugins\Tune.vst3".into(), m(vec![class(a, "Tune", &["Fx", "Pitch Shift"], "10.3.1")], 1));        let mut shell = vec![            class("CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC", "Comp Stereo", &["Fx", "Dynamics"], "17"),            class("DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD", "Comp 5.1", &["Fx", "Dynamics"], "17"),            class("EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE", "Wrapper 7.1.4", &["Fx", "Surround"], "17"),            class("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "Center Mono/Stereo", &["Fx", "Imaging"], "17"),        ];        // Controllers and other classes aren't plugins; nor is a class ID that isn't one.        shell.push(Class { category: "Component Controller Class".into(), ..class("1111111111111111111111111111111A", "Comp ctl", &[], "1") });        shell.push(class("not hex", "Odd", &["Fx"], "1"));        cache.modules.insert(r"D:\Plugins\Shell.vst3".into(), m(shell, 2));        let found = plugins(&cache);        let ids: Vec<_> = found.iter().map(|f| (f.plugin.name.as_str(), f.plugin.kind.as_str(), f.plugin.surround)).collect();        assert_eq!(            ids,            [                ("Center Mono/Stereo", "effect", false),                ("Comp 5.1", "effect", true),                ("Comp Stereo", "effect", false),                ("Tune", "effect", false),                ("Wrapper 7.1.4", "effect", true),                ("Keys", "instrument", false),            ]        );        // The newer version wins over the newer file, and the other copy is noted.        let tune = found.iter().find(|f| f.cid == a).unwrap();        assert_eq!((tune.plugin.version.as_str(), tune.module.as_str()), ("10.3.1", r"D:\Plugins\Tune.vst3"));        assert_eq!(tune.others, [r"D:\Plugins\Old\Tune.vst3"]);        assert!(found.iter().filter(|f| f.cid != a).all(|f| f.others.is_empty()));        assert_eq!(tune.plugin.module, "Tune.vst3");        // A class without a vendor of its own takes the factory's.        assert_eq!(tune.plugin.vendor, "V");    }    #[test]    fn hidden_plugins_and_vendors() {        let p = Plugin {            id: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".into(),            name: "Tune".into(),            vendor: "Example Audio".into(),            version: "1".into(),            kind: "effect".into(),            categories: vec![],            surround: false,            module: "Tune.vst3".into(),            sdk_version: String::new(),        };        assert!(!is_hidden(&p, &[], &[]));        assert!(is_hidden(&p, &["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".into()], &[]));        assert!(is_hidden(&p, &[], &["example audio".into()]));        assert!(!is_hidden(&p, &["BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB".into()], &["Example".into()]));    }    #[test]    fn surround_names() {        for s in ["Comp 5.1", "Wrapper 7.1.4", "Thing 5.0.2", "X Mono/5.0", "Y Stereo/5.1", "Z Quad"] {            assert!(surround_name(s), "{s}");        }        for s in ["Comp Stereo", "Center Mono/Stereo", "Example EQ 4", "Tune Pro", "Saturator 2", "V 3.5"] {            assert!(!surround_name(s), "{s}");        }    }    /// The walk: into vendors' subfolders, never through a junction (a loop to its own parent included), never into    /// FL Studio's Fruity folder or an AAX bundle, not past its depth limit; `.dll`s are collected for the listing.    #[test]    fn the_walk_goes_into_subfolders_but_never_through_links_or_into_other_formats() {        let d = tmp("walk");        // Seven folders down: one past the limit.        let deep = d.join("a").join("b").join("c").join("d").join("e").join("f").join("g");        std::fs::create_dir_all(&deep).unwrap();        std::fs::write(d.join("a").join("b").join("Nested.vst3"), b"MZ").unwrap();        std::fs::write(deep.join("Too Deep.vst3"), b"MZ").unwrap();        std::fs::write(d.join("a").join("Old.dll"), b"MZ").unwrap();        let fruity = d.join("FL").join("Plugins").join("Fruity").join("Effects");        std::fs::create_dir_all(&fruity).unwrap();        std::fs::write(fruity.join("Fruity Thing_x64.dll"), b"MZ").unwrap();        let aax = d.join("Comp.aaxplugin").join("Contents").join("x64");        std::fs::create_dir_all(&aax).unwrap();        std::fs::write(aax.join("Comp.dll"), b"MZ").unwrap();        // A junction back to the folder itself (a loop) and one to a folder elsewhere with a module in it.        let elsewhere = tmp("walk-elsewhere");        std::fs::write(elsewhere.join("Elsewhere.vst3"), b"MZ").unwrap();        for (link, target) in [(d.join("loop"), d.clone()), (d.join("away"), elsewhere.clone())] {            // mklink takes backslashes only (JW_TEST_DIR may come with forward ones).            let win = |p: &Path| p.to_string_lossy().replace('/', "\\");            let out = std::process::Command::new("cmd").arg("/C").arg("mklink").arg("/J").arg(win(&link)).arg(win(&target)).output().unwrap();            assert!(out.status.success(), "couldn't make a junction for the test: {}", String::from_utf8_lossy(&out.stdout));        }        let t0 = Instant::now();        let w = walk(&[d.clone()]);        let took = t0.elapsed();        // The links go first (a junction is removed as an empty folder, never what it points to), whatever comes next.        let _ = std::fs::remove_dir(d.join("loop"));        let _ = std::fs::remove_dir(d.join("away"));        assert!(elsewhere.join("Elsewhere.vst3").exists());        let names: Vec<_> = w.modules.iter().map(|p| p.file_name().unwrap().to_string_lossy().to_string()).collect();        assert_eq!(names, ["Nested.vst3"], "not too deep, not through a junction");        let dlls: Vec<_> = w.dlls.iter().map(|p| p.file_name().unwrap().to_string_lossy().to_string()).collect();        assert_eq!(dlls, ["Old.dll"], "no Fruity or AAX file");        assert!(!w.capped && took < Duration::from_secs(2));        // Six folders down is still looked into.        std::fs::write(deep.parent().unwrap().join("Six Down.vst3"), b"MZ").unwrap();        assert_eq!(walk(&[d.clone()]).modules.len(), 2);        let _ = std::fs::remove_dir_all(&d);        let _ = std::fs::remove_dir_all(&elsewhere);    }    /// Plugins that can't run here are told apart from their files' headers, listed greyed and never loaded: old-format    /// `.dll`s (noting when the VST 3 version is installed), 32-bit and ARM modules. A helper DLL isn't listed at all.    #[test]    fn plugins_that_cant_run_here_are_listed_from_their_files_and_never_loaded() {        use pe::tests::image;        let d = tmp("unsupported");        std::fs::write(d.join("Delay.vst3"), image(pe::X64, &[pe::VST3_ENTRY])).unwrap();        std::fs::write(d.join("Delay.dll"), image(pe::X64, &["VSTPluginMain"])).unwrap();        std::fs::write(d.join("Legacy (Mono).dll"), image(pe::X64, &["main"])).unwrap();        std::fs::write(d.join("Helper.dll"), image(pe::X64, &["InitLibrary"])).unwrap();        std::fs::write(d.join("Crusher.dll"), image(pe::X86, &["VSTPluginMain"])).unwrap();        std::fs::write(d.join("Tiny32.vst3"), image(pe::X86, &[pe::VST3_ENTRY])).unwrap();        let arm = d.join("Arm.vst3").join("Contents").join("arm64-win");        std::fs::create_dir_all(&arm).unwrap();        std::fs::write(arm.join("Arm.vst3"), image(pe::ARM64, &[pe::VST3_ENTRY])).unwrap();        let old32 = d.join("Old32.vst3").join("Contents").join("x86-win");        std::fs::create_dir_all(&old32).unwrap();        std::fs::write(old32.join("Old32.vst3"), image(pe::X86, &[pe::VST3_ENTRY])).unwrap();        let loads = std::cell::RefCell::new(Vec::new());        let mut load = |p: &Path| -> Loaded {            loads.borrow_mut().push(p.file_name().unwrap().to_string_lossy().to_string());            Ok(("Me".into(), vec![class("0123456789ABCDEF0123456789ABCDEF", "Delay", &["Fx"], "2.0")]))        };        let stop = AtomicBool::new(false);        let (cache, stats) = scan(&[d.clone()], &Cache::default(), &HashSet::new(), &stop, &mut load, &mut no_progress);        assert_eq!(*loads.borrow(), ["Delay.vst3"], "only the module that can run here loads");        assert_eq!((stats.modules, stats.plugins, stats.unsupported), (1, 1, 6));        let u = unsupported(&cache);        let got: Vec<_> = u.iter().map(|x| (x.file.as_str(), x.reason.as_str(), x.arch.as_str(), x.replacement, x.why.as_str())).collect();        assert_eq!(            got,            [                ("Arm.vst3", "arm", "arm64", false, "Built for ARM, not supported on this PC"),                ("Crusher.dll", "32-bit", "x86", false, "32-bit VST 2, not supported"),                ("Delay.dll", "vst2", "x64", true, "VST 2, not supported: use its VST 3 version (VST 3 version installed)"),                ("Legacy (Mono).dll", "vst2", "x64", false, "VST 2, not supported: use its VST 3 version"),                ("Old32.vst3", "32-bit", "x86", false, "32-bit, not supported"),                ("Tiny32.vst3", "32-bit", "x86", false, "32-bit, not supported"),            ]        );        // The page gets file names, never folders.        let json = serde_json::to_string(&u).unwrap();        assert!(!json.contains(&*d.to_string_lossy().replace('\\', "\\\\")) && !json.contains("\"path\""), "{json}");        // Scanned again: nothing is read from disk again (every file is in the cache), nothing loads.        let l = listing(&[d.clone()], &cache, &HashSet::new());        assert_eq!((l.checked, l.to_load.len()), (0, 0));        assert_eq!(l.files, cache.files);        let _ = std::fs::remove_dir_all(&d);    }    /// The quick check, as the app runs it at start-up and when the studio asks for the list: a module with a    /// moduleinfo.json is in the list before anything loads; what was learnt is handed over for saving after each    /// module; a new module loads, a changed one loads again, a removed one drops out, a remembered failure stays    /// unloaded, and Try again loads that one module.    #[test]    fn the_quick_check_adds_at_once_saves_as_it_goes_and_remembers_failures() {        let d = tmp("quick");        let stop = AtomicBool::new(false);        let loaded = std::cell::RefCell::new(Vec::<String>::new());        let mut load = |p: &Path| -> Loaded {            let name = p.file_name().unwrap().to_string_lossy().to_string();            loaded.borrow_mut().push(name.clone());            if name.starts_with("Locked") {                return Err(LoadError::new(Problem::Timeout, "it didn't answer within 30 s"));            }            // A class ID of its own for each module: its name's bytes in hex, padded to 32 digits.            let cid = format!("{:0<32}", name.bytes().map(|b| format!("{b:02X}")).collect::<String>());            Ok(("Me".into(), vec![class(&cid, &name, &["Fx"], "1")]))        };        std::fs::write(d.join("A.vst3"), b"MZ a").unwrap();        std::fs::write(d.join("Locked.vst3"), b"MZ locked").unwrap();        let (base, _) = scan(&[d.clone()], &Cache::default(), &HashSet::new(), &stop, &mut load, &mut no_progress);        assert_eq!(*loaded.borrow(), ["A.vst3", "Locked.vst3"]);        loaded.borrow_mut().clear();        // New: a bundle with a moduleinfo.json and two plain modules; A changed; nothing else.        let info = bundle(&d, "Example Width", Some(INFO));        std::fs::write(d.join("B.vst3"), b"MZ b").unwrap();        std::fs::write(d.join("C2.vst3"), b"MZ c").unwrap();        std::fs::write(d.join("A.vst3"), b"MZ a, updated").unwrap();        let mut checkpoints: Vec<Cache> = Vec::new();        let (after, stats) = scan_saving(&[d.clone()], &base, &HashSet::new(), &Hidden::default(), &stop, &mut load, &mut no_progress, &mut |c| checkpoints.push(c.clone()));        assert_eq!(*loaded.borrow(), ["A.vst3", "B.vst3", "C2.vst3"], "the changed and the new, not the remembered failure");        assert_eq!((stats.read, stats.loaded, stats.failed), (4, 3, 1));        // The first checkpoint comes before any load: the moduleinfo.json plugin is listed at once, the failure kept,        // and A still has its old entry until it's read again.        assert_eq!(checkpoints.len(), 4, "after the listing, then after each module");        let first = &checkpoints[0];        assert!(plugins(first).iter().any(|f| f.plugin.name == "Example Width"));        assert_eq!(first.modules[&d.join("Locked.vst3").to_string_lossy().to_string()].problem, Some(Problem::Timeout));        assert_eq!(first.modules[&d.join("A.vst3").to_string_lossy().to_string()], base.modules[&d.join("A.vst3").to_string_lossy().to_string()]);        assert!(!first.modules.contains_key(&d.join("B.vst3").to_string_lossy().to_string()));        assert_eq!(checkpoints.last().unwrap(), &after);        assert_eq!(after.modules[&info.to_string_lossy().to_string()].source, "moduleinfo");        loaded.borrow_mut().clear();        // Removed: it drops out with no load; the failure still isn't loaded.        std::fs::remove_file(d.join("B.vst3")).unwrap();        let (gone, stats) = scan(&[d.clone()], &after, &HashSet::new(), &stop, &mut load, &mut no_progress);        assert!(loaded.borrow().is_empty());        assert!(!gone.modules.contains_key(&d.join("B.vst3").to_string_lossy().to_string()));        assert_eq!((stats.modules, stats.failed), (4, 1));        // Try again: that module alone.        let retry: HashSet<String> = [d.join("Locked.vst3").to_string_lossy().to_string()].into();        let (_, stats) = scan(&[d.clone()], &gone, &retry, &stop, &mut load, &mut no_progress);        assert_eq!(*loaded.borrow(), ["Locked.vst3"]);        assert_eq!(stats.loaded, 1);        // A failure survives a cache from another format version: it's never loaded again on its own.        loaded.borrow_mut().clear();        let mut older = gone.clone();        older.version = 1;        let (_, _) = scan(&[d.clone()], &older, &HashSet::new(), &stop, &mut load, &mut no_progress);        assert!(!loaded.borrow().contains(&"Locked.vst3".to_string()), "{:?}", loaded.borrow());        let _ = std::fs::remove_dir_all(&d);    }    #[test]    fn names_match_their_vst3_versions() {        assert_eq!(name_key("FabFilter Pro-C 3 (Mono)"), name_key("FabFilter Pro-C 3"));        assert_eq!(name_key("Delay_x64"), "delay");        assert_eq!(name_key("Delay VST"), "delay");        assert_eq!(name_key("VST"), "vst", "a name that's only a variant word stays");        assert_ne!(name_key("Pro-Q 4"), name_key("Pro-Q 3"));        assert_eq!(name_key("FabFilter Pro-Q 4"), name_key(&format!("{} {}", "FabFilter", "Pro-Q 4")));    }    /// The same module file in two folders (a vendor's older version left behind): the newest is read, the older is    /// noted as its copy and never loaded, unless the user asks for it (Try again) or the newest crashed or wouldn't    /// load (the older may work).    #[test]    fn an_older_copy_in_another_folder_is_noted_not_loaded() {        let d = tmp("copies");        std::fs::create_dir_all(d.join("Vendor")).unwrap();        let newer = d.join("Tune.vst3");        let older = d.join("Vendor").join("Tune.vst3");        std::fs::write(&newer, b"MZ new").unwrap();        std::fs::write(&older, b"MZ old").unwrap();        let set = |p: &Path, secs: u64| std::fs::File::options().write(true).open(p).unwrap().set_modified(UNIX_EPOCH + Duration::from_secs(secs)).unwrap();        set(&older, 1_600_000_000);        set(&newer, 1_700_000_000);        let key = |p: &Path| p.to_string_lossy().to_string();        let tune = "0123456789ABCDEF0123456789ABCDEF";        let loaded = std::cell::RefCell::new(Vec::<String>::new());        let crash = std::cell::Cell::new(false);        let mut load = |p: &Path| -> Loaded {            loaded.borrow_mut().push(key(p));            if crash.get() && p == newer {                return Err(LoadError::new(Problem::Crashed, "it crashed as it loaded (0xC0000005)"));            }            Ok(("Me".into(), vec![class(tune, "Tune", &["Fx"], if p == newer { "2.0" } else { "1.0" })]))        };        let stop = AtomicBool::new(false);        let (cache, _) = scan(&[d.clone()], &Cache::default(), &HashSet::new(), &stop, &mut load, &mut no_progress);        assert_eq!(*loaded.borrow(), [key(&newer)], "the older copy isn't loaded");        assert_eq!(cache.modules[&key(&older)].copy_of, Some(key(&newer)));        let found = plugins(&cache);        assert_eq!((found.len(), found[0].plugin.version.as_str()), (1, "2.0"));        assert_eq!(found[0].others, [key(&older)], "the other copy is noted");        assert!(unread(&cache).is_empty());        // Again: nothing loads.        loaded.borrow_mut().clear();        let (cache, _) = scan(&[d.clone()], &cache, &HashSet::new(), &stop, &mut load, &mut no_progress);        assert!(loaded.borrow().is_empty());        // Try again on the older copy: it alone loads; still one plugin, the newer version.        let (tried, _) = scan(&[d.clone()], &cache, &[key(&older)].into(), &stop, &mut load, &mut no_progress);        assert_eq!(*loaded.borrow(), [key(&older)]);        let found = plugins(&tried);        assert_eq!((found.len(), found[0].plugin.version.as_str(), found[0].others.clone()), (1, "2.0", vec![key(&older)]));        // The newer one changes and now crashes as it loads: the next scan reads the older copy after all.        loaded.borrow_mut().clear();        crash.set(true);        std::fs::write(&newer, b"MZ new, broken").unwrap();        set(&newer, 1_800_000_000);        let (broken, _) = scan(&[d.clone()], &cache, &HashSet::new(), &stop, &mut load, &mut no_progress);        assert_eq!(*loaded.borrow(), [key(&newer)]);        loaded.borrow_mut().clear();        let (fallback, _) = scan(&[d.clone()], &broken, &HashSet::new(), &stop, &mut load, &mut no_progress);        assert_eq!(*loaded.borrow(), [key(&older)], "the older copy is read once the newer one is broken");        assert_eq!(plugins(&fallback)[0].plugin.version, "1.0");        let _ = std::fs::remove_dir_all(&d);    }    /// A folder unticked (or a drive unplugged) for a while: what was known of its modules stays in the cache, out of    /// the lists; ticked again, nothing in it loads again, the module that needed activation least of all.    #[test]    fn a_folder_unticked_for_a_while_is_remembered() {        let d = tmp("away");        let (a, b) = (d.join("A"), d.join("B"));        std::fs::create_dir_all(&a).unwrap();        std::fs::create_dir_all(&b).unwrap();        std::fs::write(a.join("Fine.vst3"), b"MZ fine").unwrap();        std::fs::write(b.join("Locked.vst3"), b"MZ locked").unwrap();        let loads = std::cell::RefCell::new(Vec::<String>::new());        let mut load = |p: &Path| -> Loaded {            loads.borrow_mut().push(p.file_name().unwrap().to_string_lossy().to_string());            if p.ends_with("Locked.vst3") {                return Err(LoadError::new(Problem::Window, "it opened a window as it loaded"));            }            Ok(("Me".into(), vec![class("0123456789ABCDEF0123456789ABCDEF", "Fine", &["Fx"], "1")]))        };        let stop = AtomicBool::new(false);        let both = [a.clone(), b.clone()];        let (cache, _) = scan(&both, &Cache::default(), &HashSet::new(), &stop, &mut load, &mut no_progress);        assert_eq!(loads.borrow().len(), 2);        // B unticked: its failure leaves the lists, not the cache.        let (cache, stats) = scan(&[a.clone()], &cache, &HashSet::new(), &stop, &mut load, &mut no_progress);        assert_eq!((stats.modules, stats.failed), (1, 0));        assert!(unread(&only_under(&cache, &[a.clone()])).is_empty());        assert!(cache.modules.contains_key(&b.join("Locked.vst3").to_string_lossy().to_string()));        // Ticked again: nothing loads.        let (cache, stats) = scan(&both, &cache, &HashSet::new(), &stop, &mut load, &mut no_progress);        assert_eq!(loads.borrow().len(), 2, "{:?}", loads.borrow());        assert_eq!((stats.modules, stats.failed, stats.read), (2, 1, 0));        assert_eq!(unread(&cache).len(), 1);        assert!(under(r"X:\Plugins\a.vst3", &[PathBuf::from("x:/plugins/")]) && !under(r"X:\Plugins2\a.vst3", &[PathBuf::from(r"X:\Plugins")]));        let _ = std::fs::remove_dir_all(&d);    }    /// A module that needed activation, then became an older copy (a newer version went in beside it), then the newest    /// again (the newer one uninstalled): its file never changed, so it's never loaded again on its own.    #[test]    fn a_failure_is_remembered_while_the_module_is_an_older_copy() {        let d = tmp("copyfail");        let (a, b) = (d.join("A"), d.join("B"));        std::fs::create_dir_all(&a).unwrap();        std::fs::create_dir_all(&b).unwrap();        let old = a.join("Locked.vst3");        std::fs::write(&old, b"MZ old").unwrap();        let set = |p: &Path, secs: u64| std::fs::File::options().write(true).open(p).unwrap().set_modified(UNIX_EPOCH + Duration::from_secs(secs)).unwrap();        set(&old, 1_600_000_000);        let roots = [a.clone(), b.clone()];        let loads = std::cell::RefCell::new(Vec::<String>::new());        let mut load = |p: &Path| -> Loaded {            loads.borrow_mut().push(p.to_string_lossy().to_string());            if p.starts_with(&a) {                return Err(LoadError::new(Problem::Window, "it opened a window as it loaded"));            }            Ok(("Me".into(), vec![class("0123456789ABCDEF0123456789ABCDEF", "Locked", &["Fx"], "2")]))        };        let stop = AtomicBool::new(false);        let (c1, _) = scan(&roots, &Cache::default(), &HashSet::new(), &stop, &mut load, &mut no_progress);        let (c2, _) = scan(&roots, &c1, &HashSet::new(), &stop, &mut load, &mut no_progress);        assert_eq!(loads.borrow().len(), 1);        // A newer version goes in beside it: that one loads; the old one is its copy, its failure remembered, unlisted.        let newer = b.join("Locked.vst3");        std::fs::write(&newer, b"MZ newer").unwrap();        set(&newer, 1_700_000_000);        let (c3, _) = scan(&roots, &c2, &HashSet::new(), &stop, &mut load, &mut no_progress);        assert_eq!(*loads.borrow(), [old.to_string_lossy().to_string(), newer.to_string_lossy().to_string()]);        let rec = &c3.modules[&old.to_string_lossy().to_string()];        assert_eq!((rec.source.as_str(), rec.problem), ("copy", Some(Problem::Window)));        assert!(unread(&c3).is_empty(), "an older copy's failure isn't listed: the newer one is used");        // The newer one is uninstalled: the old one is the newest again, and still isn't loaded.        std::fs::remove_file(&newer).unwrap();        let (c4, stats) = scan(&roots, &c3, &HashSet::new(), &stop, &mut load, &mut no_progress);        assert_eq!(loads.borrow().len(), 2, "{:?}", loads.borrow());        assert_eq!((stats.failed, unread(&c4).len()), (1, 1));        assert_eq!(c4.modules[&old.to_string_lossy().to_string()].copy_of, None);        let _ = std::fs::remove_dir_all(&d);    }    /// The same folder spelt another way (the folder picker's spelling, then FL Studio's in lower case): nothing in it    /// loads again, a module that needed activation least of all.    #[test]    fn a_folder_spelt_another_way_loads_nothing_again() {        let d = tmp("Spelling");        let root = d.join("Plugins");        std::fs::create_dir_all(&root).unwrap();        std::fs::write(root.join("Locked.vst3"), b"MZ locked").unwrap();        std::fs::write(root.join("Fine.vst3"), b"MZ fine").unwrap();        std::fs::write(root.join("Old.dll"), pe::tests::image(pe::X64, &["VSTPluginMain"])).unwrap();        let loads = std::cell::RefCell::new(0);        let mut load = |p: &Path| -> Loaded {            *loads.borrow_mut() += 1;            if p.ends_with("Locked.vst3") {                return Err(LoadError::new(Problem::Timeout, "it didn't answer within 30 s"));            }            Ok(("Me".into(), vec![class("0123456789ABCDEF0123456789ABCDEF", "Fine", &["Fx"], "1")]))        };        let stop = AtomicBool::new(false);        let (c1, _) = scan(&[root.clone()], &Cache::default(), &HashSet::new(), &stop, &mut load, &mut no_progress);        assert_eq!(*loads.borrow(), 2);        let lower = PathBuf::from(root.to_string_lossy().to_lowercase().replace('\\', "/"));        let l = listing(&[lower.clone()], &c1, &HashSet::new());        assert_eq!((l.to_load.len(), l.checked), (0, 0), "nothing loads and no file is read again");        let (c2, stats) = scan(&[lower.clone()], &c1, &HashSet::new(), &stop, &mut load, &mut no_progress);        assert_eq!(*loads.borrow(), 2);        assert_eq!((stats.plugins, stats.failed, stats.unsupported), (1, 1, 1));        // Under one spelling only: the old spelling's entries didn't stay beside the new ones.        assert_eq!(c2.modules.len(), 2);        // Try again by the old spelling still finds it.        let retry: HashSet<String> = [root.join("Locked.vst3").to_string_lossy().to_string()].into();        let _ = scan(&[lower], &c2, &retry, &stop, &mut load, &mut no_progress);        assert_eq!(*loads.borrow(), 3);        let _ = std::fs::remove_dir_all(&d);    }    /// Hidden by the user: a changed module whose every plugin is hidden isn't loaded (its last entry stands); shown    /// again, it is.    #[test]    fn a_changed_module_whose_plugins_are_all_hidden_isnt_loaded() {        let d = tmp("hidden");        std::fs::write(d.join("Nag.vst3"), b"MZ nag").unwrap();        std::fs::write(d.join("Other.vst3"), b"MZ other").unwrap();        let loads = std::cell::RefCell::new(Vec::<String>::new());        let mut load = |p: &Path| -> Loaded {            let name = p.file_name().unwrap().to_string_lossy().to_string();            loads.borrow_mut().push(name.clone());            let (cid, vendor) = if name == "Nag.vst3" { ("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "Naggy Audio") } else { ("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB", "Fine Audio") };            // As most modules write it: the vendor in the factory, none in the class.            Ok((vendor.into(), vec![Class { vendor: String::new(), ..class(cid, &name, &["Fx"], "1") }]))        };        let stop = AtomicBool::new(false);        let (c1, _) = scan(&[d.clone()], &Cache::default(), &HashSet::new(), &stop, &mut load, &mut no_progress);        loads.borrow_mut().clear();        // Both change (an update); the user hid Naggy Audio.        std::fs::write(d.join("Nag.vst3"), b"MZ nag, updated").unwrap();        std::fs::write(d.join("Other.vst3"), b"MZ other, updated").unwrap();        let hidden = Hidden { plugins: vec![], vendors: vec!["naggy audio".into()] };        let (c2, _) = scan_saving(&[d.clone()], &c1, &HashSet::new(), &hidden, &stop, &mut load, &mut no_progress, &mut |_| {});        assert_eq!(*loads.borrow(), ["Other.vst3"]);        assert_eq!(c2.modules[&d.join("Nag.vst3").to_string_lossy().to_string()], c1.modules[&d.join("Nag.vst3").to_string_lossy().to_string()]);        // Again: still not loaded. Hidden by its ID instead: the same.        let by_id = Hidden { plugins: vec!["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa".into()], vendors: vec![] };        let (c3, _) = scan_saving(&[d.clone()], &c2, &HashSet::new(), &by_id, &stop, &mut load, &mut no_progress, &mut |_| {});        assert_eq!(loads.borrow().len(), 1);        // Shown again: the changed module loads now.        let (_, _) = scan(&[d.clone()], &c3, &HashSet::new(), &stop, &mut load, &mut no_progress);        assert_eq!(*loads.borrow(), ["Other.vst3", "Nag.vst3"]);        let _ = std::fs::remove_dir_all(&d);    }}