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
/// Base variable types in the RS2 type system.
/// Every ScriptVarType maps to one of these for stack operations.
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub enum BaseVarType {
    Integer,
    String,
    Long,
}

/// All script variable types supported by RuneScript.
/// These correspond to the types used in the 2004Scape/RS2 engine.
#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub enum Type {
    // Primitive types
    Int,
    Boolean,
    String,
    Long,
    Char,
    Colour,
    Coord,

    // Game entity types
    Loc,
    Npc,
    Obj,
    NamedObj,
    PlayerUid,
    NpcUid,

    // Visual/audio types
    Seq,
    Spotanim,
    Synth,
    Vorbis,
    Midi,
    Jingle,
    Graphic,
    FontMetrics,
    ChatCat,
    ChatPhrase,
    Model,
    Texture,
    IdKit,
    MesAnim,
    Hitmark,

    // UI types
    Stat,
    Component,
    IfType,
    SetPosH,
    SetPosV,
    SetSize,
    SetTextAlignH,
    SetTextAlignV,
    Interface,
    TopLevelInterface,
    OverlayInterface,
    Inv,

    // Data types
    Enum,
    Struct,
    Param,
    DbTable,
    DbRow,
    DbColumn,
    Category,

    // Variable types
    Varp,
    Varn,
    Vars,
    Varbit,

    // Map types
    MapZone,
    MapArea,
    MapElement,

    // Misc types
    LocShape,
    NpcMode,
    NpcStat,
    Area,
    Hunt,
    MoveSpeed,
    EntityOverlay,
    StringVector,
    WriteinvObj,

    // Controller type (instanced area manager)
    Controller,
    RegionCoord,

    // Script reference types (for parameters that hold script IDs)
    Proc,
    ClientScript,
    Label,
    Queue,
    SoftTimer,
    Timer,
    Walktrigger,

    // Meta types (used internally by the compiler)
    Any,
    Error,
    Void,
}

impl Type {
    pub fn to_char(self) -> char {
        "is§1clnoOpNAtPMjdmxKSIavgJyVÐþ`µzf«ke"
            .chars()
            .find(|&ch| Self::from_char(ch) == Some(self))
            .unwrap_or('i')
    }

    pub fn from_char(ch: char) -> Option<Self> {
        Some(match ch {
            'i' => Self::Int,
            's' => Self::String,
            '§' => Self::Long,
            'z' => Self::Char,
            'f' => Self::FontMetrics,
            '«' => Self::Vorbis,
            'k' => Self::ChatCat,
            'e' => Self::ChatPhrase,
            '1' => Self::Boolean,
            'c' => Self::Coord,
            'l' => Self::Loc,
            'n' => Self::Npc,
            'o' => Self::Obj,
            'O' => Self::NamedObj,
            'p' => Self::PlayerUid,
            'N' => Self::NpcUid,
            'A' => Self::Seq,
            't' => Self::Spotanim,
            'P' => Self::Synth,
            'M' => Self::Midi,
            'j' => Self::Jingle,
            'd' => Self::Graphic,
            'm' => Self::Model,
            'x' => Self::Texture,
            'K' => Self::IdKit,
            'S' => Self::Stat,
            'I' => Self::Component,
            'a' => Self::Interface,
            'v' => Self::Inv,
            'g' => Self::Enum,
            'J' => Self::Struct,
            'y' => Self::Category,
            'V' => Self::Varp,
            'Ð' => Self::DbRow,
            'þ' => Self::NpcStat,
            '`' => Self::MapArea,
            'µ' => Self::MapElement,
            _ => return None,
        })
    }

    /// Returns the base var type (determines which stack to use).
    pub fn base_type(&self) -> BaseVarType {
        match self {
            Type::String => BaseVarType::String,
            Type::Long => BaseVarType::Long,
            _ => BaseVarType::Integer,
        }
    }

    /// Default value for uninitialised local variable declarations.
    /// Boolean and Int default to 0; all other int-based types default to -1 (null).
    pub fn default_int_value(&self) -> i32 {
        match self {
            Type::Int
            | Type::Boolean
            | Type::IfType
            | Type::SetPosH
            | Type::SetPosV
            | Type::SetSize
            | Type::SetTextAlignH
            | Type::SetTextAlignV => 0,
            _ => -1,
        }
    }

    /// Default value for implicit return values at end of scripts.
    /// Only Int pushes 0; everything else (including Boolean) pushes -1.
    pub fn default_return_value(&self) -> i32 {
        match self {
            Type::Int
            | Type::IfType
            | Type::SetPosH
            | Type::SetPosV
            | Type::SetSize
            | Type::SetTextAlignH
            | Type::SetTextAlignV => 0,
            _ => -1,
        }
    }

    /// Parse a type name from a def_ prefix (e.g., "def_int" -> Int).
    pub fn from_def_str(s: &str) -> Option<Type> {
        let type_name = s.strip_prefix("def_")?;
        Self::from_name(type_name)
    }

    /// Parse a type from its bare name (e.g., "int" -> Int).
    pub fn from_name(s: &str) -> Option<Type> {
        match s {
            "int" => Some(Type::Int),
            "boolean" | "bool" => Some(Type::Boolean),
            "string" => Some(Type::String),
            "long" => Some(Type::Long),
            "char" => Some(Type::Char),
            "colour" | "color" | "rgb" => Some(Type::Colour),
            "iftype" => Some(Type::IfType),
            "setposh" => Some(Type::SetPosH),
            "setposv" => Some(Type::SetPosV),
            "setsize" => Some(Type::SetSize),
            "settextalignh" => Some(Type::SetTextAlignH),
            "settextalignv" => Some(Type::SetTextAlignV),
            "coord" => Some(Type::Coord),
            "loc" => Some(Type::Loc),
            "npc" => Some(Type::Npc),
            "obj" => Some(Type::Obj),
            "namedobj" => Some(Type::NamedObj),
            "playeruid" | "player_uid" => Some(Type::PlayerUid),
            "npcuid" | "npc_uid" => Some(Type::NpcUid),
            "seq" => Some(Type::Seq),
            "spotanim" => Some(Type::Spotanim),
            "synth" => Some(Type::Synth),
            "vorbis" => Some(Type::Vorbis),
            "midi" => Some(Type::Midi),
            "jingle" => Some(Type::Jingle),
            "graphic" => Some(Type::Graphic),
            "fontmetrics" => Some(Type::FontMetrics),
            "chatcat" => Some(Type::ChatCat),
            "chatphrase" => Some(Type::ChatPhrase),
            "model" => Some(Type::Model),
            "texture" => Some(Type::Texture),
            "idk" | "idkit" => Some(Type::IdKit),
            "mesanim" => Some(Type::MesAnim),
            "hitmark" => Some(Type::Hitmark),
            "stat" => Some(Type::Stat),
            "component" => Some(Type::Component),
            "interface" => Some(Type::Interface),
            "toplevelinterface" => Some(Type::TopLevelInterface),
            "overlayinterface" => Some(Type::OverlayInterface),
            "inv" => Some(Type::Inv),
            "enum" => Some(Type::Enum),
            "struct" => Some(Type::Struct),
            "param" => Some(Type::Param),
            "dbtable" => Some(Type::DbTable),
            "dbrow" => Some(Type::DbRow),
            "dbcolumn" => Some(Type::DbColumn),
            "category" => Some(Type::Category),
            "varp" => Some(Type::Varp),
            "varn" => Some(Type::Varn),
            "vars" => Some(Type::Vars),
            "varbit" => Some(Type::Varbit),
            "mapzone" => Some(Type::MapZone),
            "maparea" => Some(Type::MapArea),
            "mapelement" => Some(Type::MapElement),
            "locshape" | "loc_shape" => Some(Type::LocShape),
            "npc_mode" | "npcmode" => Some(Type::NpcMode),
            "npc_stat" | "npcstat" => Some(Type::NpcStat),
            "area" => Some(Type::Area),
            "hunt" => Some(Type::Hunt),
            "movespeed" => Some(Type::MoveSpeed),
            "entityoverlay" => Some(Type::EntityOverlay),
            "stringvector" => Some(Type::StringVector),
            "writeinvobj" => Some(Type::WriteinvObj),
            "controller" => Some(Type::Controller),
            "regioncoord" => Some(Type::RegionCoord),
            // Script reference types (used when passing proc/label/queue references as params)
            "label" => Some(Type::Label),
            "proc" => Some(Type::Proc),
            "clientscript" => Some(Type::ClientScript),
            "command" => Some(Type::Int),
            "queue" => Some(Type::Queue),
            "softtimer" => Some(Type::SoftTimer),
            "timer" => Some(Type::Timer),
            "walktrigger" => Some(Type::Walktrigger),
            "any" => Some(Type::Any),
            "intparam" | "stringparam" => Some(Type::Param),
            "type" => Some(Type::Int),
            _ => None,
        }
    }

    /// Get the name of this type as used in RuneScript source.
    pub fn name(&self) -> &'static str {
        match self {
            Type::Int => "int",
            Type::Boolean => "boolean",
            Type::String => "string",
            Type::Long => "long",
            Type::Char => "char",
            Type::Colour => "colour",
            Type::Coord => "coord",
            Type::Loc => "loc",
            Type::Npc => "npc",
            Type::Obj => "obj",
            Type::NamedObj => "namedobj",
            Type::PlayerUid => "playeruid",
            Type::NpcUid => "npcuid",
            Type::Seq => "seq",
            Type::Spotanim => "spotanim",
            Type::Synth => "synth",
            Type::Vorbis => "vorbis",
            Type::Midi => "midi",
            Type::Jingle => "jingle",
            Type::Graphic => "graphic",
            Type::FontMetrics => "fontmetrics",
            Type::ChatCat => "chatcat",
            Type::ChatPhrase => "chatphrase",
            Type::Model => "model",
            Type::Texture => "texture",
            Type::IdKit => "idkit",
            Type::MesAnim => "mesanim",
            Type::Hitmark => "hitmark",
            Type::Stat => "stat",
            Type::Component => "component",
            Type::IfType => "iftype",
            Type::SetPosH => "setposh",
            Type::SetPosV => "setposv",
            Type::SetSize => "setsize",
            Type::SetTextAlignH => "settextalignh",
            Type::SetTextAlignV => "settextalignv",
            Type::Interface => "interface",
            Type::TopLevelInterface => "toplevelinterface",
            Type::OverlayInterface => "overlayinterface",
            Type::Inv => "inv",
            Type::Enum => "enum",
            Type::Struct => "struct",
            Type::Param => "param",
            Type::DbTable => "dbtable",
            Type::DbRow => "dbrow",
            Type::DbColumn => "dbcolumn",
            Type::Category => "category",
            Type::Varp => "varp",
            Type::Varn => "varn",
            Type::Vars => "vars",
            Type::Varbit => "varbit",
            Type::MapZone => "mapzone",
            Type::MapArea => "maparea",
            Type::MapElement => "mapelement",
            Type::LocShape => "loc_shape",
            Type::NpcMode => "npc_mode",
            Type::NpcStat => "npc_stat",
            Type::Area => "area",
            Type::Hunt => "hunt",
            Type::MoveSpeed => "movespeed",
            Type::EntityOverlay => "entityoverlay",
            Type::StringVector => "stringvector",
            Type::WriteinvObj => "writeinvobj",
            Type::Controller => "controller",
            Type::RegionCoord => "regioncoord",
            Type::Proc => "proc",
            Type::ClientScript => "clientscript",
            Type::Label => "label",
            Type::Queue => "queue",
            Type::SoftTimer => "softtimer",
            Type::Timer => "timer",
            Type::Walktrigger => "walktrigger",
            Type::Any => "any",
            Type::Error => "error",
            Type::Void => "void",
        }
    }

    pub fn allow_switch(&self) -> bool {
        self.base_type() == BaseVarType::Integer
    }

    /// Returns true if this is a numeric type that supports arithmetic.
    pub fn is_numeric(&self) -> bool {
        matches!(self, Type::Int | Type::Long)
    }

    /// Returns true if this is a type that uses object-style comparison (string equality).
    pub fn is_obj_type(&self) -> bool {
        matches!(self, Type::String)
    }

    /// Returns all known def_ type names.
    pub fn all_def_keywords() -> Vec<&'static str> {
        vec![
            "def_int",
            "def_boolean",
            "def_string",
            "def_long",
            "def_char",
            "def_coord",
            "def_loc",
            "def_npc",
            "def_obj",
            "def_namedobj",
            "def_playeruid",
            "def_npcuid",
            "def_seq",
            "def_spotanim",
            "def_synth",
            "def_midi",
            "def_jingle",
            "def_graphic",
            "def_fontmetrics",
            "def_model",
            "def_texture",
            "def_idkit",
            "def_mesanim",
            "def_hitmark",
            "def_stat",
            "def_component",
            "def_interface",
            "def_toplevelinterface",
            "def_overlayinterface",
            "def_inv",
            "def_enum",
            "def_struct",
            "def_param",
            "def_dbtable",
            "def_dbrow",
            "def_dbcolumn",
            "def_category",
            "def_varp",
            "def_varn",
            "def_vars",
            "def_varbit",
            "def_mapzone",
            "def_maparea",
            "def_mapelement",
            "def_loc_shape",
            "def_npc_mode",
            "def_npc_stat",
            "def_area",
            "def_hunt",
            "def_movespeed",
            "def_entityoverlay",
            "def_stringvector",
            "def_writeinvobj",
        ]
    }
}

impl std::fmt::Display for Type {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.name())
    }
}