wow-mpq 0.6.4

High-performance parser for World of Warcraft MPQ archives with parallel processing support
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
//! Test data generation utilities
//!
//! Replaces the functionality of generate_test_data.py

use rand::{Rng, SeedableRng, rngs::StdRng};
use std::fs;
use std::path::{Path, PathBuf};

/// Configuration for test data generation
#[derive(Debug, Clone)]
pub struct TestDataConfig {
    /// Name of the test configuration
    pub name: String,
    /// Human-readable description of what this configuration creates
    pub description: String,
    /// List of directories to create in the test structure
    pub directories: Vec<String>,
    /// List of files to create with their configurations
    pub files: Vec<FileConfig>,
}

/// Configuration for a single test file
#[derive(Debug, Clone)]
pub struct FileConfig {
    /// Relative path where the file should be created
    pub path: String,
    /// Type of content to generate for this file
    pub file_type: FileType,
    /// Size of the file in kilobytes
    pub size_kb: usize,
}

/// Type of file content to generate
#[derive(Debug, Clone, Copy)]
pub enum FileType {
    /// Generate text content with repeating patterns
    Text,
    /// Generate pseudo-random binary content
    Binary,
    /// Create an empty file
    Empty,
}

/// Content compressibility level for generated data
#[derive(Debug, Clone, Copy)]
pub enum Compressibility {
    /// Highly compressible data (repeated characters)
    High,
    /// Medium compressibility (repeating patterns)
    Medium,
    /// Low compressibility (pseudo-random data)
    Low,
}

/// Result of test data generation
#[derive(Debug)]
pub struct GenerationResult {
    /// Path to the base directory where test data was created
    pub base_path: PathBuf,
    /// List of files that were successfully created
    pub files_created: Vec<String>,
}

impl TestDataConfig {
    /// Create a simple flat structure with text files
    pub fn simple() -> Self {
        Self {
            name: "simple".to_string(),
            description: "Simple flat structure with text files".to_string(),
            directories: vec![],
            files: vec![
                FileConfig {
                    path: "readme.txt".to_string(),
                    file_type: FileType::Text,
                    size_kb: 2,
                },
                FileConfig {
                    path: "data.txt".to_string(),
                    file_type: FileType::Text,
                    size_kb: 5,
                },
                FileConfig {
                    path: "config.ini".to_string(),
                    file_type: FileType::Text,
                    size_kb: 1,
                },
            ],
        }
    }

    /// Create a game-like asset structure
    pub fn game_assets() -> Self {
        Self {
            name: "game_assets".to_string(),
            description: "Game-like asset structure".to_string(),
            directories: vec![
                "textures".to_string(),
                "models".to_string(),
                "sounds".to_string(),
                "scripts".to_string(),
            ],
            files: vec![
                FileConfig {
                    path: "textures/player.dds".to_string(),
                    file_type: FileType::Binary,
                    size_kb: 256,
                },
                FileConfig {
                    path: "textures/terrain.dds".to_string(),
                    file_type: FileType::Binary,
                    size_kb: 512,
                },
                FileConfig {
                    path: "models/player.mdx".to_string(),
                    file_type: FileType::Binary,
                    size_kb: 128,
                },
                FileConfig {
                    path: "models/building.mdx".to_string(),
                    file_type: FileType::Binary,
                    size_kb: 64,
                },
                FileConfig {
                    path: "sounds/music/theme.mp3".to_string(),
                    file_type: FileType::Binary,
                    size_kb: 1024,
                },
                FileConfig {
                    path: "sounds/effects/click.wav".to_string(),
                    file_type: FileType::Binary,
                    size_kb: 32,
                },
                FileConfig {
                    path: "scripts/main.lua".to_string(),
                    file_type: FileType::Text,
                    size_kb: 10,
                },
                FileConfig {
                    path: "scripts/utils.lua".to_string(),
                    file_type: FileType::Text,
                    size_kb: 5,
                },
            ],
        }
    }

    /// Create a deeply nested directory structure
    pub fn nested() -> Self {
        Self {
            name: "nested".to_string(),
            description: "Deeply nested directory structure".to_string(),
            directories: vec![],
            files: vec![
                FileConfig {
                    path: "level1/readme.txt".to_string(),
                    file_type: FileType::Text,
                    size_kb: 1,
                },
                FileConfig {
                    path: "level1/level2/data.bin".to_string(),
                    file_type: FileType::Binary,
                    size_kb: 10,
                },
                FileConfig {
                    path: "level1/level2/level3/config.xml".to_string(),
                    file_type: FileType::Text,
                    size_kb: 2,
                },
                FileConfig {
                    path: "level1/level2/level3/level4/deep.txt".to_string(),
                    file_type: FileType::Text,
                    size_kb: 1,
                },
            ],
        }
    }

    /// Create files with various sizes
    pub fn mixed_sizes() -> Self {
        Self {
            name: "mixed_sizes".to_string(),
            description: "Mix of file sizes from tiny to large".to_string(),
            directories: vec![],
            files: vec![
                FileConfig {
                    path: "tiny.txt".to_string(),
                    file_type: FileType::Empty,
                    size_kb: 0,
                },
                FileConfig {
                    path: "small.dat".to_string(),
                    file_type: FileType::Binary,
                    size_kb: 1,
                },
                FileConfig {
                    path: "medium.bin".to_string(),
                    file_type: FileType::Binary,
                    size_kb: 100,
                },
                FileConfig {
                    path: "large.pak".to_string(),
                    file_type: FileType::Binary,
                    size_kb: 1024,
                },
                FileConfig {
                    path: "config.json".to_string(),
                    file_type: FileType::Text,
                    size_kb: 5,
                },
            ],
        }
    }

    /// Create files with special characters and spaces
    pub fn special_names() -> Self {
        Self {
            name: "special_names".to_string(),
            description: "Files with special characters and spaces".to_string(),
            directories: vec![],
            files: vec![
                FileConfig {
                    path: "file with spaces.txt".to_string(),
                    file_type: FileType::Text,
                    size_kb: 1,
                },
                FileConfig {
                    path: "special-chars_$#@.dat".to_string(),
                    file_type: FileType::Binary,
                    size_kb: 5,
                },
                FileConfig {
                    path: "unicode_文件.txt".to_string(),
                    file_type: FileType::Text,
                    size_kb: 2,
                },
                FileConfig {
                    path: ".hidden_file".to_string(),
                    file_type: FileType::Text,
                    size_kb: 1,
                },
            ],
        }
    }

    /// Get all predefined configurations
    pub fn all_configs() -> Vec<Self> {
        vec![
            Self::simple(),
            Self::game_assets(),
            Self::nested(),
            Self::mixed_sizes(),
            Self::special_names(),
        ]
    }
}

/// Generate test data with specified configuration
pub fn generate_test_data(
    base_path: &Path,
    config: &TestDataConfig,
) -> Result<GenerationResult, std::io::Error> {
    let output_dir = base_path.join(&config.name);

    // Clean up if exists
    if output_dir.exists() {
        fs::remove_dir_all(&output_dir)?;
    }

    // Create base directory
    fs::create_dir_all(&output_dir)?;

    // Create subdirectories
    for dir in &config.directories {
        fs::create_dir_all(output_dir.join(dir))?;
    }

    let mut files_created = Vec::new();

    // Create files
    for file_config in &config.files {
        let file_path = output_dir.join(&file_config.path);

        // Ensure parent directory exists
        if let Some(parent) = file_path.parent() {
            fs::create_dir_all(parent)?;
        }

        match file_config.file_type {
            FileType::Text => {
                let content = generate_text_content(file_config.size_kb, Compressibility::Medium);
                fs::write(&file_path, content)?;
            }
            FileType::Binary => {
                let content = generate_binary_content(file_config.size_kb);
                fs::write(&file_path, content)?;
            }
            FileType::Empty => {
                fs::File::create(&file_path)?;
            }
        }

        files_created.push(file_config.path.clone());
    }

    Ok(GenerationResult {
        base_path: output_dir,
        files_created,
    })
}

/// Generate text content with specified size and compressibility
fn generate_text_content(size_kb: usize, compressibility: Compressibility) -> Vec<u8> {
    let target_size = size_kb * 1024;
    let mut content = Vec::with_capacity(target_size);
    let mut rng = StdRng::seed_from_u64(42);

    match compressibility {
        Compressibility::High => {
            // Highly compressible - repeated character
            content.resize(target_size, b'A');
        }
        Compressibility::Medium => {
            // Repeating pattern
            let pattern = b"The quick brown fox jumps over the lazy dog. ";
            while content.len() < target_size {
                let remaining = target_size - content.len();
                let to_copy = remaining.min(pattern.len());
                content.extend_from_slice(&pattern[..to_copy]);
            }
        }
        Compressibility::Low => {
            // Mix of lorem ipsum and structured data
            let words = [
                "lorem",
                "ipsum",
                "dolor",
                "sit",
                "amet",
                "consectetur",
                "adipiscing",
                "elit",
                "sed",
                "do",
                "eiusmod",
                "tempor",
                "incididunt",
                "ut",
                "labore",
                "et",
                "dolore",
                "magna",
            ];

            while content.len() < target_size {
                // Alternate between text paragraphs and structured data
                if rng.random_bool(0.5) {
                    // Generate paragraph
                    let word_count = rng.random_range(50..200);
                    for _ in 0..word_count {
                        let word = words[rng.random_range(0..words.len())];
                        content.extend_from_slice(word.as_bytes());
                        content.push(b' ');
                    }
                    content.extend_from_slice(b"\n\n");
                } else {
                    // Generate structured data (JSON-like or CSV-like)
                    if rng.random_bool(0.5) {
                        let id = rng.random_range(1..1000);
                        let value: String = (0..10)
                            .map(|_| (b'a' + rng.random::<u8>() % 26) as char)
                            .collect();
                        let json = format!("{{\"id\": {id}, \"value\": \"{value}\"}}\n");
                        content.extend_from_slice(json.as_bytes());
                    } else {
                        let csv = format!(
                            "{},{},{:.3}\n",
                            rng.random_range(1..100),
                            (b'A' + rng.random::<u8>() % 26) as char,
                            rng.random::<f32>()
                        );
                        content.extend_from_slice(csv.as_bytes());
                    }
                }
            }
        }
    }

    content.truncate(target_size);
    content
}

/// Generate binary content with specified size
fn generate_binary_content(size_kb: usize) -> Vec<u8> {
    let mut rng = StdRng::seed_from_u64(42);
    let size_bytes = size_kb * 1024;
    let mut content = vec![0u8; size_bytes];
    rng.fill(&mut content[..]);
    content
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn test_simple_data_generation() {
        let temp_dir = TempDir::new().unwrap();
        let config = TestDataConfig::simple();
        let result = generate_test_data(temp_dir.path(), &config).unwrap();

        assert_eq!(result.files_created.len(), 3);
        assert!(result.base_path.join("readme.txt").exists());
        assert!(result.base_path.join("data.txt").exists());
        assert!(result.base_path.join("config.ini").exists());
    }

    #[test]
    fn test_nested_structure() {
        let temp_dir = TempDir::new().unwrap();
        let config = TestDataConfig::nested();
        let result = generate_test_data(temp_dir.path(), &config).unwrap();

        assert!(
            result
                .base_path
                .join("level1/level2/level3/level4/deep.txt")
                .exists()
        );
    }
}