maclarian 0.1.3

Larian file format library for Baldur's Gate 3 - PAK, LSF, LSX, GR2, DDS, and more
Documentation
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
//! Core PAK archive operations

use super::super::lspk::{
    CompressionMethod, FileTableEntry, LspkReader, LspkWriter, PakPhase, PakProgress,
};
use super::ProgressCallback;
use super::decompression::decompress_data;
use super::helpers::{get_part_path, get_virtual_texture_subfolder, is_virtual_texture_file};
use crate::error::{Error, Result};
use rayon::prelude::*;
use std::collections::HashMap;
use std::fs::File;
use std::io::{Read, Seek, SeekFrom};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};

/// Compressed file data ready for parallel decompression
struct CompressedFile {
    entry: FileTableEntry,
    compressed_data: Vec<u8>,
}

/// High-level PAK archive operations.
pub struct PakOperations;

impl PakOperations {
    /// Extract a PAK file to a directory
    ///
    /// # Errors
    ///
    /// Returns [`Error::Io`] if the PAK file cannot be opened or output directory cannot be created.
    /// Returns [`Error::InvalidPakMagic`] if the file is not a valid PAK archive.
    /// Returns [`Error::Lz4DecompressionFailed`] or [`Error::ZlibDecompressionFailed`] if file decompression fails.
    /// Returns [`Error::PakExtractionPartialFailure`] if extraction completes with partial failures.
    ///
    /// [`Error::Io`]: crate::Error::Io
    /// [`Error::InvalidPakMagic`]: crate::Error::InvalidPakMagic
    /// [`Error::Lz4DecompressionFailed`]: crate::Error::Lz4DecompressionFailed
    /// [`Error::ZlibDecompressionFailed`]: crate::Error::ZlibDecompressionFailed
    /// [`Error::PakExtractionPartialFailure`]: crate::Error::PakExtractionPartialFailure
    pub fn extract<P: AsRef<Path>>(pak_path: P, output_dir: P) -> Result<()> {
        Self::extract_with_progress(pak_path, output_dir, &|_| {})
    }

    /// Extract a PAK file to a directory with progress callback
    ///
    /// The callback receives [`PakProgress`] with phase and file information.
    /// Uses parallel decompression for improved performance on multi-core systems.
    /// Supports multi-part archives (e.g., `Textures.pak` with `Textures_1.pak`, `Textures_2.pak`).
    ///
    /// # Errors
    ///
    /// Returns [`Error::Io`] if the PAK file cannot be opened or output directory cannot be created.
    /// Returns [`Error::InvalidPakMagic`] if the file is not a valid PAK archive.
    /// Returns [`Error::Lz4DecompressionFailed`] or [`Error::ZlibDecompressionFailed`] if file decompression fails.
    /// Returns [`Error::PakExtractionPartialFailure`] if extraction completes with partial failures.
    ///
    /// [`Error::Io`]: crate::Error::Io
    /// [`Error::InvalidPakMagic`]: crate::Error::InvalidPakMagic
    /// [`Error::Lz4DecompressionFailed`]: crate::Error::Lz4DecompressionFailed
    /// [`Error::ZlibDecompressionFailed`]: crate::Error::ZlibDecompressionFailed
    /// [`Error::PakExtractionPartialFailure`]: crate::Error::PakExtractionPartialFailure
    pub fn extract_with_progress<P: AsRef<Path>>(
        pak_path: P,
        output_dir: P,
        progress: ProgressCallback,
    ) -> Result<()> {
        let pak_path = pak_path.as_ref();
        let output_dir = output_dir.as_ref();

        let mut reader = LspkReader::with_path(File::open(pak_path)?, pak_path);

        progress(&PakProgress {
            phase: PakPhase::ReadingTable,
            current: 1,
            total: 1,
            current_file: None,
        });

        // Get file list without decompressing
        let entries = reader.list_files()?;

        std::fs::create_dir_all(output_dir)?;

        // Filter entries (skip .DS_Store files)
        let filtered_entries: Vec<_> = entries
            .into_iter()
            .filter(|entry| entry.path.file_name() != Some(std::ffi::OsStr::new(".DS_Store")))
            .collect();

        let total_files = filtered_entries.len();
        let processed = AtomicUsize::new(0);
        let error_count = AtomicUsize::new(0);

        // Single-pass parallel extraction: each thread opens its own file handle
        let errors: Vec<(PathBuf, String)> = filtered_entries
            .par_iter()
            .filter_map(|entry| {
                let file_name = entry.path.file_name().map_or_else(
                    || entry.path.to_string_lossy().to_string(),
                    |n| n.to_string_lossy().to_string(),
                );

                // Update progress (atomic)
                let current = processed.fetch_add(1, Ordering::SeqCst) + 1;
                progress(&PakProgress {
                    phase: PakPhase::DecompressingFiles,
                    current,
                    total: total_files,
                    current_file: Some(file_name.clone()),
                });

                // Get the correct part file path for this entry
                let part_path = if let Some(p) = get_part_path(pak_path, entry.archive_part) {
                    p
                } else {
                    error_count.fetch_add(1, Ordering::SeqCst);
                    return Some((
                        entry.path.clone(),
                        format!(
                            "Cannot determine path for archive part {}",
                            entry.archive_part
                        ),
                    ));
                };

                // Open file handle for this thread, seek, and read
                let mut file = match File::open(&part_path) {
                    Ok(f) => f,
                    Err(e) => {
                        error_count.fetch_add(1, Ordering::SeqCst);
                        return Some((
                            entry.path.clone(),
                            format!("Failed to open {}: {e}", part_path.display()),
                        ));
                    }
                };

                if let Err(e) = file.seek(SeekFrom::Start(entry.offset)) {
                    error_count.fetch_add(1, Ordering::SeqCst);
                    return Some((entry.path.clone(), format!("Failed to seek: {e}")));
                }

                let mut compressed_data = vec![0u8; entry.size_compressed as usize];
                if let Err(e) = file.read_exact(&mut compressed_data) {
                    error_count.fetch_add(1, Ordering::SeqCst);
                    return Some((entry.path.clone(), format!("Failed to read: {e}")));
                }

                // Decompress
                let data = match decompress_data(
                    &compressed_data,
                    entry.compression,
                    entry.size_decompressed,
                ) {
                    Ok(data) => data,
                    Err(e) => {
                        error_count.fetch_add(1, Ordering::SeqCst);
                        tracing::warn!("Failed to decompress {}: {}", entry.path.display(), e);
                        return Some((entry.path.clone(), e.to_string()));
                    }
                };

                // Calculate output path (handle virtual texture subfolders)
                let output_path = if is_virtual_texture_file(&file_name) {
                    if let Some(subfolder) = get_virtual_texture_subfolder(&file_name) {
                        if let Some(parent) = entry.path.parent() {
                            output_dir.join(parent).join(&subfolder).join(&file_name)
                        } else {
                            output_dir.join(&subfolder).join(&file_name)
                        }
                    } else {
                        output_dir.join(&entry.path)
                    }
                } else {
                    output_dir.join(&entry.path)
                };

                // Create parent directories (idempotent)
                if let Some(parent) = output_path.parent()
                    && let Err(e) = std::fs::create_dir_all(parent)
                {
                    error_count.fetch_add(1, Ordering::SeqCst);
                    return Some((entry.path.clone(), format!("Failed to create dir: {e}")));
                }

                // Write file
                if let Err(e) = std::fs::write(&output_path, &data) {
                    error_count.fetch_add(1, Ordering::SeqCst);
                    return Some((entry.path.clone(), format!("Failed to write: {e}")));
                }

                None
            })
            .collect();

        // If there were errors, return a summary error
        if !errors.is_empty() {
            return Err(Error::PakExtractionPartialFailure {
                total: total_files,
                failed: errors.len(),
                first_error: errors[0].1.clone(),
            });
        }

        Ok(())
    }

    /// Create a PAK file from a directory
    ///
    /// # Errors
    ///
    /// Returns [`Error::Io`] if the source directory cannot be read or output file cannot be written.
    /// Returns [`Error::WalkDirError`] if directory traversal fails.
    /// Returns [`Error::CompressionError`] if file compression fails.
    ///
    /// [`Error::Io`]: crate::Error::Io
    /// [`Error::WalkDirError`]: crate::Error::WalkDirError
    /// [`Error::CompressionError`]: crate::Error::CompressionError
    pub fn create<P: AsRef<Path>>(source_dir: P, output_pak: P) -> Result<()> {
        Self::create_with_progress(source_dir, output_pak, &|_| {})
    }

    /// Create a PAK file from a directory with progress callback
    ///
    /// # Errors
    ///
    /// Returns [`Error::Io`] if the source directory cannot be read or output file cannot be written.
    /// Returns [`Error::WalkDirError`] if directory traversal fails.
    /// Returns [`Error::CompressionError`] if file compression fails.
    ///
    /// [`Error::Io`]: crate::Error::Io
    /// [`Error::WalkDirError`]: crate::Error::WalkDirError
    /// [`Error::CompressionError`]: crate::Error::CompressionError
    pub fn create_with_progress<P: AsRef<Path>>(
        source_dir: P,
        output_pak: P,
        progress: ProgressCallback,
    ) -> Result<()> {
        let writer = LspkWriter::new(source_dir.as_ref())?;
        writer.write_with_progress(output_pak.as_ref(), progress)?;
        Ok(())
    }

    /// Create a PAK file from a directory with specified compression
    ///
    /// # Errors
    ///
    /// Returns [`Error::Io`] if the source directory cannot be read or output file cannot be written.
    /// Returns [`Error::WalkDirError`] if directory traversal fails.
    /// Returns [`Error::CompressionError`] if file compression fails.
    ///
    /// [`Error::Io`]: crate::Error::Io
    /// [`Error::WalkDirError`]: crate::Error::WalkDirError
    /// [`Error::CompressionError`]: crate::Error::CompressionError
    pub fn create_with_compression<P: AsRef<Path>>(
        source_dir: P,
        output_pak: P,
        compression: CompressionMethod,
    ) -> Result<()> {
        Self::create_with_compression_and_progress(source_dir, output_pak, compression, &|_| {})
    }

    /// Create a PAK file from a directory with compression and progress callback
    ///
    /// # Errors
    ///
    /// Returns [`Error::Io`] if the source directory cannot be read or output file cannot be written.
    /// Returns [`Error::WalkDirError`] if directory traversal fails.
    /// Returns [`Error::CompressionError`] if file compression fails.
    ///
    /// [`Error::Io`]: crate::Error::Io
    /// [`Error::WalkDirError`]: crate::Error::WalkDirError
    /// [`Error::CompressionError`]: crate::Error::CompressionError
    pub fn create_with_compression_and_progress<P: AsRef<Path>>(
        source_dir: P,
        output_pak: P,
        compression: CompressionMethod,
        progress: ProgressCallback,
    ) -> Result<()> {
        let writer = LspkWriter::new(source_dir.as_ref())?.with_compression(compression);
        writer.write_with_progress(output_pak.as_ref(), progress)?;
        Ok(())
    }

    /// List contents of a PAK file
    ///
    /// # Errors
    ///
    /// Returns [`Error::Io`] if the PAK file cannot be opened.
    /// Returns [`Error::InvalidPakMagic`] if the file is not a valid PAK archive.
    ///
    /// [`Error::Io`]: crate::Error::Io
    /// [`Error::InvalidPakMagic`]: crate::Error::InvalidPakMagic
    pub fn list<P: AsRef<Path>>(pak_path: P) -> Result<Vec<String>> {
        Self::list_with_progress(pak_path, &|_| {})
    }

    /// List contents of a PAK file with progress callback
    ///
    /// # Errors
    ///
    /// Returns [`Error::Io`] if the PAK file cannot be opened.
    /// Returns [`Error::InvalidPakMagic`] if the file is not a valid PAK archive.
    ///
    /// [`Error::Io`]: crate::Error::Io
    /// [`Error::InvalidPakMagic`]: crate::Error::InvalidPakMagic
    pub fn list_with_progress<P: AsRef<Path>>(
        pak_path: P,
        progress: ProgressCallback,
    ) -> Result<Vec<String>> {
        let file = File::open(pak_path.as_ref())?;

        let mut reader = LspkReader::with_path(file, pak_path.as_ref());

        progress(&PakProgress {
            phase: PakPhase::ReadingHeader,
            current: 1,
            total: 1,
            current_file: None,
        });

        let entries = reader.list_files()?;

        progress(&PakProgress {
            phase: PakPhase::Complete,
            current: entries.len(),
            total: entries.len(),
            current_file: None,
        });

        Ok(entries
            .iter()
            .map(|e| e.path.to_string_lossy().to_string())
            .collect())
    }

    /// List contents of a PAK file with detailed information
    ///
    /// Returns full file entries including sizes and compression info.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Io`] if the PAK file cannot be opened.
    /// Returns [`Error::InvalidPakMagic`] if the file is not a valid PAK archive.
    ///
    /// [`Error::Io`]: crate::Error::Io
    /// [`Error::InvalidPakMagic`]: crate::Error::InvalidPakMagic
    pub(crate) fn list_detailed<P: AsRef<Path>>(pak_path: P) -> Result<Vec<FileTableEntry>> {
        let file = File::open(pak_path.as_ref())?;
        let mut reader = LspkReader::with_path(file, pak_path.as_ref());
        reader.list_files()
    }

    /// Extract specific files from a PAK to a directory
    ///
    /// Takes a list of file paths (as they appear in the PAK) and extracts only those files.
    /// File paths should match exactly as returned by `list()`.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Io`] if the PAK file cannot be opened or output directory cannot be created.
    /// Returns [`Error::InvalidPakMagic`] if the file is not a valid PAK archive.
    /// Returns [`Error::Lz4DecompressionFailed`] or [`Error::ZlibDecompressionFailed`] if file decompression fails.
    /// Returns [`Error::RequestedFilesNotFound`] if none of the requested files are found.
    /// Returns [`Error::PakExtractionPartialFailure`] if extraction completes with partial failures.
    ///
    /// [`Error::Io`]: crate::Error::Io
    /// [`Error::InvalidPakMagic`]: crate::Error::InvalidPakMagic
    /// [`Error::DecompressionError`]: crate::Error::DecompressionError
    /// [`Error::ConversionError`]: crate::Error::ConversionError
    pub fn extract_files<P: AsRef<Path>, S: AsRef<str>>(
        pak_path: P,
        output_dir: P,
        file_paths: &[S],
    ) -> Result<()> {
        Self::extract_files_with_progress(pak_path, output_dir, file_paths, &|_| {})
    }

    /// Extract specific files from a PAK to a directory with progress callback
    ///
    /// Takes a list of file paths (as they appear in the PAK) and extracts only those files.
    /// The callback receives [`PakProgress`] with phase and file information.
    /// Uses parallel decompression for improved performance on multi-core systems.
    /// Supports multi-part archives (e.g., `Textures.pak` with `Textures_1.pak`, `Textures_2.pak`).
    ///
    /// # Errors
    ///
    /// Returns [`Error::Io`] if the PAK file cannot be opened or output directory cannot be created.
    /// Returns [`Error::InvalidPakMagic`] if the file is not a valid PAK archive.
    /// Returns [`Error::Lz4DecompressionFailed`] or [`Error::ZlibDecompressionFailed`] if file decompression fails.
    /// Returns [`Error::RequestedFilesNotFound`] if none of the requested files are found.
    /// Returns [`Error::PakExtractionPartialFailure`] if extraction completes with partial failures.
    ///
    /// [`Error::Io`]: crate::Error::Io
    /// [`Error::InvalidPakMagic`]: crate::Error::InvalidPakMagic
    /// [`Error::DecompressionError`]: crate::Error::DecompressionError
    /// [`Error::ConversionError`]: crate::Error::ConversionError
    pub fn extract_files_with_progress<P: AsRef<Path>, S: AsRef<str>>(
        pak_path: P,
        output_dir: P,
        file_paths: &[S],
        progress: ProgressCallback,
    ) -> Result<()> {
        if file_paths.is_empty() {
            return Ok(());
        }

        let pak_path = pak_path.as_ref();
        let mut reader = LspkReader::with_path(File::open(pak_path)?, pak_path);

        // Build a set of requested paths for fast lookup
        let requested: std::collections::HashSet<&str> =
            file_paths.iter().map(std::convert::AsRef::as_ref).collect();

        // Get file list and filter to only requested files
        let all_entries = reader.list_files()?;
        let entries_to_extract: Vec<_> = all_entries
            .into_iter()
            .filter(|e| {
                // Skip .DS_Store files
                if e.path.file_name() == Some(std::ffi::OsStr::new(".DS_Store")) {
                    return false;
                }
                requested.contains(e.path.to_string_lossy().as_ref())
            })
            .collect();

        if entries_to_extract.is_empty() {
            return Err(Error::RequestedFilesNotFound);
        }

        std::fs::create_dir_all(&output_dir)?;

        let total_files = entries_to_extract.len();
        let processed = AtomicUsize::new(0);
        let output_dir = output_dir.as_ref();

        // Single-pass parallel extraction: each thread opens its own file handle
        let errors: Vec<(PathBuf, String)> = entries_to_extract
            .par_iter()
            .filter_map(|entry| {
                let file_name = entry.path.file_name().map_or_else(
                    || entry.path.to_string_lossy().to_string(),
                    |n| n.to_string_lossy().to_string(),
                );

                // Update progress (atomic)
                let current = processed.fetch_add(1, Ordering::SeqCst) + 1;
                progress(&PakProgress {
                    phase: PakPhase::DecompressingFiles,
                    current,
                    total: total_files,
                    current_file: Some(file_name.clone()),
                });

                // Get the correct part file path for this entry
                let part_path = match get_part_path(pak_path, entry.archive_part) {
                    Some(p) => p,
                    None => {
                        return Some((
                            entry.path.clone(),
                            format!(
                                "Cannot determine path for archive part {}",
                                entry.archive_part
                            ),
                        ));
                    }
                };

                // Open file handle for this thread, seek, and read
                let mut file = match File::open(&part_path) {
                    Ok(f) => f,
                    Err(e) => {
                        return Some((
                            entry.path.clone(),
                            format!("Failed to open {}: {e}", part_path.display()),
                        ));
                    }
                };

                if let Err(e) = file.seek(SeekFrom::Start(entry.offset)) {
                    return Some((entry.path.clone(), format!("Failed to seek: {e}")));
                }

                let mut compressed_data = vec![0u8; entry.size_compressed as usize];
                if let Err(e) = file.read_exact(&mut compressed_data) {
                    return Some((entry.path.clone(), format!("Failed to read: {e}")));
                }

                // Decompress
                let data = match decompress_data(
                    &compressed_data,
                    entry.compression,
                    entry.size_decompressed,
                ) {
                    Ok(data) => data,
                    Err(e) => {
                        tracing::warn!("Failed to decompress {}: {}", entry.path.display(), e);
                        return Some((entry.path.clone(), e.to_string()));
                    }
                };

                // Calculate output path (handle virtual texture subfolders)
                let output_path = if is_virtual_texture_file(&file_name) {
                    if let Some(subfolder) = get_virtual_texture_subfolder(&file_name) {
                        if let Some(parent) = entry.path.parent() {
                            output_dir.join(parent).join(&subfolder).join(&file_name)
                        } else {
                            output_dir.join(&subfolder).join(&file_name)
                        }
                    } else {
                        output_dir.join(&entry.path)
                    }
                } else {
                    output_dir.join(&entry.path)
                };

                // Create parent directories (idempotent)
                if let Some(parent) = output_path.parent()
                    && let Err(e) = std::fs::create_dir_all(parent)
                {
                    return Some((entry.path.clone(), format!("Failed to create dir: {e}")));
                }

                // Write file
                if let Err(e) = std::fs::write(&output_path, &data) {
                    return Some((entry.path.clone(), format!("Failed to write: {e}")));
                }

                None
            })
            .collect();

        // If there were errors, return a summary error
        if !errors.is_empty() {
            return Err(Error::PakExtractionPartialFailure {
                total: total_files,
                failed: errors.len(),
                first_error: errors[0].1.clone(),
            });
        }

        Ok(())
    }

    /// Read a single file's bytes from a PAK without writing to disk
    ///
    /// Returns the decompressed file contents, or an error if the file is not found.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Io`] if the PAK file cannot be opened.
    /// Returns [`Error::InvalidPakMagic`] if the file is not a valid PAK archive.
    /// Returns [`Error::FileNotFoundInPak`] if the requested file path is not in the archive.
    /// Returns [`Error::DecompressionError`] if file decompression fails.
    ///
    /// [`Error::Io`]: crate::Error::Io
    /// [`Error::InvalidPakMagic`]: crate::Error::InvalidPakMagic
    /// [`Error::FileNotFoundInPak`]: crate::Error::FileNotFoundInPak
    /// [`Error::DecompressionError`]: crate::Error::DecompressionError
    pub fn read_file_bytes<P: AsRef<Path>>(pak_path: P, file_path: &str) -> Result<Vec<u8>> {
        let file = File::open(pak_path.as_ref())?;
        let mut reader = LspkReader::with_path(file, pak_path.as_ref());

        // Get file list
        let entries = reader.list_files()?;

        // Find the requested file
        let entry = entries
            .into_iter()
            .find(|e| e.path.to_string_lossy() == file_path)
            .ok_or_else(|| Error::FileNotFoundInPak(file_path.to_string()))?;

        // Decompress and return
        reader.decompress_file(&entry)
    }

    /// Read multiple files' bytes from a PAK without writing to disk
    ///
    /// Returns a map of file paths to their decompressed contents.
    /// Files that fail to decompress are skipped with a warning.
    /// Uses parallel decompression for improved performance on multi-core systems.
    /// Supports multi-part archives (e.g., `Textures.pak` with `Textures_1.pak`, `Textures_2.pak`).
    ///
    /// # Errors
    ///
    /// Returns [`Error::Io`] if the PAK file cannot be opened.
    /// Returns [`Error::InvalidPakMagic`] if the file is not a valid PAK archive.
    /// Returns [`Error::ConversionError`] if a required archive part file cannot be found.
    ///
    /// [`Error::Io`]: crate::Error::Io
    /// [`Error::InvalidPakMagic`]: crate::Error::InvalidPakMagic
    /// [`Error::ConversionError`]: crate::Error::ConversionError
    pub fn read_files_bytes<P: AsRef<Path>, S: AsRef<str>>(
        pak_path: P,
        file_paths: &[S],
    ) -> Result<HashMap<String, Vec<u8>>> {
        if file_paths.is_empty() {
            return Ok(HashMap::new());
        }

        let pak_path = pak_path.as_ref();
        let mut reader = LspkReader::with_path(File::open(pak_path)?, pak_path);

        // Build a set of requested paths
        let requested: std::collections::HashSet<&str> =
            file_paths.iter().map(std::convert::AsRef::as_ref).collect();

        // Get file list and filter
        let all_entries = reader.list_files()?;
        let entries_to_read: Vec<_> = all_entries
            .into_iter()
            .filter(|e| requested.contains(e.path.to_string_lossy().as_ref()))
            .collect();

        // Group entries by archive part for multi-part PAK support
        let mut entries_by_part: HashMap<u8, Vec<FileTableEntry>> = HashMap::new();
        for entry in entries_to_read {
            entries_by_part
                .entry(entry.archive_part)
                .or_default()
                .push(entry);
        }

        // Phase 1: Read all compressed data sequentially from each part file
        let mut compressed_files: Vec<(String, CompressedFile)> = Vec::new();

        for (part, part_entries) in &entries_by_part {
            let part_path =
                get_part_path(pak_path, *part).ok_or(Error::ArchivePartNotFound { part: *part })?;

            if !part_path.exists() {
                tracing::warn!("Archive part file not found: {}", part_path.display());
                continue;
            }

            let mut part_file = File::open(&part_path)?;

            for entry in part_entries {
                // Seek and read compressed data from the correct part file
                if part_file.seek(SeekFrom::Start(entry.offset)).is_err() {
                    tracing::warn!(
                        "Failed to seek to {} in {}",
                        entry.path.display(),
                        part_path.display()
                    );
                    continue;
                }

                let mut compressed_data = vec![0u8; entry.size_compressed as usize];
                if part_file.read_exact(&mut compressed_data).is_err() {
                    tracing::warn!(
                        "Failed to read {} from {}",
                        entry.path.display(),
                        part_path.display()
                    );
                    continue;
                }

                compressed_files.push((
                    entry.path.to_string_lossy().to_string(),
                    CompressedFile {
                        entry: entry.clone(),
                        compressed_data,
                    },
                ));
            }
        }

        // Phase 2: Decompress in parallel and collect results
        let decompressed: Vec<(String, Vec<u8>)> = compressed_files
            .par_iter()
            .filter_map(|(path, cf)| {
                match decompress_data(
                    &cf.compressed_data,
                    cf.entry.compression,
                    cf.entry.size_decompressed,
                ) {
                    Ok(data) => Some((path.clone(), data)),
                    Err(e) => {
                        tracing::warn!("Failed to decompress {}: {}", path, e);
                        None
                    }
                }
            })
            .collect();

        // Collect into HashMap
        Ok(decompressed.into_iter().collect())
    }

    /// Extract meta.lsx from a PAK
    ///
    /// # Errors
    ///
    /// Returns [`Error::Io`] if the PAK file cannot be opened.
    /// Returns [`Error::InvalidPakMagic`] if the file is not a valid PAK archive.
    /// Returns [`Error::FileNotFoundInPak`] if `meta.lsx` is not found in the archive.
    /// Returns [`Error::ConversionError`] if `meta.lsx` contains invalid UTF-8.
    ///
    /// [`Error::Io`]: crate::Error::Io
    /// [`Error::InvalidPakMagic`]: crate::Error::InvalidPakMagic
    /// [`Error::FileNotFoundInPak`]: crate::Error::FileNotFoundInPak
    /// [`Error::ConversionError`]: crate::Error::ConversionError
    pub fn extract_meta<P: AsRef<Path>>(pak_path: P) -> Result<String> {
        let file = File::open(pak_path.as_ref())?;

        let mut reader = LspkReader::with_path(file, pak_path.as_ref());
        let contents = reader.read_all(None)?;

        // Find meta.lsx
        let meta_file = contents
            .files
            .iter()
            .find(|f| {
                let path = &f.path;
                let mut components = path.components();

                // Look for Mods/*/meta.lsx pattern
                if let Some(first) = components.next()
                    && first.as_os_str() == "Mods"
                    && components.next().is_some()
                    && let Some(third) = components.next()
                {
                    return third.as_os_str() == "meta.lsx";
                }
                false
            })
            .ok_or_else(|| Error::FileNotFoundInPak("meta.lsx".to_string()))?;

        String::from_utf8(meta_file.data.clone()).map_err(Error::from)
    }
}