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
use clap::{Parser as ClapParser, Subcommand};
use runec::clientscript;
use runec::sources::{self, Scope};
use std::error::Error;
use std::fs;
use std::panic::resume_unwind;
use std::path::PathBuf;
use std::thread;
#[cfg(feature = "memprof")]
#[global_allocator]
static GLOBAL: runec::memprof::Counting = runec::memprof::Counting;
#[derive(ClapParser)]
#[command(author, version, about = "RuneScript Compiler")]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Serve compiler diagnostics and editor features over stdio LSP
Lsp {
/// Revision scripts root, searched recursively for all script languages
#[arg(long)]
source: Option<PathBuf>,
#[arg(long)]
pack: Option<PathBuf>,
/// Restrict the language server to ClientScript and GlobalScript
#[arg(long, conflicts_with = "server", requires = "source")]
client: bool,
/// Restrict the language server to RuneScript and GlobalScript
#[arg(long, requires = "source")]
server: bool,
/// Restrict the language server to LobbyScript
#[arg(long, conflicts_with_all = ["client", "server"], requires = "source")]
lobby: bool,
},
/// Compile .cs2 sources to individual cache script files
CompileClient {
#[arg(short, long)]
source: PathBuf,
#[arg(short, long)]
output: PathBuf,
#[arg(long)]
pack: PathBuf,
},
/// Compile all .rs2 scripts to binary output
Compile {
/// Source directory containing .rs2 files
#[arg(short, long)]
source: String,
/// Output directory for compiled scripts
#[arg(short, long, default_value = "./data/pack/server")]
output: String,
/// Pack directory (overrides auto-detection)
#[arg(long)]
pack: Option<String>,
/// Run lint passes (unused locals, unreachable code) after compilation
#[arg(long, default_value_t = false)]
lint: bool,
/// Trade compilation time for lower peak memory by recompiling scripts
/// on demand. Output is byte-identical to normal compilation.
#[arg(long, visible_alias = "recompile", default_value_t = false)]
low_mem: bool,
},
/// Run all analysis passes without writing output
Lint {
/// Source directory containing .rs2 files
#[arg(short, long)]
source: String,
/// Pack directory (overrides auto-detection)
#[arg(long)]
pack: Option<String>,
},
/// Compile all .ls2 scripts to binary output for the lobby server
CompileLobby {
/// Source directory containing .ls2 files
#[arg(short, long)]
source: String,
/// Output directory for compiled scripts
#[arg(short, long, default_value = "./data/pack/lobby")]
output: String,
/// Pack directory (overrides auto-detection)
#[arg(long)]
pack: Option<String>,
/// Run lint passes (unused locals, unreachable code) after compilation
#[arg(long, default_value_t = false)]
lint: bool,
/// Trade compilation time for lower peak memory by recompiling scripts
/// on demand. Output is byte-identical to normal compilation.
#[arg(long, visible_alias = "recompile", default_value_t = false)]
low_mem: bool,
},
/// Run all analysis passes on .ls2 scripts without writing output
LintLobby {
/// Source directory containing .ls2 files
#[arg(short, long)]
source: String,
/// Pack directory (overrides auto-detection)
#[arg(long)]
pack: Option<String>,
},
}
fn main() -> Result<(), Box<dyn Error>> {
match thread::Builder::new()
.name("runec".into())
.stack_size(8 << 20)
.spawn(|| run().map_err(|error| error.to_string()))?
.join()
{
Ok(result) => result.map_err(Into::into),
Err(panic) => resume_unwind(panic),
}
}
fn run() -> Result<(), Box<dyn Error>> {
tracing_subscriber::fmt()
.with_env_filter(
tracing_subscriber::EnvFilter::try_from_default_env()
.unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
)
.with_writer(std::io::stderr)
.init();
let cli = Cli::parse();
match cli.command {
Commands::Lsp {
source,
pack,
client,
server,
lobby,
} => {
if let Some(source) = source {
let source = sources::source_root(&source);
let files = sources::config_files(&source);
let has_client = files
.iter()
.any(|path| Scope::from_path(path) == Some(Scope::Client));
let has_server = files
.iter()
.any(|path| Scope::from_path(path) == Some(Scope::Rune));
let has_lobby = files
.iter()
.any(|path| path.file_name().is_some_and(|name| name == "engine.ls2"));
let targets = if client {
vec![Scope::Client]
} else if server {
vec![Scope::Rune]
} else if lobby {
vec![Scope::Lobby]
} else {
let mut targets = Vec::new();
if has_server || !has_client {
targets.push(Scope::Rune);
}
if has_client {
targets.push(Scope::Client);
}
if has_lobby {
targets.push(Scope::Lobby);
}
targets
};
runec::lsp::serve_workspace_with_loaders(
targets
.into_iter()
.map(|scope| {
let source = source.clone();
let pack = pack.clone();
let loader: Box<dyn Fn() -> Result<runec::project::Project, String>> =
Box::new(move || {
runec::filesystem::editor_project_for_scope(
&source,
pack.as_deref(),
scope,
)
.map_err(|error| error.to_string())
});
(scope, loader)
})
.collect(),
)?;
} else {
runec::lsp::serve(None)?;
}
}
Commands::CompileClient {
source,
output,
pack,
} => {
let compiled = clientscript::compile_directory(&source, &pack)?;
fs::create_dir_all(&output)?;
for (id, bytes) in compiled {
fs::write(output.join(format!("{id}.dat")), bytes)?;
}
}
Commands::Compile {
source,
output,
pack,
lint,
low_mem,
} => {
let scripts_dir = PathBuf::from(source);
let pack_dir = pack.map(PathBuf::from);
let output_dir = PathBuf::from(output);
runec::compile_with_options(
&scripts_dir,
pack_dir.as_deref(),
&output_dir,
lint,
low_mem,
)?;
}
Commands::Lint { source, pack } => {
let scripts_dir = PathBuf::from(source);
let pack_dir = pack.map(PathBuf::from);
runec::lint(&scripts_dir, pack_dir.as_deref())?;
}
Commands::CompileLobby {
source,
output,
pack,
lint,
low_mem,
} => {
let scripts_dir = PathBuf::from(source);
let pack_dir = pack.map(PathBuf::from);
let output_dir = PathBuf::from(output);
runec::compile_lobby_with_options(
&scripts_dir,
pack_dir.as_deref(),
&output_dir,
lint,
low_mem,
)?;
}
Commands::LintLobby { source, pack } => {
let scripts_dir = PathBuf::from(source);
let pack_dir = pack.map(PathBuf::from);
runec::lint_lobby(&scripts_dir, pack_dir.as_deref())?;
}
}
#[cfg(feature = "memprof")]
tracing::info!(
"[mem] PEAK HEAP {:.1} MB across {} allocations",
runec::memprof::peak_bytes() as f64 / 1_048_576.0,
runec::memprof::alloc_count(),
);
Ok(())
}