From 7ee008e150bc5bcf76082d726f719ee0fdfda982 Mon Sep 17 00:00:00 2001 From: SulfurNitride Date: Wed, 11 Feb 2026 02:37:39 -0600 Subject: Fluorine Manager: full Linux port of Mod Organizer 2 Complete native Linux port with FUSE-based virtual filesystem, Proton/umu-run integration, and Flatpak packaging. Key features: - FUSE VFS replacing Windows USVFS (in-process + standalone helper for Flatpak) - Proton/GE-Proton/umu-run launcher with env var forwarding - Flatpak support (sandbox-aware VFS, NXM handler, umu-run) - Wine prefix management UI - Case-insensitive path resolution for Linux filesystems - QSettings-safe INI handling (avoids Bethesda INI corruption) - Portable instance support with auto-generated launcher scripts Co-Authored-By: Claude Opus 4.6 --- libs/bsa_ffi/src/archive/ba2_reader.rs | 125 +++++++++ libs/bsa_ffi/src/archive/ba2_writer.rs | 336 ++++++++++++++++++++++++ libs/bsa_ffi/src/archive/mod.rs | 436 ++++++++++++++++++++++++++++++++ libs/bsa_ffi/src/archive/reader.rs | 154 +++++++++++ libs/bsa_ffi/src/archive/tes3_reader.rs | 104 ++++++++ libs/bsa_ffi/src/archive/writer.rs | 256 +++++++++++++++++++ libs/bsa_ffi/src/lib.rs | 325 ++++++++++++++++++++++++ 7 files changed, 1736 insertions(+) create mode 100644 libs/bsa_ffi/src/archive/ba2_reader.rs create mode 100644 libs/bsa_ffi/src/archive/ba2_writer.rs create mode 100644 libs/bsa_ffi/src/archive/mod.rs create mode 100644 libs/bsa_ffi/src/archive/reader.rs create mode 100644 libs/bsa_ffi/src/archive/tes3_reader.rs create mode 100644 libs/bsa_ffi/src/archive/writer.rs create mode 100644 libs/bsa_ffi/src/lib.rs (limited to 'libs/bsa_ffi/src') diff --git a/libs/bsa_ffi/src/archive/ba2_reader.rs b/libs/bsa_ffi/src/archive/ba2_reader.rs new file mode 100644 index 0000000..e6dd984 --- /dev/null +++ b/libs/bsa_ffi/src/archive/ba2_reader.rs @@ -0,0 +1,125 @@ +//! BA2 (Fallout 4/Starfield) archive reading +//! +//! Provides read support for FO4 format BA2 files (Fallout 4, Fallout 76, Starfield). + +use anyhow::{bail, Context, Result}; +use ba2::fo4::{Archive, File as Ba2File, FileWriteOptions}; +use ba2::prelude::*; +use ba2::ByteSlice; +use rayon::prelude::*; +use std::collections::HashSet; +use std::io::Cursor; +use std::path::Path; +use std::sync::atomic::{AtomicUsize, Ordering}; +use tracing::debug; + +/// Entry for a file in a BA2 archive +#[derive(Debug, Clone)] +pub struct Ba2FileEntry { + pub path: String, +} + +/// List all files in a BA2 archive +pub fn list_files(ba2_path: &Path) -> Result> { + let (archive, _options): (Archive, _) = Archive::read(ba2_path) + .with_context(|| format!("Failed to open BA2: {}", ba2_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(Ba2FileEntry { path }); + } + + debug!("Listed {} files in BA2 {}", files.len(), ba2_path.display()); + Ok(files) +} + +/// Extract a single file from a BA2 archive +#[allow(dead_code)] +pub fn extract_file(ba2_path: &Path, file_path: &str) -> Result> { + let (archive, options): (Archive, _) = Archive::read(ba2_path) + .with_context(|| format!("Failed to open BA2: {}", ba2_path.display()))?; + + let write_options: FileWriteOptions = options.into(); + + // Normalize path for comparison (BA2 uses forward slashes typically) + let normalized = file_path.replace('\\', "/").to_lowercase(); + let normalized_backslash = file_path.replace('/', "\\").to_lowercase(); + + for (key, file) in archive.iter() { + let current_path = String::from_utf8_lossy(key.name().as_bytes()).to_lowercase(); + + // Try both slash conventions + if current_path == normalized + || current_path == normalized_backslash + || current_path.replace('\\', "/") == normalized + || current_path.replace('/', "\\") == normalized_backslash + { + // Write to memory buffer + let mut buffer = Cursor::new(Vec::new()); + file.write(&mut buffer, &write_options) + .with_context(|| format!("Failed to extract file: {}", file_path))?; + + return Ok(buffer.into_inner()); + } + } + + bail!( + "File not found in BA2: {} (searched for '{}')", + file_path, + normalized + ) +} + +/// Extract multiple files from a BA2 archive in parallel. +/// Opens the archive once, collects matching entries, then decompresses +/// and writes them in parallel using rayon. +/// `wanted` should contain lowercase forward-slash-separated paths. +pub fn extract_files_batch( + ba2_path: &Path, + wanted: &HashSet, + callback: F, +) -> Result +where + F: Fn(&str, Vec) -> Result<()> + Send + Sync, +{ + let (archive, options): (Archive, _) = Archive::read(ba2_path) + .with_context(|| format!("Failed to open BA2: {}", ba2_path.display()))?; + + let write_options: FileWriteOptions = options.into(); + + // Collect matching entries with references + let mut entries: Vec<(String, &Ba2File)> = Vec::new(); + for (key, file) in archive.iter() { + let path = String::from_utf8_lossy(key.name().as_bytes()).to_string(); + let lookup = path.replace('\\', "/").to_lowercase(); + if wanted.contains(&lookup) { + entries.push((path, file)); + } + } + + // Decompress + write in parallel + let extracted = AtomicUsize::new(0); + entries + .par_iter() + .try_for_each(|(path, file)| -> Result<()> { + let mut buffer = Cursor::new(Vec::new()); + file.write(&mut buffer, &write_options) + .with_context(|| format!("Failed to extract file: {}", path))?; + + callback(path, buffer.into_inner())?; + extracted.fetch_add(1, Ordering::Relaxed); + Ok(()) + })?; + + let count = extracted.load(Ordering::Relaxed); + debug!( + "Batch extracted {} of {} wanted files from BA2 {}", + count, + wanted.len(), + ba2_path.display() + ); + Ok(count) +} diff --git a/libs/bsa_ffi/src/archive/ba2_writer.rs b/libs/bsa_ffi/src/archive/ba2_writer.rs new file mode 100644 index 0000000..aa461d5 --- /dev/null +++ b/libs/bsa_ffi/src/archive/ba2_writer.rs @@ -0,0 +1,336 @@ +//! BA2 (Fallout 4/Starfield) archive creation +//! +//! Provides write support for FO4 format BA2 files (Fallout 4, Fallout 76, Starfield). + +use anyhow::{bail, Context, Result}; +use ba2::fo4::{ + Archive, ArchiveKey, ArchiveOptionsBuilder, Chunk, ChunkCompressionOptions, + CompressionFormat as Ba2CrateCompression, CompressionLevel, File as Ba2File, + FileReadOptionsBuilder, Format, Version, +}; +use ba2::prelude::*; +use ba2::{CompressionResult, Copied}; +use rayon::prelude::*; +use std::collections::HashMap; +use std::fs; +use std::io::BufWriter; +use std::path::Path; +use tracing::info; + +/// BA2 archive version +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum Ba2Version { + /// Fallout 4 Old Gen, Fallout 76 + V1, + /// Starfield + V2, + /// Starfield + V3, + /// Fallout 4 Next Gen + #[default] + V7, + /// Fallout 4 Next Gen + V8, +} + +impl Ba2Version { + /// Convert to the ba2 crate's Version type + pub fn to_crate_version(self) -> Version { + match self { + Ba2Version::V1 => Version::v1, + Ba2Version::V2 => Version::v2, + Ba2Version::V3 => Version::v3, + Ba2Version::V7 => Version::v7, + Ba2Version::V8 => Version::v8, + } + } +} + +/// Compression format for BA2 archives +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum Ba2CompressionFormat { + /// No compression + None, + /// zlib compression (Fallout 4, Fallout 76) + #[default] + Zlib, + /// LZ4 compression (Starfield) + Lz4, +} + +/// Archive format variant +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum Ba2Format { + /// General archive (GNRL) - for meshes, scripts, etc. + #[default] + General, + /// DirectX 10 textures (DX10) - for DDS textures + DX10, +} + +/// Builder for creating BA2 archives +pub struct Ba2Builder { + /// Files organized by path -> data + files: HashMap>, + /// Archive format (General or DX10) + format: Ba2Format, + /// Compression format + compression: Ba2CompressionFormat, + /// Whether to include string table + strings: bool, + /// Archive version + version: Ba2Version, +} + +impl Ba2Builder { + pub fn new() -> Self { + Self { + files: HashMap::new(), + format: Ba2Format::General, + compression: Ba2CompressionFormat::Zlib, + strings: true, + version: Ba2Version::default(), + } + } + + /// Create builder with settings detected from BA2 name + #[allow(dead_code)] + pub fn from_name(name: &str) -> Self { + let name_lower = name.to_lowercase(); + + // Texture archives need DX10 format for proper texture headers + let is_texture_archive = { + let filename = name_lower.rsplit(['/', '\\']).next().unwrap_or(&name_lower); + filename.contains(" - textures") + || filename.starts_with("textures") + || (filename.contains("textures") + && !filename.contains(" - main") + && !filename.contains("_main")) + }; + + let format = if is_texture_archive { + Ba2Format::DX10 + } else { + Ba2Format::General + }; + + // Default to zlib compression for FO4 + let compression = Ba2CompressionFormat::Zlib; + + Self { + files: HashMap::new(), + format, + compression, + strings: true, + version: Ba2Version::default(), + } + } + + /// Set archive version + pub fn with_version(mut self, version: Ba2Version) -> Self { + self.version = version; + self + } + + /// Set archive format + pub fn with_format(mut self, format: Ba2Format) -> Self { + self.format = format; + self + } + + /// Set compression format + pub fn with_compression(mut self, compression: Ba2CompressionFormat) -> Self { + self.compression = compression; + self + } + + /// Enable or disable string table + #[allow(dead_code)] + pub fn with_strings(mut self, strings: bool) -> Self { + self.strings = strings; + self + } + + /// Add a file to the archive + pub fn add_file(&mut self, path: &str, data: Vec) { + // Normalize: forward slashes, strip leading slash + let normalized = path.replace('\\', "/"); + let normalized = normalized.trim_start_matches('/').to_string(); + self.files.insert(normalized, data); + } + + /// Get number of files + pub fn file_count(&self) -> usize { + self.files.len() + } + + /// Check if empty + pub fn is_empty(&self) -> bool { + self.files.is_empty() + } + + /// Build and write the BA2 to disk with progress callback + pub fn build_with_progress(self, output_path: &Path, progress: F) -> Result<()> + where + F: Fn(usize, usize, &str) + Send + Sync, + { + if self.is_empty() { + bail!("Cannot create empty BA2 archive"); + } + + let file_count = self.file_count(); + let total_size: u64 = self.files.values().map(|data| data.len() as u64).sum(); + + info!( + "Building BA2: {} ({} files, {} MB, format {:?}, compression {:?})", + output_path.display(), + file_count, + total_size / 1_000_000, + self.format, + self.compression + ); + + // For DX10 (texture) archives, we need special handling + if self.format == Ba2Format::DX10 { + return self.build_dx10_with_progress(output_path, progress); + } + + // Build archive entries in parallel + let entries: Vec<(String, Vec)> = self.files.into_iter().collect(); + let total = entries.len(); + let processed_count = std::sync::atomic::AtomicUsize::new(0); + let compression = self.compression; + + let archive_entries: Result, Ba2File<'static>)>> = entries + .par_iter() + .map(|(path, data)| { + // Create chunk from data + let chunk = Chunk::from_decompressed(data.clone().into_boxed_slice()); + + // Optionally compress the chunk + let chunk = if compression != Ba2CompressionFormat::None { + let options = ChunkCompressionOptions::default(); + match chunk.compress(&options) { + Ok(compressed) => compressed, + Err(_) => chunk, // Fall back to uncompressed if compression fails + } + } else { + chunk + }; + + // Create file from chunk + let file: Ba2File = [chunk].into_iter().collect(); + + // Create key from path + let key: ArchiveKey = path.as_bytes().into(); + + let current = + processed_count.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1; + progress(current, total, path); + + Ok((key, file)) + }) + .collect(); + + let archive_entries = archive_entries?; + + // Build archive from entries + let archive: Archive = archive_entries.into_iter().collect(); + + let options = ArchiveOptionsBuilder::default() + .version(self.version.to_crate_version()) + .strings(self.strings) + .build(); + + // Create parent directory + if let Some(parent) = output_path.parent() { + fs::create_dir_all(parent)?; + } + + // Write archive + let file = fs::File::create(output_path) + .with_context(|| format!("Failed to create BA2: {}", output_path.display()))?; + let mut writer = BufWriter::new(file); + + archive + .write(&mut writer, &options) + .with_context(|| format!("Failed to write BA2: {}", output_path.display()))?; + + info!("Created BA2: {}", output_path.display()); + Ok(()) + } + + /// Build a DX10 (texture) archive with progress callback + fn build_dx10_with_progress(self, output_path: &Path, progress: F) -> Result<()> + where + F: Fn(usize, usize, &str) + Send + Sync, + { + let compress = self.compression != Ba2CompressionFormat::None; + let entries: Vec<(String, Vec)> = self.files.into_iter().collect(); + let total = entries.len(); + let processed_count = std::sync::atomic::AtomicUsize::new(0); + + // Build read options for DX10 format + let read_options = FileReadOptionsBuilder::new() + .format(Format::DX10) + .compression_format(Ba2CrateCompression::Zip) + .compression_level(CompressionLevel::FO4) + .compression_result(if compress { + CompressionResult::Compressed + } else { + CompressionResult::Decompressed + }) + .build(); + + let archive_entries: Result, Ba2File<'static>)>> = entries + .par_iter() + .map(|(path, data)| { + let file = Ba2File::read(Copied(data), &read_options) + .with_context(|| format!("Failed to parse DDS texture: {}", path))?; + + let key: ArchiveKey = path.as_bytes().into(); + + let current = + processed_count.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1; + progress(current, total, path); + + Ok((key, file)) + }) + .collect(); + + let archive_entries = archive_entries?; + let archive: Archive = archive_entries.into_iter().collect(); + + let options = ArchiveOptionsBuilder::default() + .version(self.version.to_crate_version()) + .format(Format::DX10) + .compression_format(Ba2CrateCompression::Zip) + .strings(self.strings) + .build(); + + if let Some(parent) = output_path.parent() { + fs::create_dir_all(parent)?; + } + + let file = fs::File::create(output_path) + .with_context(|| format!("Failed to create BA2: {}", output_path.display()))?; + let mut writer = BufWriter::new(file); + + archive + .write(&mut writer, &options) + .with_context(|| format!("Failed to write BA2: {}", output_path.display()))?; + + info!( + "Created DX10 BA2: {} ({} files)", + output_path.display(), + total + ); + Ok(()) + } +} + +impl Default for Ba2Builder { + fn default() -> Self { + Self::new() + } +} diff --git a/libs/bsa_ffi/src/archive/mod.rs b/libs/bsa_ffi/src/archive/mod.rs new file mode 100644 index 0000000..247789e --- /dev/null +++ b/libs/bsa_ffi/src/archive/mod.rs @@ -0,0 +1,436 @@ +//! BSA/BA2 (Bethesda Archive) handling +//! +//! Provides read/write support for: +//! - TES3 format BSA files (Morrowind) +//! - TES4 format BSA files (Oblivion, FO3, FNV, Skyrim) +//! - FO4 format BA2 files (Fallout 4, Fallout 76, Starfield) + +mod ba2_reader; +mod ba2_writer; +mod reader; +mod tes3_reader; +mod writer; + +pub use reader::{ + extract_file, extract_files_batch as extract_bsa_files_batch, list_files, BsaFileEntry, +}; +pub use writer::BsaBuilder; + +// TES3 (Morrowind) support +pub use tes3_reader::{ + extract_file as extract_tes3_file, extract_files_batch as extract_tes3_files_batch, + list_files as list_tes3_files, +}; + +// BA2 support for Fallout 4/Starfield +pub use ba2_reader::{ + extract_file as extract_ba2_file, extract_files_batch as extract_ba2_files_batch, + list_files as list_ba2_files, +}; +pub use ba2_writer::{Ba2Builder, Ba2CompressionFormat, Ba2Format, Ba2Version}; + +use anyhow::{bail, Result}; +use ba2::tes4::{ArchiveFlags, ArchiveTypes, Version}; +use ba2::{guess_format, FileFormat, Reader}; +use std::collections::HashSet; +use std::fs::File; +use std::io::BufReader; +use std::path::Path; +use tracing::debug; + +/// Archive format type +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum ArchiveFormat { + /// TES3 BSA (Morrowind) + Tes3Bsa, + /// TES4 BSA (Oblivion, FO3, FNV, Skyrim) + Bsa, + /// FO4 BA2 (Fallout 4, Fallout 76, Starfield) + Ba2, +} + +/// Detect archive format using ba2 crate's guess_format +pub fn detect_format(path: &Path) -> Option { + // Use ba2's built-in format detection + if let Ok(file) = File::open(path) { + let mut reader = BufReader::new(file); + if let Some(format) = guess_format(&mut reader) { + let result = match format { + FileFormat::TES3 => ArchiveFormat::Tes3Bsa, + FileFormat::TES4 => ArchiveFormat::Bsa, + FileFormat::FO4 => ArchiveFormat::Ba2, + }; + debug!("Detected {:?} format for: {}", result, path.display()); + return Some(result); + } + } + + // Fall back to extension + let ext = path.extension()?.to_str()?.to_lowercase(); + match ext.as_str() { + "bsa" => { + debug!( + "Detected BSA by extension (assuming TES4): {}", + path.display() + ); + Some(ArchiveFormat::Bsa) + } + "ba2" => { + debug!("Detected BA2 by extension: {}", path.display()); + Some(ArchiveFormat::Ba2) + } + _ => None, + } +} + +/// Universal archive file entry +#[derive(Debug, Clone)] +pub struct ArchiveFileEntry { + pub path: String, +} + +/// List files from any Bethesda archive (TES3 BSA, TES4 BSA, or BA2) +pub fn list_archive_files(archive_path: &Path) -> Result> { + match detect_format(archive_path) { + Some(ArchiveFormat::Tes3Bsa) => { + let files = list_tes3_files(archive_path)?; + Ok(files + .into_iter() + .map(|f| ArchiveFileEntry { path: f.path }) + .collect()) + } + Some(ArchiveFormat::Bsa) => { + let files = list_files(archive_path)?; + Ok(files + .into_iter() + .map(|f| ArchiveFileEntry { path: f.path }) + .collect()) + } + Some(ArchiveFormat::Ba2) => { + let files = list_ba2_files(archive_path)?; + Ok(files + .into_iter() + .map(|f| ArchiveFileEntry { path: f.path }) + .collect()) + } + None => bail!("Unknown archive format: {}", archive_path.display()), + } +} + +/// Extract a file from any Bethesda archive (TES3 BSA, TES4 BSA, or BA2) +#[allow(dead_code)] +pub fn extract_archive_file(archive_path: &Path, file_path: &str) -> Result> { + let format = detect_format(archive_path); + debug!( + "extract_archive_file: archive={}, file={}, format={:?}", + archive_path.display(), + file_path, + format + ); + match format { + Some(ArchiveFormat::Tes3Bsa) => extract_tes3_file(archive_path, file_path), + Some(ArchiveFormat::Bsa) => extract_file(archive_path, file_path), + Some(ArchiveFormat::Ba2) => extract_ba2_file(archive_path, file_path), + None => bail!("Unknown archive format: {}", archive_path.display()), + } +} + +/// Extract multiple files from any Bethesda archive in a single pass. +/// Opens the archive once and calls the callback for each extracted file. +/// `wanted_files` should contain the original paths (as returned by list_archive_files). +/// Returns the number of files successfully extracted. +pub fn extract_archive_files_batch( + archive_path: &Path, + wanted_files: &[String], + callback: F, +) -> Result +where + F: Fn(&str, Vec) -> Result<()> + Send + Sync, +{ + let format = detect_format(archive_path); + match format { + Some(ArchiveFormat::Tes3Bsa) => { + let wanted: HashSet = wanted_files.iter().map(|p| p.to_lowercase()).collect(); + extract_tes3_files_batch(archive_path, &wanted, callback) + } + Some(ArchiveFormat::Bsa) => { + // BSA uses backslash-separated paths + let wanted: HashSet = wanted_files + .iter() + .map(|p| p.replace('/', "\\").to_lowercase()) + .collect(); + extract_bsa_files_batch(archive_path, &wanted, callback) + } + Some(ArchiveFormat::Ba2) => { + // BA2 uses forward-slash paths + let wanted: HashSet = wanted_files + .iter() + .map(|p| p.replace('\\', "/").to_lowercase()) + .collect(); + extract_ba2_files_batch(archive_path, &wanted, callback) + } + None => bail!("Unknown archive format: {}", archive_path.display()), + } +} + +/// Game version for archive creation +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum GameVersion { + /// TES3 BSA (Morrowind) - no compression + Morrowind, + /// TES4 v103 (Oblivion) - no compression + Oblivion, + /// TES4 v104 (Fallout 3) - zlib compression + Fallout3, + /// TES4 v104 (Fallout: New Vegas) - zlib compression + FalloutNewVegas, + /// TES4 v104 (Skyrim LE) - zlib compression + SkyrimLE, + /// TES4 v105 (Skyrim SE) - zlib compression + SkyrimSE, + /// BA2 v1 (Fallout 4 / Fallout 76) - zlib compression + #[default] + Fallout4Fo76, + /// BA2 v7 (Fallout 4 Next Gen) - zlib compression + Fallout4NGv7, + /// BA2 v8 (Fallout 4 Next Gen) - zlib compression + Fallout4NGv8, + /// BA2 v2 (Starfield) - LZ4 compression + StarfieldV2, + /// BA2 v3 (Starfield) - LZ4 compression + StarfieldV3, +} + +impl GameVersion { + /// Get display name for this game version + pub fn display_name(&self) -> &'static str { + match self { + GameVersion::Morrowind => "Morrowind (BSA)", + GameVersion::Oblivion => "Oblivion (BSA v103)", + GameVersion::Fallout3 => "Fallout 3 (BSA v104)", + GameVersion::FalloutNewVegas => "Fallout: New Vegas (BSA v104)", + GameVersion::SkyrimLE => "Skyrim LE (BSA v104)", + GameVersion::SkyrimSE => "Skyrim SE (BSA v105)", + GameVersion::Fallout4Fo76 => "Fallout 4 / Fallout 76 (BA2 v1)", + GameVersion::Fallout4NGv7 => "Fallout 4 Next Gen (BA2 v7)", + GameVersion::Fallout4NGv8 => "Fallout 4 Next Gen (BA2 v8)", + GameVersion::StarfieldV2 => "Starfield (BA2 v2)", + GameVersion::StarfieldV3 => "Starfield (BA2 v3)", + } + } + + /// Check if this game uses BA2 format + pub fn is_ba2(&self) -> bool { + matches!( + self, + GameVersion::Fallout4Fo76 + | GameVersion::Fallout4NGv7 + | GameVersion::Fallout4NGv8 + | GameVersion::StarfieldV2 + | GameVersion::StarfieldV3 + ) + } + + /// Check if this game uses TES3 format (Morrowind) + pub fn is_tes3(&self) -> bool { + matches!(self, GameVersion::Morrowind) + } + + /// Check if compression is supported for this game + pub fn supports_compression(&self) -> bool { + !matches!(self, GameVersion::Morrowind | GameVersion::Oblivion) + } + + /// Get BSA version for TES4 format games + pub fn bsa_version(&self) -> Option { + match self { + GameVersion::Oblivion => Some(Version::v103), + GameVersion::Fallout3 | GameVersion::FalloutNewVegas | GameVersion::SkyrimLE => { + Some(Version::v104) + } + GameVersion::SkyrimSE => Some(Version::v105), + _ => None, + } + } + + /// Get BA2 version for FO4/Starfield format games + pub fn ba2_version(&self) -> Option { + match self { + GameVersion::Fallout4Fo76 => Some(Ba2Version::V1), + GameVersion::Fallout4NGv7 => Some(Ba2Version::V7), + GameVersion::Fallout4NGv8 => Some(Ba2Version::V8), + GameVersion::StarfieldV2 => Some(Ba2Version::V2), + GameVersion::StarfieldV3 => Some(Ba2Version::V3), + _ => None, + } + } + + /// Get BA2 compression format for this game + pub fn ba2_compression(&self) -> Ba2CompressionFormat { + match self { + GameVersion::StarfieldV2 | GameVersion::StarfieldV3 => Ba2CompressionFormat::Lz4, + _ => Ba2CompressionFormat::Zlib, + } + } + + /// Get all game versions + pub fn all() -> &'static [GameVersion] { + &[ + GameVersion::Morrowind, + GameVersion::Oblivion, + GameVersion::Fallout3, + GameVersion::FalloutNewVegas, + GameVersion::SkyrimLE, + GameVersion::SkyrimSE, + GameVersion::Fallout4Fo76, + GameVersion::Fallout4NGv7, + GameVersion::Fallout4NGv8, + GameVersion::StarfieldV2, + GameVersion::StarfieldV3, + ] + } + + /// Convert index to game version + pub fn from_index(index: i32) -> GameVersion { + match index { + 0 => GameVersion::Morrowind, + 1 => GameVersion::Oblivion, + 2 => GameVersion::Fallout3, + 3 => GameVersion::FalloutNewVegas, + 4 => GameVersion::SkyrimLE, + 5 => GameVersion::SkyrimSE, + 6 => GameVersion::Fallout4Fo76, + 7 => GameVersion::Fallout4NGv7, + 8 => GameVersion::Fallout4NGv8, + 9 => GameVersion::StarfieldV2, + 10 => GameVersion::StarfieldV3, + _ => GameVersion::Fallout4Fo76, + } + } + + /// Convert game version to index + pub fn index(self) -> i32 { + match self { + GameVersion::Morrowind => 0, + GameVersion::Oblivion => 1, + GameVersion::Fallout3 => 2, + GameVersion::FalloutNewVegas => 3, + GameVersion::SkyrimLE => 4, + GameVersion::SkyrimSE => 5, + GameVersion::Fallout4Fo76 => 6, + GameVersion::Fallout4NGv7 => 7, + GameVersion::Fallout4NGv8 => 8, + GameVersion::StarfieldV2 => 9, + GameVersion::StarfieldV3 => 10, + } + } + + /// Short CLI-friendly name + pub fn cli_name(&self) -> &'static str { + match self { + GameVersion::Morrowind => "morrowind", + GameVersion::Oblivion => "oblivion", + GameVersion::Fallout3 => "fo3", + GameVersion::FalloutNewVegas => "fonv", + GameVersion::SkyrimLE => "skyrimle", + GameVersion::SkyrimSE => "skyrimse", + GameVersion::Fallout4Fo76 => "fo4-fo76", + GameVersion::Fallout4NGv7 => "fo4ng-v7", + GameVersion::Fallout4NGv8 => "fo4ng-v8", + GameVersion::StarfieldV2 => "starfield-v2", + GameVersion::StarfieldV3 => "starfield-v3", + } + } + + /// Parse from CLI name (case-insensitive) + pub fn from_cli_name(name: &str) -> Option { + let lower = name.to_lowercase(); + GameVersion::all() + .iter() + .find(|v| v.cli_name() == lower) + .copied() + } +} + +/// Detect game version from archive format +pub fn detect_game_version(archive_path: &Path) -> Option { + match detect_format(archive_path) { + Some(ArchiveFormat::Tes3Bsa) => Some(GameVersion::Morrowind), + Some(ArchiveFormat::Ba2) => Some(GameVersion::Fallout4Fo76), // Default to FO4/FO76 + Some(ArchiveFormat::Bsa) => { + // Try to detect version from BSA header + let result: Result<(ba2::tes4::Archive, ba2::tes4::ArchiveOptions), _> = + ba2::tes4::Archive::read(archive_path); + if let Ok((_, options)) = result { + match options.version() { + Version::v103 => Some(GameVersion::Oblivion), + Version::v104 => Some(GameVersion::Fallout3), // Default for v104 + Version::v105 => Some(GameVersion::SkyrimSE), + } + } else { + Some(GameVersion::Fallout3) // Default + } + } + None => None, + } +} + +/// Default flags for FO3/FNV BSAs +pub fn default_flags_fo3() -> ArchiveFlags { + ArchiveFlags::DIRECTORY_STRINGS + | ArchiveFlags::FILE_STRINGS + | ArchiveFlags::COMPRESSED + | ArchiveFlags::RETAIN_DIRECTORY_NAMES + | ArchiveFlags::RETAIN_FILE_NAMES + | ArchiveFlags::RETAIN_FILE_NAME_OFFSETS +} + +/// Default flags for Oblivion BSAs (no compression) +#[allow(dead_code)] +pub fn default_flags_oblivion() -> ArchiveFlags { + ArchiveFlags::DIRECTORY_STRINGS | ArchiveFlags::FILE_STRINGS +} + +/// Detect archive types from BSA name +#[allow(dead_code)] +pub fn detect_types(name: &str) -> ArchiveTypes { + let name_lower = name.to_lowercase(); + + if name_lower.contains("meshes") { + ArchiveTypes::MESHES + } else if name_lower.contains("textures") { + ArchiveTypes::TEXTURES + } else if name_lower.contains("menuvoices") { + ArchiveTypes::MENUS | ArchiveTypes::VOICES + } else if name_lower.contains("voices") { + ArchiveTypes::VOICES + } else if name_lower.contains("sound") { + ArchiveTypes::SOUNDS + } else { + ArchiveTypes::MISC + } +} + +/// Detect BSA version from archive name +#[allow(dead_code)] +pub fn detect_version(name: &str) -> Version { + let name_lower = name.to_lowercase(); + + // Oblivion uses v103 + if name_lower.contains("oblivion") + || name_lower.contains("shiveringisles") + || name_lower.contains("dlcshiveringisles") + || name_lower.contains("dlcbattlehorn") + || name_lower.contains("dlcfrostcrag") + || name_lower.contains("dlchorse") + || name_lower.contains("dlcorrery") + || name_lower.contains("dlcthievesden") + || name_lower.contains("dlcvilelair") + || name_lower.contains("knights") + { + Version::v103 + } else { + // Default to FO3/FNV + Version::v104 + } +} diff --git a/libs/bsa_ffi/src/archive/reader.rs b/libs/bsa_ffi/src/archive/reader.rs new file mode 100644 index 0000000..21b291c --- /dev/null +++ b/libs/bsa_ffi/src/archive/reader.rs @@ -0,0 +1,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> { + 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> { + 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( + bsa_path: &Path, + wanted: &HashSet, + callback: F, +) -> Result +where + F: Fn(&str, Vec) -> 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) +} diff --git a/libs/bsa_ffi/src/archive/tes3_reader.rs b/libs/bsa_ffi/src/archive/tes3_reader.rs new file mode 100644 index 0000000..c03f155 --- /dev/null +++ b/libs/bsa_ffi/src/archive/tes3_reader.rs @@ -0,0 +1,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> { + 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> { + 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( + bsa_path: &Path, + wanted: &HashSet, + callback: F, +) -> Result +where + F: Fn(&str, Vec) -> 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) +} diff --git a/libs/bsa_ffi/src/archive/writer.rs b/libs/bsa_ffi/src/archive/writer.rs new file mode 100644 index 0000000..112619f --- /dev/null +++ b/libs/bsa_ffi/src/archive/writer.rs @@ -0,0 +1,256 @@ +//! BSA archive creation + +use anyhow::{bail, Context, Result}; +use ba2::tes4::{ + Archive, ArchiveFlags, ArchiveKey, ArchiveOptions, ArchiveTypes, Directory, DirectoryKey, + File as BsaFile, FileCompressionOptions, Version, +}; +use ba2::CompressableFrom; +use rayon::prelude::*; +use std::collections::HashMap; +use std::fs; +use std::io::BufWriter; +use std::path::Path; +use tracing::info; + +use super::{default_flags_fo3, default_flags_oblivion, detect_types, detect_version}; + +/// Helper struct to hold file data with lifetime for BSA creation +struct FileEntry { + dir_path: String, + file_name: String, + data: Vec, +} + +impl FileEntry { + /// Create a BSA file, optionally compressing it + fn as_bsa_file(&self, version: Version, should_compress: bool) -> Result> { + // Create an uncompressed file from our raw data + let uncompressed = BsaFile::from_decompressed(self.data.clone().into_boxed_slice()); + + if should_compress { + // Compress the file using ba2's compress method + let compression_options = FileCompressionOptions::builder().version(version).build(); + + uncompressed + .compress(&compression_options) + .with_context(|| { + format!("Failed to compress: {}/{}", self.dir_path, self.file_name) + }) + } else { + Ok(uncompressed) + } + } +} + +/// Builder for creating BSA archives +pub struct BsaBuilder { + /// Files organized by directory -> filename -> data + files: HashMap>>, + flags: ArchiveFlags, + types: ArchiveTypes, + version: Version, +} + +impl BsaBuilder { + pub fn new() -> Self { + Self { + files: HashMap::new(), + flags: default_flags_fo3(), + types: ArchiveTypes::empty(), + version: Version::v104, + } + } + + /// Create builder with settings detected from BSA name + #[allow(dead_code)] + pub fn from_name(name: &str) -> Self { + let version = detect_version(name); + let types = detect_types(name); + let flags = if version == Version::v103 { + default_flags_oblivion() + } else { + default_flags_fo3() + }; + + Self { + files: HashMap::new(), + flags, + types, + version, + } + } + + /// Set archive flags + #[allow(dead_code)] + pub fn with_flags(mut self, flags: ArchiveFlags) -> Self { + self.flags = flags; + self + } + + /// Set archive types + #[allow(dead_code)] + pub fn with_types(mut self, types: ArchiveTypes) -> Self { + self.types = types; + self + } + + /// Set BSA version + pub fn with_version(mut self, version: Version) -> Self { + self.version = version; + self + } + + /// Enable or disable compression + pub fn with_compression(mut self, compress: bool) -> Self { + if compress { + self.flags |= ArchiveFlags::COMPRESSED; + } else { + self.flags &= !ArchiveFlags::COMPRESSED; + } + self + } + + /// Add a file to the archive + pub fn add_file(&mut self, path: &str, data: Vec) { + // Normalize: forward slashes, strip leading slash + let normalized = path.replace('\\', "/"); + let normalized = normalized.trim_start_matches('/'); + + let (dir_path, file_name) = if let Some(idx) = normalized.rfind('/') { + ( + normalized[..idx].to_string(), + normalized[idx + 1..].to_string(), + ) + } else { + (".".to_string(), normalized.to_string()) + }; + + self.files + .entry(dir_path) + .or_default() + .insert(file_name, data); + } + + /// Get number of files + pub fn file_count(&self) -> usize { + self.files.values().map(|d| d.len()).sum() + } + + /// Check if empty + pub fn is_empty(&self) -> bool { + self.file_count() == 0 + } + + /// Build and write the BSA to disk with progress callback + pub fn build_with_progress(self, output_path: &Path, progress: F) -> Result<()> + where + F: Fn(usize, usize, &str) + Send + Sync, + { + if self.is_empty() { + bail!("Cannot create empty BSA archive"); + } + + let file_count = self.file_count(); + let total_size: u64 = self + .files + .values() + .flat_map(|files| files.values()) + .map(|data| data.len() as u64) + .sum(); + + info!( + "Building BSA: {} ({} files, {} MB, version {:?}, flags {:?})", + output_path.display(), + file_count, + total_size / 1_000_000, + self.version, + self.flags + ); + + // Check if we should compress files + let should_compress = self.flags.contains(ArchiveFlags::COMPRESSED); + + // Flatten to FileEntry structs that own their data + let entries: Vec = self + .files + .into_iter() + .flat_map(|(dir_path, files)| { + files.into_iter().map(move |(file_name, data)| FileEntry { + dir_path: dir_path.clone(), + file_name, + data, + }) + }) + .collect(); + + let total = entries.len(); + let processed_count = std::sync::atomic::AtomicUsize::new(0); + + // Process files in parallel - create and compress BsaFile entries + let version = self.version; + let processed: Result> = entries + .par_iter() + .map(|entry| { + let file = entry.as_bsa_file(version, should_compress)?; + let current = + processed_count.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1; + progress( + current, + total, + &format!("{}/{}", entry.dir_path, entry.file_name), + ); + Ok((entry.dir_path.clone(), entry.file_name.clone(), file)) + }) + .collect(); + + let processed = processed?; + + // Build archive + let mut archive = Archive::new(); + for (dir_path, file_name, file) in processed { + let archive_key = ArchiveKey::from(dir_path.as_bytes()); + let directory_key = DirectoryKey::from(file_name.as_bytes()); + + match archive.get_mut(&archive_key) { + Some(directory) => { + directory.insert(directory_key, file); + } + None => { + let mut directory = Directory::default(); + directory.insert(directory_key, file); + archive.insert(archive_key, directory); + } + } + } + + let options = ArchiveOptions::builder() + .version(self.version) + .flags(self.flags) + .types(self.types) + .build(); + + // Create parent directory + if let Some(parent) = output_path.parent() { + fs::create_dir_all(parent)?; + } + + // Write archive + let file = fs::File::create(output_path) + .with_context(|| format!("Failed to create BSA: {}", output_path.display()))?; + let mut writer = BufWriter::new(file); + + archive + .write(&mut writer, &options) + .with_context(|| format!("Failed to write BSA: {}", output_path.display()))?; + + info!("Created BSA: {}", output_path.display()); + Ok(()) + } +} + +impl Default for BsaBuilder { + fn default() -> Self { + Self::new() + } +} diff --git a/libs/bsa_ffi/src/lib.rs b/libs/bsa_ffi/src/lib.rs new file mode 100644 index 0000000..2cb2f21 --- /dev/null +++ b/libs/bsa_ffi/src/lib.rs @@ -0,0 +1,325 @@ +mod archive; + +use std::ffi::{c_char, c_int, CStr, CString}; +use std::fs; +use std::path::{Path, PathBuf}; +use std::ptr; + +use archive::{ + extract_archive_files_batch, list_archive_files, Ba2Builder, Ba2Format, BsaBuilder, + GameVersion, +}; +use walkdir::WalkDir; + +#[repr(C)] +pub struct BsaFfiStringList { + pub items: *mut *mut c_char, + pub count: usize, + pub error: *mut c_char, +} + +pub type BsaProgressCallback = + Option; + +fn to_cstring(s: &str) -> *mut c_char { + CString::new(s).unwrap_or_default().into_raw() +} + +fn error_list(msg: &str) -> BsaFfiStringList { + BsaFfiStringList { + items: ptr::null_mut(), + count: 0, + error: to_cstring(msg), + } +} + +unsafe fn from_cstr<'a>(p: *const c_char) -> Result<&'a str, &'static str> { + if p.is_null() { + return Err("null pointer"); + } + + CStr::from_ptr(p) + .to_str() + .map_err(|_| "invalid UTF-8 string") +} + +fn call_progress(progress_cb: BsaProgressCallback, done: usize, total: usize, path: &str) { + if let Some(cb) = progress_cb { + if let Ok(c_path) = CString::new(path) { + unsafe { + cb(done as u32, total as u32, c_path.as_ptr()); + } + } + } +} + +fn path_to_rel(root: &Path, child: &Path) -> anyhow::Result { + let rel = child.strip_prefix(root)?; + Ok(rel.to_string_lossy().replace('\\', "/")) +} + +fn include_file_for_mode(rel: &str, include_mode: i32) -> bool { + let is_dds = rel.to_lowercase().ends_with(".dds"); + match include_mode { + 1 => !is_dds, + 2 => is_dds, + _ => true, + } +} + +#[no_mangle] +pub unsafe extern "C" fn bsa_ffi_list_files(archive_path: *const c_char) -> BsaFfiStringList { + let archive_path = match from_cstr(archive_path) { + Ok(v) => v, + Err(e) => return error_list(e), + }; + + let entries = match list_archive_files(Path::new(archive_path)) { + Ok(v) => v, + Err(e) => return error_list(&e.to_string()), + }; + + let mut items: Vec<*mut c_char> = entries.into_iter().map(|e| to_cstring(&e.path)).collect(); + let result = BsaFfiStringList { + items: items.as_mut_ptr(), + count: items.len(), + error: ptr::null_mut(), + }; + std::mem::forget(items); + result +} + +#[no_mangle] +pub unsafe extern "C" fn bsa_ffi_string_list_free(list: BsaFfiStringList) { + if !list.items.is_null() { + let items = Vec::from_raw_parts(list.items, list.count, list.count); + for p in items { + if !p.is_null() { + let _ = CString::from_raw(p); + } + } + } + + if !list.error.is_null() { + let _ = CString::from_raw(list.error); + } +} + +#[no_mangle] +pub unsafe extern "C" fn bsa_ffi_string_free(s: *mut c_char) { + if !s.is_null() { + let _ = CString::from_raw(s); + } +} + +#[no_mangle] +pub unsafe extern "C" fn bsa_ffi_extract_all( + archive_path: *const c_char, + output_dir: *const c_char, + progress_cb: BsaProgressCallback, + cancel_flag: *const c_int, +) -> *mut c_char { + let archive_path = match from_cstr(archive_path) { + Ok(v) => v, + Err(e) => return to_cstring(e), + }; + let output_dir = match from_cstr(output_dir) { + Ok(v) => v, + Err(e) => return to_cstring(e), + }; + + let archive_path = PathBuf::from(archive_path); + let output_dir = PathBuf::from(output_dir); + + if let Err(e) = fs::create_dir_all(&output_dir) { + return to_cstring(&format!("failed to create output directory: {e}")); + } + + let entries = match list_archive_files(&archive_path) { + Ok(v) => v, + Err(e) => return to_cstring(&e.to_string()), + }; + + let total = entries.len(); + let wanted_files: Vec = entries.iter().map(|e| e.path.clone()).collect(); + let progress_count = std::sync::atomic::AtomicUsize::new(0); + let cancel_addr = cancel_flag as usize; + + let res = extract_archive_files_batch(&archive_path, &wanted_files, |path, data| { + let cancel_ptr = cancel_addr as *const c_int; + if !cancel_ptr.is_null() { + let cancelled = unsafe { *cancel_ptr } != 0; + if cancelled { + anyhow::bail!("cancelled"); + } + } + + let out_path = output_dir.join(path.replace('\\', "/")); + if let Some(parent) = out_path.parent() { + fs::create_dir_all(parent)?; + } + fs::write(&out_path, &data)?; + + let done = progress_count.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1; + call_progress(progress_cb, done, total, path); + Ok(()) + }); + + match res { + Ok(_) => ptr::null_mut(), + Err(e) => to_cstring(&e.to_string()), + } +} + +#[no_mangle] +pub unsafe extern "C" fn bsa_ffi_pack_dir( + input_dir: *const c_char, + output_archive: *const c_char, + game_id: *const c_char, + progress_cb: BsaProgressCallback, + cancel_flag: *const c_int, +) -> *mut c_char { + bsa_ffi_pack_dir_filtered( + input_dir, + output_archive, + game_id, + 0, + progress_cb, + cancel_flag, + ) +} + +#[no_mangle] +pub unsafe extern "C" fn bsa_ffi_pack_dir_filtered( + input_dir: *const c_char, + output_archive: *const c_char, + game_id: *const c_char, + include_mode: c_int, + progress_cb: BsaProgressCallback, + cancel_flag: *const c_int, +) -> *mut c_char { + let input_dir = match from_cstr(input_dir) { + Ok(v) => v, + Err(e) => return to_cstring(e), + }; + let output_archive = match from_cstr(output_archive) { + Ok(v) => v, + Err(e) => return to_cstring(e), + }; + let game_id = match from_cstr(game_id) { + Ok(v) => v, + Err(e) => return to_cstring(e), + }; + + let game = match GameVersion::from_cli_name(game_id) { + Some(v) => v, + None => { + let valid = GameVersion::all() + .iter() + .map(GameVersion::cli_name) + .collect::>() + .join(", "); + return to_cstring(&format!("unknown game_id '{game_id}', valid: {valid}")); + } + }; + + let input_dir = PathBuf::from(input_dir); + let output_archive = PathBuf::from(output_archive); + + let mut files: Vec<(String, Vec)> = Vec::new(); + for entry in WalkDir::new(&input_dir).into_iter().filter_map(|e| e.ok()) { + if !entry.file_type().is_file() { + continue; + } + + if !cancel_flag.is_null() { + let cancelled = unsafe { *cancel_flag } != 0; + if cancelled { + return to_cstring("cancelled"); + } + } + + let rel = match path_to_rel(&input_dir, entry.path()) { + Ok(v) => v, + Err(e) => return to_cstring(&format!("path error: {e}")), + }; + + if !include_file_for_mode(&rel, include_mode) { + continue; + } + + let data = match fs::read(entry.path()) { + Ok(v) => v, + Err(e) => return to_cstring(&format!("read error: {e}")), + }; + + files.push((rel, data)); + } + + if files.is_empty() { + return to_cstring("no files found in input_dir"); + } + + let total = files.len(); + + if game.is_ba2() { + let ba2_version = game.ba2_version().unwrap_or_default(); + let compression = game.ba2_compression(); + + let format = if output_archive + .file_name() + .map(|n| n.to_string_lossy().to_lowercase().contains("textures")) + .unwrap_or(false) + { + Ba2Format::DX10 + } else { + Ba2Format::General + }; + + let mut builder = Ba2Builder::new() + .with_version(ba2_version) + .with_compression(compression) + .with_format(format); + + for (idx, (rel, data)) in files.into_iter().enumerate() { + if !cancel_flag.is_null() { + let cancelled = unsafe { *cancel_flag } != 0; + if cancelled { + return to_cstring("cancelled"); + } + } + builder.add_file(&rel, data); + call_progress(progress_cb, idx + 1, total, &rel); + } + + match builder.build_with_progress(&output_archive, |_, _, _| {}) { + Ok(_) => ptr::null_mut(), + Err(e) => to_cstring(&e.to_string()), + } + } else { + let version = match game.bsa_version() { + Some(v) => v, + None => return to_cstring("TES3 BSA writing is not supported yet"), + }; + + let compress = game.supports_compression(); + + let mut builder = BsaBuilder::new().with_version(version).with_compression(compress); + + for (idx, (rel, data)) in files.into_iter().enumerate() { + if !cancel_flag.is_null() { + let cancelled = unsafe { *cancel_flag } != 0; + if cancelled { + return to_cstring("cancelled"); + } + } + builder.add_file(&rel, data); + call_progress(progress_cb, idx + 1, total, &rel); + } + + match builder.build_with_progress(&output_archive, |_, _, _| {}) { + Ok(_) => ptr::null_mut(), + Err(e) => to_cstring(&e.to_string()), + } + } +} -- cgit v1.3.1