warcraft-rs 0.7.0

Unified CLI for World of Warcraft file format parsing, conversion, and validation
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
//! Integration tests for MPQ extraction functionality
//!
//! These tests validate that the MPQ extraction works correctly in practice,
//! testing real-world scenarios with actual WoW MPQ archives.

use anyhow::{Context, Result};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use std::time::Instant;
use tempfile::TempDir;
use wow_mpq::Archive;

/// Test configuration for different MPQ archives
struct TestArchive {
    path: PathBuf,
    name: String,
    expected_min_files: usize,
}

/// Test result tracking
#[derive(Debug)]
struct ExtractionTestResult {
    archive_name: String,
    files_requested: usize,
    files_extracted: usize,
    files_failed: usize,
    total_size: u64,
    duration_ms: u128,
    errors: Vec<String>,
}

/// Get available test archives from the WoW installation directories
fn get_test_archives() -> Vec<TestArchive> {
    let mut archives = Vec::new();

    // WoW 1.12.1 archives
    let wow_1121_data = "/home/danielsreichenbach/Downloads/wow/1.12.1/Data";
    if Path::new(wow_1121_data).exists() {
        for entry in ["base.MPQ", "dbc.MPQ", "interface.MPQ", "patch.MPQ"] {
            let path = Path::new(wow_1121_data).join(entry);
            if path.exists() {
                archives.push(TestArchive {
                    path,
                    name: format!("1.12.1/{}", entry),
                    expected_min_files: match entry {
                        "base.MPQ" => 1000,     // Large archive
                        "dbc.MPQ" => 50,        // Small archive
                        "interface.MPQ" => 100, // Medium archive
                        "patch.MPQ" => 10,      // Tiny archive
                        _ => 1,
                    },
                });
            }
        }
    }

    // WoW 3.3.5a archives
    let wow_335a_data = "/home/danielsreichenbach/Downloads/wow/3.3.5a/Data";
    if Path::new(wow_335a_data).exists() {
        for entry in ["common.MPQ", "expansion.MPQ", "lichking.MPQ", "patch.MPQ"] {
            let path = Path::new(wow_335a_data).join(entry);
            if path.exists() {
                archives.push(TestArchive {
                    path,
                    name: format!("3.3.5a/{}", entry),
                    expected_min_files: match entry {
                        "common.MPQ" => 1000,
                        "expansion.MPQ" => 500,
                        "lichking.MPQ" => 500,
                        "patch.MPQ" => 10,
                        _ => 1,
                    },
                });
            }
        }
    }

    // WoW 4.3.4 archives (English)
    let wow_434_data = "/home/danielsreichenbach/Downloads/wow/4.3.4/4.3.4/Data";
    if Path::new(wow_434_data).exists() {
        for entry in ["world.MPQ", "world2.MPQ"] {
            let path = Path::new(wow_434_data).join(entry);
            if path.exists() {
                archives.push(TestArchive {
                    path,
                    name: format!("4.3.4/{}", entry),
                    expected_min_files: 1000,
                });
            }
        }
    }

    archives
}

/// Test individual file extraction (2-3 files)
#[test]
fn test_individual_file_extraction() -> Result<()> {
    let archives = get_test_archives();
    if archives.is_empty() {
        println!("No test archives found, skipping individual file extraction test");
        return Ok(());
    }

    let test_archive = &archives[0]; // Use first available archive
    let temp_dir = TempDir::new().context("Failed to create temp directory")?;

    println!(
        "Testing individual file extraction with: {}",
        test_archive.name
    );

    // Open the archive and get a list of files
    let mut archive = Archive::open(&test_archive.path).with_context(|| {
        format!(
            "Failed to open test archive: {}",
            test_archive.path.display()
        )
    })?;

    let file_list = archive.list().context("Failed to list archive files")?;

    if file_list.is_empty() {
        return Err(anyhow::anyhow!("Archive contains no files"));
    }

    // Select 2-3 files of different types for testing
    let mut test_files = Vec::new();
    let mut file_types_found = HashMap::new();

    for entry in &file_list {
        let extension = Path::new(&entry.name)
            .extension()
            .and_then(|e| e.to_str())
            .unwrap_or("")
            .to_lowercase();

        if !file_types_found.contains_key(&extension) && test_files.len() < 3 {
            file_types_found.insert(extension.clone(), entry.name.clone());
            test_files.push(entry.name.clone());
        }

        if test_files.len() >= 3 {
            break;
        }
    }

    if test_files.is_empty() {
        test_files.push(file_list[0].name.clone());
    }

    println!(
        "Testing extraction of {} files: {:?}",
        test_files.len(),
        test_files
    );

    // Test extraction
    let start_time = Instant::now();
    let mut success_count = 0;
    let mut error_count = 0;
    let mut total_size = 0u64;

    for file_name in &test_files {
        match archive.read_file(file_name) {
            Ok(data) => {
                // Write to temp directory
                let output_path = temp_dir.path().join(sanitize_filename(file_name));
                fs::write(&output_path, &data).with_context(|| {
                    format!("Failed to write extracted file: {}", output_path.display())
                })?;

                // Verify file size
                let written_size = fs::metadata(&output_path)?.len();
                assert_eq!(
                    written_size,
                    data.len() as u64,
                    "Written file size mismatch for {}",
                    file_name
                );

                total_size += data.len() as u64;
                success_count += 1;
                println!("✓ Extracted {}: {} bytes", file_name, data.len());
            }
            Err(e) => {
                error_count += 1;
                println!("✗ Failed to extract {}: {}", file_name, e);
            }
        }
    }

    let duration = start_time.elapsed();

    println!("Individual file extraction results:");
    println!("  Files requested: {}", test_files.len());
    println!("  Files extracted: {}", success_count);
    println!("  Files failed: {}", error_count);
    println!("  Total size: {} bytes", total_size);
    println!("  Duration: {}ms", duration.as_millis());

    // Assert at least some files were extracted successfully
    assert!(success_count > 0, "No files were extracted successfully");
    assert!(
        success_count >= test_files.len() / 2,
        "Too many extraction failures: {}/{}",
        error_count,
        test_files.len()
    );

    Ok(())
}

/// Test medium batch extraction (50-100 files)
#[test]
fn test_medium_batch_extraction() -> Result<()> {
    let archives = get_test_archives();
    if archives.is_empty() {
        println!("No test archives found, skipping medium batch extraction test");
        return Ok(());
    }

    // Find an archive with enough files
    let test_archive = archives
        .iter()
        .find(|a| a.expected_min_files >= 100)
        .unwrap_or(&archives[0]);

    let temp_dir = TempDir::new().context("Failed to create temp directory")?;

    println!(
        "Testing medium batch extraction with: {}",
        test_archive.name
    );

    let result = test_batch_extraction(
        &test_archive.path,
        temp_dir.path(),
        50,    // Target file count
        100,   // Max file count
        false, // Don't preserve paths for this test
    )?;

    println!("Medium batch extraction results:");
    print_test_result(&result);

    // Validate results
    assert!(result.files_extracted > 0, "No files were extracted");
    assert!(
        result.files_extracted >= result.files_requested / 2,
        "Too many extraction failures: {}/{}",
        result.files_failed,
        result.files_requested
    );

    // Performance check - should extract reasonably quickly
    let files_per_second = result.files_extracted as f64 / (result.duration_ms as f64 / 1000.0);
    println!("  Performance: {:.2} files/second", files_per_second);
    assert!(
        files_per_second > 1.0,
        "Extraction too slow: {:.2} files/second",
        files_per_second
    );

    Ok(())
}

/// Test large batch extraction (1000+ files)
#[test]
fn test_large_batch_extraction() -> Result<()> {
    let archives = get_test_archives();
    if archives.is_empty() {
        println!("No test archives found, skipping large batch extraction test");
        return Ok(());
    }

    // Find an archive with many files
    let test_archive = archives
        .iter()
        .find(|a| a.expected_min_files >= 1000)
        .unwrap_or(&archives[0]);

    let temp_dir = TempDir::new().context("Failed to create temp directory")?;

    println!("Testing large batch extraction with: {}", test_archive.name);

    let result = test_batch_extraction(
        &test_archive.path,
        temp_dir.path(),
        500,   // Target file count
        1500,  // Max file count
        false, // Don't preserve paths for this test
    )?;

    println!("Large batch extraction results:");
    print_test_result(&result);

    // Validate results
    assert!(result.files_extracted > 0, "No files were extracted");
    assert!(
        result.files_extracted >= result.files_requested / 2,
        "Too many extraction failures: {}/{}",
        result.files_failed,
        result.files_requested
    );

    // Performance check - batching should maintain good throughput
    let files_per_second = result.files_extracted as f64 / (result.duration_ms as f64 / 1000.0);
    println!("  Performance: {:.2} files/second", files_per_second);
    assert!(
        files_per_second > 5.0,
        "Large extraction too slow: {:.2} files/second",
        files_per_second
    );

    Ok(())
}

/// Test directory structure preservation
#[test]
fn test_preserve_paths() -> Result<()> {
    let archives = get_test_archives();
    if archives.is_empty() {
        println!("No test archives found, skipping preserve paths test");
        return Ok(());
    }

    let test_archive = &archives[0];
    let temp_dir = TempDir::new().context("Failed to create temp directory")?;

    println!(
        "Testing directory structure preservation with: {}",
        test_archive.name
    );

    let result = test_batch_extraction(
        &test_archive.path,
        temp_dir.path(),
        20, // Smaller number for this test
        50,
        true, // Preserve paths
    )?;

    println!("Preserve paths extraction results:");
    print_test_result(&result);

    // Validate that directories were created
    let mut found_subdirs = false;
    for entry in fs::read_dir(temp_dir.path())? {
        let entry = entry?;
        if entry.file_type()?.is_dir() {
            found_subdirs = true;
            println!(
                "  Found subdirectory: {}",
                entry.file_name().to_string_lossy()
            );
        }
    }

    if result.files_extracted > 0 {
        // We should have at least some directory structure if files were extracted
        // (unless all files were in the root, which is unlikely)
        println!("  Directory structure preserved: {}", found_subdirs);
    }

    Ok(())
}

/// Test error handling with missing files
#[test]
fn test_error_handling_missing_files() -> Result<()> {
    let archives = get_test_archives();
    if archives.is_empty() {
        println!("No test archives found, skipping error handling test");
        return Ok(());
    }

    let test_archive = &archives[0];
    let _temp_dir = TempDir::new().context("Failed to create temp directory")?;

    println!(
        "Testing error handling with missing files: {}",
        test_archive.name
    );

    let mut archive = Archive::open(&test_archive.path).with_context(|| {
        format!(
            "Failed to open test archive: {}",
            test_archive.path.display()
        )
    })?;

    // Try to extract files that don't exist
    let nonexistent_files = vec![
        "does_not_exist.txt",
        "fake/path/file.dat",
        "another_missing_file.bin",
    ];

    let mut error_count = 0;

    for file_name in &nonexistent_files {
        match archive.read_file(file_name) {
            Ok(_) => {
                println!("  Unexpected success for: {}", file_name);
            }
            Err(e) => {
                error_count += 1;
                println!("  ✓ Expected error for {}: {}", file_name, e);
            }
        }
    }

    // All files should have failed
    assert_eq!(
        error_count,
        nonexistent_files.len(),
        "Expected all nonexistent files to fail"
    );

    println!(
        "Error handling test passed: {}/{} expected failures",
        error_count,
        nonexistent_files.len()
    );

    Ok(())
}

/// Test data integrity verification
#[test]
fn test_data_integrity() -> Result<()> {
    let archives = get_test_archives();
    if archives.is_empty() {
        println!("No test archives found, skipping data integrity test");
        return Ok(());
    }

    let test_archive = &archives[0];
    let temp_dir = TempDir::new().context("Failed to create temp directory")?;

    println!("Testing data integrity with: {}", test_archive.name);

    let mut archive = Archive::open(&test_archive.path).with_context(|| {
        format!(
            "Failed to open test archive: {}",
            test_archive.path.display()
        )
    })?;

    let file_list = archive.list().context("Failed to list archive files")?;

    if file_list.is_empty() {
        println!("  Archive contains no files, skipping integrity test");
        return Ok(());
    }

    // Test up to 10 files for integrity
    let test_count = std::cmp::min(10, file_list.len());
    let mut integrity_checks = 0;

    for entry in file_list.iter().take(test_count) {
        match archive.read_file(&entry.name) {
            Ok(data) => {
                // Verify the data size matches the expected size
                assert_eq!(
                    data.len() as u64,
                    entry.size,
                    "Data size mismatch for {}: got {} bytes, expected {} bytes",
                    entry.name,
                    data.len(),
                    entry.size
                );

                // Write and re-read to verify data integrity
                let output_path = temp_dir.path().join(sanitize_filename(&entry.name));
                fs::write(&output_path, &data)
                    .with_context(|| format!("Failed to write file: {}", output_path.display()))?;

                let written_data = fs::read(&output_path).with_context(|| {
                    format!("Failed to read back file: {}", output_path.display())
                })?;

                assert_eq!(
                    data, written_data,
                    "Data integrity check failed for {}",
                    entry.name
                );

                integrity_checks += 1;
                println!(
                    "  ✓ Integrity verified for {}: {} bytes",
                    entry.name,
                    data.len()
                );
            }
            Err(e) => {
                println!("  âš  Failed to read {}: {}", entry.name, e);
            }
        }
    }

    assert!(integrity_checks > 0, "No files passed integrity check");
    println!(
        "Data integrity test passed: {}/{} files verified",
        integrity_checks, test_count
    );

    Ok(())
}

// Helper functions

/// Extract a batch of files and return test results
fn test_batch_extraction(
    archive_path: &Path,
    output_dir: &Path,
    target_files: usize,
    max_files: usize,
    preserve_paths: bool,
) -> Result<ExtractionTestResult> {
    let mut archive = Archive::open(archive_path)
        .with_context(|| format!("Failed to open archive: {}", archive_path.display()))?;

    let file_list = archive.list().context("Failed to list archive files")?;

    if file_list.is_empty() {
        return Err(anyhow::anyhow!("Archive contains no files"));
    }

    // Select files to extract
    let files_to_extract: Vec<String> = file_list
        .iter()
        .take(std::cmp::min(
            max_files,
            std::cmp::max(target_files, file_list.len()),
        ))
        .map(|e| e.name.clone())
        .collect();

    let files_requested = files_to_extract.len();

    println!("  Extracting {} files...", files_requested);

    let start_time = Instant::now();
    let mut files_extracted = 0;
    let mut files_failed = 0;
    let mut total_size = 0u64;
    let mut errors = Vec::new();

    for file_name in &files_to_extract {
        match archive.read_file(file_name) {
            Ok(data) => {
                let output_path = if preserve_paths {
                    // Convert MPQ path to system path and preserve directory structure
                    let system_path = file_name.replace('\\', "/");
                    output_dir.join(&system_path)
                } else {
                    // Extract just the filename
                    output_dir.join(sanitize_filename(file_name))
                };

                // Create parent directories if needed
                if let Some(parent) = output_path.parent() {
                    fs::create_dir_all(parent).with_context(|| {
                        format!("Failed to create directory: {}", parent.display())
                    })?;
                }

                match fs::write(&output_path, &data) {
                    Ok(()) => {
                        total_size += data.len() as u64;
                        files_extracted += 1;
                    }
                    Err(e) => {
                        files_failed += 1;
                        errors.push(format!("Failed to write {}: {}", file_name, e));
                    }
                }
            }
            Err(e) => {
                files_failed += 1;
                errors.push(format!("Failed to read {}: {}", file_name, e));
            }
        }
    }

    let duration = start_time.elapsed();

    Ok(ExtractionTestResult {
        archive_name: archive_path
            .file_name()
            .unwrap_or_default()
            .to_string_lossy()
            .to_string(),
        files_requested,
        files_extracted,
        files_failed,
        total_size,
        duration_ms: duration.as_millis(),
        errors,
    })
}

/// Print formatted test results
fn print_test_result(result: &ExtractionTestResult) {
    println!("  Archive: {}", result.archive_name);
    println!("  Files requested: {}", result.files_requested);
    println!("  Files extracted: {}", result.files_extracted);
    println!("  Files failed: {}", result.files_failed);
    println!(
        "  Total size: {} bytes ({:.2} MB)",
        result.total_size,
        result.total_size as f64 / 1024.0 / 1024.0
    );
    println!("  Duration: {}ms", result.duration_ms);

    if !result.errors.is_empty() {
        println!("  Errors (showing first 5):");
        for error in result.errors.iter().take(5) {
            println!("    - {}", error);
        }
        if result.errors.len() > 5 {
            println!("    ... and {} more errors", result.errors.len() - 5);
        }
    }
}

/// Sanitize filename for safe filesystem operations
fn sanitize_filename(filename: &str) -> String {
    // Convert MPQ path to just the filename, replacing invalid characters
    let name = filename.split('\\').next_back().unwrap_or(filename);
    name.chars()
        .map(|c| match c {
            '/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|' => '_',
            c => c,
        })
        .collect()
}

/// Performance benchmark for large extractions
#[test]
fn test_extraction_performance() -> Result<()> {
    let archives = get_test_archives();
    if archives.is_empty() {
        println!("No test archives found, skipping performance test");
        return Ok(());
    }

    // Find the largest available archive
    let test_archive = archives
        .iter()
        .max_by_key(|a| a.expected_min_files)
        .unwrap();

    let temp_dir = TempDir::new().context("Failed to create temp directory")?;

    println!("Performance testing with: {}", test_archive.name);

    // Test different batch sizes
    let batch_sizes = [10, 50, 100, 200];

    for batch_size in batch_sizes {
        let result = test_batch_extraction(
            &test_archive.path,
            temp_dir.path(),
            batch_size,
            batch_size,
            false,
        )?;

        if result.files_extracted > 0 {
            let files_per_second =
                result.files_extracted as f64 / (result.duration_ms as f64 / 1000.0);
            let mb_per_second =
                (result.total_size as f64 / 1024.0 / 1024.0) / (result.duration_ms as f64 / 1000.0);

            println!(
                "  Batch size {}: {:.2} files/sec, {:.2} MB/sec",
                batch_size, files_per_second, mb_per_second
            );
        }

        // Clean up between tests
        for entry in fs::read_dir(temp_dir.path())? {
            let entry = entry?;
            if entry.file_type()?.is_file() {
                fs::remove_file(entry.path())?;
            } else if entry.file_type()?.is_dir() {
                fs::remove_dir_all(entry.path())?;
            }
        }
    }

    Ok(())
}