rs-runec 0.5.0

Compiler, linter, and language server for RuneScript, ClientScript, and GlobalScript, with ClientScript decompilation
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
use std::collections::HashMap;
use std::fs;
use std::io;
use std::path::Path;

use crate::bytecode::{CompiledScript, Instruction, Opcode, Operand};
use crate::types::Type;

// ── CS2 Neptune binary format constants ────────────────────────────────────

/// Compiler version written into the script.dat header.
const COMPILER_VERSION: i32 = 27;

/// Returns true when the operand for `opcode` is 4 bytes (large).
/// Returns false when the operand is 1 byte (small).
fn is_large_operand(opcode: u16) -> bool {
    if opcode > 100 {
        return false;
    }
    match opcode {
        21 | 22 | 23 | 38 | 39 => false, // RETURN, GOSUB, JUMP, POP_INT_DISCARD, POP_STRING_DISCARD
        _ => true,
    }
}

/// Map a `Type` to its CS2 type-char byte (as used in ScriptVarType.ts).
fn type_to_char(t: Type) -> u8 {
    match t {
        Type::Int => b'i',
        Type::String => b's',
        Type::Boolean => b'1',
        Type::Coord => b'c',
        Type::Loc => b'l',
        Type::Npc => b'n',
        Type::Obj => b'o',
        Type::NamedObj => b'O',
        Type::PlayerUid => b'p',
        Type::NpcUid => b'u',
        Type::Seq => b'A',
        Type::Spotanim => b't',
        Type::Synth => b'P',
        Type::Stat => b'S',
        Type::Component => b'I',
        Type::Interface | Type::TopLevelInterface | Type::OverlayInterface => b'a',
        Type::Inv => b'v',
        Type::Enum => b'g',
        Type::Struct => b'J',
        Type::DbRow => 0xD0, // 'Ð' = 208
        Type::Category => b'y',
        Type::Varp => b'V',
        Type::NpcStat => 0xFE, // 'þ' = 254
        Type::IdKit => b'K',
        _ => b'i', // default to int for unknown/unrepresented types
    }
}

/// Encode a Rust string to RS2 single-byte encoding.
/// Maps Unicode characters to their RuneScript byte equivalents.
/// The RuneScript format uses the low byte of certain Unicode code points:
/// U+2019 (') → 0x19, U+2013 (–) → 0x13, etc.
fn encode_rs2_string(s: &str) -> Vec<u8> {
    let mut out = Vec::with_capacity(s.len());
    for ch in s.chars() {
        let cp = ch as u32;
        if cp < 0x80 {
            out.push(cp as u8);
        } else if cp < 0x100 {
            // Latin-1 supplement: pass through as-is
            out.push(cp as u8);
        } else if (0x2010..=0x201F).contains(&cp) {
            // Typographic punctuation: use low byte
            // U+2013 (–) → 0x13, U+2014 (—) → 0x14
            // U+2018 (') → 0x18, U+2019 (') → 0x19
            // U+201C (") → 0x1C, U+201D (") → 0x1D
            out.push((cp & 0xFF) as u8);
        } else {
            // Other Unicode: use low byte as fallback
            out.push((cp & 0xFF) as u8);
        }
    }
    out
}

// ── Script binary encoder ───────────────────────────────────────────────────

/// Encode a single compiled script to the CS2 Neptune binary format.
///
/// Layout:
/// ```text
///   [header]  scriptName\0 + sourceFilePath\0 + lookupKey(i32) +
///             paramTypeCount(u8) + paramTypes(u8[]) +
///             lineTableLen(u16) + lineTable(pc+line pairs)
///   [instrs]  per-instruction: opcode(u16) + operand (variable)
///   [trailer] _instructions(i32) + intLocals(u16) + strLocals(u16) +
///             intArgs(u16) + strArgs(u16) + switches(u8) + tables...
///   [last2]   trailerLen(u16)
/// ```
pub fn encode_script(script: &CompiledScript) -> Vec<u8> {
    if script.instructions.iter().any(|instruction| {
        matches!(
            instruction.opcode,
            Opcode::BranchIfTrue | Opcode::BranchIfFalse
        )
    }) {
        let mut lowered = script.clone();
        let mut positions = Vec::with_capacity(script.instructions.len() + 1);
        lowered.instructions.clear();
        for instruction in &script.instructions {
            positions.push(lowered.instructions.len());
            if matches!(
                instruction.opcode,
                Opcode::BranchIfTrue | Opcode::BranchIfFalse
            ) {
                lowered.instructions.push(Instruction::push_int(i32::from(
                    instruction.opcode == Opcode::BranchIfTrue,
                )));
                lowered.instructions.push(Instruction::new(
                    Opcode::BranchEquals,
                    instruction.operand.clone(),
                ));
            } else {
                lowered.instructions.push(instruction.clone());
            }
        }
        positions.push(lowered.instructions.len());
        for instruction in &mut lowered.instructions {
            if let Operand::JumpTarget(target) = &mut instruction.operand {
                *target = positions[*target];
            }
        }
        for table in &mut lowered.switch_tables {
            for (_, target) in table.iter_mut() {
                *target = positions[*target];
            }
        }
        return encode_script(&lowered);
    }
    // ── collect switch tables and assign indices ────────────────────────────
    // We scan instructions for SwitchTable operands, extract them, and
    // replace their operands with the table index.
    let mut switch_tables: Vec<Vec<(i32, i32)>> = Vec::new(); // (key, relative_offset)
    // We'll build the final instruction list with resolved operands.
    // Each entry: (effective_opcode: u16, resolved_operand)
    enum ResolvedOp {
        None,
        Small(u8),
        Large(i32),
        Str(Box<str>),
    }
    struct FlatInstr {
        opcode: u16,
        op: ResolvedOp,
    }

    // First pass: build flat instruction list, collect switch tables.
    // Jump targets are stored as absolute indices in Operand::JumpTarget;
    // we convert them to relative offsets later.
    let n = script.instructions.len();
    let mut flat: Vec<FlatInstr> = Vec::with_capacity(n);
    let mut switch_tbl_idxs: Vec<Option<usize>> = Vec::new(); // per-instruction switch table idx
    // Line number table: (flat_idx_of_sentinel, line_number)
    // After orig_to_final is built, flat_idx resolves to the PC of the next real instruction.
    let mut raw_line_entries: Vec<(usize, i32)> = Vec::new();

    for instr in &script.instructions {
        // For Command instructions, the "real" opcode is stored in the operand;
        // we extract it here and will write 0 as the operand value.
        let is_command = instr.opcode == Opcode::Command;

        // For Command instructions, the actual engine opcode is stored in the
        // lower 16 bits of the Int operand, and the small operand byte (0 or 1
        // for primary/secondary active-entity selection) is stored in bits 16-23.
        let mut cmd_small_operand: u8 = 0;
        let eff_opcode: u16 = match instr.opcode {
            Opcode::Command => {
                match &instr.operand {
                    Operand::Int(v) => {
                        cmd_small_operand = ((*v >> 16) & 0xFF) as u8;
                        (*v & 0xFFFF) as u16
                    }
                    Operand::Str(idx) => {
                        // Unknown command referenced by name – skip with 0
                        let _ = idx;
                        0
                    }
                    _ => 0,
                }
            }
            Opcode::LineNumber => {
                // Metadata – record line number and skip (add sentinel to filter).
                let line = match &instr.operand {
                    Operand::Int(v) => *v,
                    _ => 0,
                };
                raw_line_entries.push((flat.len(), line));
                switch_tbl_idxs.push(None);
                flat.push(FlatInstr {
                    opcode: 0xFFFF,
                    op: ResolvedOp::None,
                }); // sentinel to remove
                continue;
            }
            op => op as u16,
        };

        // Command instructions: the operand byte encodes which active entity
        // to use (0 = primary, 1 = secondary/dot-prefix form).
        if is_command {
            switch_tbl_idxs.push(None);
            flat.push(FlatInstr {
                opcode: eff_opcode,
                op: ResolvedOp::Small(cmd_small_operand),
            });
            continue;
        }

        let resolved = match &instr.operand {
            Operand::None => {
                // No-operand instructions still need a byte written (0) if small.
                if is_large_operand(eff_opcode) {
                    ResolvedOp::Large(0)
                } else {
                    ResolvedOp::Small(0)
                }
            }
            Operand::Int(v) => {
                if eff_opcode == 3 {
                    // PUSH_CONSTANT_STRING uses string operand; Int is a fallback
                    ResolvedOp::Large(*v)
                } else if is_large_operand(eff_opcode) {
                    ResolvedOp::Large(*v)
                } else {
                    ResolvedOp::Small(*v as u8)
                }
            }
            Operand::Long(v) => {
                // Long literal: 4-byte high + 4-byte low encoded as two push instructions.
                // For now, emit high 32-bits (this is rarely used in 2004scape).
                ResolvedOp::Large(*v as i32)
            }
            Operand::Str(idx) => ResolvedOp::Str(script.strings[*idx as usize].clone()),
            Operand::JumpTarget(target) => {
                // Will be resolved to relative offset in second pass.
                ResolvedOp::Large(*target as i32) // placeholder – absolute index
            }
            Operand::SwitchTable(idx) => {
                // The table lives in the script's pool; the operand is its index.
                let table = &script.switch_tables[*idx as usize];
                // Assign a table index and store the table.
                let tbl_idx = switch_tables.len();
                // We'll fill in relative offsets in the second pass.
                // For now, store the table with absolute targets.
                let raw: Vec<(i32, i32)> = table
                    .iter()
                    .map(|(k, abs_target)| (*k, *abs_target as i32))
                    .collect();
                switch_tables.push(raw);
                switch_tbl_idxs.push(Some(tbl_idx));
                flat.push(FlatInstr {
                    opcode: eff_opcode,
                    op: ResolvedOp::Large(tbl_idx as i32),
                });
                continue;
            }
            Operand::StringCount(c) => {
                if is_large_operand(eff_opcode) {
                    ResolvedOp::Large(*c as i32)
                } else {
                    ResolvedOp::Small(*c as u8)
                }
            }
            Operand::ArrayDef(var_id, type_char) => {
                let packed = (*var_id << 8) | (*type_char as i32);
                if is_large_operand(eff_opcode) {
                    ResolvedOp::Large(packed)
                } else {
                    ResolvedOp::Small(packed as u8)
                }
            }
        };

        switch_tbl_idxs.push(None);
        flat.push(FlatInstr {
            opcode: eff_opcode,
            op: resolved,
        });
    }

    // Remove LineNumber sentinel instructions and keep track of index mapping.
    // Build final instruction vector (without LineNumber instructions).
    let mut final_instrs: Vec<FlatInstr> = Vec::new();
    // Map from original index to final index (for resolving jump targets).
    let mut orig_to_final: Vec<usize> = Vec::new(); // orig_to_final[orig] = final
    for (i, fi) in flat.into_iter().enumerate() {
        let _ = i;
        if fi.opcode == 0xFFFF {
            // LineNumber sentinel
            orig_to_final.push(final_instrs.len()); // point to next valid instr
        } else {
            orig_to_final.push(final_instrs.len());
            final_instrs.push(fi);
        }
    }
    // Add sentinel for "one past end"
    let total = final_instrs.len();

    // Resolve raw_line_entries to (pc, line) pairs using orig_to_final.
    // Deduplicate: if multiple LineNumber entries map to the same PC, keep the last one.
    let mut line_table: Vec<(i32, i32)> = Vec::new();
    for (flat_idx, line) in &raw_line_entries {
        let pc = if *flat_idx < orig_to_final.len() {
            orig_to_final[*flat_idx] as i32
        } else {
            total as i32
        };
        // Deduplicate: skip if same PC (keep last line for that PC) or same line as previous.
        if let Some(last) = line_table.last_mut() {
            if last.0 == pc {
                last.1 = *line;
                continue;
            }
            if last.1 == *line {
                // Same line number as previous entry — Java compiler skips this.
                continue;
            }
        }
        line_table.push((pc, *line));
    }

    // Second pass: convert absolute jump targets to relative offsets.
    // Relative offset = target_final_idx - (current_final_idx + 1).
    for (i, fi) in final_instrs.iter_mut().enumerate() {
        if let ResolvedOp::Large(v) = &fi.op {
            // Check if this is a branch opcode (jump target stored as absolute idx)
            let is_branch = matches!(
                fi.opcode,
                6 | 7 | 8 | 9 | 10 | 31 | 32 | 68 | 69 | 70 | 71 | 72 | 73 | 86 | 87
            );
            if is_branch {
                let abs_target = *v as usize;
                let final_target = if abs_target < orig_to_final.len() {
                    orig_to_final[abs_target]
                } else {
                    total // out of bounds → point to end
                };
                let relative = final_target as i32 - (i as i32 + 1);
                fi.op = ResolvedOp::Large(relative);
            }
        }
    }

    // Convert switch table targets to relative offsets.
    // The switch instruction is at some index `sw_idx`; each case body has an
    // absolute target. offset = target - (sw_idx + 1).
    // We need to find where each switch instruction is in the final list to
    // compute relative offsets.
    // Re-scan original instructions to find switch instruction positions.
    let mut sw_instr_positions: HashMap<usize, usize> = HashMap::new(); // tbl_idx -> final_pos
    {
        for (orig_idx, orig_instr) in script.instructions.iter().enumerate() {
            if orig_idx >= orig_to_final.len() {
                break;
            }
            if orig_instr.opcode == Opcode::LineNumber {
                continue;
            }
            let final_pos = orig_to_final[orig_idx];
            if let Operand::SwitchTable(_) = &orig_instr.operand {
                // Use orig_idx to correctly index switch_tbl_idxs (which is populated
                // in parallel with script.instructions, including LineNumber entries).
                if let Some(tbl_idx) = switch_tbl_idxs.get(orig_idx).and_then(|x| *x) {
                    sw_instr_positions.insert(tbl_idx, final_pos);
                }
            }
        }
    }

    // Build relative-offset switch tables.
    let switch_tables_relative: Vec<Vec<(i32, i32)>> = switch_tables
        .iter()
        .enumerate()
        .map(|(tbl_idx, table)| {
            let sw_pos = *sw_instr_positions.get(&tbl_idx).unwrap_or(&0);
            table
                .iter()
                .map(|(key, abs_target)| {
                    let final_target = if (*abs_target as usize) < orig_to_final.len() {
                        orig_to_final[*abs_target as usize]
                    } else {
                        total
                    };
                    let rel = final_target as i32 - (sw_pos as i32 + 1);
                    (*key, rel)
                })
                .collect()
        })
        .collect();

    // ── build the binary buffer ────────────────────────────────────────────
    let mut buf: Vec<u8> = Vec::new();

    // Header: scriptName\0
    buf.extend_from_slice(script.name.as_bytes());
    buf.push(0);

    // sourceFilePath\0
    buf.extend_from_slice(script.source_path.as_bytes());
    buf.push(0);

    // lookupKey: -1 for non-entity scripts, or hash-based key for entity scripts
    buf.extend_from_slice(&script.lookup_key.to_be_bytes());

    // parameterTypeCount + parameterTypes
    // The Java compiler only writes param types for debugproc scripts;
    // all other triggers write 0 here.
    if script.trigger == "debugproc" {
        buf.push(script.param_types.len() as u8);
        for &pt in &script.param_types {
            buf.push(type_to_char(pt));
        }
    } else {
        buf.push(0);
    }

    // lineNumberTableLength = count of (pc, line) entries
    buf.extend_from_slice(&(line_table.len() as u16).to_be_bytes());
    for (pc, line) in &line_table {
        buf.extend_from_slice(&pc.to_be_bytes());
        buf.extend_from_slice(&line.to_be_bytes());
    }

    // ── instructions ──────────────────────────────────────────────────────
    for fi in &final_instrs {
        buf.extend_from_slice(&fi.opcode.to_be_bytes());
        match &fi.op {
            ResolvedOp::None => {}
            ResolvedOp::Small(v) => {
                buf.push(*v);
            }
            ResolvedOp::Large(v) => {
                buf.extend_from_slice(&v.to_be_bytes());
            }
            ResolvedOp::Str(s) => {
                buf.extend(encode_rs2_string(s));
                buf.push(0); // null terminator
            }
        }
    }

    // ── trailer ───────────────────────────────────────────────────────────
    let trailer_start = buf.len();

    // _instructions (instruction count)
    buf.extend_from_slice(&(final_instrs.len() as i32).to_be_bytes());

    // intLocalCount, stringLocalCount
    buf.extend_from_slice(&script.int_local_count.to_be_bytes());
    buf.extend_from_slice(&script.string_local_count.to_be_bytes());

    // intArgCount, stringArgCount
    buf.extend_from_slice(&script.int_arg_count.to_be_bytes());
    buf.extend_from_slice(&script.string_arg_count.to_be_bytes());

    // switches byte + switch tables
    buf.push(switch_tables_relative.len() as u8);
    for table in &switch_tables_relative {
        buf.extend_from_slice(&(table.len() as u16).to_be_bytes());
        for (key, offset) in table {
            buf.extend_from_slice(&key.to_be_bytes());
            buf.extend_from_slice(&offset.to_be_bytes());
        }
    }

    // trailerLen = switch count byte + switch table data (matching TS: switchOffset + 1)
    let switch_section_len = buf.len() - trailer_start - 12; // 12 = instrs(4) + locals/params(8)
    buf.extend_from_slice(&(switch_section_len as u16).to_be_bytes());

    buf
}

// ── PackedWriter: writes script.dat + script.idx ───────────────────────────

/// Encode the server container without accessing storage. Each entry is a
/// script ID slot; supply an empty vector for missing IDs.
pub fn encode_bundle(encoded: &[Vec<u8>]) -> (Vec<u8>, Vec<u8>) {
    let slot_count = encoded.len();

    let mut dat: Vec<u8> = Vec::new();
    dat.extend_from_slice(&(slot_count as u32).to_be_bytes());
    dat.extend_from_slice(&COMPILER_VERSION.to_be_bytes());
    for bytes in encoded {
        dat.extend_from_slice(bytes);
    }

    let mut idx: Vec<u8> = Vec::new();
    idx.extend_from_slice(&(slot_count as u32).to_be_bytes());
    for bytes in encoded {
        idx.extend_from_slice(&(bytes.len() as u32).to_be_bytes());
    }

    (dat, idx)
}

/// Writes compiled scripts to script.dat and script.idx in the output directory.
pub struct ScriptWriter {
    output_dir: String,
}

impl ScriptWriter {
    pub fn new(output_dir: String) -> Self {
        ScriptWriter { output_dir }
    }

    /// Write all compiled scripts to script.dat and script.idx.
    ///
    /// File layout is **sparse by script.id**: the idx has one entry per
    /// slot from 0..max_id; slots without a compiled script write length
    /// 0 and contribute no bytes to the dat. The loader (`rs-engine`)
    /// mirrors this — empty-length slots become no-op Script stubs so
    /// the script at array-index N matches compile-time id N.
    ///
    /// Packing contiguously (old behaviour) misaligned runtime array
    /// indices with compile-time ids, so bytecode `GOSUB 2405` (intended
    /// `chatnpc_page`) resolved to `chatnpc` at runtime — infinite
    /// recursion.
    pub fn write_all(&self, scripts: &[CompiledScript]) -> io::Result<()> {
        fs::create_dir_all(&self.output_dir)?;

        // Max id determines the sparse slot count. Scripts with id < 0
        // are stray (unresolved) and would collide at slot 0 — drop them
        // explicitly rather than let them mask a valid id.
        let max_id = scripts.iter().map(|s| s.id).max().unwrap_or(-1);
        let slot_count = if max_id < 0 { 0 } else { (max_id + 1) as usize };

        // Index scripts by id. Later writes at the same id overwrite
        // earlier ones (matches Java compiler's last-wins behaviour when
        // two declarations share an id — typically a compile-time bug,
        // but we preserve the shape).
        let mut by_id: Vec<Option<&CompiledScript>> = vec![None; slot_count];
        for s in scripts {
            if s.id >= 0 && (s.id as usize) < slot_count {
                by_id[s.id as usize] = Some(s);
            }
        }

        // Encode each occupied slot once. Empty slots emit zero bytes.
        // Encoding is independent per slot, so fan it out across threads.
        let encoded: Vec<Vec<u8>> =
            crate::parallel_map(&by_id, |opt| opt.map(encode_script).unwrap_or_default());

        self.write_encoded(&encoded)
    }

    /// Write `script.dat` + `script.idx` from pre-encoded per-slot byte blobs
    /// (`encoded[slot]` is that script's bytes; empty for unused slots). Lets the
    /// streaming recompile path produce output without ever holding the full
    /// `Vec<CompiledScript>` — it encodes each script as it re-compiles, places
    /// the bytes by id, then calls this. Byte-identical to `write_all`.
    pub fn write_encoded(&self, encoded: &[Vec<u8>]) -> io::Result<()> {
        fs::create_dir_all(&self.output_dir)?;
        let slot_count = encoded.len();

        let dat_path = Path::new(&self.output_dir).join("script.dat");
        {
            // Stream the dat to disk so we never hold a second full copy of the
            // output (encoded blobs + a concatenated buffer). Byte-identical.
            use std::io::Write;
            let mut dat = io::BufWriter::new(fs::File::create(&dat_path)?);
            dat.write_all(&(slot_count as u32).to_be_bytes())?;
            dat.write_all(&COMPILER_VERSION.to_be_bytes())?;
            for bytes in encoded {
                dat.write_all(bytes)?;
            }
            dat.flush()?;
        }

        let idx_path = Path::new(&self.output_dir).join("script.idx");
        {
            let mut idx: Vec<u8> = Vec::new();
            idx.extend_from_slice(&(slot_count as u32).to_be_bytes());
            for bytes in encoded {
                idx.extend_from_slice(&(bytes.len() as u32).to_be_bytes());
            }
            fs::write(&idx_path, idx)?;
        }

        Ok(())
    }

    /// Build `script.dat` + `script.idx` bytes in memory from pre-encoded per-slot
    /// blobs — the in-memory counterpart of [`ScriptWriter::write_encoded`], used
    /// by the recompile pipeline's in-memory entry point. Uses the identical
    /// header + sparse-by-id layout, so it is byte-for-byte equal to `build_all`
    /// for the same scripts.
    pub fn build_encoded(&self, encoded: &[Vec<u8>]) -> (Vec<u8>, Vec<u8>) {
        encode_bundle(encoded)
    }

    pub fn build_all(&self, scripts: &[CompiledScript]) -> io::Result<(Vec<u8>, Vec<u8>)> {
        // Max id determines the sparse slot count. Scripts with id < 0
        // are stray (unresolved) and would collide at slot 0 — drop them
        // explicitly rather than let them mask a valid id.
        let max_id = scripts.iter().map(|s| s.id).max().unwrap_or(-1);
        let slot_count = if max_id < 0 { 0 } else { (max_id + 1) as usize };

        // Index scripts by id. Later writes at the same id overwrite
        // earlier ones (matches Java compiler's last-wins behaviour when
        // two declarations share an id — typically a compile-time bug,
        // but we preserve the shape).
        let mut by_id: Vec<Option<&CompiledScript>> = vec![None; slot_count];
        for s in scripts {
            if s.id >= 0 && (s.id as usize) < slot_count {
                by_id[s.id as usize] = Some(s);
            }
        }

        // Encode each occupied slot once. Empty slots emit zero bytes.
        // Encoding is independent per slot, so fan it out across threads.
        let encoded: Vec<Vec<u8>> =
            crate::parallel_map(&by_id, |opt| opt.map(encode_script).unwrap_or_default());

        let mut dat: Vec<u8> = Vec::new();
        dat.extend_from_slice(&(slot_count as u32).to_be_bytes());
        dat.extend_from_slice(&COMPILER_VERSION.to_be_bytes());
        for bytes in &encoded {
            dat.extend_from_slice(bytes);
        }

        let mut idx: Vec<u8> = Vec::new();
        idx.extend_from_slice(&(slot_count as u32).to_be_bytes());
        for bytes in &encoded {
            idx.extend_from_slice(&(bytes.len() as u32).to_be_bytes());
        }

        // // Build script.dat
        // let dat_path = Path::new(&self.output_dir).join("script.dat");
        // {
        //     let mut dat: Vec<u8> = Vec::new();
        //     // Header: count (u32) + version (i32)
        //     dat.extend_from_slice(&(slot_count as u32).to_be_bytes());
        //     dat.extend_from_slice(&COMPILER_VERSION.to_be_bytes());
        //     for bytes in &encoded {
        //         dat.extend_from_slice(bytes);
        //     }
        //     fs::write(&dat_path, dat)?;
        // }
        //
        // // Build script.idx
        // let idx_path = Path::new(&self.output_dir).join("script.idx");
        // {
        //     let mut idx: Vec<u8> = Vec::new();
        //     idx.extend_from_slice(&(slot_count as u32).to_be_bytes());
        //     for bytes in &encoded {
        //         idx.extend_from_slice(&(bytes.len() as u32).to_be_bytes());
        //     }
        //     fs::write(&idx_path, idx)?;
        // }

        Ok((dat, idx))
    }
}

// ── PackBuffer (in-memory, for testing) ────────────────────────────────────

pub struct PackBuffer {
    pub scripts: HashMap<String, Vec<u8>>,
}

impl Default for PackBuffer {
    fn default() -> Self {
        Self::new()
    }
}

impl PackBuffer {
    pub fn new() -> Self {
        PackBuffer {
            scripts: HashMap::new(),
        }
    }

    pub fn add_script(&mut self, script: &CompiledScript) {
        let bytes = encode_script(script);
        self.scripts.insert(script.name.clone(), bytes);
    }
}