1use crate::bytecode::{CompiledScript, Opcode, Operand};
2use crate::lexer::Lexer;
3use crate::parser::{Expr, Parser, ScriptFile, Statement, StringPart};
4use crate::pointer::{PointerHolder, PointerType};
5use crate::pointer_checker::PointerChecker;
6use crate::symbol::{ReturnType, SymbolKind, SymbolRegistry, identifier_form};
7use crate::symloader::{load_constant_files, load_game_var_types, load_packs};
8use crate::typechecker::TypeChecker;
9use crate::types::BaseVarType;
10use crate::types::Type;
11use std::collections::{BTreeMap, HashMap, HashSet};
12use std::fs;
13use std::io::{Error, ErrorKind, Result};
14use std::path::{Path, PathBuf};
15
16pub mod decode;
17pub mod decompile;
18pub mod format;
19pub(crate) mod hooks;
20pub(crate) mod triggers;
21use format::{Format, Width};
22pub use hooks::{HookArg, component_hook};
23
24#[derive(Clone, Copy, PartialEq, Eq)]
25pub enum CommandKind {
26 Fixed,
27 Hook,
28 HookComponent,
29 Enum,
30 EnumOutput,
31 Param,
32}
33
34pub struct Command {
35 pub opcode: u16,
36 pub name: String,
37 pub params: Vec<Type>,
38 pub returns: Vec<Type>,
39 pub kind: CommandKind,
40 pub component_last: bool,
41}
42
43pub(crate) fn command_pointers(registry: &SymbolRegistry) -> HashMap<String, PointerHolder> {
44 let mut pointers = HashMap::new();
45 for name in registry.commands.keys() {
46 let name = name.to_lowercase();
47 if !(name.starts_with("cc_") || name == "if_find") || name == "cc_deleteall" {
48 continue;
49 }
50 for (prefix, pointer) in [
51 ("", PointerType::ActiveComponent),
52 (".", PointerType::ActiveComponent2),
53 ] {
54 let mut holder = PointerHolder::default();
55 match name.as_str() {
56 "cc_create" => holder.set.insert(pointer),
57 "cc_find" | "if_find" => {
58 holder.set.insert(pointer);
59 holder.conditional_set = true;
60 }
61 _ => holder.required.insert(pointer),
62 }
63 pointers.insert(format!("{prefix}{name}"), holder);
64 }
65 }
66 pointers
67}
68
69pub fn hook_types(signature: &str, triggers: usize, explicit: bool) -> Vec<Type> {
70 let mut types = vec![Type::ClientScript];
71 types.extend(
72 signature
73 .trim_end_matches('Y')
74 .chars()
75 .map(|ch| Type::from_char(ch).unwrap_or(Type::Int)),
76 );
77 if signature.ends_with('Y') {
78 types.extend(vec![Type::Int; triggers + 1]);
79 }
80 types.push(Type::String);
81 if explicit {
82 types.push(Type::Component);
83 }
84 types
85}
86
87#[derive(Clone, Debug)]
88pub struct Source {
89 pub path: PathBuf,
90 pub file: ScriptFile,
91}
92
93impl Source {
94 pub fn parse(text: &str, path: &Path) -> Result<Self> {
95 Ok(Self {
96 path: path.to_path_buf(),
97 file: parse(text, path)?,
98 })
99 }
100
101 pub fn compile(
102 &self,
103 registry: &SymbolRegistry,
104 format: &Format,
105 ) -> Result<Vec<(i32, Vec<u8>)>> {
106 compile(&self.file, &self.path, registry, format)
107 }
108}
109
110pub fn script_key(name: &str) -> String {
111 let (trigger, name) = name
112 .strip_prefix('[')
113 .and_then(|name| name.strip_suffix(']'))
114 .and_then(|name| name.split_once(','))
115 .unwrap_or(("proc", name));
116 format!("[{trigger},{}]", identifier_form(name))
117}
118
119pub fn script_ids(pack: &Path) -> Result<HashMap<String, i32>> {
120 let mut ids = HashMap::new();
121 for line in fs::read_to_string(pack.join("clientscript.pack"))?.lines() {
122 if let Some((id, name)) = line.split_once('=') {
123 let key = script_key(name);
124 if ids
125 .insert(key.clone(), id.parse::<i32>().map_err(invalid)?)
126 .is_some()
127 {
128 return Err(invalid(format!("Duplicate clientscript name {key}")));
129 }
130 }
131 }
132 Ok(ids)
133}
134
135pub fn compile_sources(
136 sources: &[Source],
137 source_dir: &Path,
138 pack: &Path,
139) -> Result<BTreeMap<u32, Vec<u8>>> {
140 compile_registered_sources(sources, pack, registry(source_dir, pack)?)
141}
142
143pub fn compile_directory(source_dir: &Path, pack: &Path) -> Result<BTreeMap<u32, Vec<u8>>> {
144 let registry = registry(source_dir, pack)?;
145 let sources = load_sources(source_dir)?;
146 compile_registered_sources(&sources, pack, registry)
147}
148
149fn register_sources(
151 sources: &[Source],
152 pack: &Path,
153 registry: &mut SymbolRegistry,
154) -> Result<Vec<(i32, String)>> {
155 let mut ids = script_ids(pack)?;
156 let mut next = ids.values().copied().max().unwrap_or(-1) + 1;
157 for source in sources {
158 for script in &source.file.scripts {
159 if let Some(id) = script
160 .name
161 .strip_prefix("clientscript_")
162 .and_then(|id| id.parse::<i32>().ok())
163 {
164 next = next.max(
165 id.checked_add(1)
166 .ok_or_else(|| invalid("Clientscript ID overflow"))?,
167 );
168 }
169 }
170 }
171 let mut new_ids = Vec::new();
172 let mut declared = HashSet::new();
173 for source in sources {
174 std::sync::Arc::make_mut(&mut registry.visibility)
175 .declare_source(&source.path, &source.file);
176 for script in &source.file.scripts {
177 let key = format!("[{},{}]", script.trigger, script.name);
178 if !declared.insert(key.clone()) {
179 return Err(invalid(format!("Duplicate script {key}")));
180 }
181 if !ids.contains_key(&key)
182 && !script
183 .name
184 .strip_prefix("clientscript_")
185 .is_some_and(|id| id.parse::<u32>().is_ok())
186 {
187 ids.insert(key.clone(), next);
188 new_ids.push((next, key));
189 next = next
190 .checked_add(1)
191 .ok_or_else(|| invalid("Clientscript ID overflow"))?;
192 }
193 }
194 register(&source.file, &ids, registry)?;
195 }
196 Ok(new_ids)
197}
198
199pub fn compile_registered_sources(
200 sources: &[Source],
201 pack: &Path,
202 mut registry: SymbolRegistry,
203) -> Result<BTreeMap<u32, Vec<u8>>> {
204 let format = Format::load(&pack.join("cs2_command.pack"))?;
205 let new_ids = register_sources(sources, pack, &mut registry)?;
206 let mut scripts = Vec::new();
207 for source in sources {
208 scripts.extend(checked_codegen(&source.file, &source.path, ®istry)?);
209 }
210 PointerChecker::new_client(&scripts, ®istry)
211 .run()
212 .print_all();
213 let mut compiled = BTreeMap::new();
214 for script in scripts {
215 if compiled
216 .insert(
217 u32::try_from(script.id).map_err(invalid)?,
218 encode(&script, &format)?,
219 )
220 .is_some()
221 {
222 return Err(invalid(format!("Duplicate clientscript ID {}", script.id)));
223 }
224 }
225 if !new_ids.is_empty() {
226 let path = pack.join("clientscript.pack");
227 let mut text = fs::read_to_string(&path)?;
228 if !text.is_empty() && !text.ends_with('\n') {
229 text.push('\n');
230 }
231 for (id, key) in new_ids {
232 text.push_str(&format!("{id}={key}\n"));
233 }
234 fs::write(path, text)?;
235 }
236 Ok(compiled)
237}
238
239pub fn load_sources(dir: &Path) -> Result<Vec<Source>> {
240 let mut sources = Vec::new();
241 for path in crate::sources::script_files(dir, "cs2")? {
242 if path
243 .file_name()
244 .is_some_and(|name| name != "engine.cs2" && name != "engine.gs2")
245 {
246 sources.push(Source::parse(&fs::read_to_string(&path)?, &path)?);
247 }
248 }
249 sources.sort_by(|a, b| a.path.cmp(&b.path));
250 Ok(sources)
251}
252
253pub fn load_commands(path: &Path, engine_path: &Path) -> Result<Vec<Command>> {
254 let format = Format::load(path)?;
255 let engine_text = fs::read_to_string(engine_path)?;
256 commands(&format, &engine_text, engine_path)
257}
258
259pub fn commands(format: &Format, engine_text: &str, label: &Path) -> Result<Vec<Command>> {
261 let engine = parse(engine_text, label)?;
262 let mut signatures = HashMap::new();
263 for script in engine.scripts {
264 if script.trigger != "command" || !script.body.is_empty() {
265 return Err(invalid("engine.cs2 must contain command signatures"));
266 }
267 if signatures.insert(script.name.clone(), script).is_some() {
268 return Err(invalid("Duplicate engine.cs2 command"));
269 }
270 }
271 let mut commands = Vec::new();
272 for (&opcode, name) in &format.commands {
273 let signature = signatures
274 .remove(name)
275 .ok_or_else(|| invalid(format!("Missing engine.cs2 signature for {name}")))?;
276 let annotation = |name| {
277 signature
278 .annotations
279 .iter()
280 .find(|annotation| annotation.name == name)
281 .map(|annotation| annotation.value.as_str())
282 };
283 let kind = match annotation("signature").unwrap_or("fixed") {
284 "fixed" => CommandKind::Fixed,
285 "hook" => CommandKind::Hook,
286 "hook_component" => CommandKind::HookComponent,
287 "enum" => CommandKind::Enum,
288 "enum_output" => CommandKind::EnumOutput,
289 "param" => CommandKind::Param,
290 kind => return Err(invalid(format!("Unknown command kind {kind}"))),
291 };
292 let component_last = annotation("stack") == Some("component_last");
293 commands.push(Command {
294 opcode,
295 name: name.clone(),
296 params: signature
297 .params
298 .iter()
299 .map(|param| param.param_type)
300 .collect(),
301 returns: signature.return_types,
302 kind,
303 component_last,
304 });
305 }
306 if !signatures.is_empty() {
307 return Err(invalid(
308 "engine.cs2 contains commands without opcode definitions",
309 ));
310 }
311 Ok(commands)
312}
313
314pub fn registry_with_scripts(source_dir: &Path, pack: &Path) -> Result<SymbolRegistry> {
316 let mut registry = registry(source_dir, pack)?;
317 register_sources(&load_sources(source_dir)?, pack, &mut registry)?;
318 Ok(registry)
319}
320
321pub fn registry(source_dir: &Path, pack: &Path) -> Result<SymbolRegistry> {
322 let root = crate::sources::target_dir(source_dir, crate::sources::Scope::Client);
323 let source_dir = root.as_path();
324 let mut registry = SymbolRegistry::new();
325 load_packs(&mut registry, pack).map_err(invalid)?;
326 for (key, id) in script_ids(pack)? {
327 let (trigger, name) = key[1..key.len() - 1].split_once(',').unwrap();
328 registry
329 .trigger_script_ids
330 .insert(format!("{trigger}:{name}"), id);
331 registry.script_ids.insert(name.to_owned(), id);
332 if trigger == "proc" {
333 registry.proc_script_ids.insert(name.to_owned(), id);
334 }
335 }
336 registry.clear_commands();
337 registry.game_vars.retain(|_, symbol| {
338 matches!(
339 &symbol.kind, SymbolKind::GameVar { category, .. }
340 if matches!(category.as_str(), "varp" | "varpbit")
341 )
342 });
343 let engine_path = crate::sources::engine_file(source_dir, crate::sources::Scope::Client)?
344 .ok_or_else(|| {
345 invalid(format!(
346 "No engine.cs2 found under {}",
347 source_dir.display()
348 ))
349 })?;
350 for command in load_commands(&pack.join("cs2_command.pack"), &engine_path)? {
351 registry.register_command(
352 command.name.to_owned(),
353 command.opcode as i32,
354 command.params.clone(),
355 command.returns.clone(),
356 );
357 registry.stack_commands.insert(command.name.to_owned());
358 registry.set_component_first_command(&command.name, command.component_last);
359 if matches!(command.kind, CommandKind::Hook | CommandKind::HookComponent) {
360 registry.hook_commands.insert(
361 command.name.to_owned(),
362 command.kind == CommandKind::HookComponent,
363 );
364 }
365 let type_arguments = match command.kind {
366 CommandKind::Enum => 2,
367 CommandKind::EnumOutput => 1,
368 _ => 0,
369 };
370 if type_arguments > 0 {
371 registry
372 .type_arguments
373 .insert(command.name.clone(), type_arguments);
374 }
375 let dynamic = match command.kind {
376 CommandKind::Enum => Some(ReturnType::TypeArgument(1)),
377 CommandKind::Param => Some(ReturnType::ParamArgument(
378 command.params.len().saturating_sub(1),
379 )),
380 _ => None,
381 };
382 if let Some(dynamic) = dynamic {
383 registry
384 .dynamic_returns
385 .insert(command.name.clone(), dynamic);
386 }
387 }
388 for (name, ty) in [
389 ("__pop_int", Type::Int),
390 ("__pop_string", Type::String),
391 ("__pop_long", Type::Long),
392 ("__join", Type::String),
393 ("__get_array", Type::Int),
394 ] {
395 registry.register_command(name.to_owned(), 0, Vec::new(), vec![ty]);
396 }
397 for (kind, ty) in [("varc", Type::Int), ("varcstr", Type::String)] {
398 let path = pack.join(format!("{kind}.pack"));
399 if !path.exists() {
400 continue;
401 }
402 for line in fs::read_to_string(path)?.lines() {
403 if line.trim_start().starts_with("//") {
404 continue;
405 }
406 if let Some((id, name)) = line.split_once('=') {
407 registry.register_game_var(
408 name.to_owned(),
409 ty,
410 id.parse::<i32>().map_err(invalid)?,
411 kind.to_owned(),
412 );
413 }
414 }
415 }
416 let configs = crate::sources::target_config_files(source_dir, crate::sources::Scope::Client)?;
417 {
418 for file in configs
419 .iter()
420 .filter(|path| path.extension().is_some_and(|ext| ext == "param"))
421 {
422 let text = fs::read_to_string(file)?;
423 let mut id = None;
424 for line in text.lines().map(str::trim) {
425 if let Some(name) = line
426 .strip_prefix('[')
427 .and_then(|line| line.strip_suffix(']'))
428 {
429 id = registry.lookup_reference(name, Type::Param);
430 if let Some(id) = id {
431 registry.register_param_type(id, Type::Int);
432 }
433 } else if let Some(ty) = line.strip_prefix("type=")
434 && let Some(id) = id
435 {
436 let ty = ty.trim();
437 let ty = if let Some(byte) = ty.strip_prefix("byte:0x") {
438 let byte = u8::from_str_radix(byte, 16).map_err(invalid)?;
439 Type::from_char(byte as char).unwrap_or(Type::Int)
440 } else {
441 Type::from_name(ty)
442 .ok_or_else(|| invalid(format!("Unknown parameter type {ty}")))?
443 };
444 registry.register_param_type(id, ty);
445 }
446 }
447 }
448 }
449 load_constant_files(&mut registry, &configs);
450 load_game_var_types(&mut registry, &configs);
451 load_varc_types(&mut registry, &configs)?;
452 load_clan_settings(&mut registry, &configs, pack)?;
453 registry.visibility =
454 std::sync::Arc::new(crate::sources::Visibility::load(source_dir, ®istry));
455 Ok(registry)
456}
457
458fn load_varc_types(registry: &mut SymbolRegistry, files: &[PathBuf]) -> Result<()> {
460 for file in files
461 .iter()
462 .filter(|path| path.extension().is_some_and(|ext| ext == "varc"))
463 {
464 let mut section = None;
465 for line in fs::read_to_string(file)?.lines().map(str::trim) {
466 if let Some(name) = line
467 .strip_prefix('[')
468 .and_then(|line| line.strip_suffix(']'))
469 {
470 section = Some(name.to_owned());
471 } else if let Some(ty) = line.strip_prefix("type=").and_then(Type::from_name)
472 && let Some(SymbolKind::GameVar { var_type, .. }) = section
473 .as_ref()
474 .and_then(|name| registry.game_vars.get_mut(name))
475 .map(|symbol| &mut symbol.kind)
476 {
477 *var_type = ty;
478 }
479 }
480 }
481 Ok(())
482}
483
484pub(crate) fn load_clan_settings(
487 registry: &mut SymbolRegistry,
488 files: &[PathBuf],
489 pack: &Path,
490) -> Result<()> {
491 load_clan_vars(registry, files, pack, "varclansetting")?;
492 load_clan_vars(registry, files, pack, "varclan")
493}
494
495fn load_clan_vars(
496 registry: &mut SymbolRegistry,
497 files: &[PathBuf],
498 pack: &Path,
499 ext: &str,
500) -> Result<()> {
501 let ids: HashMap<String, i32> = fs::read_to_string(pack.join(format!("{ext}.pack")))
502 .unwrap_or_default()
503 .lines()
504 .filter_map(|line| line.split_once('='))
505 .filter_map(|(id, name)| Some((name.trim().to_owned(), id.parse().ok()?)))
506 .collect();
507 let mut add = |name: &str, ty: Option<&str>, bits: bool| -> Result<()> {
508 let id = ids
509 .get(name)
510 .copied()
511 .or_else(|| name.strip_prefix(&format!("{ext}_"))?.parse().ok())
512 .ok_or_else(|| invalid(format!("Unknown {ext} {name}")))?;
513 let (category, ty) = match (ext, bits, ty) {
514 ("varclan", true, _) => ("varclan_bit", Type::Int),
515 ("varclan", _, Some("long" | "clanforumqfc")) => ("varclan_long", Type::Long),
516 ("varclan", _, Some("string")) => ("varclan_string", Type::String),
517 ("varclan", _, _) => ("varclan", Type::Int),
518 (_, true, _) => ("clansetting_varbit", Type::Int),
519 (_, _, Some("int" | "bit")) => ("clansetting_int", Type::Int),
520 (_, _, Some("long")) => ("clansetting_long", Type::Long),
521 (_, _, Some("clanforumqfc")) => ("clansetting_long", Type::ClanForumQfc),
522 (_, _, Some("string")) => ("clansetting_string", Type::String),
523 _ => {
524 return Err(invalid(format!(
525 "Clan setting {name} has no opcodes for type {ty:?}"
526 )));
527 }
528 };
529 registry.register_game_var(name.to_owned(), ty, id, category.to_owned());
530 Ok(())
531 };
532 for file in files {
533 if file.extension().is_none_or(|file_ext| file_ext != ext) {
534 continue;
535 }
536 let text = fs::read_to_string(file)?;
537 let mut section: Option<(&str, Option<&str>, bool)> = None;
538 for line in text.lines().map(str::trim) {
539 if let Some(name) = line.strip_prefix('[').and_then(|l| l.strip_suffix(']')) {
540 if let Some((name, ty, bits)) = section {
541 add(name, ty, bits)?;
542 }
543 section = Some((name, None, false));
544 } else if let Some((_, ty, bits)) = &mut section {
545 if let Some(value) = line.strip_prefix("type=") {
546 *ty = Some(value.trim());
547 } else if line.starts_with("basevar=") {
548 *bits = true;
549 }
550 }
551 }
552 if let Some((name, ty, bits)) = section {
553 add(name, ty, bits)?;
554 }
555 }
556 Ok(())
557}
558
559pub fn parse(source: &str, path: &Path) -> Result<ScriptFile> {
560 let path = path.to_path_buf();
561 let tokens = Lexer::new(source, &path).tokenize().map_err(invalid)?;
562 Parser::new(tokens, &path).parse().map_err(invalid)
563}
564
565pub fn register(
566 file: &ScriptFile,
567 ids: &HashMap<String, i32>,
568 registry: &mut SymbolRegistry,
569) -> Result<()> {
570 for script in &file.scripts {
571 for statement in &script.body {
572 register_variables(statement, registry);
573 }
574 if triggers::lookup(&script.trigger).is_none() {
575 return Err(invalid(format!(
576 "Invalid client trigger {}",
577 script.trigger
578 )));
579 }
580 let id = ids
581 .get(&format!("[{},{}]", script.trigger, script.name))
582 .or_else(|| ids.get(&script.name))
583 .copied()
584 .or_else(|| {
585 script
586 .name
587 .strip_prefix("clientscript_")
588 .and_then(|id| id.parse::<i32>().ok())
589 .filter(|id| *id >= 0)
590 })
591 .ok_or_else(|| invalid(format!("Missing clientscript ID for {}", script.name)))?;
592 registry
593 .preloaded_script_ids
594 .insert(format!("{}:{}", script.trigger, script.name), id);
595 registry.register_script(
596 script.name.clone(),
597 script.trigger.clone(),
598 script.params.iter().map(|p| p.param_type).collect(),
599 script.return_types.clone(),
600 );
601 registry
602 .stack_scripts
603 .insert(format!("{}:{}", script.trigger, script.name));
604 }
605 Ok(())
606}
607
608pub(crate) fn register_variables(statement: &Statement, registry: &mut SymbolRegistry) {
609 match statement {
610 Statement::VarDeclaration {
611 value: Some(value), ..
612 }
613 | Statement::Expression { expr: value, .. }
614 | Statement::ArrayDeclaration { size: value, .. } => {
615 register_expr_variables(value, registry)
616 }
617 Statement::Assignment { target, value, .. } => {
618 register_expr_variables(target, registry);
619 register_expr_variables(value, registry);
620 }
621 Statement::Return { values, .. } => {
622 for value in values {
623 register_expr_variables(value, registry);
624 }
625 }
626 Statement::If {
627 condition,
628 body,
629 else_if,
630 else_body,
631 ..
632 } => {
633 register_expr_variables(condition, registry);
634 for statement in body {
635 register_variables(statement, registry);
636 }
637 for (condition, body, _) in else_if {
638 register_expr_variables(condition, registry);
639 for statement in body {
640 register_variables(statement, registry);
641 }
642 }
643 if let Some(body) = else_body {
644 for statement in body {
645 register_variables(statement, registry);
646 }
647 }
648 }
649 Statement::While {
650 condition, body, ..
651 } => {
652 register_expr_variables(condition, registry);
653 for statement in body {
654 register_variables(statement, registry);
655 }
656 }
657 Statement::Switch {
658 expr,
659 cases,
660 default,
661 ..
662 } => {
663 register_expr_variables(expr, registry);
664 for case in cases {
665 for statement in &case.body {
666 register_variables(statement, registry);
667 }
668 }
669 if let Some(body) = default {
670 for statement in body {
671 register_variables(statement, registry);
672 }
673 }
674 }
675 _ => {}
676 }
677}
678
679fn register_expr_variables(expr: &Expr, registry: &mut SymbolRegistry) {
680 match expr {
681 Expr::GameVar(name, _) if registry.lookup_game_var(name).is_none() => {
682 for (kind, ty) in [
683 ("varp", Type::Int),
684 ("varpbit", Type::Int),
685 ("varc", Type::Int),
686 ("varcstr", Type::String),
687 ] {
688 if let Some(id) = name
689 .strip_prefix(&format!("{}_", hooks::var_placeholder(kind)))
690 .and_then(|s| s.parse::<u16>().ok())
691 {
692 registry.register_game_var(name.clone(), ty, id as i32, kind.to_owned());
693 }
694 }
695 }
696 Expr::ArrayAccess { index, .. } | Expr::Calc(index) => {
697 register_expr_variables(index, registry)
698 }
699 Expr::BinaryOp { lhs, rhs, .. } | Expr::LogicalOp { lhs, rhs, .. } => {
700 register_expr_variables(lhs, registry);
701 register_expr_variables(rhs, registry);
702 }
703 Expr::CommandCall { name, args, .. } => {
704 if let Some(&explicit) = registry.hook_commands.get(name.trim_start_matches('.'))
705 && args.len() == 1 + usize::from(explicit)
706 && let Some(Expr::StringLiteral(text)) = args.first()
707 && let Ok(values) = hooks::parse(text)
708 {
709 if hooks::transmit_kind(name).is_some_and(|kind| kind.starts_with("var"))
710 && let Some(Expr::Tuple(transmits)) = values.get(2)
711 {
712 for value in transmits {
713 if let Expr::Identifier(name) = value {
714 register_expr_variables(&Expr::GameVar(name.clone(), 0), registry);
715 }
716 }
717 }
718 for value in &values {
719 register_expr_variables(value, registry);
720 }
721 }
722 for arg in args {
723 register_expr_variables(arg, registry);
724 }
725 }
726 Expr::ProcCall { args, .. }
727 | Expr::JumpCall { args, .. }
728 | Expr::MultiAssign(args, _)
729 | Expr::Tuple(args) => {
730 for arg in args {
731 register_expr_variables(arg, registry);
732 }
733 }
734 Expr::JoinedString { parts } => {
735 for part in parts {
736 if let StringPart::Expr(expr) = part {
737 register_expr_variables(expr, registry);
738 }
739 }
740 }
741 _ => {}
742 }
743}
744
745pub fn compile(
746 file: &ScriptFile,
747 path: &Path,
748 registry: &SymbolRegistry,
749 format: &Format,
750) -> Result<Vec<(i32, Vec<u8>)>> {
751 let scripts = checked_codegen(file, path, registry)?;
752 PointerChecker::new_client(&scripts, registry)
753 .run()
754 .print_all();
755 scripts
756 .into_iter()
757 .map(|compiled| Ok((compiled.id, encode(&compiled, format)?)))
758 .collect()
759}
760
761fn checked_codegen(
762 file: &ScriptFile,
763 path: &Path,
764 registry: &SymbolRegistry,
765) -> Result<Vec<CompiledScript>> {
766 let mut checker = TypeChecker::new(registry);
767 checker.check_file(file, path);
768 if checker.diagnostics.has_errors() {
769 return Err(invalid(
770 checker
771 .diagnostics
772 .errors()
773 .iter()
774 .map(ToString::to_string)
775 .collect::<Vec<_>>()
776 .join("\n"),
777 ));
778 }
779 codegen(file, path, registry)
780}
781
782pub(crate) fn codegen(
783 file: &ScriptFile,
784 path: &Path,
785 registry: &SymbolRegistry,
786) -> Result<Vec<CompiledScript>> {
787 let mut diagnostics = crate::diagnostics::DiagnosticsCollector::new();
788 let compiled = crate::compiler::compile_file(file, path, registry, true, &mut diagnostics);
789 if diagnostics.has_errors() {
790 return Err(invalid(
791 diagnostics
792 .errors()
793 .iter()
794 .map(ToString::to_string)
795 .collect::<Vec<_>>()
796 .join(
797 "
798",
799 ),
800 ));
801 }
802 Ok(compiled)
803}
804
805pub fn encode(script: &CompiledScript, format: &Format) -> Result<Vec<u8>> {
806 encode_named(script, "", format)
807}
808
809pub fn encode_named(script: &CompiledScript, name: &str, format: &Format) -> Result<Vec<u8>> {
810 let lowered = |opcode: Opcode| {
812 matches!(opcode, Opcode::BranchIfTrue | Opcode::BranchIfFalse) && !format.has(opcode)
813 };
814 let mut positions = Vec::with_capacity(script.instructions.len() + 1);
815 let mut count = 0;
816 for instruction in &script.instructions {
817 positions.push(count);
818 if lowered(instruction.opcode) {
819 count += 2;
820 } else if instruction.opcode != Opcode::LineNumber {
821 count += 1;
822 }
823 }
824 positions.push(count);
825 let mut out = Vec::new();
826 write_string(&mut out, name)?;
827 let mut switches = Vec::new();
828 for (i, instruction) in script.instructions.iter().enumerate() {
829 if instruction.opcode == Opcode::LineNumber {
830 continue;
831 }
832 if instruction.opcode == Opcode::Raw {
833 let Operand::Long(value) = instruction.operand else {
834 return Err(invalid("Invalid raw client instruction"));
835 };
836 let opcode = (value >> 32) as u16;
837 let operand = value as i32;
838 out.extend_from_slice(&opcode.to_be_bytes());
839 format.width(opcode).integer(&mut out, operand as i64)?;
840 continue;
841 }
842 if lowered(instruction.opcode) {
843 let &Operand::JumpTarget(target) = &instruction.operand else {
844 return Err(invalid("Invalid branch operand"));
845 };
846 let (push, push_width) = format.instruction(Opcode::PushConstantInt)?;
847 out.extend_from_slice(&push.to_be_bytes());
848 push_width.integer(
849 &mut out,
850 i64::from(instruction.opcode == Opcode::BranchIfTrue),
851 )?;
852 let (branch, branch_width) = format.instruction(Opcode::BranchEquals)?;
853 let target = positions
854 .get(target)
855 .ok_or_else(|| invalid("Invalid branch target"))?;
856 out.extend_from_slice(&branch.to_be_bytes());
857 branch_width.integer(&mut out, i64::from(*target - positions[i] - 2))?;
858 continue;
859 }
860 let (opcode, width) = match instruction.opcode {
861 Opcode::Command => match instruction.operand {
862 Operand::Int(value) => (value as u16, format.width(value as u16)),
863 _ => return Err(invalid("Unresolved client command")),
864 },
865 value => format.instruction(value)?,
866 };
867 let value = match &instruction.operand {
868 Operand::None => 0,
869 Operand::Int(value) if instruction.opcode == Opcode::Command => (value >> 16) & 255,
870 Operand::Int(value) => *value,
871 Operand::JumpTarget(target) => {
872 *positions
873 .get(*target)
874 .ok_or_else(|| invalid("Invalid branch target"))?
875 - positions[i]
876 - 1
877 }
878 Operand::StringCount(value) => i32::try_from(*value).map_err(invalid)?,
879 Operand::ArrayDef(slot, kind) => (slot << 16) | *kind as i32,
880 Operand::SwitchTable(index) => {
881 let table = script
882 .switch_tables
883 .get(*index as usize)
884 .ok_or_else(|| invalid("Invalid switch table"))?;
885 let mut cases = Vec::with_capacity(table.len());
886 for (key, target) in table {
887 let position = positions
888 .get(*target)
889 .ok_or_else(|| invalid("Invalid switch target"))?;
890 cases.push((*key, *position - positions[i] - 1));
891 }
892 switches.push(cases);
893 i32::try_from(switches.len() - 1).map_err(invalid)?
894 }
895 Operand::Str(index) => {
896 if width != Width::String {
897 return Err(invalid(
898 "String instruction requires a string operand definition",
899 ));
900 }
901 out.extend_from_slice(&opcode.to_be_bytes());
902 write_string(&mut out, &script.strings[*index as usize])?;
903 continue;
904 }
905 Operand::Long(value) => {
906 out.extend_from_slice(&opcode.to_be_bytes());
907 width.integer(&mut out, *value)?;
908 continue;
909 }
910 };
911 out.extend_from_slice(&opcode.to_be_bytes());
912 width.integer(&mut out, value as i64)?;
913 }
914 out.extend_from_slice(&count.to_be_bytes());
915 for counts in [
916 [
917 script.int_local_count,
918 script.string_local_count,
919 script.long_local_count,
920 ],
921 [
922 script.int_arg_count,
923 script.string_arg_count,
924 script.long_arg_count,
925 ],
926 ] {
927 for (kind, value) in [BaseVarType::Integer, BaseVarType::String, BaseVarType::Long]
928 .into_iter()
929 .zip(counts)
930 {
931 if value != 0 && !format.local_types.contains(&kind) {
932 return Err(invalid(format!("Unsupported {kind:?} locals")));
933 }
934 }
935 for kind in &format.local_types {
936 let index = match kind {
937 BaseVarType::Integer => 0,
938 BaseVarType::String => 1,
939 BaseVarType::Long => 2,
940 };
941 out.extend_from_slice(&counts[index].to_be_bytes());
942 }
943 }
944 if !format.switches {
945 if !switches.is_empty() {
946 return Err(invalid(
947 "Switch tables are unsupported by input bytecode definitions",
948 ));
949 }
950 return Ok(out);
951 }
952 let start = out.len();
953 out.push(u8::try_from(switches.len()).map_err(invalid)?);
954 for table in switches {
955 out.extend_from_slice(&u16::try_from(table.len()).map_err(invalid)?.to_be_bytes());
956 for (key, offset) in table {
957 out.extend_from_slice(&key.to_be_bytes());
958 out.extend_from_slice(&offset.to_be_bytes());
959 }
960 }
961 let size = u16::try_from(out.len() - start).map_err(invalid)?;
962 out.extend_from_slice(&size.to_be_bytes());
963 Ok(out)
964}
965
966pub fn write_string(out: &mut Vec<u8>, text: &str) -> Result<()> {
967 const CP1252: &str = "€\u{81}‚ƒ„…†‡ˆ‰Š‹Œ\u{8d}Ž\u{8f}\u{90}‘’“”•–—˜™š›œ\u{9d}žŸ";
968 for ch in text.chars() {
969 let value = ch as u32;
970 if value == 0 {
971 return Err(invalid("NUL in clientscript string"));
972 }
973 if value < 128 || (160..=255).contains(&value) {
974 out.push(value as u8);
975 } else if let Some(index) = CP1252.chars().position(|c| c == ch) {
976 out.push(128 + index as u8);
977 } else {
978 return Err(invalid(format!(
979 "Character {ch:?} cannot be encoded in CP1252"
980 )));
981 }
982 }
983 out.push(0);
984 Ok(())
985}
986
987fn invalid(message: impl ToString) -> Error {
988 Error::new(ErrorKind::InvalidData, message.to_string())
989}
990
991#[cfg(test)]
992mod tests {
993 use super::*;
994
995 #[test]
996 fn clan_settings_take_their_opcodes_from_their_type() {
997 let dir = std::env::temp_dir().join(format!("runec_clan_{}", std::process::id()));
998 fs::create_dir_all(&dir).unwrap();
999 let file = dir.join("all.varclansetting");
1000 fs::write(
1001 &file,
1002 "// 0\n[varclansetting_0]\ntype=int\n\n// 1\n[named]\ntype=long\n\n\
1003 // 2\n[varclansetting_2]\ntype=string\n\n\
1004 // 3\n[varclansetting_3]\ntype=byte:0x01\nbasevar=2\nstartbit=0\nendbit=0\n",
1005 )
1006 .unwrap();
1007 fs::write(dir.join("varclansetting.pack"), "1=named\n").unwrap();
1008 let mut registry = SymbolRegistry::new();
1009 load_clan_settings(&mut registry, std::slice::from_ref(&file), &dir).unwrap();
1010 for (name, id, expected) in [
1011 ("varclansetting_0", 0, "clansetting_int"),
1012 ("named", 1, "clansetting_long"),
1013 ("varclansetting_2", 2, "clansetting_string"),
1014 ("varclansetting_3", 3, "clansetting_varbit"),
1015 ] {
1016 let Some(SymbolKind::GameVar {
1017 var_id, category, ..
1018 }) = registry.lookup_game_var(name).map(|symbol| &symbol.kind)
1019 else {
1020 panic!("{name} is not registered");
1021 };
1022 assert_eq!((*var_id, category.as_str()), (id, expected), "{name}");
1023 }
1024 fs::remove_dir_all(dir).unwrap();
1025 }
1026
1027 #[test]
1028 fn clan_vars_take_their_opcodes_from_their_type() {
1029 let dir = std::env::temp_dir().join(format!("runec_varclan_{}", std::process::id()));
1030 fs::create_dir_all(&dir).unwrap();
1031 let file = dir.join("all.varclan");
1032 fs::write(
1033 &file,
1034 "// 0\n[varclan_0]\ntype=int\n\n// 1\n[named]\ntype=long\n\n\
1035 // 2\n[varclan_2]\ntype=string\n\n\
1036 // 3\n[varclan_3]\ntype=bit\nbasevar=0\nstartbit=0\nendbit=6\n",
1037 )
1038 .unwrap();
1039 fs::write(dir.join("varclan.pack"), "1=named\n").unwrap();
1040 let mut registry = SymbolRegistry::new();
1041 load_clan_settings(&mut registry, std::slice::from_ref(&file), &dir).unwrap();
1042 for (name, id, expected, opcode) in [
1043 ("varclan_0", 0, "varclan", Opcode::PushVarClan),
1044 ("named", 1, "varclan_long", Opcode::PushVarClanLong),
1045 ("varclan_2", 2, "varclan_string", Opcode::PushVarClanString),
1046 ("varclan_3", 3, "varclan_bit", Opcode::PushVarClanBit),
1047 ] {
1048 let Some(SymbolKind::GameVar {
1049 var_id, category, ..
1050 }) = registry.lookup_game_var(name).map(|symbol| &symbol.kind)
1051 else {
1052 panic!("{name} is not registered");
1053 };
1054 assert_eq!((*var_id, category.as_str()), (id, expected), "{name}");
1055 assert_eq!(Opcode::push_var(category), opcode, "{name}");
1056 }
1057 fs::remove_dir_all(dir).unwrap();
1058 }
1059
1060 #[test]
1061 fn a_varc_takes_the_type_its_file_declares() {
1062 let dir = std::env::temp_dir().join(format!("runec-varc-types-{}", std::process::id()));
1063 fs::create_dir_all(&dir).unwrap();
1064 let file = dir.join("all.varc");
1065 fs::write(
1066 &file,
1067 "// 5\n[varclient_5]\ntype=boolean\n\n// 6\n[varclient_6]\ntype=byte:0x65\n\n// 7\n[varclient_7]\n",
1068 )
1069 .unwrap();
1070 let mut registry = SymbolRegistry::new();
1071 for id in 5..8 {
1072 registry.register_game_var(format!("varclient_{id}"), Type::Int, id, "varc".into());
1073 }
1074 load_varc_types(&mut registry, std::slice::from_ref(&file)).unwrap();
1075 let declared = |name: &str| match registry.lookup_game_var(name).map(|symbol| &symbol.kind)
1076 {
1077 Some(SymbolKind::GameVar { var_type, .. }) => *var_type,
1078 _ => panic!("{name} is not registered"),
1079 };
1080 assert_eq!(declared("varclient_5"), Type::Boolean);
1081 assert_eq!(declared("varclient_6"), Type::Int);
1082 assert_eq!(declared("varclient_7"), Type::Int);
1083 fs::remove_dir_all(dir).unwrap();
1084 }
1085
1086 #[test]
1087 fn a_transmit_name_is_never_the_dotted_alias() {
1088 let mut registry = SymbolRegistry::new();
1089 registry.register_game_var("bank_open".into(), Type::Int, 90, "varp".into());
1090 registry.register_game_var(".bank_open".into(), Type::Int, 90, "varp".into());
1091 assert_eq!(
1092 hooks::transmit_name(®istry, "varp", 90).as_deref(),
1093 Some("bank_open")
1094 );
1095 }
1096}