aboutsummaryrefslogtreecommitdiff
path: root/libs/bsa_ffi/src/archive/reader.rs
blob: 21b291c03d8f4122ebbd7091698e683b0330af0e (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//! BSA reading with parallel extraction

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

/// Entry for a file in a BSA archive
#[derive(Debug, Clone)]
pub struct BsaFileEntry {
    pub path: String,
}

/// List all files in a 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 BSA: {}", bsa_path.display()))?;

    let mut files = Vec::new();

    for (dir_key, folder) in archive.iter() {
        let dir_name = String::from_utf8_lossy(dir_key.name().as_bytes());

        for (file_key, _file) in folder.iter() {
            let file_name = String::from_utf8_lossy(file_key.name().as_bytes());

            // Build full path with backslash (BSA convention)
            let full_path = if dir_name.is_empty() || dir_name == "." {
                file_name.to_string()
            } else {
                format!("{}\\{}", dir_name, file_name)
            };

            files.push(BsaFileEntry { path: full_path });
        }
    }

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

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

    // Convert archive options to compression options (includes version info)
    let compression_options: FileCompressionOptions = (&options).into();

    // Normalize to backslashes and split
    let normalized = file_path.replace('/', "\\");
    let (dir_name, file_name) = if let Some(idx) = normalized.rfind('\\') {
        (&normalized[..idx], &normalized[idx + 1..])
    } else {
        ("", normalized.as_str())
    };

    // Search case-insensitively
    for (dir_key, folder) in archive.iter() {
        let current_dir = String::from_utf8_lossy(dir_key.name().as_bytes());

        if current_dir.eq_ignore_ascii_case(dir_name) {
            for (file_key, file) in folder.iter() {
                let current_file = String::from_utf8_lossy(file_key.name().as_bytes());

                if current_file.eq_ignore_ascii_case(file_name) {
                    // Extract with decompression if needed (uses version from archive options)
                    let data = if file.is_decompressed() {
                        file.as_bytes().to_vec()
                    } else {
                        file.decompress(&compression_options)?.as_bytes().to_vec()
                    };
                    return Ok(data);
                }
            }
        }
    }

    bail!(
        "File not found in BSA: {} (dir='{}', file='{}')",
        file_path,
        dir_name,
        file_name
    )
}

/// Extract multiple files from a BSA archive in a single parallel pass.
/// Opens the archive once, collects matching entries, then decompresses
/// and 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, options): (Archive, _) = Archive::read(bsa_path)
        .with_context(|| format!("Failed to open BSA: {}", bsa_path.display()))?;

    let compression_options: FileCompressionOptions = (&options).into();

    // Collect matching entries with references to file data
    let mut entries: Vec<(String, &BsaFile)> = Vec::new();
    for (dir_key, folder) in archive.iter() {
        let dir_name = String::from_utf8_lossy(dir_key.name().as_bytes());

        for (file_key, file) in folder.iter() {
            let file_name = String::from_utf8_lossy(file_key.name().as_bytes());

            let full_path = if dir_name.is_empty() || dir_name == "." {
                file_name.to_string()
            } else {
                format!("{}\\{}", dir_name, file_name)
            };

            let lookup = full_path.to_lowercase();
            if wanted.contains(&lookup) {
                entries.push((full_path, file));
            }
        }
    }

    // Decompress + write in parallel
    let extracted = AtomicUsize::new(0);
    entries
        .par_iter()
        .try_for_each(|(path, file)| -> Result<()> {
            let data = if file.is_decompressed() {
                file.as_bytes().to_vec()
            } else {
                file.decompress(&compression_options)?.as_bytes().to_vec()
            };

            callback(path, data)?;
            extracted.fetch_add(1, Ordering::Relaxed);
            Ok(())
        })?;

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