bl4-cli 0.5.20

Command-line tool for Borderlands 4 save editing
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
//! NCS file command handlers
//!
//! Handlers for checking, decompressing, and extracting NCS files.

use anyhow::Result;
use std::collections::HashMap;
use std::fs;
use std::path::Path;

/// Handle the ExtractCommand::NcsCheck command
///
/// Checks if a file is a valid NCS file.
pub fn handle_check(input: &Path) -> Result<()> {
    let data = fs::read(input)?;

    if bl4_ncs::is_ncs_manifest(&data) {
        let manifest = bl4_ncs::NcsManifest::parse(&data)?;
        println!("{:?}: valid NCS manifest file", input);
        println!("  Entry count: {}", manifest.entry_count);
        println!("  Entries found: {}", manifest.entries.len());
    } else if bl4_ncs::is_ncs(&data) {
        let header = bl4_ncs::NcsHeader::from_bytes(&data)?;
        println!("{:?}: valid NCS data file", input);
        println!("  Version: {}", header.version);
        println!("  Compressed: {}", header.is_compressed());
    } else {
        println!("{:?}: NOT a valid NCS file", input);
        if data.len() >= 5 {
            println!(
                "  Header bytes: {:02x} {:02x} {:02x} {:02x} {:02x}",
                data[0], data[1], data[2], data[3], data[4]
            );
        }
    }

    Ok(())
}

/// Handle the ExtractCommand::NcsDecompress command
///
/// Decompresses an NCS file.
pub fn handle_decompress(input: &Path, output: Option<std::path::PathBuf>) -> Result<()> {
    let data = fs::read(input)?;

    if bl4_ncs::is_ncs_manifest(&data) {
        let manifest = bl4_ncs::NcsManifest::parse(&data)?;
        println!("NCS manifest file (not compressed)");
        println!("  Entry count: {}", manifest.entry_count);
        println!("  Entries:");
        for entry in &manifest.entries {
            println!("    - {}", entry.filename);
        }
        return Ok(());
    }

    if !bl4_ncs::is_ncs(&data) {
        anyhow::bail!("Not a valid NCS file");
    }

    println!("Parsing NCS header...");
    let header = bl4_ncs::NcsHeader::from_bytes(&data)?;
    println!(
        "  Version: {}, compressed: {}",
        header.version,
        header.is_compressed()
    );
    println!("Decompressing...");
    let decompressed = bl4_ncs::decompress_ncs(&data)?;

    let out_path = output.unwrap_or_else(|| {
        let mut p = input.to_path_buf();
        let stem = p.file_stem().unwrap_or_default().to_string_lossy();
        let ext = p.extension().map(|e| e.to_string_lossy().to_string());
        let new_name = if let Some(ext) = ext {
            format!("{}.decompressed.{}", stem, ext)
        } else {
            format!("{}.decompressed", stem)
        };
        p.set_file_name(new_name);
        p
    });

    fs::write(&out_path, &decompressed)?;
    println!(
        "Decompressed {} bytes -> {} bytes",
        data.len(),
        decompressed.len()
    );
    println!("Written to: {:?}", out_path);

    Ok(())
}

/// Handle the ExtractCommand::NcsInfo command
///
/// Displays detailed information about an NCS file.
pub fn handle_info(input: &Path) -> Result<()> {
    let data = fs::read(input)?;

    if bl4_ncs::is_ncs_manifest(&data) {
        let manifest = bl4_ncs::NcsManifest::parse(&data)?;

        println!("NCS Manifest: {:?}", input);
        println!("  File size: {} bytes", data.len());
        println!("  Entry count (header): {}", manifest.entry_count);
        println!("  Entries found: {}", manifest.entries.len());
        println!();
        println!("  Referenced NCS files:");
        for entry in &manifest.entries {
            println!("    - {}", entry.filename);
        }
    } else if bl4_ncs::is_ncs(&data) {
        let header = bl4_ncs::NcsHeader::from_bytes(&data)?;

        println!("NCS Data File: {:?}", input);
        println!("  File size: {} bytes", data.len());
        println!("  Version: {}", header.version);
        println!("  Compressed: {}", header.is_compressed());

        if header.is_compressed() {
            match bl4_ncs::decompress_ncs(&data) {
                Ok(decompressed) => {
                    println!("  Decompressed size: {} bytes", decompressed.len());
                }
                Err(e) => {
                    println!("  Decompression failed: {}", e);
                }
            }
        }
    } else {
        println!("Unknown file format: {:?}", input);
        println!("  File size: {} bytes", data.len());
        if data.len() >= 8 {
            println!(
                "  Header bytes: {:02x} {:02x} {:02x} {:02x} {:02x} {:02x} {:02x} {:02x}",
                data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7]
            );
        }
    }

    Ok(())
}

/// Handle the ExtractCommand::NcsFind command
///
/// Searches for NCS files in a directory.
pub fn handle_find(path: &Path, recursive: bool) -> Result<()> {
    let mut found = 0;

    if recursive {
        search_dir(path, &mut found)?;
    } else {
        for entry in fs::read_dir(path)? {
            let entry = entry?;
            let file_path = entry.path();
            if file_path.is_file() {
                check_file(&file_path, &mut found)?;
            }
        }
    }

    println!("\nFound {} NCS files", found);
    Ok(())
}

/// Recursively search for NCS files in a directory
fn search_dir(dir: &Path, found: &mut usize) -> Result<()> {
    for entry in fs::read_dir(dir)? {
        let entry = entry?;
        let path = entry.path();
        if path.is_dir() {
            search_dir(&path, found)?;
        } else if path.is_file() {
            check_file(&path, found)?;
        }
    }
    Ok(())
}

/// Check if a file is an NCS file and print info
fn check_file(path: &Path, found: &mut usize) -> Result<()> {
    use std::io::Read;
    let file = fs::File::open(path)?;
    let mut reader = std::io::BufReader::new(file);
    let mut header = [0u8; 5];
    if reader.read_exact(&mut header).is_ok() {
        if bl4_ncs::is_ncs(&header) {
            println!("{}: NCS data", path.display());
            *found += 1;
        } else if bl4_ncs::is_ncs_manifest(&header) {
            println!("{}: NCS manifest", path.display());
            *found += 1;
        }
    }
    Ok(())
}

/// Handle the ExtractCommand::NcsScan command
///
/// Scans a file for embedded NCS chunks.
pub fn handle_scan(input: &Path, _all: bool) -> Result<()> {
    println!("Scanning {:?} for NCS chunks...", input);
    let data = fs::read(input)?;
    let file_size = data.len();

    let chunks = bl4_ncs::scan_for_ncs(&data);

    if chunks.is_empty() {
        println!("No NCS chunks found");
    } else {
        println!("Found {} NCS chunks:\n", chunks.len());
        for (i, (offset, header)) in chunks.iter().enumerate() {
            println!(
                "  [{:4}] offset: 0x{:08x} ({:>12}), version: {}, compressed: {:>8}, decompressed: {:>8}",
                i,
                offset,
                offset,
                header.version,
                header.compressed_size,
                header.decompressed_size
            );
        }
        println!();

        let total_compressed: u64 = chunks.iter().map(|(_, h)| h.compressed_size as u64).sum();
        let total_decompressed: u64 = chunks.iter().map(|(_, h)| h.decompressed_size as u64).sum();

        println!("Total compressed size: {} bytes", total_compressed);
        println!("Total decompressed size: {} bytes", total_decompressed);
        println!(
            "Coverage: {:.2}% of file",
            (total_compressed as f64 / file_size as f64) * 100.0
        );
    }

    Ok(())
}

/// Handle the ExtractCommand::NcsExtract command
///
/// Extracts NCS chunks from a file using manifest for proper naming.
pub fn handle_extract(input: &Path, output: &Path, decompress: bool) -> Result<()> {
    println!("Extracting NCS from {:?}...", input);
    let data = fs::read(input)?;

    // Use manifest-based extraction for proper correlation
    let result = bl4_ncs::extract_from_pak(&data);

    if result.files.is_empty() && result.orphan_chunks.is_empty() {
        println!("No NCS data found in this file");
        return Ok(());
    }

    println!(
        "Found {} NCS files, {} missing entries, {} orphan chunks",
        result.files.len(),
        result.missing_chunks.len(),
        result.orphan_chunks.len()
    );

    fs::create_dir_all(output)?;

    let mut extracted = 0;
    let mut failed = 0;

    for (i, file) in result.files.iter().enumerate() {
        // Decompress the chunk
        let decompressed = match file.decompress(&data) {
            Ok(d) => d,
            Err(e) => {
                eprintln!(
                    "Failed to decompress {}: {}",
                    file.filename, e
                );
                failed += 1;
                continue;
            }
        };

        // Use manifest filename (strip Nexus-Data- prefix for cleaner output)
        let clean_name = file.filename
            .strip_prefix("Nexus-Data-")
            .unwrap_or(&file.filename);

        let out_path = if decompress {
            output.join(format!("{}.bin", clean_name.strip_suffix(".ncs").unwrap_or(clean_name)))
        } else {
            output.join(clean_name)
        };

        // Write decompressed or raw
        let chunk_end = file.offset + file.header.total_size();
        let write_data = if decompress {
            decompressed
        } else {
            data[file.offset..chunk_end].to_vec()
        };

        fs::write(&out_path, &write_data)?;
        extracted += 1;

        if extracted <= 10 {
            println!("  {} -> {:?}", file.type_name, out_path.file_name().unwrap());
        }

        if (i + 1) % 100 == 0 {
            println!("  Extracted {}/{}...", i + 1, result.files.len());
        }
    }

    println!();
    println!("Extracted: {}", extracted);
    println!("Failed: {}", failed);

    if !result.missing_chunks.is_empty() {
        println!("\nMissing (manifest entries without data):");
        for entry in result.missing_chunks.iter().take(10) {
            println!("  - {}", entry.filename);
        }
        if result.missing_chunks.len() > 10 {
            println!("  ... and {} more", result.missing_chunks.len() - 10);
        }
    }

    if !result.orphan_chunks.is_empty() {
        println!(
            "\nWarning: {} orphan chunks (no manifest) - likely false positives",
            result.orphan_chunks.len()
        );
    }

    Ok(())
}

/// Build a map from lowercase type_name to clean manifest filename
///
/// Manifest entries like "Nexus-Data-achievement0.ncs" become:
/// - Key: "achievement" (lowercase type_name)
/// - Value: "achievement" (clean name for output file)
fn build_manifest_map(
    manifests: &[(usize, bl4_ncs::NcsManifest)],
) -> HashMap<String, String> {
    let mut map = HashMap::new();

    for (_offset, manifest) in manifests {
        for entry in &manifest.entries {
            // Parse "Nexus-Data-{type_name}{N}.ncs" -> type_name
            if let Some(clean_name) = parse_manifest_filename(&entry.filename) {
                map.insert(clean_name.to_lowercase(), clean_name);
            }
        }
    }

    map
}

/// Parse manifest filename to extract clean type name
///
/// "Nexus-Data-achievement0.ncs" -> "achievement"
/// "Nexus-Data-ItemPoolList0.ncs" -> "ItemPoolList"
fn parse_manifest_filename(filename: &str) -> Option<String> {
    // Strip "Nexus-Data-" prefix
    let without_prefix = filename.strip_prefix("Nexus-Data-")?;

    // Strip ".ncs" suffix
    let without_ext = without_prefix.strip_suffix(".ncs")?;

    // Strip trailing digit(s) (pak number)
    let name = without_ext.trim_end_matches(|c: char| c.is_ascii_digit());

    if name.is_empty() {
        return None;
    }

    Some(name.to_string())
}

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

    #[test]
    fn test_handle_ncs_check_missing_file() {
        let result = handle_check(Path::new("/nonexistent/file.ncs"));
        assert!(result.is_err());
    }

    #[test]
    fn test_handle_ncs_decompress_missing_file() {
        let result = handle_decompress(Path::new("/nonexistent/file.ncs"), None);
        assert!(result.is_err());
    }

    #[test]
    fn test_handle_ncs_info_missing_file() {
        let result = handle_info(Path::new("/nonexistent/file.ncs"));
        assert!(result.is_err());
    }

    #[test]
    fn test_handle_ncs_find_missing_dir() {
        let result = handle_find(Path::new("/nonexistent/dir"), false);
        assert!(result.is_err());
    }

    #[test]
    fn test_handle_ncs_scan_missing_file() {
        let result = handle_scan(Path::new("/nonexistent/file.pak"), false);
        assert!(result.is_err());
    }

    #[test]
    fn test_handle_ncs_extract_missing_file() {
        let result = handle_extract(
            Path::new("/nonexistent/file.pak"),
            Path::new("/tmp/output"),
            false,
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_manifest_filename_basic() {
        assert_eq!(
            parse_manifest_filename("Nexus-Data-achievement0.ncs"),
            Some("achievement".to_string())
        );
    }

    #[test]
    fn test_parse_manifest_filename_mixed_case() {
        assert_eq!(
            parse_manifest_filename("Nexus-Data-ItemPoolList0.ncs"),
            Some("ItemPoolList".to_string())
        );
    }

    #[test]
    fn test_parse_manifest_filename_multi_digit() {
        assert_eq!(
            parse_manifest_filename("Nexus-Data-gbxactor123.ncs"),
            Some("gbxactor".to_string())
        );
    }

    #[test]
    fn test_parse_manifest_filename_underscore() {
        assert_eq!(
            parse_manifest_filename("Nexus-Data-damage_modifier0.ncs"),
            Some("damage_modifier".to_string())
        );
    }

    #[test]
    fn test_parse_manifest_filename_no_prefix() {
        assert_eq!(parse_manifest_filename("achievement0.ncs"), None);
    }

    #[test]
    fn test_parse_manifest_filename_no_suffix() {
        assert_eq!(parse_manifest_filename("Nexus-Data-achievement0"), None);
    }

    #[test]
    fn test_parse_manifest_filename_only_digits() {
        // "Nexus-Data-0.ncs" -> name would be empty after stripping digits
        assert_eq!(parse_manifest_filename("Nexus-Data-0.ncs"), None);
    }

    #[test]
    fn test_build_manifest_map_empty() {
        let manifests: Vec<(usize, bl4_ncs::NcsManifest)> = vec![];
        let map = build_manifest_map(&manifests);
        assert!(map.is_empty());
    }
}