opy-rs 0.3.9

Standalone OverPy-compatible .opy implementation with parsing, tooling, and bounded Workshop compilation.
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
//! Settings compile-time expression coverage.

use std::collections::BTreeMap;
use std::path::Path;

use crate::hir::SettingsNode;
use crate::{CompileStatus, Compiler, compile_with_overlay};
use workshop_rs::catalog::{Catalog, Locale};
use workshop_rs::roundtrip::equivalent;
use workshop_rs::settings::{
    Applicability, SettingId, SettingScope, SettingTarget, SettingValue, definitions_by_id,
};

fn source_with_settings(settings: &str) -> String {
    format!("{settings}\nrule \"settings\":\n    @Event global\n    pass\n")
}

#[test]
fn external_settings_jsonc_uses_the_same_canonical_emitter() {
    let overlay = BTreeMap::from([(
        "settings.opy.json".to_string(),
        r#"{
    "gamemodes": {
        "general": {
            "heroLimit": "off",
            "respawnTime%": 30
        }
    },
    "heroes": {
        "allTeams": {
            "mei": { "health%": 200 }
        }
    }
}"#
        .to_string(),
    )]);
    let hir = crate::compile_with_overlay(
        "settings \"settings.opy.json\"\nrule \"settings\":\n    @Event global\n    pass\n",
        "main.opy",
        Path::new("."),
        &overlay,
    )
    .expect("external settings should compile through the frontend");
    let artifact = Compiler::new().unwrap().compile_hir(&hir).unwrap();
    assert!(artifact.emitted.contains("Hero Limit: Off"));
    assert!(artifact.emitted.contains("Health: 200%"));
}

#[test]
fn external_settings_diagnostics_use_the_external_file_registry_entry() {
    let overlay = BTreeMap::from([(
        "settings.opy.json".to_string(),
        "{\n    \"gamemodes\": [\n".to_string(),
    )]);
    let error = crate::compile_with_overlay(
        "settings \"settings.opy.json\"\nrule \"settings\":\n    @Event global\n    pass\n",
        "main.opy",
        Path::new("."),
        &overlay,
    )
    .expect_err("malformed external settings must retain its source boundary");
    assert_eq!(error.code, "settings-invalid");
    assert_eq!(error.span.expect("external settings span").file, 1);
}

#[test]
fn multiline_define_string_composition_resolves_in_settings() {
    let overlay = BTreeMap::from([
        (
            "constants.opy".to_string(),
            r##"#!define DESCRIPTION \
    "hello" \
    "world"
"##
            .to_string(),
        ),
        (
            "settings.opy".to_string(),
            r##"settings {
    "main": {
        "description": DESCRIPTION
    },
    "gamemodes": {}
}"##
            .to_string(),
        ),
    ]);
    let source = "#!include \"constants.opy\"\n#!include \"settings.opy\"\nrule \"settings\":\n    @Event global\n    pass\n";
    let hir = compile_with_overlay(source, "source.opy", Path::new("."), &overlay).unwrap();
    let settings = hir.settings.as_ref().unwrap();
    let main = match &settings.children[0] {
        SettingsNode::Group { children, .. } => children,
        other => panic!("expected main group, got {other:?}"),
    };
    assert!(matches!(
        &main[0],
        SettingsNode::String { value, .. } if value == "helloworld"
    ));
}

#[test]
fn object_defines_chain_and_string_composition_resolve_before_emission() {
    let source = source_with_settings(
        r##"#!define BASE 3
#!define SCALE(value) value*2
#!define DOUBLE SCALE(BASE)
#!define TITLE "hello"
#!define FULL_TITLE TITLE + " world"
settings {
    "main": {
        "description": FULL_TITLE
    },
    "gamemodes": {},
    "heroes": {
        "allTeams": {
            "dva": {
                "health%": DOUBLE
            }
        }
    }
}"##,
    );
    let hir = crate::compile(&source, "source.opy", Path::new(".")).unwrap();
    let settings = hir.settings.as_ref().unwrap();
    let main = match &settings.children[0] {
        SettingsNode::Group { children, .. } => children,
        other => panic!("expected main group, got {other:?}"),
    };
    assert!(matches!(
        &main[0],
        SettingsNode::String { value, .. } if value == "hello world"
    ));
    let heroes = match &settings.children[2] {
        SettingsNode::Group { children, .. } => children,
        other => panic!("expected heroes group, got {other:?}"),
    };
    let all_teams = match &heroes[0] {
        SettingsNode::Group { children, .. } => children,
        other => panic!("expected allTeams group, got {other:?}"),
    };
    let dva = match &all_teams[0] {
        SettingsNode::Group { children, .. } => children,
        other => panic!("expected dva group, got {other:?}"),
    };
    assert!(matches!(
        &dva[0],
        SettingsNode::Number { value, .. } if *value == 6.0
    ));

    let artifact = Compiler::new().unwrap().compile_hir(&hir).unwrap();
    assert!(artifact.emitted.contains("Description: \"hello world\""));
    assert!(artifact.emitted.contains("Health: 6%"));
    let parsed = workshop_rs::parser::parse(
        &artifact.emitted,
        &Catalog::builtin().unwrap(),
        &Locale::new("en-US"),
    )
    .unwrap();
    assert!(equivalent(&super::canonical_program(&artifact), &parsed));
}

#[test]
fn source_function_macro_and_compile_time_math_resolve_to_a_number() {
    let source = source_with_settings(
        r##"settings {
    "gamemodes": {},
    "heroes": {
        "allTeams": {
            "ana": {
                "health%": percent(1 + 1)
            }
        }
    }
}
macro percent(value):
    100 * value"##,
    );
    let hir = crate::compile(&source, "source.opy", Path::new(".")).unwrap();
    let artifact = Compiler::new().unwrap().compile_hir(&hir).unwrap();
    assert!(artifact.emitted.contains("Health: 200%"));
}

#[test]
fn settings_expression_failure_is_source_attributed_and_frontend_classified() {
    let source = source_with_settings(
        r##"settings {
    "gamemodes": {},
    "main": {
        "description": runtime_value
    }
}"##,
    );
    let report =
        Compiler::new()
            .unwrap()
            .compile_source_report(&source, "settings.opy", Path::new("."));
    assert_eq!(report.compile.status, CompileStatus::Failure);
    assert_eq!(
        report.compile.failure_class,
        Some(crate::CompileFailureClass::Frontend)
    );
    let diagnostic = report.compile.diagnostics.first().unwrap();
    assert_eq!(diagnostic.code, "settings-expression");
    assert_eq!(diagnostic.span.as_ref().unwrap().path, "settings.opy");
    assert_eq!(diagnostic.span.as_ref().unwrap().start.line, 4);
}

#[test]
fn settings_macro_failure_keeps_the_original_value_span() {
    let source = source_with_settings(
        r##"#!define LOOP LOOP
settings {
    "gamemodes": {},
    "main": {
        "description": LOOP
    }
}"##,
    );
    let report =
        Compiler::new()
            .unwrap()
            .compile_source_report(&source, "settings.opy", Path::new("."));
    assert_eq!(report.compile.status, CompileStatus::Failure);
    let diagnostic = report.compile.diagnostics.first().unwrap();
    assert_eq!(diagnostic.code, "macro-recursion");
    assert_eq!(diagnostic.span.as_ref().unwrap().path, "settings.opy");
    assert_eq!(diagnostic.span.as_ref().unwrap().start.line, 1);
}

#[test]
fn unresolved_expression_valued_setting_is_a_frontend_error() {
    let source = "settings {\n    \"lobby\": {\n        \"modeName\": GAMEMODE_NAME\" \"GAMEMODE_VERSION,\n    },\n    \"gamemodes\": {}\n}\nrule \"r\":\n    @Event global\n    pass\n";
    let error = crate::compile(source, "source.opy", Path::new(".")).unwrap_err();
    assert_eq!(error.code, "settings-expression");
    assert_eq!(error.span.unwrap().start.line, 3);
}

#[test]
fn hero_general_settings_are_emitted_at_team_scope() {
    let source = "settings {\n    \"gamemodes\": {},\n    \"heroes\": {\n        \"team1\": {\n            \"general\": {\n                \"damageReceived%\": 50\n            }\n        }\n    }\n}\nrule \"r\":\n    @Event global\n    pass\n";
    let hir = crate::compile(source, "source.opy", Path::new(".")).expect("settings resolve");
    let artifact = Compiler::new()
        .expect("compiler loads")
        .compile_hir(&hir)
        .expect("hero general settings must lower at team scope");
    assert!(artifact.emitted.contains("Team 1 {"));
    assert!(artifact.emitted.contains("Damage Received: 50%"));
}

#[test]
fn compiled_settings_are_queryable_through_the_canonical_consumer_api() {
    let source = source_with_settings(
        r#"settings {
    "gamemodes": {},
    "lobby": {
        "spectatorSlots": 2
    }
}"#,
    );
    let artifact = Compiler::new()
        .expect("released Workshop contract must load")
        .compile_source_artifact(&source, "settings.opy", Path::new("."))
        .expect("OPY settings must compile");

    let definition = definitions_by_id(&SettingId::from("setting.lobby.spectatorSlots"))
        .next()
        .expect("canonical lobby setting");
    assert_eq!(definition.scope(), SettingScope::Lobby);
    assert_eq!(
        definition.presentation().localized_name("en-US"),
        Some("Max Spectators")
    );
    assert!(definition.source().reviewed);
    assert_eq!(
        definition
            .applicability(&SettingTarget::Global)
            .expect("global lobby applicability"),
        Applicability::Applicable
    );

    let occurrence = definition
        .read(
            artifact.wir.settings.as_ref().expect("compiled settings"),
            &SettingTarget::Global,
        )
        .expect("compiled setting must be readable through the typed API");
    assert_eq!(occurrence.authored, SettingValue::Number(2.0));
    assert!(artifact.emitted.contains("Max Spectators: 2"));
}

#[test]
fn extension_directives_lower_as_canonical_flags_without_authored_settings() {
    let source = "#!extension beamEffects\n#!extension projectiles\nrule \"extensions\":\n    @Event global\n    pass\n";
    let artifact = Compiler::new()
        .expect("released Workshop contract must load")
        .compile_source_artifact(source, "extensions.opy", Path::new("."))
        .expect("extension directives must compile");

    let settings = artifact.wir.settings.as_ref().expect("generated settings");
    let workshop_rs::settings::SettingsNode::Group { children, .. } = &settings.children[0] else {
        panic!(
            "expected extensions group, got {:?}",
            settings.children[0].name()
        );
    };
    assert_eq!(
        children
            .iter()
            .map(workshop_rs::settings::SettingsNode::name)
            .collect::<Vec<_>>(),
        ["beamEffects", "projectiles"]
    );
    assert_eq!(
        children[0].span().expect("first directive span").start.line,
        1
    );
    assert_eq!(
        children[1]
            .span()
            .expect("second directive span")
            .start
            .line,
        2
    );
    assert!(artifact.emitted.contains("Beam Effects"));
    assert!(artifact.emitted.contains("Projectiles"));
}

#[test]
fn gamemode_kill_cam_compiles_into_the_deathmatch_block() {
    // opy-rs#411: `gamemodes.<mode>` inherits the `gamemodes.general`
    // settings the pinned OverPy merges into every mode schema, so
    // `enableKillCam` under `ffa` must reach the Deathmatch block instead of
    // dying as an outside-the-emission-table key.
    let source = source_with_settings(
        r#"settings {
    "main": {"description": "t"},
    "gamemodes": {"ffa": {"enabledMaps": ["workshopIsland"], "enableKillCam": false}}
}"#,
    );
    let artifact = Compiler::new()
        .expect("released Workshop contract must load")
        .compile_source_artifact(&source, "issue-411.opy", Path::new("."))
        .expect("inherited gamemode setting must compile");
    let deathmatch = artifact
        .emitted
        .find("Deathmatch {")
        .expect("ffa lowers to the Deathmatch mode block");
    let mode_block = &artifact.emitted[deathmatch..];
    let mode_end = mode_block
        .find("\n        }")
        .expect("the mode block closes at mode depth");
    assert!(
        mode_block[..mode_end].contains("Kill Cam: Off"),
        "Kill Cam must emit inside the Deathmatch block: {}",
        &mode_block[..mode_end]
    );
}

#[test]
fn extension_directives_merge_with_authored_settings_and_preserve_provenance() {
    let source = "#!extension projectiles\n#!extension beamEffects\n#!extension projectiles\nsettings {\n    \"main\": {\n        \"description\": \"authored\"\n    },\n    \"gamemodes\": {}\n}\nrule \"extensions\":\n    @Event global\n    pass\n";
    let artifact = Compiler::new()
        .expect("released Workshop contract must load")
        .compile_source_artifact(source, "extensions.opy", Path::new("."))
        .expect("extension directives must compile with authored settings");

    let settings = artifact.wir.settings.as_ref().expect("authored settings");
    let main = settings
        .children
        .iter()
        .find(|node| node.name() == "main")
        .expect("authored main settings");
    assert_eq!(main.span().expect("authored group span").start.line, 5);
    let workshop_rs::settings::SettingsNode::Group { children, .. } = main else {
        panic!("expected authored main group");
    };
    assert_eq!(
        children[0]
            .span()
            .expect("authored setting span")
            .start
            .line,
        6
    );

    let extensions = settings
        .children
        .iter()
        .find(|node| node.name() == "extensions")
        .expect("generated extensions group");
    let workshop_rs::settings::SettingsNode::Group { children, .. } = extensions else {
        panic!("expected generated extensions group");
    };
    assert_eq!(children.len(), 2);
    assert_eq!(
        children
            .iter()
            .map(workshop_rs::settings::SettingsNode::name)
            .collect::<Vec<_>>(),
        ["projectiles", "beamEffects"]
    );
    assert_eq!(children[0].span().expect("directive span").start.line, 1);
    assert!(artifact.emitted.contains("Description: \"authored\""));
    assert!(artifact.emitted.contains("Projectiles"));
}