openstranded-s2mod-tool 0.2.3

Convert original Stranded II mods to the .s2mod Content Pack format
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
// openstranded-s2mod-tool — convert Stranded II mods to .s2mod format
// Copyright (C) 2025  openstranded-s2mod-tool contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

//! openstranded-s2mod-tool — CLI tool for converting original Stranded II mods.
//!
//! Transforms .inf / .b3d / .bmp / .bmpf / .s2s / .s2 files into a .s2mod Content Pack.
//! Structure is 1:1 with the original — every file keeps its relative path,
//! only extensions change:
//!
//!   .inf  → .ron [+ .ron.<id>.lua for embedded scripts]
//!   .s2s  → .lua
//!   .b3d  → .glb
//!   .bmp  → .png  (magenta → transparent colour key)
//!   .bmpf → .fnt  (+ .png texture atlas)
//!   .s2   → .osmap  (binary map → RON map format)
//!   .map  → .osmap  (text format map → RON map format, same parser)
//!
//! Pipeline:
//!   1. Walk input directory, classify files
//!   2. Parse .inf → .ron (same tree) + extract scripts → .ron.<id>.lua
//!      2.5 Scan all scripts for cross-references (dialogue/msgbox/button/addscript/etc.)
//!   3. Transpile .s2s → .lua / .txt (using reference map to classify each file)
//!   4. Convert .b3d → .glb (same tree)
//!   5. Convert .bmp → .png (same tree, magenta → transparent)
//!   6. Convert .bmpf → .fnt + .png (same tree)
//!   7. Convert .s2/.map → .osmap (same tree)
//!   8. Copy remaining files as-is
//!   9. Update asset paths in .ron files (.b3d→.glb, .bmp→.png, .s2→.osmap)
//!  10. Generate manifest.toml with [registry] section
//!  11. Pack to .s2mod (zip) or directory

use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::{Path, PathBuf};

use anyhow::{anyhow, Context, Result};
use clap::Parser;
use s2mod_packer::{ContentPackManifest, S2ModPackage};

use openstranded_s2mod_tool::convert::{convert_b3d_to_glb, convert_bmp_to_png, convert_bmpf_to_fnt, convert_inf_to_ron, convert_s2_to_osmap};
use openstranded_s2mod_tool::registry::register_ron;
use openstranded_s2mod_tool::scanner::{build_reference_map, classify_s2s, resolve_missing_script_refs, S2sClass, S2sRefType};
use openstranded_s2mod_tool::script::{convert_sectioned_file, parse_dialogue_to_lua, InfRawSource};
use openstranded_s2mod_tool::util::{copy_to_staging, normalize_id, parse_inf_file, read_file_lossy, relative_path, write_ron};

#[derive(Parser)]
#[command(
    name = "openstranded-s2mod-tool",
    about = "Convert Stranded II mods to .s2mod format"
)]
struct Cli {
    /// Path to the original mod directory
    #[arg(short, long)]
    input: String,

    /// Output .s2mod file path (or directory if --dir)
    #[arg(short, long)]
    output: String,

    /// Keep output as a directory (don't zip)
    #[arg(long)]
    dir: bool,

    /// Skip .b3d → .glb model conversion
    #[arg(long)]
    skip_models: bool,

    /// Skip .bmp → .png texture conversion
    #[arg(long)]
    skip_textures: bool,

    /// Skip .s2/.map → .osmap map conversion
    #[arg(long)]
    skip_maps: bool,

    /// Enable debug output
    #[arg(long, default_value_t = false)]
    debug: bool,
}

fn main() -> Result<()> {
    let cli = Cli::parse();
    let debug = cli.debug;

    println!("openstranded-s2mod-tool v{}", env!("CARGO_PKG_VERSION"));
    println!("Input:  {}", cli.input);
    println!("Output: {}", cli.output);
    println!();

    let input_path = Path::new(&cli.input);
    if !input_path.is_dir() {
        return Err(anyhow!("Input path is not a directory: {}", cli.input));
    }

    // ── Create staging directory ───────────────────────────
    let staging = tempfile::tempdir().context("failed to create temp dir")?;
    let stage_path = staging.path().to_path_buf();
    if debug {
        eprintln!("Staging at: {:?}", stage_path);
    }

    // Mapping: original relative path → new relative path (for extension updates)
    let mut path_mappings: HashMap<String, String> = HashMap::new();

    // Registry tracking: domain → list of .ron file paths (relative to pack root)
    // Key is derived from the .ron filename:
    //   items_edible.ron   → "items"
    //   vehicles_flying.ron → "vehicles"
    //   strings.ron        → "strings"
    //   game.ron           → "game"
    let mut registry: HashMap<String, Vec<String>> = HashMap::new();

    // Collect all files by type for bucketed processing
    let mut inf_files: HashMap<String, Vec<PathBuf>> = HashMap::new();
    let mut s2s_files: Vec<PathBuf> = Vec::new();
    let mut b3d_files: Vec<PathBuf> = Vec::new();
    let mut bmp_files: Vec<PathBuf> = Vec::new();
    let mut bmpf_files: Vec<PathBuf> = Vec::new();
    let mut s2_map_files: Vec<PathBuf> = Vec::new();
    let mut other_files: Vec<PathBuf> = Vec::new();

    // All parsed .inf entries (keyed by .inf path), used for reference scanning
    let mut parsed_inf_entries: HashMap<PathBuf, Vec<inf2ron::InfEntry>> = HashMap::new();

    // ── Step 1: Walk & Classify ────────────────────────────
    println!("[1/10] Walking input directory...");

    for entry in walkdir::WalkDir::new(input_path)
        .into_iter()
        .filter_map(|e| e.ok())
    {
        let path = entry.path();
        if !path.is_file() {
            continue;
        }

        match path.extension().and_then(|s| s.to_str()) {
            Some("inf") => {
                // Derive domain from filename (first token before '_', or full stem)
                let stem = path.file_stem().and_then(|s| s.to_str()).unwrap_or("");
                let domain = stem.split('_').next().unwrap_or(stem).to_string();
                inf_files.entry(domain).or_default().push(path.to_path_buf());
            }
            Some("s2s") => s2s_files.push(path.to_path_buf()),
            Some("b3d") => b3d_files.push(path.to_path_buf()),
            Some("bmp") => bmp_files.push(path.to_path_buf()),
            Some("bmpf") => bmpf_files.push(path.to_path_buf()),
            Some("s2" | "map") => s2_map_files.push(path.to_path_buf()),
            _ => other_files.push(path.to_path_buf()),
        }
    }

    // ── Step 2: Parse .inf → .ron + extract scripts ───────
    println!("[2/10] Parsing .inf files...");

    for (domain, paths) in &inf_files {
        if debug {
            eprintln!("  Domain {:?}: {} file(s)", domain, paths.len());
        }
        for inf_path in paths {
            let entries = match parse_inf_file(inf_path) {
                Ok(e) => e,
                Err(e) => {
                    eprintln!("  Warning: failed to parse {:?}: {}", inf_path, e);
                    // Write .ron with raw source content instead of copying .inf
                    let raw = InfRawSource {
                        parse_error: format!("{:#}", e),
                        raw_content: read_file_lossy(inf_path).unwrap_or_default(),
                    };
                    let ron_rel = relative_path(inf_path, input_path).with_extension("ron");
                    let ron_path = stage_path.join(&ron_rel);
                    if let Some(parent) = ron_path.parent() {
                        fs::create_dir_all(parent)?;
                    }
                    if let Err(we) = write_ron(&ron_path, &raw) {
                        eprintln!("  Warning: failed to write raw .ron for {:?}: {:#}", inf_path, we);
                    } else {
                        register_ron(&ron_rel, &mut registry);
                    }
                    continue;
                }
            };

            // Unified conversion: extract scripts, write .ron, register
            convert_inf_to_ron(inf_path, input_path, &stage_path, &mut entries.clone(), &mut registry, debug)
                .unwrap_or_else(|e| {
                    eprintln!("  Warning: failed to convert {:?}: {:#}", inf_path, e);
                });

            // Collect parsed entries for reference scanning (step 2.5)
            parsed_inf_entries.insert(inf_path.to_path_buf(), entries);
        }
    }

    println!("    {} .inf files processed, {} registry entries",
        parsed_inf_entries.len(),
        registry.len());

    // ── Step 2.5: Build S2S reference map ──────────────────
    println!("[2.5/10] Scanning script references...");
    let (mut s2s_ref_map, mut s2s_section_map) =
        build_reference_map(&parsed_inf_entries, &s2s_files, input_path);

    // Resolve references where the referenced extension doesn't match the
    // actual file on disk (e.g., msgbox points to `.s2s` but file is `.txt`).
    let extra_script_files = resolve_missing_script_refs(&mut s2s_ref_map, &mut s2s_section_map);
    if !extra_script_files.is_empty() {
        println!("    {} resolved with alternate extension", extra_script_files.len());
    }

    let dialogue_count = s2s_ref_map.values()
        .filter(|refs| refs.iter().any(|r| matches!(r, S2sRefType::Dialogue { .. })))
        .count();
    let msgbox_count = s2s_ref_map.values()
        .filter(|refs| refs.iter().any(|r| matches!(r, S2sRefType::Msgbox { .. })))
        .count();
    if dialogue_count > 0 || msgbox_count > 0 {
        println!("    {} dialogue files, {} msgbox files", dialogue_count, msgbox_count);
    }

    // ── Step 3: Transpile .s2s → .lua (also referenced .txt → .lua) ──
    println!("[3/10] Transpiling scripts...");
    let mut s2s_ok = 0u32;
    let mut s2s_err = 0u32;
    let mut s2s_dialogue = 0u32;
    let mut s2s_msgbox = 0u32;

    // Process both .s2s files and extra referenced files (.txt etc.)
    let all_script_files: Vec<&PathBuf> = s2s_files.iter()
        .chain(extra_script_files.iter())
        .collect();

    for script_path_ptr in &all_script_files {
        let script_path = *script_path_ptr;
        let content = match read_file_lossy(script_path) {
            Ok(s) => s,
            Err(e) => {
                eprintln!("  Warning: {:#}", e);
                copy_to_staging(script_path, input_path, &stage_path)?;
                s2s_err += 1;
                continue;
            }
        };

        let s2s_rel = relative_path(script_path, input_path);
        let lua_rel = s2s_rel.with_extension("lua");
        let lua_path = stage_path.join(&lua_rel);

        // ── Sectioned files (contain //~ markers) ──────────
        if content.contains("//~") {
            let sections_refs = s2s_section_map
                .get(script_path)
                .cloned()
                .unwrap_or_default();
            let lua = convert_sectioned_file(&content, &sections_refs);

            if let Some(parent) = lua_path.parent() {
                fs::create_dir_all(parent)?;
            }
            fs::write(&lua_path, &lua)
                .with_context(|| format!("writing {:?}", lua_path))?;
            s2s_ok += 1;

            if debug {
                eprintln!("    {:?}{:?} (sectioned)", script_path, lua_path);
            }
            continue;
        }

        // ── Non-sectioned files: classify and convert ──────
        let classification = classify_s2s(script_path, &s2s_ref_map);

        // For non-.s2s files (e.g., .txt), force msgbox-style text conversion.
        let is_plain_text = script_path
            .extension()
            .and_then(|e| e.to_str())
            .is_some_and(|e| e != "s2s");

        if is_plain_text && !matches!(classification, S2sClass::Dialogue { .. }) {
            // Treat as msgbox-style text — wrap in return string
            let lua_content = format!(
                "-- Generated by openstranded-s2mod-tool (plain text ref)\nreturn [===[\n{}]===]\n",
                content
            );
            if let Some(parent) = lua_path.parent() {
                fs::create_dir_all(parent)?;
            }
            fs::write(&lua_path, &lua_content)
                .with_context(|| format!("writing {:?}", lua_path))?;
            s2s_msgbox += 1;

            if debug {
                eprintln!("    {:?}{:?} (plain text, {})", script_path, lua_path, lua_rel.display());
            }
        } else {
            match classification {
                S2sClass::Dialogue { start_page: _ } => {
                    // Parse as dialog data → Lua table
                    match parse_dialogue_to_lua(&content) {
                        Ok(lua) => {
                            if let Some(parent) = lua_path.parent() {
                                fs::create_dir_all(parent)?;
                            }
                            fs::write(&lua_path, &lua)
                                .with_context(|| format!("writing {:?}", lua_path))?;
                            s2s_dialogue += 1;

                            if debug {
                                eprintln!("    {:?}{:?} (dialogue)", script_path, lua_path);
                            }
                        }
                        Err(e) => {
                            eprintln!("  Warning: failed to parse dialog {:?}: {:#}", script_path, e);
                            copy_to_staging(script_path, input_path, &stage_path)?;
                            s2s_err += 1;
                        }
                    }
                }
                S2sClass::Msgbox { title: _ } => {
                    // Plain text — write as .lua returning the string
                    let lua_content = format!(
                        "-- Generated by openstranded-s2mod-tool (msgbox text)\nreturn [===[\n{}]===]\n",
                        content
                    );
                    if let Some(parent) = lua_path.parent() {
                        fs::create_dir_all(parent)?;
                    }
                    fs::write(&lua_path, &lua_content)
                        .with_context(|| format!("writing {:?}", lua_path))?;
                    s2s_msgbox += 1;

                    if debug {
                        eprintln!("    {:?}{:?} (msgbox text)", script_path, lua_path);
                    }
                }
                S2sClass::Script | S2sClass::Unknown => {
                    // Transpile as s2s script → Lua
                    match s2s2lua::S2sParser::parse(&content) {
                        Ok(script) => {
                            match s2s2lua::LuaGenerator::generate(&script, s2s2lua::GenOptions::default()) {
                                Ok(lua) => {
                                    if let Some(parent) = lua_path.parent() {
                                        fs::create_dir_all(parent)?;
                                    }
                                    fs::write(&lua_path, &lua)
                                        .with_context(|| format!("writing {:?}", lua_path))?;
                                    s2s_ok += 1;

                                    if debug {
                                        eprintln!("    {:?}{:?}", script_path, lua_path);
                                    }
                                }
                                Err(e) => {
                                    eprintln!("  Warning: failed to generate lua for {:?}: {:#}", script_path, e);
                                    copy_to_staging(script_path, input_path, &stage_path)?;
                                    s2s_err += 1;
                                }
                            }
                        }
                        Err(e) => {
                            if matches!(classification, S2sClass::Unknown) {
                                if debug {
                                    eprintln!("    {:?} → copy-as-is (not valid s2s)", script_path);
                                }
                                copy_to_staging(script_path, input_path, &stage_path)?;
                            } else {
                                eprintln!("  Warning: failed to parse {:?}: {}", script_path, e);
                                copy_to_staging(script_path, input_path, &stage_path)?;
                                s2s_err += 1;
                            }
                        }
                    }
                }
            }
        }
    }

    println!("    {s2s_ok} transpiled, {s2s_dialogue} dialogue, {s2s_msgbox} msgbox, {s2s_err} failed");

    // ── Step 4: Convert .b3d → .glb ───────────────────────
    if cli.skip_models {
        if !b3d_files.is_empty() {
            println!("[4/10] {} .b3d files skipped (--skip-models)", b3d_files.len());
        }
        for b3d_path in &b3d_files {
            copy_to_staging(b3d_path, input_path, &stage_path)?;
        }
    } else {
        println!("[4/10] Converting .b3d models → .glb...");
        let mut ok = 0u32;
        let mut err = 0u32;

        for b3d_path in &b3d_files {
            let glb_rel = relative_path(b3d_path, input_path).with_extension("glb");
            let glb_path = stage_path.join(&glb_rel);
            if let Some(parent) = glb_path.parent() {
                fs::create_dir_all(parent)?;
            }

            let stem = b3d_path
                .file_stem()
                .and_then(|s| s.to_str())
                .unwrap_or("model");

            match convert_b3d_to_glb(b3d_path, &glb_path, input_path, stem) {
                Ok(()) => {
                    ok += 1;
                    let orig_rel = relative_path(b3d_path, input_path).to_string_lossy().to_string();
                    let new_rel = glb_rel.to_string_lossy().to_string();
                    path_mappings.insert(orig_rel, new_rel);

                    if debug {
                        eprintln!("    {:?}{:?}", b3d_path, glb_path);
                    }
                }
                Err(e) => {
                    eprintln!("  Warning: failed to convert {:?}: {:#}", b3d_path, e);
                    copy_to_staging(b3d_path, input_path, &stage_path)?;
                    err += 1;
                }
            }
        }
        if ok > 0 || err > 0 {
            println!("    {ok} converted, {err} failed");
        }
    }

    // ── Step 5: Convert .bmp → .png ───────────────────────
    if cli.skip_textures {
        if !bmp_files.is_empty() {
            println!("[5/10] {} .bmp files skipped (--skip-textures)", bmp_files.len());
        }
        for bmp_path in &bmp_files {
            copy_to_staging(bmp_path, input_path, &stage_path)?;
        }
    } else {
        println!("[5/10] Converting .bmp textures → .png...");
        let mut ok = 0u32;
        let mut err = 0u32;

        for bmp_path in &bmp_files {
            let png_rel = relative_path(bmp_path, input_path).with_extension("png");
            let png_path = stage_path.join(&png_rel);
            if let Some(parent) = png_path.parent() {
                fs::create_dir_all(parent)?;
            }

            match convert_bmp_to_png(bmp_path, &png_path) {
                Ok(()) => {
                    ok += 1;
                    let orig_rel = relative_path(bmp_path, input_path).to_string_lossy().to_string();
                    let new_rel = png_rel.to_string_lossy().to_string();
                    path_mappings.insert(orig_rel, new_rel);

                    if debug {
                        eprintln!("    {:?}{:?}", bmp_path, png_path);
                    }
                }
                Err(e) => {
                    eprintln!("  Warning: failed to convert {:?}: {:#}", bmp_path, e);
                    copy_to_staging(bmp_path, input_path, &stage_path)?;
                    err += 1;
                }
            }
        }
        if ok > 0 || err > 0 {
            println!("    {ok} converted, {err} failed");
        }
    }

    // ── Step 6: Convert .bmpf → .fnt + .png ───────────────
    if !bmpf_files.is_empty() {
        println!("[6/10] Converting .bmpf bitmap fonts...");

        for bmpf_path in &bmpf_files {
            let stem = bmpf_path
                .file_stem()
                .and_then(|s| s.to_str())
                .unwrap_or("font");

            // Look for corresponding .bmp
            let bmp_path = bmpf_path.with_extension("bmp");
            if !bmp_path.exists() {
                eprintln!("  Warning: no matching .bmp for {:?}, copying as-is", bmpf_path);
                copy_to_staging(bmpf_path, input_path, &stage_path)?;
                continue;
            }

            // .fnt goes alongside the .bmpf
            let fnt_rel = relative_path(bmpf_path, input_path).with_extension("fnt");
            let fnt_path = stage_path.join(&fnt_rel);
            if let Some(parent) = fnt_path.parent() {
                fs::create_dir_all(parent)?;
            }

            // .png from the .bmp goes alongside
            let png_rel = relative_path(&bmp_path, input_path).with_extension("png");
            let png_path = stage_path.join(&png_rel);
            if let Some(parent) = png_path.parent() {
                fs::create_dir_all(parent)?;
            }

            let bmp_rel = relative_path(&bmp_path, input_path).to_string_lossy().to_string();

            match convert_bmpf_to_fnt(bmpf_path, &bmp_path, &fnt_path, &png_path, stem, input_path) {
                Ok(()) => {
                    if debug {
                        eprintln!("    {stem}.fnt + .png generated");
                    }
                    // Record .bmp mapping if not already done
                    path_mappings.entry(bmp_rel).or_insert_with(|| {
                        png_rel.to_string_lossy().to_string()
                    });
                }
                Err(e) => {
                    eprintln!("  Warning: failed to convert {:?}: {:#}", bmpf_path, e);
                    copy_to_staging(bmpf_path, input_path, &stage_path)?;
                }
            }
        }
        println!("    {} fonts processed", bmpf_files.len());
    }

    // ── Step 7: Convert .s2/.map → .osmap ─────────────────
    if cli.skip_maps {
        if !s2_map_files.is_empty() {
            println!("[7/11] {} .s2/.map files skipped (--skip-maps)", s2_map_files.len());
        }
        for s2_path in &s2_map_files {
            copy_to_staging(s2_path, input_path, &stage_path)?;
        }
    } else {
        println!("[7/11] Converting .s2/.map maps → .osmap...");
        let mut ok = 0u32;
        let mut err = 0u32;

        for s2_path in &s2_map_files {
            let osmap_rel = relative_path(s2_path, input_path).with_extension("osmap");
            let osmap_path = stage_path.join(&osmap_rel);

            match convert_s2_to_osmap(s2_path, &osmap_path, input_path) {
                Ok(orig_rel) => {
                    ok += 1;
                    let new_rel = osmap_rel.to_string_lossy().to_string();
                    path_mappings.insert(orig_rel, new_rel);

                    if debug {
                        eprintln!("    {:?}{:?}", s2_path, osmap_path);
                    }
                }
                Err(e) => {
                    eprintln!("  Warning: failed to convert {:?}: {:#}", s2_path, e);
                    copy_to_staging(s2_path, input_path, &stage_path)?;
                    err += 1;
                }
            }
        }
        if ok > 0 || err > 0 {
            println!("    {ok} converted, {err} failed");
        }
    }

    // ── Step 8: Copy remaining files ───────────────────────
    println!("[8/11] Copying remaining files...");

    let extra_set: HashSet<&PathBuf> = extra_script_files.iter().collect();
    let mut copied = 0u32;
    for file_path in &other_files {
        if extra_set.contains(file_path) {
            continue; // Already converted to .lua in step 3
        }
        match copy_to_staging(file_path, input_path, &stage_path) {
            Ok(()) => copied += 1,
            Err(e) => eprintln!("  Warning: failed to copy {:?}: {:#}", file_path, e),
        }
    }
    println!("    {copied} files copied");

    // ── Step 9: Update asset paths in .ron files ───────────
    if !path_mappings.is_empty() {
        println!("[9/11] Updating asset paths in .ron files...");
        let mut updated_files = 0u32;
        let mut total_replacements = 0u32;

        for entry in walkdir::WalkDir::new(&stage_path)
            .into_iter()
            .filter_map(|e| e.ok())
        {
            let path = entry.path();
            if path.extension().and_then(|s| s.to_str()) != Some("ron") {
                continue;
            }

            let content = match fs::read_to_string(path) {
                Ok(c) => c,
                Err(_) => continue,
            };

            let original_content = content.clone();
            let mut new_content = content;
            let mut replaced = false;

            // ── Phase A: Normalize backslashes → forward slashes ──
            let raw_double_bs = "\\\\";
            if new_content.contains(raw_double_bs) {
                new_content = new_content.replace(raw_double_bs, "/");
                replaced = true;
            }

            // ── Phase B: Replace .b3d → .glb, .bmp → .png ──
            for (orig_path, new_path) in &path_mappings {
                let orig_fs = orig_path.replace('\\', "/");
                let new_fs = new_path.replace('\\', "/");

                if new_content.contains(&orig_fs) {
                    new_content = new_content.replace(&orig_fs, &new_fs);
                    replaced = true;
                }
            }

            if replaced && new_content != original_content {
                fs::write(path, &new_content)
                    .with_context(|| format!("writing updated {:?}", path))?;
                updated_files += 1;
                total_replacements += 1;
            }
        }
        println!("    {updated_files} .ron files updated ({total_replacements} replacements)");
    } else {
        println!("[9/11] No asset path updates needed");
    }

    // ── Step 10: Generate manifest ───────────────────────────
    println!("[10/11] Generating manifest...");

    let pack_name = input_path
        .file_name()
        .map(|s| s.to_string_lossy().to_string())
        .unwrap_or_else(|| "content".to_string());
    let pack_id = normalize_id(&pack_name);

    let mut manifest = ContentPackManifest::new(&pack_id, &pack_name);

    // Populate registry from the auto-registered HashMap — each key is
    // derived from the .ron filename and maps directly to a TOML domain.
    for (key, paths) in &registry {
        manifest.registry.domains.insert(key.clone(), paths.clone());
    }

    if debug {
        eprintln!("  Registry keys: {:?}", registry.keys().collect::<Vec<_>>());
    }

    let manifest_toml = manifest.to_toml()?;
    fs::write(stage_path.join("manifest.toml"), &manifest_toml)?;
    if debug {
        eprintln!("  manifest.toml:\n{}", manifest_toml);
    }

    // ── Step 11: Pack ───────────────────────────────────────
    println!("[11/11] Packing...");

    let output_path = Path::new(&cli.output);
    if cli.dir {
        if output_path.exists() {
            fs::remove_dir_all(output_path)?;
        }
        S2ModPackage::pack_to_dir(&stage_path, output_path)?;
        println!("  Written to directory: {}", cli.output);
    } else {
        if let Some(parent) = output_path.parent() {
            fs::create_dir_all(parent)?;
        }
        if output_path.exists() {
            fs::remove_file(output_path)?;
        }
        S2ModPackage::pack_to_zip(&stage_path, output_path)?;
        println!("  Written to archive: {}", cli.output);
    }

    println!();
    println!("Done!");

    Ok(())
}