Skip to main content

runec/
symloader.rs

1//! Loads pack files and populates the SymbolRegistry.
2//!
3//! Pack files use 2004scape's `id=name` format (matching CompilerTypeInfo.load).
4//! Constants are loaded from `.constant` files in the scripts directory.
5
6use std::fs;
7use std::path::Path;
8
9use tracing::{info, warn};
10
11use crate::symbol::{SymbolKind, SymbolRegistry};
12use crate::types::Type;
13
14/// Parse the parameter types declared in an `engine.rs2` file and populate
15/// `registry.command_param_types`. Lines like:
16///   `[command,sound_synth](synth $sound, int $loops, int $delay)`
17/// are parsed to extract the ordered list of parameter types per command.
18pub fn load_engine_command_params(registry: &mut SymbolRegistry, engine_rs2: &Path) {
19    let text = match fs::read_to_string(engine_rs2) {
20        Ok(t) => t,
21        Err(_) => return,
22    };
23    for line in text.lines() {
24        let line = line.trim();
25        // Match lines starting with `[command,<name>]`
26        if !line.starts_with("[command,") {
27            continue;
28        }
29        // Extract command name (between the comma and closing bracket)
30        let rest = &line["[command,".len()..];
31        let name_end = match rest.find(']') {
32            Some(i) => i,
33            None => continue,
34        };
35        let name = rest[..name_end].trim().to_string();
36
37        // Extract parameter list between first `(` and matching `)`
38        let after_bracket = &rest[name_end + 1..];
39        let params_start = match after_bracket.find('(') {
40            Some(i) => i + 1,
41            None => {
42                // No params at all
43                registry.command_param_types.insert(name, Vec::new());
44                continue;
45            }
46        };
47        let params_end = match after_bracket[params_start..].find(')') {
48            Some(i) => params_start + i,
49            None => continue,
50        };
51        let params_str = &after_bracket[params_start..params_end];
52
53        let mut param_types: Vec<Type> = Vec::new();
54        for param in params_str.split(',') {
55            let param = param.trim();
56            if param.is_empty() {
57                continue;
58            }
59            // Each param is like `synth $sound` or `int $loops` or just `int`
60            let type_name = param.split_whitespace().next().unwrap_or(param);
61            if let Some(t) = Type::from_name(type_name) {
62                param_types.push(t);
63            } else {
64                // Unknown type — push Int as placeholder so arg count stays correct
65                param_types.push(Type::Int);
66            }
67        }
68        registry
69            .command_param_types
70            .insert(name.clone(), param_types.clone());
71
72        if let Some(sym) = registry.commands.get_mut(&name)
73            && let SymbolKind::Command {
74                param_types: ref mut pt,
75                ..
76            } = sym.kind
77        {
78            *pt = param_types;
79        }
80
81        // Parse return types from the second parenthesized group: `(param_types)(return_types)`
82        let after_params = &after_bracket[params_end + 1..];
83        if let Some(ret_start) = after_params.find('(')
84            && let Some(ret_end) = after_params[ret_start + 1..].find(')')
85        {
86            let ret_str = &after_params[ret_start + 1..ret_start + 1 + ret_end];
87            let mut return_types: Vec<Type> = Vec::new();
88            for ret in ret_str.split(',') {
89                let ret = ret.trim();
90                if ret.is_empty() {
91                    continue;
92                }
93                if let Some(t) = Type::from_name(ret) {
94                    return_types.push(t);
95                }
96            }
97            if !return_types.is_empty() {
98                // Update the command's return types in the registry
99                if let Some(sym) = registry.commands.get_mut(&name)
100                    && let SymbolKind::Command {
101                        return_types: ref mut rt,
102                        ..
103                    } = sym.kind
104                {
105                    *rt = return_types;
106                }
107            }
108        }
109    }
110}
111
112/// Patch return types for commands whose signatures are not in engine.rs2 but are
113/// known to return values in the Neptune compiler. Without this, statement-level
114/// calls to these commands will be missing POP_INT_DISCARD / POP_STRING_DISCARD.
115pub fn patch_command_return_types(registry: &mut SymbolRegistry) {
116    let patches: &[(&str, Vec<Type>)] = &[
117        ("db_find_with_count", vec![Type::Int]),
118        ("db_find_refine_with_count", vec![Type::Int]),
119        ("db_listall_with_count", vec![Type::Int]),
120    ];
121    for (name, ret_types) in patches {
122        if let Some(sym) = registry.commands.get_mut(*name)
123            && let SymbolKind::Command { return_types, .. } = &mut sym.kind
124            && return_types.is_empty()
125        {
126            *return_types = ret_types.clone();
127        }
128    }
129}
130
131/// Load script.pack: `id=[trigger,name]` — pre-assigns script IDs so the
132/// Rust compiler uses the same IDs as the Java compiler.
133fn load_script_pack(registry: &mut SymbolRegistry, path: &Path) {
134    let text = match fs::read_to_string(path) {
135        Ok(t) => t,
136        Err(_) => return,
137    };
138    for line in text.lines() {
139        let line = line.trim();
140        if line.is_empty() {
141            continue;
142        }
143        let (id_str, rest) = match line.split_once('=') {
144            Some(pair) => pair,
145            None => continue,
146        };
147        let id: i32 = match id_str.parse() {
148            Ok(v) => v,
149            Err(_) => continue,
150        };
151        let rest = rest.trim();
152        // Expected format: [trigger,name]
153        if !rest.starts_with('[') || !rest.ends_with(']') {
154            continue;
155        }
156        let inner = &rest[1..rest.len() - 1];
157        let comma = match inner.find(',') {
158            Some(i) => i,
159            None => continue,
160        };
161        let trigger = &inner[..comma];
162        let name = &inner[comma + 1..];
163        if trigger.is_empty() || name.is_empty() {
164            continue;
165        }
166        let key = format!("{}:{}", trigger, name);
167        registry.preloaded_script_ids.insert(key, id);
168    }
169}
170
171/// Generate/update script.pack from .rs2 files, matching 2004scape's regenScriptPack().
172/// Scans all [trigger,name] declarations, preserves existing IDs, assigns new ones.
173pub fn generate_script_pack(scripts_dir: &Path, pack_dir: &Path) {
174    use std::collections::BTreeMap;
175
176    let pack_path = pack_dir.join("script.pack");
177
178    // Load existing entries
179    let mut id_to_name: BTreeMap<i32, String> = BTreeMap::new();
180    let mut name_to_id: std::collections::HashMap<String, i32> = std::collections::HashMap::new();
181    let mut max_id: i32 = 0;
182
183    if let Ok(text) = fs::read_to_string(&pack_path) {
184        for line in text.lines() {
185            let line = line.trim();
186            if line.is_empty() {
187                continue;
188            }
189            if let Some((id_str, name)) = line.split_once('=')
190                && let Ok(id) = id_str.parse::<i32>()
191            {
192                let name = name.trim().to_string();
193                name_to_id.insert(name.clone(), id);
194                id_to_name.insert(id, name);
195                if id >= max_id {
196                    max_id = id + 1;
197                }
198            }
199        }
200    }
201
202    // Scan .rs2 files for [trigger,name] declarations
203    let mut rs2_files = Vec::new();
204    collect_rs2_files(scripts_dir, &mut rs2_files);
205    rs2_files.sort();
206
207    for path in &rs2_files {
208        // Skip engine.rs2 (command signatures, not compiled scripts)
209        if path.file_name().and_then(|n| n.to_str()) == Some("engine.rs2") {
210            continue;
211        }
212        let Ok(text) = fs::read_to_string(path) else {
213            continue;
214        };
215        for line in text.lines() {
216            let line = line.split("//").next().unwrap_or("").trim();
217            if !line.starts_with('[') {
218                continue;
219            }
220            let bracket_end = match line.find(']') {
221                Some(i) => i,
222                None => continue,
223            };
224            let full_name = &line[..bracket_end + 1]; // e.g. "[proc,foo]"
225            let inner = &line[1..bracket_end];
226            let comma = match inner.find(',') {
227                Some(i) => i,
228                None => continue,
229            };
230            let trigger = &inner[..comma];
231            // Skip command declarations
232            if trigger == "command" {
233                continue;
234            }
235
236            if !name_to_id.contains_key(full_name) {
237                name_to_id.insert(full_name.to_string(), max_id);
238                id_to_name.insert(max_id, full_name.to_string());
239                max_id += 1;
240            }
241        }
242    }
243
244    // Save
245    if let Err(e) = std::fs::create_dir_all(pack_dir) {
246        warn!(target: "runec", "Failed to create pack dir: {}", e);
247        return;
248    }
249    let mut out = String::new();
250    for (id, name) in &id_to_name {
251        out.push_str(&format!("{}={}\n", id, name));
252    }
253    if let Err(e) = fs::write(&pack_path, &out) {
254        warn!(target: "runec", "Failed to write script.pack: {}", e);
255    } else {
256        info!(target: "runec", "script.pack: {} entries", id_to_name.len());
257    }
258}
259
260fn collect_rs2_files(dir: &Path, out: &mut Vec<std::path::PathBuf>) {
261    let Ok(entries) = fs::read_dir(dir) else {
262        return;
263    };
264    let mut sorted: Vec<std::path::PathBuf> = entries.flatten().map(|e| e.path()).collect();
265    sorted.sort();
266    for path in sorted {
267        if path.is_dir() {
268            collect_rs2_files(&path, out);
269        } else if path.extension().and_then(|e| e.to_str()) == Some("rs2") {
270            out.push(path);
271        }
272    }
273}
274
275/// Load all .pack files from `pack_dir` into `registry`.
276///
277/// Pack files use 2004scape format: `id=name` (one per line).
278/// This matches how 2004scape's Compiler.ts loads CompilerTypeInfo from pack files.
279pub fn load_packs(registry: &mut SymbolRegistry, pack_dir: &Path) {
280    if !pack_dir.exists() {
281        return;
282    }
283
284    // Register engine constants FIRST (baseline defaults).
285    // Pack files loaded below may overwrite these with content-specific values.
286    register_engine_constants(registry);
287
288    load_commands(registry, &pack_dir.join("command.pack"));
289
290    load_game_vars(registry, &pack_dir.join("varp.pack"), "varp");
291    load_game_vars(registry, &pack_dir.join("varn.pack"), "varn");
292    load_game_vars(registry, &pack_dir.join("vars.pack"), "vars");
293    load_game_vars(registry, &pack_dir.join("varbit.pack"), "varbit");
294
295    // Load entity IDs.
296    // Load in ascending priority order: later entries overwrite earlier ones
297    // when the same name exists in multiple tables.
298    //
299    // Priority (highest wins for plain identifier resolution):
300    //   Low:    UI types (interface, overlayinterface, component, idk),
301    //           abstract types (struct, dbrow, dbtable, dbcolumn, param)
302    //   Medium: audio/animation types (synth, seq, spotanim, mesanim, fontmetrics)
303    //   High:   core game entity types (stat, obj, npc, loc, inv, category, enum, ...)
304    //
305    // e.g. "prayer" is in interface.pack (id=5608) AND stat.pack (id=5);
306    //      stat.pack wins so stat_sub(prayer,...) resolves to 5.
307    // e.g. "stafforb" is in struct.pack (id=45) AND obj.pack (id=567);
308    //      obj.pack wins so inv_add(..., stafforb, ...) resolves to 567.
309    for (file, type_hint) in &[
310        // Lowest priority — category overridden by UI types, synth, and all above
311        ("category.pack", Type::Category),
312        // UI types (override category)
313        ("idk.pack", Type::IdKit),
314        ("interface.pack", Type::Interface), // Combined: interfaces + components
315        ("overlayinterface.pack", Type::OverlayInterface),
316        // Abstract / structural types
317        ("struct.pack", Type::Struct),
318        ("dbrow.pack", Type::DbRow),
319        ("dbtable.pack", Type::DbTable),
320        ("dbcolumn.pack", Type::DbColumn),
321        ("param.pack", Type::Param),
322        // Audio / animation / model types (medium priority)
323        ("model.pack", Type::Model),
324        ("fontmetrics.pack", Type::FontMetrics),
325        ("mesanim.pack", Type::MesAnim),
326        ("jingle.pack", Type::Jingle),
327        ("midi.pack", Type::Midi),
328        ("synth.pack", Type::Synth),
329        ("spotanim.pack", Type::Spotanim),
330        ("seq.pack", Type::Seq),
331        // Core game entity types (highest priority — overwrite everything above)
332        ("locshape.pack", Type::LocShape),
333        ("hunt.pack", Type::Hunt),
334        ("npc_mode.pack", Type::NpcMode),
335        ("enum.pack", Type::Enum),
336        ("writeinv.pack", Type::WriteinvObj),
337        ("inv.pack", Type::Inv),
338        ("loc.pack", Type::Loc),
339        ("npc_stat.pack", Type::NpcStat),
340        ("stat.pack", Type::Stat),
341        ("npc.pack", Type::Npc),
342        ("obj.pack", Type::Obj),
343        ("controller.pack", Type::Controller),
344    ] {
345        load_entity_ids(registry, &pack_dir.join(file), *type_hint);
346    }
347
348    // Register type-name identifiers so they resolve to their CS2 type chars.
349    register_type_chars(registry);
350
351    // Load pre-assigned script IDs LAST so they are available before script registration.
352    load_script_pack(registry, &pack_dir.join("script.pack"));
353
354    // Load clientscript.pack: CS2 scripts the server references by name via
355    // runclientscript(). They are never compiled as RS2, so register them
356    // directly into the resolution maps. Safe no-op if file doesn't exist.
357    load_clientscript_pack(registry, &pack_dir.join("clientscript.pack"));
358}
359
360/// Load clientscript.pack: `id=[clientscript,name]` — registers CS2 script IDs
361/// directly so server-side RS2 can reference them by name
362/// (e.g. `runclientscript(playerdesign4_open_name_entry)` resolves to 3943).
363fn load_clientscript_pack(registry: &mut SymbolRegistry, path: &Path) {
364    let text = match fs::read_to_string(path) {
365        Ok(t) => t,
366        Err(_) => return,
367    };
368    for line in text.lines() {
369        let line = line.trim();
370        if line.is_empty() {
371            continue;
372        }
373        let (id_str, rest) = match line.split_once('=') {
374            Some(pair) => pair,
375            None => continue,
376        };
377        let id: i32 = match id_str.parse() {
378            Ok(v) => v,
379            Err(_) => continue,
380        };
381        let rest = rest.trim();
382        if !rest.starts_with('[') || !rest.ends_with(']') {
383            continue;
384        }
385        let inner = &rest[1..rest.len() - 1];
386        let (_trigger, name) = match inner.split_once(',') {
387            Some(pair) => pair,
388            None => continue,
389        };
390        if name.is_empty() {
391            continue;
392        }
393        registry.script_ids.insert(name.to_string(), id);
394        registry
395            .trigger_script_ids
396            .insert(format!("clientscript:{}", name), id);
397    }
398}
399
400/// Load a game var pack file: `id=name`
401/// Type defaults to Int (matching 2004scape — var types come from config definitions).
402/// Load command.pack: `opcode=name`
403/// Game commands are provided by the engine, not hardcoded in the compiler.
404/// This matches the RuneScriptTS architecture where the consuming engine
405/// passes its `ScriptOpcodeMap` as the `symbols['command']` CompilerTypeInfo.
406fn load_commands(registry: &mut SymbolRegistry, path: &Path) {
407    let text = match fs::read_to_string(path) {
408        Ok(t) => t,
409        Err(_) => return,
410    };
411    for line in text.lines() {
412        let line = line.trim();
413        if line.is_empty() {
414            continue;
415        }
416        let (id_str, name) = match line.split_once('=') {
417            Some(pair) => pair,
418            None => continue,
419        };
420        let opcode: i32 = match id_str.parse() {
421            Ok(v) => v,
422            Err(_) => continue,
423        };
424        let name = name.trim().to_string();
425        if name.is_empty() {
426            continue;
427        }
428        registry.register_command(name, opcode, Vec::new(), Vec::new());
429    }
430}
431
432fn load_game_vars(registry: &mut SymbolRegistry, path: &Path, category: &str) {
433    let text = match fs::read_to_string(path) {
434        Ok(t) => t,
435        Err(_) => return,
436    };
437    for line in text.lines() {
438        let line = line.trim();
439        if line.is_empty() {
440            continue;
441        }
442        let (id_str, name) = match line.split_once('=') {
443            Some(pair) => pair,
444            None => continue,
445        };
446        let idx: i32 = match id_str.parse() {
447            Ok(v) => v,
448            Err(_) => continue,
449        };
450        let name = name.trim().to_string();
451        if name.is_empty() {
452            continue;
453        }
454        // The parser prefixes names with '.' for secondary entity scope
455        // (e.g., .%npc_attacking_uid for varn, .%aggressive_npc for varp on
456        // another player). Register the dot-prefixed version for all categories.
457        registry.register_game_var(format!(".{}", name), Type::Int, idx, category.to_string());
458        registry.register_game_var(name, Type::Int, idx, category.to_string());
459    }
460}
461
462/// Scan `.dbtable` files under `scripts_dir` and register each column as a
463/// compound entity ID `table:column` → packed int, mirroring 2004scape's
464/// runescript lookup.
465///
466/// Packed format: `(tableId << 12) | (colIdx << 4) | tupleNibble`
467/// where `tupleNibble` is 0 for the whole tuple and `N+1` for slot N.
468///
469/// Also populates `registry.dbcolumn_types` so `db_find(music:playbutton, ...)`
470/// emits the correct BaseVarType ordinal.
471///
472/// `dbtable_ids` is the pre-loaded name→id mapping from `dbtable.pack` — the
473/// same IDs the runtime uses when indexing rows.
474pub fn load_dbtable_configs(
475    registry: &mut SymbolRegistry,
476    scripts_dir: &Path,
477    dbtable_ids: &std::collections::HashMap<String, u16>,
478) {
479    let mut dbtable_files: Vec<std::path::PathBuf> = Vec::new();
480    collect_files_by_ext(scripts_dir, "dbtable", &mut dbtable_files);
481    dbtable_files.sort();
482
483    for path in &dbtable_files {
484        let Ok(text) = fs::read_to_string(path) else {
485            continue;
486        };
487        let lines = strip_comments(&text);
488
489        let mut current_table: Option<String> = None;
490        let mut col_idx: u16 = 0;
491        for line in &lines {
492            // Section header: [tablename]
493            if line.starts_with('[') && line.ends_with(']') {
494                let name = line[1..line.len() - 1].trim().to_string();
495                current_table = Some(name);
496                col_idx = 0;
497                continue;
498            }
499            // column=name,type1[,type2...][,INDEXED][,REQUIRED]
500            let Some(rest) = line.strip_prefix("column=") else {
501                continue;
502            };
503            let Some(table_name) = current_table.as_deref() else {
504                continue;
505            };
506            let Some(&table_id) = dbtable_ids.get(table_name) else {
507                continue;
508            };
509
510            let parts: Vec<&str> = rest.split(',').map(str::trim).collect();
511            if parts.is_empty() {
512                continue;
513            }
514            let col_name = parts[0];
515            if col_name.is_empty() {
516                continue;
517            }
518
519            // Collect types, skipping INDEXED/REQUIRED flags.
520            let types: Vec<Type> = parts[1..]
521                .iter()
522                .filter(|s| !matches!(**s, "INDEXED" | "REQUIRED"))
523                .filter_map(|s| Type::from_name(s))
524                .collect();
525
526            let compound = format!("{}:{}", table_name, col_name);
527            let packed = ((table_id as i32) << 12) | ((col_idx as i32) << 4);
528
529            // Register whole-tuple form
530            registry.register_entity_id(compound.clone(), Type::DbColumn, packed);
531
532            // Register per-slot tuple indexes for multi-field columns
533            if types.len() > 1 {
534                for (i, _) in types.iter().enumerate() {
535                    let slot_compound = format!("{}:{}:{}", table_name, col_name, i);
536                    let slot_packed = packed | (((i as i32) + 1) & 0xF);
537                    registry.register_entity_id(slot_compound, Type::DbColumn, slot_packed);
538                }
539            }
540
541            // First-field type drives db_find's BaseVarType ordinal.
542            if let Some(first_type) = types.first().copied() {
543                registry.dbcolumn_types.insert(compound, first_type);
544            }
545
546            col_idx += 1;
547        }
548    }
549}
550
551/// Load dbtable.pack as a `name → id` map. Used by `load_dbtable_configs`.
552pub fn load_dbtable_pack(path: &Path) -> std::collections::HashMap<String, u16> {
553    let mut out = std::collections::HashMap::new();
554    let Ok(text) = fs::read_to_string(path) else {
555        return out;
556    };
557    for line in text.lines() {
558        let line = line.trim();
559        if line.is_empty() {
560            continue;
561        }
562        let Some((id_str, name)) = line.split_once('=') else {
563            continue;
564        };
565        let Ok(id) = id_str.parse::<u16>() else {
566            continue;
567        };
568        let name = name.trim();
569        if name.is_empty() {
570            continue;
571        }
572        out.insert(name.to_string(), id);
573    }
574    out
575}
576
577/// Load all `.constant` files from the scripts directory, matching 2004scape's
578/// `loadDirExtFull(scripts_dir, '.constant', ...)` behavior.
579///
580/// Format: `^name = value` per line, with `//` and `/* */` comment stripping.
581pub fn load_constant_files(registry: &mut SymbolRegistry, scripts_dir: &Path) {
582    let mut constant_files: Vec<std::path::PathBuf> = Vec::new();
583    collect_files_by_ext(scripts_dir, "constant", &mut constant_files);
584    constant_files.sort();
585
586    for path in &constant_files {
587        let text = match fs::read_to_string(path) {
588            Ok(t) => t,
589            Err(_) => continue,
590        };
591        let lines = strip_comments(&text);
592        for line in &lines {
593            // Split on '=' into name and value
594            let eq_pos = match line.find('=') {
595                Some(i) => i,
596                None => continue,
597            };
598            let mut name = line[..eq_pos].trim();
599            let value = line[eq_pos + 1..].trim();
600
601            // Strip leading ^ from name
602            if let Some(stripped) = name.strip_prefix('^') {
603                name = stripped;
604            }
605
606            if name.is_empty() {
607                continue;
608            }
609
610            // Strip surrounding quotes from string values
611            let val_str = if value.starts_with('"') && value.ends_with('"') && value.len() >= 2 {
612                &value[1..value.len() - 1]
613            } else {
614                value
615            };
616
617            register_constant_value(registry, name.to_string(), val_str);
618        }
619    }
620}
621
622/// Register a constant value, auto-detecting its type (int, hex, coord, or string).
623fn register_constant_value(registry: &mut SymbolRegistry, name: String, val_str: &str) {
624    if let Ok(i) = val_str.parse::<i32>() {
625        registry.register_constant(name, Type::Int, Some(i), None);
626    } else if let Some(hex_str) = val_str
627        .strip_prefix("0x")
628        .or_else(|| val_str.strip_prefix("0X"))
629    {
630        let v = u32::from_str_radix(hex_str, 16).unwrap_or(0) as i32;
631        registry.register_constant(name, Type::Int, Some(v), None);
632    } else if let Some(packed) = parse_coord_string(val_str) {
633        registry.register_constant(name, Type::Coord, Some(packed), None);
634    } else {
635        registry.register_constant(name, Type::String, None, Some(val_str.to_string()));
636    }
637}
638
639/// Strip `//` single-line and `/* */` multi-line comments from source text,
640/// matching 2004scape's `loadFileFull()` behavior.
641fn strip_comments(text: &str) -> Vec<String> {
642    let mut result = Vec::new();
643    let mut multi_depth: usize = 0;
644
645    for line in text.lines() {
646        let mut line = line.trim().to_string();
647
648        // Handle continuation of multi-line comments
649        if multi_depth > 0 {
650            while let Some(start) = line.find("/*") {
651                line = line[start + 2..].trim_start().to_string();
652                multi_depth += 1;
653            }
654            while let Some(end) = line.find("*/") {
655                line = line[end + 2..].trim_start().to_string();
656                multi_depth -= 1;
657                if multi_depth == 0 {
658                    break;
659                }
660            }
661            if multi_depth > 0 {
662                continue;
663            }
664        }
665
666        if line.is_empty() {
667            continue;
668        }
669
670        // Strip single-line comments
671        if let Some(idx) = line.find("//") {
672            line = line[..idx].trim_end().to_string();
673            if line.is_empty() {
674                continue;
675            }
676        }
677
678        // Strip inline multi-line comments
679        if let Some(start) = line.find("/*") {
680            if let Some(end) = line.find("*/") {
681                line = format!("{}{}", &line[..start], &line[end + 2..]);
682            } else {
683                line = line[..start].to_string();
684                multi_depth += 1;
685            }
686            if line.is_empty() {
687                continue;
688            }
689        }
690
691        result.push(line);
692    }
693
694    result
695}
696
697/// Scan `.varp`/`.varn`/`.vars`/`.varbit` configs and refine the type
698/// of each matching game-var. `load_game_vars` defaults everything to
699/// `Type::Int`; the real type comes from the `type=<typename>` line
700/// in the config block.
701pub fn load_game_var_types(registry: &mut SymbolRegistry, scripts_dir: &Path) {
702    // Track the next auto-assigned ID per category for vars only found in config files.
703    let mut next_auto_id: std::collections::HashMap<String, i32> = std::collections::HashMap::new();
704    for ext in &["varp", "varn", "vars", "varbit"] {
705        // Compute the next available ID (one past the highest existing).
706        let max_id = registry
707            .game_vars
708            .values()
709            .filter_map(|sym| {
710                if let crate::symbol::SymbolKind::GameVar {
711                    var_id, category, ..
712                } = &sym.kind
713                {
714                    if category == *ext {
715                        Some(*var_id)
716                    } else {
717                        None
718                    }
719                } else {
720                    None
721                }
722            })
723            .max()
724            .unwrap_or(-1);
725        next_auto_id.insert(ext.to_string(), max_id + 1);
726    }
727
728    for ext in &["varp", "varn", "vars", "varbit"] {
729        let mut files = Vec::new();
730        collect_files_by_ext(scripts_dir, ext, &mut files);
731        for path in &files {
732            let Ok(text) = fs::read_to_string(path) else {
733                continue;
734            };
735            let mut current_name: Option<String> = None;
736            for line in text.lines() {
737                let line = line.trim();
738                if line.is_empty() || line.starts_with("//") {
739                    continue;
740                }
741                if let Some(inner) = line.strip_prefix('[').and_then(|s| s.strip_suffix(']')) {
742                    let name = inner.trim().to_string();
743                    // Register the var if it doesn't exist yet (config-only var)
744                    if !registry.game_vars.contains_key(&name) {
745                        let id = next_auto_id.get_mut(*ext).unwrap();
746                        let auto_id = *id;
747                        *id += 1;
748                        registry.register_game_var(
749                            name.clone(),
750                            Type::Int,
751                            auto_id,
752                            ext.to_string(),
753                        );
754                        registry.register_game_var(
755                            format!(".{}", name),
756                            Type::Int,
757                            auto_id,
758                            ext.to_string(),
759                        );
760                    }
761                    current_name = Some(name);
762                    continue;
763                }
764                let Some(name) = current_name.as_ref() else {
765                    continue;
766                };
767                if let Some((k, v)) = line.split_once('=')
768                    && k.trim() == "type"
769                {
770                    let type_name = v.trim();
771                    if let Some(ty) = Type::from_name(type_name) {
772                        if let Some(sym) = registry.game_vars.get_mut(name)
773                            && let crate::symbol::SymbolKind::GameVar { var_type, .. } =
774                                &mut sym.kind
775                        {
776                            *var_type = ty;
777                        }
778                        let dot_name = format!(".{}", name);
779                        if let Some(sym) = registry.game_vars.get_mut(&dot_name)
780                            && let crate::symbol::SymbolKind::GameVar { var_type, .. } =
781                                &mut sym.kind
782                        {
783                            *var_type = ty;
784                        }
785                    }
786                }
787            }
788        }
789    }
790}
791
792/// Recursively collect files with a given extension.
793fn collect_files_by_ext(dir: &Path, ext: &str, out: &mut Vec<std::path::PathBuf>) {
794    let entries = match fs::read_dir(dir) {
795        Ok(e) => e,
796        Err(_) => return,
797    };
798    for entry in entries.flatten() {
799        let path = entry.path();
800        if path.is_dir() {
801            collect_files_by_ext(&path, ext, out);
802        } else if path.extension().and_then(|e| e.to_str()) == Some(ext) {
803            out.push(path);
804        }
805    }
806}
807
808/// Parse a coord string in `z_bx_bz_lx_lz` format and return the packed integer.
809/// Packing: x = bx*64 + lx, z = bz*64 + lz, packed = (level << 28) | (x << 14) | z
810fn parse_coord_string(s: &str) -> Option<i32> {
811    let parts: Vec<&str> = s.split('_').collect();
812    if parts.len() != 5 {
813        return None;
814    }
815    let y: i32 = parts[0].parse().ok()?;
816    let bx: i32 = parts[1].parse().ok()?;
817    let bz: i32 = parts[2].parse().ok()?;
818    let lx: i32 = parts[3].parse().ok()?;
819    let lz: i32 = parts[4].parse().ok()?;
820    let x = bx * 64 + lx;
821    let z = bz * 64 + lz;
822    Some((y << 28) | (x << 14) | z)
823}
824
825/// Load an entity pack file. Two `interface.pack` dialects are accepted:
826///
827/// 1. **if3** (rev 647 / 2004scape): key encodes the IDs, value is the short name.
828///    - `<iface_id>=<iface_name>`            → root interface
829///    - `<iface_id>:<comp_id>=<comp_name>`   → component, packed idx = (iface<<16)|comp
830///
831/// 2. **if1** (rev 225 era): key is a flat global index, value carries the full path.
832///    - `<flat_id>=<iface_name>`             → root interface
833///    - `<flat_id>=<iface_name>:<comp_name>` → component, idx = flat_id
834///
835/// Detection is per-line, so a pack written entirely in either dialect parses cleanly,
836/// and neither produces a misleading "subject did not resolve" for entries that are
837/// actually present.
838fn load_entity_ids(registry: &mut SymbolRegistry, path: &Path, entity_type: Type) {
839    let text = match fs::read_to_string(path) {
840        Ok(t) => t,
841        Err(_) => return,
842    };
843
844    let is_interface_pack = entity_type == Type::Interface;
845
846    // First pass: collect interface name→ID for component resolution.
847    // Both dialects agree on the root form: `<id>=<name>` with no `:` on either side.
848    let mut iface_id_to_name: std::collections::HashMap<i32, String> =
849        std::collections::HashMap::new();
850
851    for line in text.lines() {
852        let line = line.trim();
853        if line.is_empty() {
854            continue;
855        }
856        let (id_str, name) = match line.split_once('=') {
857            Some(pair) => pair,
858            None => continue,
859        };
860        let name = name.trim();
861        if name.is_empty() || name == "null" {
862            continue;
863        }
864
865        if is_interface_pack
866            && !id_str.contains(':')
867            && !name.contains(':')
868            && let Ok(id) = id_str.parse::<i32>()
869        {
870            registry.interface_ids.insert(name.to_string(), id);
871            iface_id_to_name.insert(id, name.to_string());
872        }
873    }
874
875    // Second pass: register entities + components
876    for line in text.lines() {
877        let line = line.trim();
878        if line.is_empty() {
879            continue;
880        }
881        let (id_str, name) = match line.split_once('=') {
882            Some(pair) => pair,
883            None => continue,
884        };
885        let name = name.trim();
886        if name.is_empty() || name == "null" {
887            continue;
888        }
889
890        // Decide shape: native component (LHS has `:`), Jordan component
891        // (RHS has `:` on an interface pack), or plain root.
892        let (idx, component_key, register_name): (i32, Option<String>, String) =
893            if let Some((iface_str, comp_str)) = id_str.split_once(':') {
894                // Native component: `<iface_id>:<comp_id>=<comp_name>`
895                let iface: i32 = match iface_str.parse() {
896                    Ok(v) => v,
897                    Err(_) => continue,
898                };
899                let comp: i32 = match comp_str.parse() {
900                    Ok(v) => v,
901                    Err(_) => continue,
902                };
903                let packed = (iface << 16) | comp;
904                let key = iface_id_to_name
905                    .get(&iface)
906                    .map(|iface_name| format!("{}:{}", iface_name, name));
907                (packed, key, name.to_string())
908            } else if is_interface_pack {
909                let flat_id: i32 = match id_str.parse() {
910                    Ok(v) => v,
911                    Err(_) => continue,
912                };
913                if name.contains(':') {
914                    // Jordan component: `<flat_id>=<iface_name>:<comp_name>`
915                    (flat_id, Some(name.to_string()), name.to_string())
916                } else {
917                    // Root interface in either dialect
918                    (flat_id, None, name.to_string())
919                }
920            } else {
921                // Non-interface pack
922                let id: i32 = match id_str.parse() {
923                    Ok(v) => v,
924                    Err(_) => continue,
925                };
926                (id, None, name.to_string())
927            };
928
929        // In combined interface.pack: component entries register as Type::Component,
930        // interface roots register as Type::Interface.
931        let actual_type = if is_interface_pack && component_key.is_some() {
932            Type::Component
933        } else {
934            entity_type
935        };
936
937        // Normalize name matching TS SymbolTable.normalizeName for Basic symbols:
938        // name.toLowerCase().replace(/\s+/g, '_')
939        let normalized = register_name.to_lowercase().replace(' ', "_");
940        registry.register_entity_id(normalized, actual_type, idx);
941
942        if is_interface_pack && let Some(key) = component_key {
943            let normalized_key = if let Some((iface_part, comp_part)) = key.split_once(':') {
944                format!(
945                    "{}:{}",
946                    iface_part,
947                    crate::symbol::normalize_comp_name(comp_part)
948                )
949            } else {
950                key
951            };
952            registry.components.insert(normalized_key, idx);
953        }
954    }
955}
956
957/// Register type-name identifiers as type-char constants.
958/// These are stored in a separate map (registry.type_chars) checked LAST in identifier
959/// resolution, so commands like `coord` take priority over the type name `coord`.
960fn register_type_chars(registry: &mut SymbolRegistry) {
961    let type_chars: &[(&str, i32)] = &[
962        ("int", b'i' as i32),     // 105
963        ("string", b's' as i32),  // 115
964        ("boolean", b'1' as i32), // 49
965        ("bool", b'1' as i32),
966        ("coord", b'c' as i32),     // 99
967        ("loc", b'l' as i32),       // 108
968        ("npc", b'n' as i32),       // 110
969        ("obj", b'o' as i32),       // 111
970        ("namedobj", b'O' as i32),  // 79
971        ("playeruid", b'p' as i32), // 112
972        ("player_uid", b'p' as i32),
973        ("npcuid", b'u' as i32), // 117
974        ("npc_uid", b'u' as i32),
975        ("seq", b'A' as i32),       // 65
976        ("spotanim", b't' as i32),  // 116
977        ("synth", b'P' as i32),     // 80
978        ("stat", b'S' as i32),      // 83
979        ("component", b'I' as i32), // 73
980        ("interface", b'a' as i32), // 97
981        ("toplevelinterface", b'a' as i32),
982        ("overlayinterface", b'a' as i32),
983        ("inv", b'v' as i32),     // 118
984        ("enum", b'g' as i32),    // 103
985        ("struct", b'J' as i32),  // 74
986        ("dbrow", 0xD0),          // 208
987        ("dbtable", b'i' as i32), // treat as int
988        ("dbcolumn", b'i' as i32),
989        ("category", b'y' as i32), // 121
990        ("varp", b'V' as i32),     // 86
991        ("npc_stat", 0xFE),        // 254
992        ("npcstat", 0xFE),
993        ("idkit", b'K' as i32), // 75
994        ("idk", b'K' as i32),
995    ];
996    for (name, val) in type_chars {
997        registry.type_chars.insert(name.to_string(), *val);
998    }
999}
1000
1001/// Register engine-provided constants that the TS compiler passes via
1002/// CompilerTypeInfo objects built from engine enums/maps. These are
1003/// identical across all supported revisions (225-274+).
1004fn register_engine_constants(registry: &mut SymbolRegistry) {
1005    // NpcMode (from NpcModeMap in engine/src/engine/entity/NpcMode.ts)
1006    let npc_modes: &[(&str, i32)] = &[
1007        ("null", -1),
1008        ("none", 0),
1009        ("wander", 1),
1010        ("patrol", 2),
1011        ("playerescape", 3),
1012        ("playerfollow", 4),
1013        ("playerface", 5),
1014        ("playerfaceclose", 6),
1015        ("opplayer1", 7),
1016        ("opplayer2", 8),
1017        ("opplayer3", 9),
1018        ("opplayer4", 10),
1019        ("opplayer5", 11),
1020        ("applayer1", 12),
1021        ("applayer2", 13),
1022        ("applayer3", 14),
1023        ("applayer4", 15),
1024        ("applayer5", 16),
1025        ("oploc1", 17),
1026        ("oploc2", 18),
1027        ("oploc3", 19),
1028        ("oploc4", 20),
1029        ("oploc5", 21),
1030        ("aploc1", 22),
1031        ("aploc2", 23),
1032        ("aploc3", 24),
1033        ("aploc4", 25),
1034        ("aploc5", 26),
1035        ("opobj1", 27),
1036        ("opobj2", 28),
1037        ("opobj3", 29),
1038        ("opobj4", 30),
1039        ("opobj5", 31),
1040        ("apobj1", 32),
1041        ("apobj2", 33),
1042        ("apobj3", 34),
1043        ("apobj4", 35),
1044        ("apobj5", 36),
1045        ("opnpc1", 37),
1046        ("opnpc2", 38),
1047        ("opnpc3", 39),
1048        ("opnpc4", 40),
1049        ("opnpc5", 41),
1050        ("apnpc1", 42),
1051        ("apnpc2", 43),
1052        ("apnpc3", 44),
1053        ("apnpc4", 45),
1054        ("apnpc5", 46),
1055        ("queue1", 47),
1056        ("queue2", 48),
1057        ("queue3", 49),
1058        ("queue4", 50),
1059        ("queue5", 51),
1060        ("queue6", 52),
1061        ("queue7", 53),
1062        ("queue8", 54),
1063        ("queue9", 55),
1064        ("queue10", 56),
1065        ("queue11", 57),
1066        ("queue12", 58),
1067        ("queue13", 59),
1068        ("queue14", 60),
1069        ("queue15", 61),
1070        ("queue16", 62),
1071        ("queue17", 63),
1072        ("queue18", 64),
1073        ("queue19", 65),
1074        ("queue20", 66),
1075    ];
1076    for &(name, id) in npc_modes {
1077        registry.register_entity_id(name.to_string(), Type::NpcMode, id);
1078    }
1079
1080    // NpcStat FIRST (lower priority — overlapping names will be overwritten by PlayerStat)
1081    let npc_stats: &[(&str, i32)] = &[
1082        ("attack", 0),
1083        ("defence", 1),
1084        ("strength", 2),
1085        ("hitpoints", 3),
1086        ("ranged", 4),
1087        ("magic", 5),
1088    ];
1089    for &(name, id) in npc_stats {
1090        registry.register_entity_id(name.to_string(), Type::NpcStat, id);
1091    }
1092
1093    // PlayerStat SECOND (higher priority — "magic"=6 overwrites npc_stat "magic"=5
1094    // in the flat entity_ids map, matching load_packs ordering)
1095    let stats: &[(&str, i32)] = &[
1096        ("attack", 0),
1097        ("defence", 1),
1098        ("strength", 2),
1099        ("hitpoints", 3),
1100        ("ranged", 4),
1101        ("prayer", 5),
1102        ("magic", 6),
1103        ("cooking", 7),
1104        ("woodcutting", 8),
1105        ("fletching", 9),
1106        ("fishing", 10),
1107        ("firemaking", 11),
1108        ("crafting", 12),
1109        ("smithing", 13),
1110        ("mining", 14),
1111        ("herblore", 15),
1112        ("agility", 16),
1113        ("thieving", 17),
1114        ("slayer", 18),
1115        ("stat18", 18),
1116        ("farming", 19),
1117        ("stat19", 19),
1118        ("runecraft", 20),
1119        ("hunter", 21),
1120        ("construction", 22),
1121        ("summoning", 23),
1122        ("dungeoneering", 24),
1123    ];
1124    for &(name, id) in stats {
1125        registry.register_entity_id(name.to_string(), Type::Stat, id);
1126    }
1127
1128    // Fontmetrics (both naming conventions: 225 uses short, 274 uses _full suffix)
1129    let fontmetrics: &[(&str, i32)] = &[
1130        ("p11", 0),
1131        ("p12", 1),
1132        ("b12", 2),
1133        ("q8", 3),
1134        ("p11_full", 0),
1135        ("p12_full", 1),
1136        ("b12_full", 2),
1137        ("q8_full", 3),
1138    ];
1139    for &(name, id) in fontmetrics {
1140        registry.register_entity_id(name.to_string(), Type::FontMetrics, id);
1141    }
1142
1143    // LocShape (from locshapeInfo in engine/tools/pack/Compiler.ts)
1144    let locshapes: &[(&str, i32)] = &[
1145        ("wall_straight", 0),
1146        ("wall_diagonalcorner", 1),
1147        ("wall_l", 2),
1148        ("wall_squarecorner", 3),
1149        ("walldecor_straight_nooffset", 4),
1150        ("walldecor_straight_offset", 5),
1151        ("walldecor_diagonal_offset", 6),
1152        ("walldecor_diagonal_nooffset", 7),
1153        ("walldecor_diagonal_both", 8),
1154        ("wall_diagonal", 9),
1155        ("centrepiece_straight", 10),
1156        ("centrepiece_diagonal", 11),
1157        ("roof_straight", 12),
1158        ("roof_diagonal_with_roofedge", 13),
1159        ("roof_diagonal", 14),
1160        ("roof_l_concave", 15),
1161        ("roof_l_convex", 16),
1162        ("roof_flat", 17),
1163        ("roofedge_straight", 18),
1164        ("roofedge_diagonalcorner", 19),
1165        ("roofedge_l", 20),
1166        ("roofedge_squarecorner", 21),
1167        ("grounddecor", 22),
1168    ];
1169    for &(name, id) in locshapes {
1170        registry.register_entity_id(name.to_string(), Type::LocShape, id);
1171    }
1172}
1173
1174#[cfg(test)]
1175mod tests {
1176    use super::*;
1177    use crate::symbol::SymbolRegistry;
1178    use crate::types::Type;
1179    use std::io::Write;
1180    use std::path::PathBuf;
1181
1182    fn write_temp_pack(name: &str, content: &str) -> PathBuf {
1183        let dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
1184            .join("target")
1185            .join("test-symloader");
1186        fs::create_dir_all(&dir).unwrap();
1187        let path = dir.join(name);
1188        let mut f = fs::File::create(&path).unwrap();
1189        f.write_all(content.as_bytes()).unwrap();
1190        path
1191    }
1192
1193    // ── 647-style pack ─────────────────────────────────────────────────
1194    // Format: `<iface_id>=<name>` for roots, `<iface_id>:<comp_id>=<comp_name>`
1195
1196    #[test]
1197    fn load_647_style_registers_components() {
1198        let pack = write_temp_pack(
1199            "interface_647.pack",
1200            "\
1201228=multi2\n\
1202228:0=com0\n\
1203228:1=com1\n\
1204228:2=com2\n\
1205228:3=com3\n\
1206228:6=thin_swords\n\
1207228:9=wide_swords\n",
1208        );
1209        let mut r = SymbolRegistry::new();
1210        load_entity_ids(&mut r, &pack, Type::Interface);
1211
1212        // Root interface registered
1213        assert_eq!(r.interface_ids.get("multi2"), Some(&228));
1214
1215        // Components use normalized keys (com0, not com_0)
1216        assert_eq!(r.lookup_component("multi2", "com0"), Some(228 << 16));
1217        assert_eq!(r.lookup_component("multi2", "com2"), Some((228 << 16) | 2));
1218        assert_eq!(r.lookup_component("multi2", "com3"), Some((228 << 16) | 3));
1219
1220        // Named components (no digits) are unaffected by normalization
1221        assert_eq!(
1222            r.lookup_component("multi2", "thin_swords"),
1223            Some((228 << 16) | 6)
1224        );
1225        assert_eq!(
1226            r.lookup_component("multi2", "wide_swords"),
1227            Some((228 << 16) | 9)
1228        );
1229    }
1230
1231    #[test]
1232    fn load_647_style_resolves_225_references() {
1233        let pack = write_temp_pack(
1234            "interface_647_cross.pack",
1235            "\
1236228=multi2\n\
1237228:0=com0\n\
1238228:1=com1\n\
1239228:2=com2\n",
1240        );
1241        let mut r = SymbolRegistry::new();
1242        load_entity_ids(&mut r, &pack, Type::Interface);
1243
1244        // 225-style references (com_0, com_1, com_2) resolve against 647 pack
1245        assert_eq!(r.lookup_component("multi2", "com_0"), Some(228 << 16));
1246        assert_eq!(r.lookup_component("multi2", "com_1"), Some((228 << 16) | 1));
1247        assert_eq!(r.lookup_component("multi2", "com_2"), Some((228 << 16) | 2));
1248    }
1249
1250    // ── 225-style pack ─────────────────────────────────────────────────
1251    // Format: `<flat_id>=<iface_name>` for roots, `<flat_id>=<iface_name>:<comp_name>`
1252
1253    #[test]
1254    fn load_225_style_registers_components() {
1255        let pack = write_temp_pack(
1256            "interface_225.pack",
1257            "\
12582459=multi2\n\
12592460=multi2:com_0\n\
12602461=multi2:com_1\n\
12612462=multi2:com_2\n\
12622463=multi2:com_4\n\
12632464=multi2:com_5\n",
1264        );
1265        let mut r = SymbolRegistry::new();
1266        load_entity_ids(&mut r, &pack, Type::Interface);
1267
1268        // Root interface registered
1269        assert_eq!(r.interface_ids.get("multi2"), Some(&2459));
1270
1271        // 225 references resolve (com_0 normalized to com0 at registration)
1272        assert_eq!(r.lookup_component("multi2", "com_0"), Some(2460));
1273        assert_eq!(r.lookup_component("multi2", "com_2"), Some(2462));
1274        assert_eq!(r.lookup_component("multi2", "com_5"), Some(2464));
1275    }
1276
1277    #[test]
1278    fn load_225_style_resolves_647_references() {
1279        let pack = write_temp_pack(
1280            "interface_225_cross.pack",
1281            "\
12822459=multi2\n\
12832460=multi2:com_0\n\
12842461=multi2:com_1\n\
12852462=multi2:com_2\n",
1286        );
1287        let mut r = SymbolRegistry::new();
1288        load_entity_ids(&mut r, &pack, Type::Interface);
1289
1290        // 647-style references (com0, com1, com2) resolve against 225 pack
1291        assert_eq!(r.lookup_component("multi2", "com0"), Some(2460));
1292        assert_eq!(r.lookup_component("multi2", "com1"), Some(2461));
1293        assert_eq!(r.lookup_component("multi2", "com2"), Some(2462));
1294    }
1295
1296    // ── Entity type correctness ────────────────────────────────────────
1297
1298    #[test]
1299    fn components_register_as_component_type() {
1300        let pack = write_temp_pack(
1301            "interface_types.pack",
1302            "\
1303228=multi2\n\
1304228:0=com0\n",
1305        );
1306        let mut r = SymbolRegistry::new();
1307        load_entity_ids(&mut r, &pack, Type::Interface);
1308
1309        // Root → Interface type, component → Component type
1310        assert!(
1311            r.lookup_entity_id_typed("multi2", Type::Interface)
1312                .is_some()
1313        );
1314        assert!(r.lookup_entity_id_typed("com0", Type::Component).is_some());
1315        assert!(r.lookup_entity_id_typed("com0", Type::Interface).is_none());
1316    }
1317}