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
//! Item serial command handlers

use anyhow::{bail, Context, Result};
use std::collections::HashMap;
use std::fs;
use std::io::Write;
use std::path::Path;

/// Handle `serial decode` command
#[allow(clippy::too_many_lines)] // Serial decoding with debug output
pub fn decode(
    serial: &str,
    verbose: bool,
    debug: bool,
    analyze: bool,
    parts_db: &Path,
) -> Result<()> {
    let item = bl4::ItemSerial::decode(serial).context("Failed to decode serial")?;

    println!("Serial: {}", item.original);
    println!(
        "Item type: {} ({})",
        item.item_type,
        item.item_type_description()
    );

    // Show weapon info based on format type
    if let Some((mfr, weapon_type)) = item.weapon_info() {
        println!("Weapon: {} {}", mfr, weapon_type);
    } else if let Some(group_id) = item.part_group_id() {
        let category_name =
            bl4::category_name_for_type(item.item_type, group_id).unwrap_or("Unknown");
        println!("Category: {} ({})", category_name, group_id);
    }

    // Show elements if detected
    if let Some(elements) = item.element_names() {
        println!("Element: {}", elements);
    }

    // Show rarity if detected
    if let Some(rarity) = item.rarity_name() {
        println!("Rarity: {}", rarity);
    }

    // Show raw manufacturer ID if we couldn't resolve it
    if item.weapon_info().is_none() {
        if let Some(mfr) = item.manufacturer_name() {
            println!("Manufacturer: {}", mfr);
        } else if let Some(mfr_id) = item.manufacturer {
            println!("Manufacturer ID: {} (unknown)", mfr_id);
        }
    }

    // Show level and seed for VarInt-first format
    if let Some(level) = item.level {
        if let Some(raw) = item.raw_level {
            if raw > level {
                println!(
                    "Level: {} (WARNING: decoded as {}, capped - decoding may be wrong)",
                    level, raw
                );
            } else {
                println!("Level: {}", level);
            }
        } else {
            println!("Level: {}", level);
        }
    }
    if let Some(seed) = item.seed {
        println!("Seed: {}", seed);
    }

    println!("Decoded bytes: {}", item.raw_bytes.len());
    println!("Hex: {}", item.hex_dump());
    println!("Tokens: {}", item.format_tokens());

    // Show resolved parts using compiled-in manifest data
    let parts_summary = item.parts_summary();
    if !parts_summary.is_empty() {
        println!("\nParts: {}", parts_summary);
    }

    // Show detailed parts with full names if verbose
    let parts = item.parts_with_names();
    if !parts.is_empty() && verbose {
        println!("\nResolved parts ({}):", item.parts_category().unwrap_or(-1));
        for (index, name, values) in &parts {
            let extra = if values.is_empty() {
                String::new()
            } else if values.len() == 1 {
                format!(" (value: {})", values[0])
            } else {
                format!(" (values: {:?})", values)
            };

            // Check if this is an element marker first (128-142 range)
            if *index >= 128 && *index <= 142 {
                let elem_id = index - 128;
                let elem_name = match elem_id {
                    0 => "Kinetic",
                    5 => "Corrosive",
                    8 => "Shock",
                    9 => "Radiation",
                    13 => "Cryo",
                    14 => "Fire",
                    _ => "Unknown Element",
                };
                println!("  [{:3}] <element: {}>{}", index, elem_name, extra);
            } else if let Some(n) = name {
                println!("  [{:3}] {}{}", index, n, extra);
            } else {
                println!("  [{:3}] <unknown>{}", index, extra);
            }
        }
    }

    // For backwards compatibility, also check parts_db file for additional parts
    let _ = parts_db; // Suppress unused warning - kept for API compatibility

    if verbose {
        println!("\n{}", item.detailed_dump());
    }

    if debug {
        println!("\nDebug parsing:");
        bl4::serial::parse_tokens_debug(&item.raw_bytes);
    }

    if analyze {
        analyze_first_token(&item)?;
    }

    Ok(())
}

/// Analyze the first token for group ID research
fn analyze_first_token(item: &bl4::ItemSerial) -> Result<()> {
    use bl4::serial::Token;

    if let Some(first_token) = item.tokens.first() {
        let value = match first_token {
            Token::VarInt(v) => Some((*v, "VarInt")),
            Token::VarBit(v) => Some((*v, "VarBit")),
            _ => None,
        };

        if let Some((value, token_type)) = value {
            println!("\n=== First Token Analysis ===");
            println!("Type:   {}", token_type);
            println!("Value:  {} (decimal)", value);
            println!("Hex:    0x{:x}", value);
            println!("Binary: {:024b}", value);
            println!();

            // Decode Part Group ID based on item type
            println!("Part Group ID decoding:");
            match item.item_type {
                'r' | 'a'..='d' | 'f' | 'g' | 'v'..='z' => {
                    let group_id = value / 8192;
                    let offset = value % 8192;
                    println!("  Formula: group_id = value / 8192 (weapons)");
                    println!("  Group ID: {} (offset {})", group_id, offset);

                    let group_name = bl4::category_name(group_id as i64).unwrap_or("Unknown");
                    println!("  Identified: {}", group_name);
                }
                'e' => {
                    let group_id = value / 384;
                    let offset = value % 384;
                    println!("  Formula: group_id = value / 384 (equipment)");
                    println!("  Group ID: {} (offset {})", group_id, offset);

                    let group_name =
                        bl4::category_name(group_id as i64).unwrap_or("Unknown Equipment");
                    println!("  Identified: {}", group_name);
                }
                'u' => {
                    println!("  Utility items - encoding formula not yet determined");
                    println!("  Raw value: {}", value);
                }
                '!' | '#' => {
                    println!("  Class mods - encoding formula not yet determined");
                    println!("  Raw value: {}", value);
                }
                _ => {
                    println!(
                        "  Unknown item type '{}' - encoding formula not determined",
                        item.item_type
                    );
                }
            }

            println!();
            println!("Bit split analysis (for research):");
            for split in [8, 10, 12, 13, 14] {
                let high = value >> split;
                let low = value & ((1 << split) - 1);
                println!("  Split at bit {:2}: high={:6}  low={:6}", split, high, low);
            }
        } else {
            println!("\n=== First Token Analysis ===");
            println!("First token is not numeric: {:?}", first_token);
        }
    }

    Ok(())
}

/// Handle `serial encode` command
pub fn encode(serial: &str) -> Result<()> {
    let item = bl4::ItemSerial::decode(serial).context("Failed to decode serial")?;
    let re_encoded = item.encode();

    println!("Original:   {}", serial);
    println!("Re-encoded: {}", re_encoded);

    if serial == re_encoded {
        println!("\n✓ Round-trip encoding successful!");
    } else {
        println!("\n✗ Round-trip encoding differs");
        println!("  Original length:   {}", serial.len());
        println!("  Re-encoded length: {}", re_encoded.len());

        // Decode both to compare tokens
        let re_item = bl4::ItemSerial::decode(&re_encoded)?;
        println!("\nOriginal tokens:   {}", item.format_tokens());
        println!("Re-encoded tokens: {}", re_item.format_tokens());
    }

    Ok(())
}

/// Handle `serial compare` command
#[allow(clippy::too_many_lines)] // Serial comparison output
pub fn compare(serial1: &str, serial2: &str) -> Result<()> {
    let item1 = bl4::ItemSerial::decode(serial1).context("Failed to decode serial 1")?;
    let item2 = bl4::ItemSerial::decode(serial2).context("Failed to decode serial 2")?;

    // Header comparison
    println!("=== SERIAL 1 ===");
    println!("Serial: {}", item1.original);
    println!(
        "Type: {} ({})",
        item1.item_type,
        item1.item_type_description()
    );
    if let Some((mfr, wtype)) = item1.weapon_info() {
        println!("Weapon: {} {}", mfr, wtype);
    }
    if let Some(level) = item1.level {
        println!("Level: {}", level);
    }
    if let Some(seed) = item1.seed {
        println!("Seed: {}", seed);
    }
    println!("Tokens: {}", item1.format_tokens());

    println!();
    println!("=== SERIAL 2 ===");
    println!("Serial: {}", item2.original);
    println!(
        "Type: {} ({})",
        item2.item_type,
        item2.item_type_description()
    );
    if let Some((mfr, wtype)) = item2.weapon_info() {
        println!("Weapon: {} {}", mfr, wtype);
    }
    if let Some(level) = item2.level {
        println!("Level: {}", level);
    }
    if let Some(seed) = item2.seed {
        println!("Seed: {}", seed);
    }
    println!("Tokens: {}", item2.format_tokens());

    // Byte comparison
    println!();
    println!("=== BYTE COMPARISON ===");
    println!(
        "Lengths: {} vs {} bytes",
        item1.raw_bytes.len(),
        item2.raw_bytes.len()
    );

    let max_len = std::cmp::max(item1.raw_bytes.len(), item2.raw_bytes.len());
    let mut first_diff = None;
    let mut diff_count = 0;

    for i in 0..max_len {
        let b1 = item1.raw_bytes.get(i);
        let b2 = item2.raw_bytes.get(i);
        if b1 != b2 {
            diff_count += 1;
            if first_diff.is_none() {
                first_diff = Some(i);
            }
        }
    }

    if diff_count == 0 {
        println!("Bytes: IDENTICAL");
    } else {
        println!("Bytes: {} differences", diff_count);
        if let Some(first) = first_diff {
            println!("First diff at byte {}", first);
            println!();
            println!("Byte-by-byte (first 20 bytes or until divergence + 5):");
            println!("{:>4}  {:>12}  {:>12}", "Idx", "Serial 1", "Serial 2");
            let show_until = std::cmp::min(max_len, first + 10);
            for i in 0..show_until {
                let b1 = item1.raw_bytes.get(i).copied();
                let b2 = item2.raw_bytes.get(i).copied();
                let marker = if b1 != b2 { " <--" } else { "" };
                let s1 = b1
                    .map(|b| format!("{:3} {:08b}", b, b))
                    .unwrap_or_else(|| "-".to_string());
                let s2 = b2
                    .map(|b| format!("{:3} {:08b}", b, b))
                    .unwrap_or_else(|| "-".to_string());
                println!("{:4}  {}  {}{}", i, s1, s2, marker);
            }
        }
    }

    Ok(())
}

/// Handle `serial modify` command
#[allow(clippy::too_many_lines)] // Serial modification command
pub fn modify(base: &str, source: &str, parts: &str) -> Result<()> {
    use bl4::serial::Token;

    // Parse part indices
    let part_indices: Vec<u64> = parts
        .split(',')
        .filter_map(|s| s.trim().parse().ok())
        .collect();

    if part_indices.is_empty() {
        bail!("No valid part indices provided");
    }

    let base_item = bl4::ItemSerial::decode(base).context("Failed to decode base serial")?;
    let source_item = bl4::ItemSerial::decode(source).context("Failed to decode source serial")?;

    println!("Base serial:   {}", base);
    println!("Source serial: {}", source);
    println!("Copying part indices: {:?}", part_indices);
    println!();

    // Build a map of source parts by index
    let source_parts: HashMap<u64, Vec<u64>> = source_item
        .tokens
        .iter()
        .filter_map(|t| {
            if let Token::Part { index, values } = t {
                Some((*index, values.clone()))
            } else {
                None
            }
        })
        .collect();

    // Modify base tokens - replace parts at specified indices
    let new_tokens: Vec<Token> = base_item
        .tokens
        .iter()
        .map(|t| {
            if let Token::Part { index, values } = t {
                if part_indices.contains(index) {
                    if let Some(source_values) = source_parts.get(index) {
                        println!(
                            "  Swapping part {}: {:?} -> {:?}",
                            index, values, source_values
                        );
                        return Token::Part {
                            index: *index,
                            values: source_values.clone(),
                        };
                    }
                }
            }
            t.clone()
        })
        .collect();

    // Encode the new serial
    let modified = base_item.with_tokens(new_tokens);
    let new_serial = modified.encode();

    println!();
    println!("New serial: {}", new_serial);

    Ok(())
}

/// Handle `serial batch-decode` command
pub fn batch_decode(input: &Path, output: &Path) -> Result<()> {
    use std::io::{BufRead, BufReader, BufWriter};

    let file =
        fs::File::open(input).with_context(|| format!("Failed to open input file: {:?}", input))?;
    let reader = BufReader::new(file);

    let out_file = fs::File::create(output)
        .with_context(|| format!("Failed to create output file: {:?}", output))?;
    let mut writer = BufWriter::new(out_file);

    let mut count = 0;
    let mut errors = 0;

    for line in reader.lines() {
        let serial = line.context("Failed to read line")?;
        let serial = serial.trim();
        if serial.is_empty() {
            continue;
        }

        match bl4::ItemSerial::decode(serial) {
            Ok(item) => {
                // Write length as u16 followed by raw bytes
                let bytes = &item.raw_bytes;
                let len = bytes.len() as u16;
                writer.write_all(&len.to_le_bytes())?;
                writer.write_all(bytes)?;
                count += 1;
            }
            Err(_) => {
                // Write 0 length to indicate decode failure (keeps alignment)
                writer.write_all(&0u16.to_le_bytes())?;
                errors += 1;
            }
        }
    }

    writer.flush()?;
    println!(
        "Decoded {} serials to {:?} ({} errors)",
        count, output, errors
    );

    Ok(())
}