aboutsummaryrefslogtreecommitdiff
path: root/libs/bsa_ffi/src/archive/tes3_reader.rs
blob: c03f1559341d7f99455e875537c2def37c853ea1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//! TES3 (Morrowind) BSA reading

use anyhow::{bail, Context, Result};
use ba2::tes3::{Archive, File as Tes3File};
use ba2::{ByteSlice, Reader};
use rayon::prelude::*;
use std::collections::HashSet;
use std::path::Path;
use std::sync::atomic::{AtomicUsize, Ordering};
use tracing::debug;

use super::BsaFileEntry;

/// List all files in a TES3 (Morrowind) BSA archive
pub fn list_files(bsa_path: &Path) -> Result<Vec<BsaFileEntry>> {
    let archive: Archive = Archive::read(bsa_path)
        .with_context(|| format!("Failed to open TES3 BSA: {}", bsa_path.display()))?;

    let mut files = Vec::new();

    for (key, _file) in archive.iter() {
        let path = String::from_utf8_lossy(key.name().as_bytes()).to_string();

        files.push(BsaFileEntry { path });
    }

    debug!(
        "Listed {} files in TES3 BSA {}",
        files.len(),
        bsa_path.display()
    );
    Ok(files)
}

/// Extract a single file from a TES3 (Morrowind) BSA archive
#[allow(dead_code)]
pub fn extract_file(bsa_path: &Path, file_path: &str) -> Result<Vec<u8>> {
    let archive: Archive = Archive::read(bsa_path)
        .with_context(|| format!("Failed to open TES3 BSA: {}", bsa_path.display()))?;

    // Normalize path separators
    let normalized = file_path.replace('/', "\\");

    // Search case-insensitively
    for (key, file) in archive.iter() {
        let current_path = String::from_utf8_lossy(key.name().as_bytes());

        if current_path.eq_ignore_ascii_case(&normalized) {
            // TES3 BSAs are uncompressed, so just return the raw bytes
            return Ok(file.as_bytes().to_vec());
        }
    }

    bail!(
        "File not found in TES3 BSA: {} (looking for '{}')",
        bsa_path.display(),
        file_path
    )
}

/// Extract multiple files from a TES3 BSA archive in parallel.
/// Opens the archive once, collects matching entries, then writes
/// them in parallel using rayon.
/// `wanted` should contain lowercase backslash-separated paths.
pub fn extract_files_batch<F>(
    bsa_path: &Path,
    wanted: &HashSet<String>,
    callback: F,
) -> Result<usize>
where
    F: Fn(&str, Vec<u8>) -> Result<()> + Send + Sync,
{
    let archive: Archive = Archive::read(bsa_path)
        .with_context(|| format!("Failed to open TES3 BSA: {}", bsa_path.display()))?;

    // Collect matching entries
    let mut entries: Vec<(String, &Tes3File)> = Vec::new();
    for (key, file) in archive.iter() {
        let path = String::from_utf8_lossy(key.name().as_bytes()).to_string();
        let lookup = path.to_lowercase();
        if wanted.contains(&lookup) {
            entries.push((path, file));
        }
    }

    // Write in parallel
    let extracted = AtomicUsize::new(0);
    entries
        .par_iter()
        .try_for_each(|(path, file)| -> Result<()> {
            callback(path, file.as_bytes().to_vec())?;
            extracted.fetch_add(1, Ordering::Relaxed);
            Ok(())
        })?;

    let count = extracted.load(Ordering::Relaxed);
    debug!(
        "Batch extracted {} of {} wanted files from TES3 BSA {}",
        count,
        wanted.len(),
        bsa_path.display()
    );
    Ok(count)
}