Skip to main content

runec/
writer.rs

1use std::collections::HashMap;
2use std::fs;
3use std::io;
4use std::path::Path;
5
6use crate::bytecode::{CompiledScript, Opcode, Operand};
7use crate::types::Type;
8
9// ── CS2 Neptune binary format constants ────────────────────────────────────
10
11/// Compiler version written into the script.dat header.
12const COMPILER_VERSION: i32 = 27;
13
14/// Returns true when the operand for `opcode` is 4 bytes (large).
15/// Returns false when the operand is 1 byte (small).
16fn is_large_operand(opcode: u16) -> bool {
17    if opcode > 100 {
18        return false;
19    }
20    match opcode {
21        21 | 22 | 23 | 38 | 39 => false, // RETURN, GOSUB, JUMP, POP_INT_DISCARD, POP_STRING_DISCARD
22        _ => true,
23    }
24}
25
26/// Map a `Type` to its CS2 type-char byte (as used in ScriptVarType.ts).
27fn type_to_char(t: Type) -> u8 {
28    match t {
29        Type::Int => b'i',
30        Type::String => b's',
31        Type::Boolean => b'1',
32        Type::Coord => b'c',
33        Type::Loc => b'l',
34        Type::Npc => b'n',
35        Type::Obj => b'o',
36        Type::NamedObj => b'O',
37        Type::PlayerUid => b'p',
38        Type::NpcUid => b'u',
39        Type::Seq => b'A',
40        Type::Spotanim => b't',
41        Type::Synth => b'P',
42        Type::Stat => b'S',
43        Type::Component => b'I',
44        Type::Interface | Type::TopLevelInterface | Type::OverlayInterface => b'a',
45        Type::Inv => b'v',
46        Type::Enum => b'g',
47        Type::Struct => b'J',
48        Type::DbRow => 0xD0, // 'Ð' = 208
49        Type::Category => b'y',
50        Type::Varp => b'V',
51        Type::NpcStat => 0xFE, // 'þ' = 254
52        Type::IdKit => b'K',
53        _ => b'i', // default to int for unknown/unrepresented types
54    }
55}
56
57/// Encode a Rust string to RS2 single-byte encoding.
58/// Maps Unicode characters to their RuneScript byte equivalents.
59/// The RuneScript format uses the low byte of certain Unicode code points:
60/// U+2019 (') → 0x19, U+2013 (–) → 0x13, etc.
61fn encode_rs2_string(s: &str) -> Vec<u8> {
62    let mut out = Vec::with_capacity(s.len());
63    for ch in s.chars() {
64        let cp = ch as u32;
65        if cp < 0x80 {
66            out.push(cp as u8);
67        } else if cp < 0x100 {
68            // Latin-1 supplement: pass through as-is
69            out.push(cp as u8);
70        } else if (0x2010..=0x201F).contains(&cp) {
71            // Typographic punctuation: use low byte
72            // U+2013 (–) → 0x13, U+2014 (—) → 0x14
73            // U+2018 (') → 0x18, U+2019 (') → 0x19
74            // U+201C (") → 0x1C, U+201D (") → 0x1D
75            out.push((cp & 0xFF) as u8);
76        } else {
77            // Other Unicode: use low byte as fallback
78            out.push((cp & 0xFF) as u8);
79        }
80    }
81    out
82}
83
84// ── Script binary encoder ───────────────────────────────────────────────────
85
86/// Encode a single compiled script to the CS2 Neptune binary format.
87///
88/// Layout:
89///   [header]  scriptName\0 + sourceFilePath\0 + lookupKey(i32) +
90///             paramTypeCount(u8) + paramTypes(u8[]) +
91///             lineTableLen(u16) + lineTable(pc+line pairs)
92///   [instrs]  per-instruction: opcode(u16) + operand (variable)
93///   [trailer] _instructions(i32) + intLocals(u16) + strLocals(u16) +
94///             intArgs(u16) + strArgs(u16) + switches(u8) + tables...
95///   [last2]   trailerLen(u16)
96pub fn encode_script(script: &CompiledScript) -> Vec<u8> {
97    // ── collect switch tables and assign indices ────────────────────────────
98    // We scan instructions for SwitchTable operands, extract them, and
99    // replace their operands with the table index.
100    let mut switch_tables: Vec<Vec<(i32, i32)>> = Vec::new(); // (key, relative_offset)
101    // We'll build the final instruction list with resolved operands.
102    // Each entry: (effective_opcode: u16, resolved_operand)
103    enum ResolvedOp {
104        None,
105        Small(u8),
106        Large(i32),
107        Str(String),
108    }
109    struct FlatInstr {
110        opcode: u16,
111        op: ResolvedOp,
112    }
113
114    // First pass: build flat instruction list, collect switch tables.
115    // Jump targets are stored as absolute indices in Operand::JumpTarget;
116    // we convert them to relative offsets later.
117    let n = script.instructions.len();
118    let mut flat: Vec<FlatInstr> = Vec::with_capacity(n);
119    let mut switch_tbl_idxs: Vec<Option<usize>> = Vec::new(); // per-instruction switch table idx
120    // Line number table: (flat_idx_of_sentinel, line_number)
121    // After orig_to_final is built, flat_idx resolves to the PC of the next real instruction.
122    let mut raw_line_entries: Vec<(usize, i32)> = Vec::new();
123
124    for instr in &script.instructions {
125        // For Command instructions, the "real" opcode is stored in the operand;
126        // we extract it here and will write 0 as the operand value.
127        let is_command = instr.opcode == Opcode::Command;
128
129        // For Command instructions, the actual engine opcode is stored in the
130        // lower 16 bits of the Int operand, and the small operand byte (0 or 1
131        // for primary/secondary active-entity selection) is stored in bits 16-23.
132        let mut cmd_small_operand: u8 = 0;
133        let eff_opcode: u16 = match instr.opcode {
134            Opcode::Command => {
135                match &instr.operand {
136                    Operand::Int(v) => {
137                        cmd_small_operand = ((*v >> 16) & 0xFF) as u8;
138                        (*v & 0xFFFF) as u16
139                    }
140                    Operand::Str(s) => {
141                        // Unknown command referenced by name – skip with 0
142                        let _ = s;
143                        0
144                    }
145                    _ => 0,
146                }
147            }
148            Opcode::LineNumber => {
149                // Metadata – record line number and skip (add sentinel to filter).
150                let line = match &instr.operand {
151                    Operand::Int(v) => *v,
152                    _ => 0,
153                };
154                raw_line_entries.push((flat.len(), line));
155                switch_tbl_idxs.push(None);
156                flat.push(FlatInstr {
157                    opcode: 0xFFFF,
158                    op: ResolvedOp::None,
159                }); // sentinel to remove
160                continue;
161            }
162            op => op as u16,
163        };
164
165        // Command instructions: the operand byte encodes which active entity
166        // to use (0 = primary, 1 = secondary/dot-prefix form).
167        if is_command {
168            switch_tbl_idxs.push(None);
169            flat.push(FlatInstr {
170                opcode: eff_opcode,
171                op: ResolvedOp::Small(cmd_small_operand),
172            });
173            continue;
174        }
175
176        let resolved = match &instr.operand {
177            Operand::None => {
178                // No-operand instructions still need a byte written (0) if small.
179                if is_large_operand(eff_opcode) {
180                    ResolvedOp::Large(0)
181                } else {
182                    ResolvedOp::Small(0)
183                }
184            }
185            Operand::Int(v) => {
186                if eff_opcode == 3 {
187                    // PUSH_CONSTANT_STRING uses string operand; Int is a fallback
188                    ResolvedOp::Large(*v)
189                } else if is_large_operand(eff_opcode) {
190                    ResolvedOp::Large(*v)
191                } else {
192                    ResolvedOp::Small(*v as u8)
193                }
194            }
195            Operand::Long(v) => {
196                // Long literal: 4-byte high + 4-byte low encoded as two push instructions.
197                // For now, emit high 32-bits (this is rarely used in 2004scape).
198                ResolvedOp::Large(*v as i32)
199            }
200            Operand::Str(s) => ResolvedOp::Str(s.clone()),
201            Operand::JumpTarget(target) => {
202                // Will be resolved to relative offset in second pass.
203                ResolvedOp::Large(*target as i32) // placeholder – absolute index
204            }
205            Operand::SwitchTable(table) => {
206                // Assign a table index and store the table.
207                let tbl_idx = switch_tables.len();
208                // We'll fill in relative offsets in the second pass.
209                // For now, store the table with absolute targets.
210                let raw: Vec<(i32, i32)> = table
211                    .iter()
212                    .map(|(k, abs_target)| (*k, *abs_target as i32))
213                    .collect();
214                switch_tables.push(raw);
215                switch_tbl_idxs.push(Some(tbl_idx));
216                flat.push(FlatInstr {
217                    opcode: eff_opcode,
218                    op: ResolvedOp::Large(tbl_idx as i32),
219                });
220                continue;
221            }
222            Operand::StringCount(c) => {
223                if is_large_operand(eff_opcode) {
224                    ResolvedOp::Large(*c as i32)
225                } else {
226                    ResolvedOp::Small(*c as u8)
227                }
228            }
229            Operand::ArrayDef(var_id, type_char) => {
230                let packed = (*var_id << 8) | (*type_char as i32);
231                if is_large_operand(eff_opcode) {
232                    ResolvedOp::Large(packed)
233                } else {
234                    ResolvedOp::Small(packed as u8)
235                }
236            }
237        };
238
239        switch_tbl_idxs.push(None);
240        flat.push(FlatInstr {
241            opcode: eff_opcode,
242            op: resolved,
243        });
244    }
245
246    // Remove LineNumber sentinel instructions and keep track of index mapping.
247    // Build final instruction vector (without LineNumber instructions).
248    let mut final_instrs: Vec<FlatInstr> = Vec::new();
249    // Map from original index to final index (for resolving jump targets).
250    let mut orig_to_final: Vec<usize> = Vec::new(); // orig_to_final[orig] = final
251    for (i, fi) in flat.into_iter().enumerate() {
252        let _ = i;
253        if fi.opcode == 0xFFFF {
254            // LineNumber sentinel
255            orig_to_final.push(final_instrs.len()); // point to next valid instr
256        } else {
257            orig_to_final.push(final_instrs.len());
258            final_instrs.push(fi);
259        }
260    }
261    // Add sentinel for "one past end"
262    let total = final_instrs.len();
263
264    // Resolve raw_line_entries to (pc, line) pairs using orig_to_final.
265    // Deduplicate: if multiple LineNumber entries map to the same PC, keep the last one.
266    let mut line_table: Vec<(i32, i32)> = Vec::new();
267    for (flat_idx, line) in &raw_line_entries {
268        let pc = if *flat_idx < orig_to_final.len() {
269            orig_to_final[*flat_idx] as i32
270        } else {
271            total as i32
272        };
273        // Deduplicate: skip if same PC (keep last line for that PC) or same line as previous.
274        if let Some(last) = line_table.last_mut() {
275            if last.0 == pc {
276                last.1 = *line;
277                continue;
278            }
279            if last.1 == *line {
280                // Same line number as previous entry — Java compiler skips this.
281                continue;
282            }
283        }
284        line_table.push((pc, *line));
285    }
286
287    // Second pass: convert absolute jump targets to relative offsets.
288    // Relative offset = target_final_idx - (current_final_idx + 1).
289    for (i, fi) in final_instrs.iter_mut().enumerate() {
290        if let ResolvedOp::Large(v) = &fi.op {
291            // Check if this is a branch opcode (jump target stored as absolute idx)
292            let is_branch = matches!(
293                fi.opcode,
294                6 | 7 | 8 | 9 | 10 | 31 | 32 | 68 | 69 | 70 | 71 | 72 | 73 | 86 | 87
295            );
296            if is_branch {
297                let abs_target = *v as usize;
298                let final_target = if abs_target < orig_to_final.len() {
299                    orig_to_final[abs_target]
300                } else {
301                    total // out of bounds → point to end
302                };
303                let relative = final_target as i32 - (i as i32 + 1);
304                fi.op = ResolvedOp::Large(relative);
305            }
306        }
307    }
308
309    // Convert switch table targets to relative offsets.
310    // The switch instruction is at some index `sw_idx`; each case body has an
311    // absolute target. offset = target - (sw_idx + 1).
312    // We need to find where each switch instruction is in the final list to
313    // compute relative offsets.
314    // Re-scan original instructions to find switch instruction positions.
315    let mut sw_instr_positions: HashMap<usize, usize> = HashMap::new(); // tbl_idx -> final_pos
316    {
317        for (orig_idx, orig_instr) in script.instructions.iter().enumerate() {
318            if orig_idx >= orig_to_final.len() {
319                break;
320            }
321            if orig_instr.opcode == Opcode::LineNumber {
322                continue;
323            }
324            let final_pos = orig_to_final[orig_idx];
325            if let Operand::SwitchTable(_) = &orig_instr.operand {
326                // Use orig_idx to correctly index switch_tbl_idxs (which is populated
327                // in parallel with script.instructions, including LineNumber entries).
328                if let Some(tbl_idx) = switch_tbl_idxs.get(orig_idx).and_then(|x| *x) {
329                    sw_instr_positions.insert(tbl_idx, final_pos);
330                }
331            }
332        }
333    }
334
335    // Build relative-offset switch tables.
336    let switch_tables_relative: Vec<Vec<(i32, i32)>> = switch_tables
337        .iter()
338        .enumerate()
339        .map(|(tbl_idx, table)| {
340            let sw_pos = *sw_instr_positions.get(&tbl_idx).unwrap_or(&0);
341            table
342                .iter()
343                .map(|(key, abs_target)| {
344                    let final_target = if (*abs_target as usize) < orig_to_final.len() {
345                        orig_to_final[*abs_target as usize]
346                    } else {
347                        total
348                    };
349                    let rel = final_target as i32 - (sw_pos as i32 + 1);
350                    (*key, rel)
351                })
352                .collect()
353        })
354        .collect();
355
356    // ── build the binary buffer ────────────────────────────────────────────
357    let mut buf: Vec<u8> = Vec::new();
358
359    // Header: scriptName\0
360    buf.extend_from_slice(script.name.as_bytes());
361    buf.push(0);
362
363    // sourceFilePath\0
364    buf.extend_from_slice(script.source_path.as_bytes());
365    buf.push(0);
366
367    // lookupKey: -1 for non-entity scripts, or hash-based key for entity scripts
368    buf.extend_from_slice(&script.lookup_key.to_be_bytes());
369
370    // parameterTypeCount + parameterTypes
371    // The Java compiler only writes param types for debugproc scripts;
372    // all other triggers write 0 here.
373    if script.trigger == "debugproc" {
374        buf.push(script.param_types.len() as u8);
375        for &pt in &script.param_types {
376            buf.push(type_to_char(pt));
377        }
378    } else {
379        buf.push(0);
380    }
381
382    // lineNumberTableLength = count of (pc, line) entries
383    buf.extend_from_slice(&(line_table.len() as u16).to_be_bytes());
384    for (pc, line) in &line_table {
385        buf.extend_from_slice(&pc.to_be_bytes());
386        buf.extend_from_slice(&line.to_be_bytes());
387    }
388
389    // ── instructions ──────────────────────────────────────────────────────
390    for fi in &final_instrs {
391        buf.extend_from_slice(&fi.opcode.to_be_bytes());
392        match &fi.op {
393            ResolvedOp::None => {}
394            ResolvedOp::Small(v) => {
395                buf.push(*v);
396            }
397            ResolvedOp::Large(v) => {
398                buf.extend_from_slice(&v.to_be_bytes());
399            }
400            ResolvedOp::Str(s) => {
401                buf.extend(encode_rs2_string(s));
402                buf.push(0); // null terminator
403            }
404        }
405    }
406
407    // ── trailer ───────────────────────────────────────────────────────────
408    let trailer_start = buf.len();
409
410    // _instructions (instruction count)
411    buf.extend_from_slice(&(final_instrs.len() as i32).to_be_bytes());
412
413    // intLocalCount, stringLocalCount
414    buf.extend_from_slice(&script.int_local_count.to_be_bytes());
415    buf.extend_from_slice(&script.string_local_count.to_be_bytes());
416
417    // intArgCount, stringArgCount
418    buf.extend_from_slice(&script.int_arg_count.to_be_bytes());
419    buf.extend_from_slice(&script.string_arg_count.to_be_bytes());
420
421    // switches byte + switch tables
422    buf.push(switch_tables_relative.len() as u8);
423    for table in &switch_tables_relative {
424        buf.extend_from_slice(&(table.len() as u16).to_be_bytes());
425        for (key, offset) in table {
426            buf.extend_from_slice(&key.to_be_bytes());
427            buf.extend_from_slice(&offset.to_be_bytes());
428        }
429    }
430
431    // trailerLen = switch count byte + switch table data (matching TS: switchOffset + 1)
432    let switch_section_len = buf.len() - trailer_start - 12; // 12 = instrs(4) + locals/params(8)
433    buf.extend_from_slice(&(switch_section_len as u16).to_be_bytes());
434
435    buf
436}
437
438// ── PackedWriter: writes script.dat + script.idx ───────────────────────────
439
440/// Writes compiled scripts to script.dat and script.idx in the output directory.
441pub struct ScriptWriter {
442    output_dir: String,
443}
444
445impl ScriptWriter {
446    pub fn new(output_dir: String) -> Self {
447        ScriptWriter { output_dir }
448    }
449
450    /// Write all compiled scripts to script.dat and script.idx.
451    ///
452    /// File layout is **sparse by script.id**: the idx has one entry per
453    /// slot from 0..max_id; slots without a compiled script write length
454    /// 0 and contribute no bytes to the dat. The loader (`rs-engine`)
455    /// mirrors this — empty-length slots become no-op Script stubs so
456    /// the script at array-index N matches compile-time id N.
457    ///
458    /// Packing contiguously (old behaviour) misaligned runtime array
459    /// indices with compile-time ids, so bytecode `GOSUB 2405` (intended
460    /// `chatnpc_page`) resolved to `chatnpc` at runtime — infinite
461    /// recursion.
462    pub fn write_all(&self, scripts: &[CompiledScript]) -> io::Result<()> {
463        fs::create_dir_all(&self.output_dir)?;
464
465        // Max id determines the sparse slot count. Scripts with id < 0
466        // are stray (unresolved) and would collide at slot 0 — drop them
467        // explicitly rather than let them mask a valid id.
468        let max_id = scripts.iter().map(|s| s.id).max().unwrap_or(-1);
469        let slot_count = if max_id < 0 { 0 } else { (max_id + 1) as usize };
470
471        // Index scripts by id. Later writes at the same id overwrite
472        // earlier ones (matches Java compiler's last-wins behaviour when
473        // two declarations share an id — typically a compile-time bug,
474        // but we preserve the shape).
475        let mut by_id: Vec<Option<&CompiledScript>> = vec![None; slot_count];
476        for s in scripts {
477            if s.id >= 0 && (s.id as usize) < slot_count {
478                by_id[s.id as usize] = Some(s);
479            }
480        }
481
482        // Encode each occupied slot once. Empty slots emit zero bytes.
483        // Encoding is independent per slot, so fan it out across threads.
484        let encoded: Vec<Vec<u8>> =
485            crate::parallel_map(&by_id, |opt| opt.map(encode_script).unwrap_or_default());
486
487        // Build script.dat
488        let dat_path = Path::new(&self.output_dir).join("script.dat");
489        {
490            let mut dat: Vec<u8> = Vec::new();
491            // Header: count (u32) + version (i32)
492            dat.extend_from_slice(&(slot_count as u32).to_be_bytes());
493            dat.extend_from_slice(&COMPILER_VERSION.to_be_bytes());
494            for bytes in &encoded {
495                dat.extend_from_slice(bytes);
496            }
497            fs::write(&dat_path, dat)?;
498        }
499
500        // Build script.idx
501        let idx_path = Path::new(&self.output_dir).join("script.idx");
502        {
503            let mut idx: Vec<u8> = Vec::new();
504            idx.extend_from_slice(&(slot_count as u32).to_be_bytes());
505            for bytes in &encoded {
506                idx.extend_from_slice(&(bytes.len() as u32).to_be_bytes());
507            }
508            fs::write(&idx_path, idx)?;
509        }
510
511        Ok(())
512    }
513
514    pub fn build_all(&self, scripts: &[CompiledScript]) -> io::Result<(Vec<u8>, Vec<u8>)> {
515        // Max id determines the sparse slot count. Scripts with id < 0
516        // are stray (unresolved) and would collide at slot 0 — drop them
517        // explicitly rather than let them mask a valid id.
518        let max_id = scripts.iter().map(|s| s.id).max().unwrap_or(-1);
519        let slot_count = if max_id < 0 { 0 } else { (max_id + 1) as usize };
520
521        // Index scripts by id. Later writes at the same id overwrite
522        // earlier ones (matches Java compiler's last-wins behaviour when
523        // two declarations share an id — typically a compile-time bug,
524        // but we preserve the shape).
525        let mut by_id: Vec<Option<&CompiledScript>> = vec![None; slot_count];
526        for s in scripts {
527            if s.id >= 0 && (s.id as usize) < slot_count {
528                by_id[s.id as usize] = Some(s);
529            }
530        }
531
532        // Encode each occupied slot once. Empty slots emit zero bytes.
533        // Encoding is independent per slot, so fan it out across threads.
534        let encoded: Vec<Vec<u8>> =
535            crate::parallel_map(&by_id, |opt| opt.map(encode_script).unwrap_or_default());
536
537        let mut dat: Vec<u8> = Vec::new();
538        dat.extend_from_slice(&(slot_count as u32).to_be_bytes());
539        dat.extend_from_slice(&COMPILER_VERSION.to_be_bytes());
540        for bytes in &encoded {
541            dat.extend_from_slice(bytes);
542        }
543
544        let mut idx: Vec<u8> = Vec::new();
545        idx.extend_from_slice(&(slot_count as u32).to_be_bytes());
546        for bytes in &encoded {
547            idx.extend_from_slice(&(bytes.len() as u32).to_be_bytes());
548        }
549
550        // // Build script.dat
551        // let dat_path = Path::new(&self.output_dir).join("script.dat");
552        // {
553        //     let mut dat: Vec<u8> = Vec::new();
554        //     // Header: count (u32) + version (i32)
555        //     dat.extend_from_slice(&(slot_count as u32).to_be_bytes());
556        //     dat.extend_from_slice(&COMPILER_VERSION.to_be_bytes());
557        //     for bytes in &encoded {
558        //         dat.extend_from_slice(bytes);
559        //     }
560        //     fs::write(&dat_path, dat)?;
561        // }
562        //
563        // // Build script.idx
564        // let idx_path = Path::new(&self.output_dir).join("script.idx");
565        // {
566        //     let mut idx: Vec<u8> = Vec::new();
567        //     idx.extend_from_slice(&(slot_count as u32).to_be_bytes());
568        //     for bytes in &encoded {
569        //         idx.extend_from_slice(&(bytes.len() as u32).to_be_bytes());
570        //     }
571        //     fs::write(&idx_path, idx)?;
572        // }
573
574        Ok((dat, idx))
575    }
576}
577
578// ── PackBuffer (in-memory, for testing) ────────────────────────────────────
579
580pub struct PackBuffer {
581    pub scripts: HashMap<String, Vec<u8>>,
582}
583
584impl Default for PackBuffer {
585    fn default() -> Self {
586        Self::new()
587    }
588}
589
590impl PackBuffer {
591    pub fn new() -> Self {
592        PackBuffer {
593            scripts: HashMap::new(),
594        }
595    }
596
597    pub fn add_script(&mut self, script: &CompiledScript) {
598        let bytes = encode_script(script);
599        self.scripts.insert(script.name.clone(), bytes);
600    }
601}