concinnity-cook 0.18.66

Asset cook pipeline that bakes an authored Concinnity world into a blob
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
// src/world/defaults.rs
// Engine-default injection: complete a world with the standard assets it does
// not declare itself. A world that renders (has a GraphicsConfig after the
// first companion round) receives a DebugHud with its chip TextLabels and
// font, a StatHud with its chips when the world declares a MainMenu (the
// menu's performance-stats toggles drive them), a LoadingOverlay with its
// screen and progress bar when the world declares Scenes and a
// StreamingConfig, and, when an EnvironmentMap is present, the sky mesh that
// displays it. A world with physics content receives the PhysicsConfig its
// simulation already runs on, so those settings are visible rather than
// implicit.
//
// Override and opt-out rules:
//   - Declaring an asset of the same type pre-empts the whole default (an
//     authored StatHud suppresses the synthesized one; its unset label fields
//     are still filled in with default chips).
//   - Declaring an asset with a default's exact name and type replaces that
//     one piece (an authored `fps_chip` TextLabel restyles the fps chip).
//   - A default's name landing on an unrelated asset type is a hard error.
//   - An `EngineDefaults` asset turns individual defaults off entirely.

use super::expand::{ExpandReport, asset_name, type_norm};
use concinnity_asset::cook::EngineDefaults;

pub(crate) const HUD_FONT_NAME: &str = "hud_font";
const HUD_FONT_SIZE_PX: u32 = 20;

const PHYSICS_CONFIG_NAME: &str = "physics_config";

// The sky mesh must stay inside the camera far plane.
const SKY_SIZE_MAX: f32 = 400.0;
const SKY_FAR_FRACTION: f32 = 0.9;
const CAMERA_FAR_DEFAULT: f32 = 200.0;

// Consume every EngineDefaults entry and apply the enabled defaults to the
// world. Runs after the content expansion passes and the first companion round
// (so the GraphicsConfig render marker is present when implied) and before
// menu expansion (so the MainMenu lines that gate the StatHud are still
// visible).
pub(crate) fn inject_engine_defaults(
    assets: &mut Vec<serde_json::Value>,
    report: &mut ExpandReport,
) -> Result<(), String> {
    let toggles = drain_toggles(assets)?;
    let renders = assets.iter().any(|v| type_norm(v) == "graphicsconfig");

    if toggles.sky {
        inject_sky(assets, report)?;
    }
    // Physics is gated on content, not on rendering, so a headless world with
    // bodies gets its config too.
    if toggles.physics_config {
        inject_physics_config(assets, report)?;
    }
    if !renders {
        return Ok(());
    }
    // The StatHud exists to serve the menu's performance-stats toggles, so it
    // is synthesized only for worlds with a MainMenu; an authored StatHud
    // still gets its unset labels filled in anywhere.
    let has_menu = assets.iter().any(|v| type_norm(v) == "mainmenu");
    let has_stat_hud = assets.iter().any(|v| type_norm(v) == "stathud");
    if toggles.hud && (has_menu || has_stat_hud) {
        inject_hud(
            assets,
            report,
            "hud",
            "StatHud",
            "stat_hud",
            &[
                ("fps_label", "fps_chip"),
                ("vram_label", "vram_chip"),
                ("ram_label", "ram_chip"),
                ("ev_label", "ev_chip"),
                ("edr_label", "edr_chip"),
            ],
        )?;
    }
    if toggles.debug_hud {
        inject_hud(
            assets,
            report,
            "debug_hud",
            "DebugHud",
            "debug_hud",
            &[
                ("passes_label", "passes_chip"),
                ("mouse_label", "mouse_chip"),
                ("camera_label", "camera_chip"),
                ("sys_label", "sys_chip"),
            ],
        )?;
    }
    if toggles.loading_overlay {
        inject_loading_overlay(assets, report)?;
    }
    // A story world with no menu of its own gets an Escape-toggled pause menu.
    // Injected after the HUD defaults so it does not turn on the StatHud: the
    // trimmed pause menu carries no performance-stats toggles to drive it.
    if toggles.story_pause_menu {
        inject_story_pause_menu(assets, report)?;
    }
    Ok(())
}

// One LoadingOverlay ref field and the default UI piece that fills it. The
// pieces are named `loading_screen_*` so the screen-prefix rule also binds them
// to the overlay's screen.
struct LoadingPiece {
    field: &'static str,
    name: &'static str,
    asset_type: &'static str,
    args: serde_json::Value,
}

fn loading_overlay_pieces() -> [LoadingPiece; 5] {
    let piece = |field, name, asset_type, args| LoadingPiece {
        field,
        name,
        asset_type,
        args,
    };
    [
        piece(
            "screen",
            "loading_screen",
            "Screen",
            serde_json::json!({ "fade_in_secs": 0.15 }),
        ),
        piece(
            "backdrop",
            "loading_screen_backdrop",
            "Sprite",
            serde_json::json!({
                "x": 0, "y": 0, "width": 1280, "height": 720,
                "tint": [0.0, 0.0, 0.0, 1.0], "fit": "cover",
            }),
        ),
        piece(
            "track",
            "loading_screen_track",
            "Sprite",
            serde_json::json!({
                "x": 400, "y": 600, "width": 480, "height": 8,
                "tint": [0.25, 0.25, 0.25, 1.0], "corner_radius": 4,
            }),
        ),
        piece(
            "fill",
            "loading_screen_fill",
            "Sprite",
            serde_json::json!({
                "x": 400, "y": 600, "width": 0, "height": 8,
                "tint": [0.92, 0.92, 0.92, 1.0], "corner_radius": 4,
                "visible": false,
            }),
        ),
        piece(
            "label",
            "loading_screen_label",
            "TextLabel",
            serde_json::json!({
                "font": HUD_FONT_NAME, "content": "Loading",
                "x": 640, "y": 566, "align": "center",
            }),
        ),
    ]
}

// Synthesize the LoadingOverlay for a world that jumps between streamed
// scenes, fill its unset ref fields with the default piece names, and inject
// any pieces (and the label's font) those fields now reference but the world
// does not provide. An authored LoadingOverlay is completed the same way
// wherever it appears; one is synthesized only when the world declares Scenes
// and a StreamingConfig (without both there is nothing to wait for).
fn inject_loading_overlay(
    assets: &mut Vec<serde_json::Value>,
    report: &mut ExpandReport,
) -> Result<(), String> {
    let overlay_index = match assets.iter().position(|v| type_norm(v) == "loadingoverlay") {
        Some(i) => i,
        None => {
            let has_scene = assets.iter().any(|v| type_norm(v) == "scene");
            let has_streaming = assets.iter().any(|v| type_norm(v) == "streamingconfig");
            if !(has_scene && has_streaming) {
                return Ok(());
            }
            if name_claimed(
                assets,
                report,
                "loading_overlay",
                "loading_overlay",
                "LoadingOverlay",
                None,
            )? {
                // Unreachable in practice: a same-name same-type asset would
                // have matched the type scan above.
                return Ok(());
            }
            inject(
                assets,
                report,
                "loading_overlay",
                "loading_overlay",
                "LoadingOverlay",
                serde_json::json!({}),
            );
            assets.len() - 1
        }
    };

    // Fill unset ref fields on the overlay (authored or synthesized) and
    // collect the pieces those fields now reference.
    let mut needed: Vec<LoadingPiece> = Vec::new();
    {
        let overlay = &mut assets[overlay_index];
        if overlay.get("args").is_none() {
            overlay["args"] = serde_json::json!({});
        }
        let args = overlay
            .get_mut("args")
            .and_then(|a| a.as_object_mut())
            .ok_or_else(|| "LoadingOverlay: args must be an object".to_string())?;
        for piece in loading_overlay_pieces() {
            let unset = args
                .get(piece.field)
                .and_then(|v| v.as_str())
                .map(|s| s.is_empty())
                .unwrap_or(true);
            if unset {
                args.insert(piece.field.to_string(), serde_json::json!(piece.name));
                needed.push(piece);
            }
        }
    }

    // Keep the report's copy of a synthesized overlay in sync with the
    // filled-in ref fields, so the lock shows the args the blob carries.
    if let Some(entry) = report
        .injected
        .iter_mut()
        .find(|i| i.asset_type == "LoadingOverlay")
    {
        entry.args = assets[overlay_index]
            .get("args")
            .cloned()
            .unwrap_or_else(|| serde_json::json!({}));
    }

    let mut label_injected = false;
    for LoadingPiece {
        field,
        name: piece,
        asset_type: piece_type,
        args: piece_args,
    } in needed
    {
        if name_claimed(
            assets,
            report,
            "loading_overlay",
            piece,
            piece_type,
            Some(&piece_args),
        )? {
            continue;
        }
        inject(
            assets,
            report,
            "loading_overlay",
            piece,
            piece_type,
            piece_args,
        );
        label_injected |= field == "label";
    }

    if label_injected {
        let font_args = serde_json::json!({ "size_px": HUD_FONT_SIZE_PX });
        if !name_claimed(
            assets,
            report,
            "loading_overlay",
            HUD_FONT_NAME,
            "Font",
            Some(&font_args),
        )? {
            inject(
                assets,
                report,
                "loading_overlay",
                HUD_FONT_NAME,
                "Font",
                font_args,
            );
        }
    }
    Ok(())
}

// Give a story world an Escape pause menu (Resume / Save / Load / Settings /
// Quit) when it plays a Story but declares no MainMenu. The menu targets the
// story's own title screen for its Quit item and uses the trimmed settings
// profile: a 2D story renders no scene, so only window / output and volume are
// worth configuring. Runs after story expansion, so the compiled Story asset
// (named for the sanitized import) and its `<name>_title` screen are present.
fn inject_story_pause_menu(
    assets: &mut Vec<serde_json::Value>,
    report: &mut ExpandReport,
) -> Result<(), String> {
    // An authored MainMenu takes over the pause role; only inject when the
    // world declares none of its own.
    if assets.iter().any(|v| type_norm(v) == "mainmenu") {
        return Ok(());
    }
    let Some(prefix) = assets
        .iter()
        .find(|v| type_norm(v) == "story")
        .map(asset_name)
        .filter(|n| !n.is_empty())
    else {
        return Ok(());
    };

    let name = format!("{}_pause", prefix);
    // No MainMenu exists (checked above), so this can only flag a same-name
    // collision with an unrelated asset, which is a hard error rather than a
    // silent skip.
    if name_claimed(assets, report, "story_pause_menu", &name, "MainMenu", None)? {
        return Ok(());
    }

    // Main Menu returns to the story's own title screen; it is offered only
    // when the story has one (a story built without a title screen has nowhere
    // to return to, so its pause menu skips the item). Quit always exits to
    // desktop.
    let title_screen = format!("{}_title", prefix);
    let has_title = assets
        .iter()
        .any(|v| type_norm(v) == "screen" && asset_name(v) == title_screen);

    let mut items = vec![
        serde_json::json!({ "label": "Resume", "action": "story:pause" }),
        serde_json::json!({ "label": "Save", "action": "story:save" }),
        serde_json::json!({ "label": "Load", "action": "story:load" }),
        serde_json::json!({ "label": "Settings", "action": "story:settings" }),
    ];
    if has_title {
        items.push(serde_json::json!({
            "label": "Main Menu",
            "action": format!("screen:show:{}", title_screen),
        }));
    }
    items.push(serde_json::json!({ "label": "Quit", "action": "quit" }));

    // The story system drives the pause and settings navigation (Resume /
    // Settings / the settings Back), so closing returns to the stage instead of
    // dismissing to an empty screen. A translucent backdrop rather than the
    // opaque MainMenu default, so the menu reads as an overlay. `toggle_key` is
    // empty: Escape is a separate story-driven binding (below), not the
    // MainMenu's own screen toggle. `settings_back_action` both routes Back
    // through the story and forces the settings screen to be generated.
    let args = serde_json::json!({
        "toggle_key": "",
        "dim": [0.0, 0.0, 0.0, 0.6],
        "settings_profile": "minimal",
        "settings_back_action": "story:settings_back",
        "items": items,
    });
    inject(assets, report, "story_pause_menu", &name, "MainMenu", args);

    // Escape toggles the pause through the story system.
    inject(
        assets,
        report,
        "story_pause_menu",
        &format!("{}_key", name),
        "KeyBinding",
        serde_json::json!({ "key": "Escape", "action": "story:pause" }),
    );

    // Point the story at its pause menu and settings entry screens so the story
    // system can show them. Those screens are generated later by the menu
    // expansion; the names are stable and intern to the same ids.
    patch_story_scaffold(assets, prefix.as_str(), &name);
    Ok(())
}

// Add the injected pause menu's screen (`<menu>`) and settings entry screen
// (`<menu>_settings_video`) to the Story asset's scaffold, so the story system
// resolves them like every other stage reference.
fn patch_story_scaffold(assets: &mut [serde_json::Value], story_name: &str, menu_name: &str) {
    let pause_screen = menu_name.to_string();
    let settings_screen = format!("{}_settings_video", menu_name);
    for v in assets.iter_mut() {
        if type_norm(v) != "story" || asset_name(v) != story_name {
            continue;
        }
        // Best-effort: a build-generated Story always carries an object args +
        // scaffold, but a hand-authored one might be malformed. Skip the patch
        // rather than panic; the malformed Story surfaces its own error at
        // validation / compile.
        let Some(args) = v.get_mut("args").and_then(|a| a.as_object_mut()) else {
            return;
        };
        let Some(scaffold) = args
            .entry("scaffold")
            .or_insert_with(|| serde_json::json!({}))
            .as_object_mut()
        else {
            return;
        };
        scaffold.insert("pause".to_string(), serde_json::json!(pause_screen));
        scaffold.insert("settings".to_string(), serde_json::json!(settings_screen));
        return;
    }
}

// Remove EngineDefaults entries from the world (they are build directives, not
// runtime assets) and merge them into one set of toggles. More than one entry
// is ambiguous and rejected.
fn drain_toggles(assets: &mut Vec<serde_json::Value>) -> Result<EngineDefaults, String> {
    let mut found: Option<(String, EngineDefaults)> = None;
    let mut result = Vec::with_capacity(assets.len());
    for value in assets.drain(..) {
        if type_norm(&value) != "enginedefaults" {
            result.push(value);
            continue;
        }
        let name = asset_name(&value);
        if let Some((first, _)) = &found {
            return Err(format!(
                "EngineDefaults '{}': the world already declares EngineDefaults '{}'; \
                 declare at most one",
                name, first
            ));
        }
        let args = value
            .get("args")
            .cloned()
            .unwrap_or_else(|| serde_json::json!({}));
        let toggles: EngineDefaults = serde_json::from_value(args)
            .map_err(|e| format!("EngineDefaults '{}': invalid args: {}", name, e))?;
        found = Some((name, toggles));
    }
    *assets = result;
    Ok(found.map(|(_, t)| t).unwrap_or_default())
}

// Whether the world already provides `name` as an asset of `asset_type` (the
// user's line is a patch of the default: the default's args are merged under
// it, so it overrides exactly the fields it names). A claimed name is recorded
// as a shadow, so a listing can show the default the world overrides rather
// than leaving it unaccounted for. A name held by a different type is a hard
// error: the default cannot be injected and silently skipping it would hide
// the conflict. `default_args` is the default that would have been injected;
// None marks call sites where a same-type claim is unreachable.
fn name_claimed(
    assets: &mut [serde_json::Value],
    report: &mut ExpandReport,
    injected_by: &'static str,
    name: &str,
    asset_type: &str,
    default_args: Option<&serde_json::Value>,
) -> Result<bool, String> {
    let Some(claim) = assets.iter().find(|v| asset_name(v) == name) else {
        return Ok(false);
    };
    if type_norm(claim) != asset_type.to_lowercase().replace('_', "") {
        return Err(format!(
            "engine default '{}' ({}) collides with your {} asset of the same name; \
             rename that asset or disable the default with an EngineDefaults entry",
            name,
            asset_type,
            claim.get("type").and_then(|t| t.as_str()).unwrap_or("?"),
        ));
    }
    let template = default_args.cloned().unwrap_or(serde_json::json!({}));
    report.record_shadowed(name, asset_type, injected_by, template.clone());
    if default_args.is_some() {
        super::shadow::merge_into_authored(assets, name, &template);
    }
    Ok(true)
}

// Push one injected asset and record it in the report.
fn inject(
    assets: &mut Vec<serde_json::Value>,
    report: &mut ExpandReport,
    injected_by: &'static str,
    name: &str,
    asset_type: &str,
    args: serde_json::Value,
) {
    assets.push(serde_json::json!({
        "name": name,
        "type": asset_type,
        "args": args.clone(),
    }));
    report.record(name, asset_type, args, injected_by);
}

// The chip TextLabel every injected HUD readout writes into.
fn chip_args() -> serde_json::Value {
    serde_json::json!({
        "font": HUD_FONT_NAME,
        "scale": 0.7,
        "color": [1, 1, 1],
        "background": [0.0, 0.18, 0.32, 0.85],
        "padding": 5,
    })
}

// Synthesize a HUD asset when the world declares none, fill its unset label
// fields with the default chip names, and inject any chips (and their font)
// those fields now reference but the world does not provide.
fn inject_hud(
    assets: &mut Vec<serde_json::Value>,
    report: &mut ExpandReport,
    injected_by: &'static str,
    hud_type: &str,
    default_name: &str,
    fields: &[(&str, &str)],
) -> Result<(), String> {
    let hud_type_norm = hud_type.to_lowercase().replace('_', "");
    let hud_index = match assets.iter().position(|v| type_norm(v) == hud_type_norm) {
        Some(i) => i,
        None => {
            if name_claimed(assets, report, injected_by, default_name, hud_type, None)? {
                // Unreachable in practice: a same-name same-type asset would
                // have matched the type scan above.
                return Ok(());
            }
            inject(
                assets,
                report,
                injected_by,
                default_name,
                hud_type,
                serde_json::json!({}),
            );
            assets.len() - 1
        }
    };

    // Fill unset label fields on the HUD (authored or synthesized) and collect
    // the chip names those fields now reference.
    let mut needed_chips: Vec<&str> = Vec::new();
    {
        let hud = &mut assets[hud_index];
        if hud.get("args").is_none() {
            hud["args"] = serde_json::json!({});
        }
        let args = hud
            .get_mut("args")
            .and_then(|a| a.as_object_mut())
            .ok_or_else(|| format!("{} '{}': args must be an object", hud_type, default_name))?;
        for (field, chip) in fields {
            let unset = args
                .get(*field)
                .and_then(|v| v.as_str())
                .map(|s| s.is_empty())
                .unwrap_or(true);
            if unset {
                args.insert(field.to_string(), serde_json::json!(chip));
                needed_chips.push(chip);
            }
        }
    }

    // Keep the report's copy of a synthesized HUD in sync with the filled-in
    // label fields, so the lock shows the args the blob actually carries.
    if let Some(entry) = report
        .injected
        .iter_mut()
        .find(|i| i.name == default_name && i.asset_type == hud_type)
    {
        entry.args = assets[hud_index]
            .get("args")
            .cloned()
            .unwrap_or_else(|| serde_json::json!({}));
    }

    let mut chip_injected = false;
    let chip_defaults = chip_args();
    for chip in needed_chips {
        if name_claimed(
            assets,
            report,
            injected_by,
            chip,
            "TextLabel",
            Some(&chip_defaults),
        )? {
            continue;
        }
        inject(assets, report, injected_by, chip, "TextLabel", chip_args());
        chip_injected = true;
    }

    if chip_injected {
        let font_args = serde_json::json!({ "size_px": HUD_FONT_SIZE_PX });
        if !name_claimed(
            assets,
            report,
            injected_by,
            HUD_FONT_NAME,
            "Font",
            Some(&font_args),
        )? {
            inject(
                assets,
                report,
                injected_by,
                HUD_FONT_NAME,
                "Font",
                font_args,
            );
        }
    }
    Ok(())
}

// The sky mesh that displays an EnvironmentMap: an inside-out skybox cube plus
// the Prop that draws it. Injected only when the world has an EnvironmentMap
// and no skybox-generator mesh of its own. The half-extent tracks the first
// camera's far plane (sky depth is pinned to the far plane, so the mesh only
// needs to enclose the camera while staying inside it).
fn inject_sky(
    assets: &mut Vec<serde_json::Value>,
    report: &mut ExpandReport,
) -> Result<(), String> {
    if !assets.iter().any(|v| type_norm(v) == "environmentmap") {
        return Ok(());
    }
    let has_sky_mesh = assets.iter().any(|v| {
        type_norm(v) == "proceduralmesh"
            && v.get("args")
                .and_then(|a| a.get("generator"))
                .and_then(|g| g.as_str())
                == Some("skybox")
    });
    if has_sky_mesh {
        return Ok(());
    }

    let far = assets
        .iter()
        .find(|v| type_norm(v) == "camera3d")
        .and_then(|v| v.get("args"))
        .and_then(|a| a.get("far"))
        .and_then(|f| f.as_f64())
        .map(|f| f as f32)
        .unwrap_or(CAMERA_FAR_DEFAULT);
    let size = (far * SKY_FAR_FRACTION).min(SKY_SIZE_MAX);

    let mesh_args = serde_json::json!({ "generator": "skybox", "size": size });
    let mesh_claimed = name_claimed(
        assets,
        report,
        "sky",
        "sky_mesh",
        "ProceduralMesh",
        Some(&mesh_args),
    )?;
    if !mesh_claimed {
        inject(
            assets,
            report,
            "sky",
            "sky_mesh",
            "ProceduralMesh",
            mesh_args,
        );
    }
    let mat_args =
        serde_json::json!({ "roughness": 1.0, "metallic": 0.0, "tint": [1.0, 1.0, 1.0] });
    if !name_claimed(
        assets,
        report,
        "sky",
        "mat_sky",
        "Material",
        Some(&mat_args),
    )? {
        inject(assets, report, "sky", "mat_sky", "Material", mat_args);
    }
    let prop_args = serde_json::json!({
        "mesh": "sky_mesh",
        "material": "mat_sky",
        "position": [0.0, 0.0, 0.0],
    });
    if !name_claimed(assets, report, "sky", "sky", "Prop", Some(&prop_args))? {
        inject(assets, report, "sky", "sky", "Prop", prop_args);
    }
    Ok(())
}

// Whether the world runs physics, mirroring the engine's schedule gate: a
// PhysicsConfig, a RigidBody, a PropBody, a TriggerVolume, or a SkinnedMesh
// that declared a character capsule.
fn has_physics_content(assets: &[serde_json::Value]) -> bool {
    assets.iter().any(|v| match type_norm(v).as_str() {
        "physicsconfig" | "rigidbody" | "propbody" | "triggervolume" => true,
        "skinnedmesh" => v
            .get("args")
            .and_then(|a| a.get("capsule"))
            .is_some_and(|c| !c.is_null()),
        _ => false,
    })
}

// Give a world with physics content the PhysicsConfig it does not declare. The
// simulation already runs on these values; injecting them puts the settings in
// world-lock.json where they can be read and overridden.
fn inject_physics_config(
    assets: &mut Vec<serde_json::Value>,
    report: &mut ExpandReport,
) -> Result<(), String> {
    if assets.iter().any(|v| type_norm(v) == "physicsconfig") || !has_physics_content(assets) {
        return Ok(());
    }
    // Serialized from the type, so the injected settings cannot drift from the
    // values the runtime falls back to.
    let args = serde_json::to_value(crate::components::PhysicsConfig::default())
        .map_err(|e| format!("PhysicsConfig: default args are not representable: {}", e))?;
    if name_claimed(
        assets,
        report,
        "physics_config",
        PHYSICS_CONFIG_NAME,
        "PhysicsConfig",
        None,
    )? {
        // Unreachable in practice: a same-name same-type asset would have
        // matched the type scan above.
        return Ok(());
    }
    inject(
        assets,
        report,
        "physics_config",
        PHYSICS_CONFIG_NAME,
        "PhysicsConfig",
        args,
    );
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn world(lines: &[serde_json::Value]) -> Vec<serde_json::Value> {
        lines.to_vec()
    }

    fn gfx() -> serde_json::Value {
        serde_json::json!({"name":"gfx","type":"GraphicsConfig","args":{}})
    }

    fn names_of_type(assets: &[serde_json::Value], t: &str) -> Vec<String> {
        assets
            .iter()
            .filter(|v| type_norm(v) == t)
            .map(asset_name)
            .collect()
    }

    #[test]
    fn non_rendering_world_gets_no_defaults() {
        let mut assets = world(&[serde_json::json!({"name":"w","type":"Window","args":{}})]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        assert_eq!(assets.len(), 1);
        assert!(report.injected.is_empty());
    }

    #[test]
    fn rendering_world_gets_debug_hud_but_no_menu_or_stat_hud() {
        let mut assets = world(&[gfx()]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();

        // No MainMenu is ever injected, and without one there is no StatHud.
        assert!(names_of_type(&assets, "mainmenu").is_empty());
        assert!(names_of_type(&assets, "stathud").is_empty());
        assert_eq!(names_of_type(&assets, "debughud"), vec!["debug_hud"]);
        let chips = names_of_type(&assets, "textlabel");
        for chip in ["passes_chip", "mouse_chip", "camera_chip", "sys_chip"] {
            assert!(chips.contains(&chip.to_string()), "missing {chip}");
        }
        assert!(!chips.contains(&"fps_chip".to_string()));
        assert_eq!(names_of_type(&assets, "font"), vec![HUD_FONT_NAME]);
        assert_eq!(report.injected.len(), 1 + 4 + 1);
    }

    #[test]
    fn main_menu_world_also_gets_the_stat_hud_set() {
        let mut assets = world(&[
            gfx(),
            serde_json::json!({"name":"pause","type":"MainMenu","args":{}}),
        ]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();

        assert_eq!(names_of_type(&assets, "mainmenu"), vec!["pause"]);
        assert_eq!(names_of_type(&assets, "stathud"), vec!["stat_hud"]);
        assert_eq!(names_of_type(&assets, "debughud"), vec!["debug_hud"]);
        let chips = names_of_type(&assets, "textlabel");
        for chip in [
            "fps_chip",
            "vram_chip",
            "ram_chip",
            "ev_chip",
            "edr_chip",
            "passes_chip",
            "mouse_chip",
            "camera_chip",
            "sys_chip",
        ] {
            assert!(chips.contains(&chip.to_string()), "missing {chip}");
        }
        assert_eq!(names_of_type(&assets, "font"), vec![HUD_FONT_NAME]);
        assert_eq!(report.injected.len(), 2 + 9 + 1);
    }

    #[test]
    fn authored_stat_hud_gets_unset_labels_filled() {
        let mut assets = world(&[
            gfx(),
            serde_json::json!({"name":"hud","type":"StatHud","args":{"fps_label":"my_fps"}}),
            serde_json::json!({"name":"my_fps","type":"TextLabel","args":{"font":"hud_font"}}),
        ]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();

        let hud = assets.iter().find(|v| type_norm(v) == "stathud").unwrap();
        assert_eq!(hud["args"]["fps_label"], serde_json::json!("my_fps"));
        assert_eq!(hud["args"]["vram_label"], serde_json::json!("vram_chip"));
        let chips = names_of_type(&assets, "textlabel");
        assert!(chips.contains(&"vram_chip".to_string()));
        assert!(!chips.contains(&"fps_chip".to_string()));
    }

    #[test]
    fn authored_chip_replaces_the_injected_one() {
        let mut assets = world(&[
            gfx(),
            serde_json::json!({"name":"menu","type":"MainMenu","args":{}}),
            serde_json::json!({"name":"fps_chip","type":"TextLabel","args":{"scale":1.4}}),
        ]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        let fps_chips: Vec<_> = assets
            .iter()
            .filter(|v| asset_name(v) == "fps_chip")
            .collect();
        assert_eq!(fps_chips.len(), 1);
        assert_eq!(fps_chips[0]["args"]["scale"], serde_json::json!(1.4));
    }

    #[test]
    fn default_name_on_unrelated_type_is_an_error() {
        let mut assets = world(&[
            gfx(),
            serde_json::json!({"name":"debug_hud","type":"Window","args":{}}),
        ]);
        let mut report = ExpandReport::default();
        let err = inject_engine_defaults(&mut assets, &mut report).unwrap_err();
        assert!(err.contains("debug_hud"));
        assert!(err.contains("EngineDefaults"));
    }

    #[test]
    fn engine_defaults_flags_opt_out() {
        let mut assets = world(&[
            gfx(),
            serde_json::json!({"name":"menu","type":"MainMenu","args":{}}),
            serde_json::json!({"name":"d","type":"EngineDefaults","args":{
                "hud": false, "debug_hud": false
            }}),
        ]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        assert!(names_of_type(&assets, "stathud").is_empty());
        assert!(names_of_type(&assets, "debughud").is_empty());
        // The directive itself never reaches the compiled world.
        assert!(names_of_type(&assets, "enginedefaults").is_empty());
    }

    #[test]
    fn duplicate_engine_defaults_is_an_error() {
        let mut assets = world(&[
            serde_json::json!({"name":"a","type":"EngineDefaults","args":{}}),
            serde_json::json!({"name":"b","type":"EngineDefaults","args":{}}),
        ]);
        let mut report = ExpandReport::default();
        let err = inject_engine_defaults(&mut assets, &mut report).unwrap_err();
        assert!(err.contains("at most one"));
    }

    #[test]
    fn environment_map_gets_a_sky_mesh() {
        let mut assets = world(&[
            gfx(),
            serde_json::json!({"name":"env","type":"EnvironmentMap","args":{"generator":"sky"}}),
        ]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        assert_eq!(names_of_type(&assets, "proceduralmesh"), vec!["sky_mesh"]);
        assert_eq!(names_of_type(&assets, "material"), vec!["mat_sky"]);
        assert_eq!(names_of_type(&assets, "prop"), vec!["sky"]);
    }

    #[test]
    fn sky_size_tracks_camera_far_and_caps_at_showcase_value() {
        let mut assets = world(&[
            gfx(),
            serde_json::json!({"name":"cam","type":"Camera3D","args":{"far":100.0}}),
            serde_json::json!({"name":"env","type":"EnvironmentMap","args":{"generator":"sky"}}),
        ]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        let mesh = assets
            .iter()
            .find(|v| type_norm(v) == "proceduralmesh")
            .unwrap();
        assert_eq!(mesh["args"]["size"], serde_json::json!(90.0));

        let mut assets = world(&[
            gfx(),
            serde_json::json!({"name":"cam","type":"Camera3D","args":{"far":900.0}}),
            serde_json::json!({"name":"env","type":"EnvironmentMap","args":{"generator":"sky"}}),
        ]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        let mesh = assets
            .iter()
            .find(|v| type_norm(v) == "proceduralmesh")
            .unwrap();
        assert_eq!(mesh["args"]["size"], serde_json::json!(400.0));
    }

    #[test]
    fn authored_skybox_mesh_suppresses_sky_injection() {
        let mut assets = world(&[
            gfx(),
            serde_json::json!({"name":"env","type":"EnvironmentMap","args":{"generator":"sky"}}),
            serde_json::json!({"name":"dome","type":"ProceduralMesh","args":{"generator":"skybox","size":250.0}}),
        ]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        assert_eq!(names_of_type(&assets, "proceduralmesh"), vec!["dome"]);
        assert!(names_of_type(&assets, "prop").is_empty());
    }

    // Turning the sky default off leaves an EnvironmentMap as image-based
    // lighting only, with no mesh drawn for it.
    #[test]
    fn engine_defaults_opts_out_of_the_sky() {
        let mut assets = world(&[
            gfx(),
            serde_json::json!({"name":"env","type":"EnvironmentMap","args":{"generator":"sky"}}),
            serde_json::json!({"name":"d","type":"EngineDefaults","args":{"sky": false}}),
        ]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        assert!(names_of_type(&assets, "proceduralmesh").is_empty());
        assert!(names_of_type(&assets, "prop").is_empty());
        assert!(names_of_type(&assets, "material").is_empty());
    }

    #[test]
    fn no_environment_map_means_no_sky() {
        let mut assets = world(&[gfx()]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        assert!(names_of_type(&assets, "proceduralmesh").is_empty());
    }

    #[test]
    fn story_world_injects_a_pause_menu() {
        let mut assets = world(&[
            gfx(),
            serde_json::json!({"name":"tale","type":"Story","args":{}}),
            serde_json::json!({"name":"tale_title","type":"Screen","args":{}}),
        ]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();

        let menu = assets
            .iter()
            .find(|v| type_norm(v) == "mainmenu")
            .expect("pause menu injected");
        assert_eq!(asset_name(menu), "tale_pause");
        assert_eq!(menu["args"]["settings_profile"], "minimal");
        // The story system drives the pause: no MainMenu screen toggle, and Back
        // routes through the story so it returns to whichever menu opened it.
        assert_eq!(menu["args"]["toggle_key"], "");
        assert_eq!(menu["args"]["settings_back_action"], "story:settings_back");
        // Translucent backdrop, not the opaque MainMenu default.
        let alpha = menu["args"]["dim"][3].as_f64().unwrap();
        assert!(
            alpha > 0.0 && alpha < 1.0,
            "pause dim should be translucent"
        );

        let items = menu["args"]["items"].as_array().unwrap();
        let actions: Vec<&str> = items
            .iter()
            .map(|i| i["action"].as_str().unwrap())
            .collect();
        for action in [
            "story:pause",
            "story:save",
            "story:load",
            "story:settings",
            // Main Menu (returns to the story's own title screen) and Quit are
            // now separate items.
            "screen:show:tale_title",
            "quit",
        ] {
            assert!(actions.contains(&action), "missing pause action {action}");
        }
        // Main Menu sits above Quit, and Quit is last.
        let labels: Vec<&str> = items.iter().map(|i| i["label"].as_str().unwrap()).collect();
        assert_eq!(labels.last(), Some(&"Quit"));
        let main_menu = labels.iter().position(|l| *l == "Main Menu").unwrap();
        let quit = labels.iter().position(|l| *l == "Quit").unwrap();
        assert!(main_menu < quit, "Main Menu should sit above Quit");

        // Escape is a story-driven binding, not the MainMenu's own toggle.
        let key = assets
            .iter()
            .find(|v| type_norm(v) == "keybinding" && asset_name(v) == "tale_pause_key")
            .expect("Escape binding injected");
        assert_eq!(key["args"]["key"], "Escape");
        assert_eq!(key["args"]["action"], "story:pause");

        // The Story scaffold points at the pause + settings screens.
        let story = assets.iter().find(|v| type_norm(v) == "story").unwrap();
        assert_eq!(story["args"]["scaffold"]["pause"], "tale_pause");
        assert_eq!(
            story["args"]["scaffold"]["settings"],
            "tale_pause_settings_video"
        );

        // The trimmed menu drives no StatHud, so none is injected for a story.
        assert!(names_of_type(&assets, "stathud").is_empty());
    }

    #[test]
    fn story_without_a_title_screen_quits_to_desktop() {
        let mut assets = world(&[
            gfx(),
            serde_json::json!({"name":"tale","type":"Story","args":{}}),
        ]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        let menu = assets
            .iter()
            .find(|v| type_norm(v) == "mainmenu")
            .expect("pause menu injected");
        let items = menu["args"]["items"].as_array().unwrap();
        let quit = items.last().unwrap();
        assert_eq!(quit["action"], "quit");
        // With no title screen there is no Main Menu item to return to.
        let labels: Vec<&str> = items.iter().map(|i| i["label"].as_str().unwrap()).collect();
        assert!(
            !labels.contains(&"Main Menu"),
            "no title screen -> no Main Menu item"
        );
    }

    #[test]
    fn authored_menu_suppresses_the_story_pause_menu() {
        let mut assets = world(&[
            gfx(),
            serde_json::json!({"name":"tale","type":"Story","args":{}}),
            serde_json::json!({"name":"tale_title","type":"Screen","args":{}}),
            serde_json::json!({"name":"my_menu","type":"MainMenu","args":{}}),
        ]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        assert_eq!(names_of_type(&assets, "mainmenu"), vec!["my_menu"]);
    }

    #[test]
    fn engine_defaults_opts_out_of_the_story_pause_menu() {
        let mut assets = world(&[
            gfx(),
            serde_json::json!({"name":"tale","type":"Story","args":{}}),
            serde_json::json!({"name":"tale_title","type":"Screen","args":{}}),
            serde_json::json!({"name":"d","type":"EngineDefaults","args":{
                "story_pause_menu": false
            }}),
        ]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        assert!(names_of_type(&assets, "mainmenu").is_empty());
    }

    #[test]
    fn malformed_story_args_do_not_panic_the_pause_injection() {
        // A hand-authored Story with a non-object args is malformed, but the
        // pause injection must skip the scaffold patch gracefully rather than
        // panic; the malformed Story surfaces its own error later.
        let mut assets = world(&[
            gfx(),
            serde_json::json!({"name":"tale","type":"Story","args":[]}),
            serde_json::json!({"name":"tale_title","type":"Screen","args":{}}),
        ]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        // The menu still injects; only the scaffold patch was skipped.
        assert!(assets.iter().any(|v| type_norm(v) == "mainmenu"));
    }

    #[test]
    fn non_story_world_gets_no_pause_menu() {
        let mut assets = world(&[gfx()]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        assert!(names_of_type(&assets, "mainmenu").is_empty());
    }

    // Every sky piece yields to an asset the world already declares under that
    // name, and the pieces it does not declare are still injected.
    #[test]
    fn authored_sky_pieces_are_kept_and_the_rest_injected() {
        let mut assets = world(&[
            gfx(),
            serde_json::json!({"name":"env","type":"EnvironmentMap","args":{"generator":"sky"}}),
            serde_json::json!({"name":"sky_mesh","type":"ProceduralMesh","args":{"generator":"cube"}}),
            serde_json::json!({"name":"mat_sky","type":"Material","args":{"roughness":0.2}}),
        ]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();

        // The authored mesh and material are untouched; only the Prop is added.
        let mesh = assets.iter().find(|v| asset_name(v) == "sky_mesh").unwrap();
        assert_eq!(mesh["args"]["generator"], "cube");
        let mat = assets.iter().find(|v| asset_name(v) == "mat_sky").unwrap();
        assert_eq!(mat["args"]["roughness"], 0.2);
        assert_eq!(names_of_type(&assets, "prop"), vec!["sky"]);
        // Both overrides are accounted for rather than silently dropped.
        let shadowed: Vec<&str> = report.shadowed.iter().map(|s| s.name.as_str()).collect();
        assert!(shadowed.contains(&"sky_mesh"), "{shadowed:?}");
        assert!(shadowed.contains(&"mat_sky"), "{shadowed:?}");
    }

    #[test]
    fn authored_sky_prop_suppresses_the_injected_one() {
        let mut assets = world(&[
            gfx(),
            serde_json::json!({"name":"env","type":"EnvironmentMap","args":{"generator":"sky"}}),
            serde_json::json!({"name":"sky","type":"Prop","args":{"mesh":"mine"}}),
        ]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        let props = names_of_type(&assets, "prop");
        assert_eq!(props, vec!["sky"]);
        let prop = assets.iter().find(|v| asset_name(v) == "sky").unwrap();
        assert_eq!(prop["args"]["mesh"], "mine");
    }

    // Each sky name landing on an unrelated type is a hard error rather than a
    // silently skipped default.
    #[test]
    fn a_sky_name_held_by_another_type_is_an_error() {
        for name in ["sky_mesh", "mat_sky", "sky"] {
            let mut assets = world(&[
                gfx(),
                serde_json::json!({"name":"env","type":"EnvironmentMap","args":{"generator":"sky"}}),
                serde_json::json!({"name": name, "type":"Window","args":{}}),
            ]);
            let mut report = ExpandReport::default();
            let err = inject_engine_defaults(&mut assets, &mut report).unwrap_err();
            assert!(err.contains(name), "{name}: {err}");
            assert!(err.contains("Window"), "{name}: {err}");
        }
    }

    // A chip or the HUD font held by an unrelated type is a hard error too.
    #[test]
    fn a_hud_chip_or_font_name_held_by_another_type_is_an_error() {
        for name in ["passes_chip", HUD_FONT_NAME] {
            let mut assets = world(&[
                gfx(),
                serde_json::json!({"name": name, "type":"Window","args":{}}),
            ]);
            let mut report = ExpandReport::default();
            let err = inject_engine_defaults(&mut assets, &mut report).unwrap_err();
            assert!(err.contains(name), "{name}: {err}");
        }
    }

    // A story world whose pause-menu name is already taken by another type
    // cannot inject the menu, and skipping it would hide the clash.
    #[test]
    fn a_pause_menu_name_held_by_another_type_is_an_error() {
        let mut assets = world(&[
            gfx(),
            serde_json::json!({"name":"tale","type":"Story","args":{}}),
            serde_json::json!({"name":"tale_pause","type":"Window","args":{}}),
        ]);
        let mut report = ExpandReport::default();
        let err = inject_engine_defaults(&mut assets, &mut report).unwrap_err();
        assert!(err.contains("tale_pause"), "{err}");
    }

    // An authored HUD carrying no args at all still gets its label fields.
    #[test]
    fn an_authored_hud_without_args_is_filled_in() {
        let mut assets = world(&[gfx(), serde_json::json!({"name":"hud","type":"StatHud"})]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        let hud = assets.iter().find(|v| type_norm(v) == "stathud").unwrap();
        assert_eq!(hud["args"]["fps_label"], "fps_chip");
        assert!(names_of_type(&assets, "textlabel").contains(&"fps_chip".to_string()));
    }

    #[test]
    fn a_hud_with_non_object_args_is_an_error() {
        let mut assets = world(&[
            gfx(),
            serde_json::json!({"name":"hud","type":"StatHud","args":[]}),
        ]);
        let mut report = ExpandReport::default();
        let err = inject_engine_defaults(&mut assets, &mut report).unwrap_err();
        assert!(err.contains("args must be an object"), "{err}");
    }

    // An EngineDefaults entry with no args is the all-defaults directive.
    #[test]
    fn engine_defaults_without_args_keeps_every_default_on() {
        let mut assets = world(&[
            gfx(),
            serde_json::json!({"name":"d","type":"EngineDefaults"}),
        ]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        assert_eq!(names_of_type(&assets, "debughud"), vec!["debug_hud"]);
    }

    #[test]
    fn malformed_engine_defaults_args_are_an_error() {
        let mut assets = world(&[serde_json::json!({
            "name":"d","type":"EngineDefaults","args":{"hud":"yes"}
        })]);
        let mut report = ExpandReport::default();
        let err = inject_engine_defaults(&mut assets, &mut report).unwrap_err();
        assert!(err.contains("EngineDefaults 'd'"), "{err}");
        assert!(err.contains("invalid args"), "{err}");
    }

    // A Story whose `scaffold` is not an object is malformed; the patch is
    // skipped rather than replacing what the author wrote.
    #[test]
    fn a_non_object_scaffold_is_left_alone() {
        let mut assets = vec![serde_json::json!({
            "name":"tale","type":"Story","args":{"scaffold": 7}
        })];
        patch_story_scaffold(&mut assets, "tale", "tale_pause");
        assert_eq!(assets[0]["args"]["scaffold"], 7);
    }

    fn scene_and_streaming() -> [serde_json::Value; 2] {
        [
            serde_json::json!({"name":"town","type":"Scene","args":{}}),
            serde_json::json!({"name":"stream","type":"StreamingConfig","args":{}}),
        ]
    }

    #[test]
    fn scene_streaming_world_gets_the_loading_overlay_set() {
        let [scene, streaming] = scene_and_streaming();
        let mut assets = world(&[gfx(), scene, streaming]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();

        assert_eq!(
            names_of_type(&assets, "loadingoverlay"),
            vec!["loading_overlay"]
        );
        assert_eq!(names_of_type(&assets, "screen"), vec!["loading_screen"]);
        let sprites = names_of_type(&assets, "sprite");
        for piece in [
            "loading_screen_backdrop",
            "loading_screen_track",
            "loading_screen_fill",
        ] {
            assert!(sprites.contains(&piece.to_string()), "missing {piece}");
        }
        let labels = names_of_type(&assets, "textlabel");
        assert!(labels.contains(&"loading_screen_label".to_string()));
        assert_eq!(names_of_type(&assets, "font"), vec![HUD_FONT_NAME]);

        // The overlay's ref fields point at the injected pieces.
        let overlay = assets
            .iter()
            .find(|v| type_norm(v) == "loadingoverlay")
            .unwrap();
        assert_eq!(overlay["args"]["screen"], "loading_screen");
        assert_eq!(overlay["args"]["fill"], "loading_screen_fill");
        // The lock's copy carries the filled-in refs.
        let entry = report
            .injected
            .iter()
            .find(|i| i.asset_type == "LoadingOverlay")
            .unwrap();
        assert_eq!(entry.args["backdrop"], "loading_screen_backdrop");
    }

    #[test]
    fn no_scenes_or_no_streaming_means_no_loading_overlay() {
        let [scene, streaming] = scene_and_streaming();
        for extra in [scene, streaming] {
            let mut assets = world(&[gfx(), extra]);
            let mut report = ExpandReport::default();
            inject_engine_defaults(&mut assets, &mut report).unwrap();
            assert!(names_of_type(&assets, "loadingoverlay").is_empty());
            assert!(names_of_type(&assets, "screen").is_empty());
        }
    }

    #[test]
    fn engine_defaults_opts_out_of_the_loading_overlay() {
        let [scene, streaming] = scene_and_streaming();
        let mut assets = world(&[
            gfx(),
            scene,
            streaming,
            serde_json::json!({"name":"d","type":"EngineDefaults","args":{
                "loading_overlay": false
            }}),
        ]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        assert!(names_of_type(&assets, "loadingoverlay").is_empty());
    }

    // An authored overlay is completed even in a world that would not have one
    // synthesized (no StreamingConfig); declaring it is the opt-in.
    #[test]
    fn authored_loading_overlay_gets_unset_refs_filled() {
        let mut assets = world(&[
            gfx(),
            serde_json::json!({"name":"load","type":"LoadingOverlay","args":{
                "backdrop":"my_backdrop"
            }}),
            serde_json::json!({"name":"my_backdrop","type":"Sprite","args":{}}),
        ]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();

        let overlay = assets
            .iter()
            .find(|v| type_norm(v) == "loadingoverlay")
            .unwrap();
        assert_eq!(asset_name(overlay), "load");
        assert_eq!(overlay["args"]["backdrop"], "my_backdrop");
        assert_eq!(overlay["args"]["track"], "loading_screen_track");
        let sprites = names_of_type(&assets, "sprite");
        assert!(!sprites.contains(&"loading_screen_backdrop".to_string()));
        assert!(sprites.contains(&"loading_screen_fill".to_string()));
    }

    #[test]
    fn authored_loading_piece_replaces_the_injected_one() {
        let [scene, streaming] = scene_and_streaming();
        let mut assets = world(&[
            gfx(),
            scene,
            streaming,
            serde_json::json!({"name":"loading_screen_backdrop","type":"Sprite","args":{
                "tint":[0.1,0.0,0.0,1.0]
            }}),
        ]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();

        let backdrops: Vec<_> = assets
            .iter()
            .filter(|v| asset_name(v) == "loading_screen_backdrop")
            .collect();
        assert_eq!(backdrops.len(), 1);
        assert_eq!(backdrops[0]["args"]["tint"][0], serde_json::json!(0.1));
        let shadowed: Vec<&str> = report.shadowed.iter().map(|s| s.name.as_str()).collect();
        assert!(
            shadowed.contains(&"loading_screen_backdrop"),
            "{shadowed:?}"
        );
    }

    #[test]
    fn a_loading_piece_name_held_by_another_type_is_an_error() {
        let [scene, streaming] = scene_and_streaming();
        let mut assets = world(&[
            gfx(),
            scene,
            streaming,
            serde_json::json!({"name":"loading_screen","type":"Window","args":{}}),
        ]);
        let mut report = ExpandReport::default();
        let err = inject_engine_defaults(&mut assets, &mut report).unwrap_err();
        assert!(err.contains("loading_screen"), "{err}");
    }

    fn prop_body() -> serde_json::Value {
        serde_json::json!({"name":"crate_body","type":"PropBody","args":{"prop_name":"crate_a"}})
    }

    fn injected_physics(assets: &[serde_json::Value]) -> Option<&serde_json::Value> {
        assets.iter().find(|v| type_norm(v) == "physicsconfig")
    }

    #[test]
    fn physics_content_without_a_config_gets_the_default_one() {
        let mut assets = world(&[gfx(), prop_body()]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();

        let config = injected_physics(&assets).expect("PhysicsConfig injected");
        assert_eq!(asset_name(config), PHYSICS_CONFIG_NAME);
        // The engine's own defaults, strict spawn cap included.
        assert_eq!(config["args"]["spawn_headroom"], serde_json::json!(0));
        assert_eq!(config["args"]["floor_y"], serde_json::json!(0.0));
        assert_eq!(config["args"]["layers"], serde_json::json!([]));
        let entry = report
            .injected
            .iter()
            .find(|i| i.asset_type == "PhysicsConfig")
            .expect("recorded in the lock");
        assert_eq!(entry.injected_by, "physics_config");
        assert_eq!(entry.args["spawn_headroom"], serde_json::json!(0));
    }

    // The injected args are exactly what the runtime falls back to when a world
    // declares no config, so injection cannot change how a world simulates.
    #[test]
    fn the_injected_args_deserialize_to_the_engine_default() {
        let mut assets = world(&[gfx(), prop_body()]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        let args = injected_physics(&assets).unwrap()["args"].clone();
        let parsed: crate::components::PhysicsConfig = serde_json::from_value(args).unwrap();
        let default = crate::components::PhysicsConfig::default();
        assert_eq!(parsed.floor_y, default.floor_y);
        assert_eq!(parsed.terrain_half_width, default.terrain_half_width);
        assert_eq!(parsed.terrain_half_depth, default.terrain_half_depth);
        assert_eq!(parsed.terrain_subdivisions, default.terrain_subdivisions);
        assert_eq!(parsed.terrain_amplitude, default.terrain_amplitude);
        assert_eq!(parsed.terrain_offset_y, default.terrain_offset_y);
        assert_eq!(parsed.terrain_mesh, default.terrain_mesh);
        assert_eq!(parsed.layers, default.layers);
        assert_eq!(parsed.no_collide, default.no_collide);
        assert_eq!(parsed.contact_min_impulse, default.contact_min_impulse);
        assert_eq!(parsed.spawn_headroom, default.spawn_headroom);
    }

    #[test]
    fn an_authored_physics_config_is_left_alone() {
        let mut assets = world(&[
            gfx(),
            prop_body(),
            serde_json::json!({"name":"physics","type":"PhysicsConfig","args":{
                "spawn_headroom": 8
            }}),
        ]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        assert_eq!(names_of_type(&assets, "physicsconfig"), vec!["physics"]);
        let config = injected_physics(&assets).unwrap();
        assert_eq!(config["args"]["spawn_headroom"], serde_json::json!(8));
        assert!(
            !report
                .injected
                .iter()
                .any(|i| i.asset_type == "PhysicsConfig")
        );
    }

    #[test]
    fn a_world_without_physics_content_gets_no_config() {
        let mut assets = world(&[gfx()]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        assert!(names_of_type(&assets, "physicsconfig").is_empty());
    }

    // Every asset the engine's physics gate reads turns the injection on by
    // itself, so cook and the runtime agree on which worlds run physics.
    #[test]
    fn each_gating_asset_triggers_the_injection() {
        let gates = [
            serde_json::json!({"name":"hero_body","type":"RigidBody","args":{}}),
            prop_body(),
            serde_json::json!({"name":"gate","type":"TriggerVolume","args":{}}),
            serde_json::json!({"name":"hero","type":"SkinnedMesh","args":{
                "capsule": {"half_height": 0.9, "radius": 0.3}
            }}),
        ];
        for gate in gates {
            let label = asset_name(&gate);
            let mut assets = world(&[gfx(), gate]);
            let mut report = ExpandReport::default();
            inject_engine_defaults(&mut assets, &mut report).unwrap();
            assert_eq!(
                names_of_type(&assets, "physicsconfig"),
                vec![PHYSICS_CONFIG_NAME],
                "{label} should turn physics on"
            );
        }
    }

    // A SkinnedMesh with no capsule is a rendered mesh, not physics content.
    #[test]
    fn a_skinned_mesh_without_a_capsule_is_not_physics_content() {
        let mut assets = world(&[
            gfx(),
            serde_json::json!({"name":"banner","type":"SkinnedMesh","args":{"capsule":null}}),
        ]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        assert!(names_of_type(&assets, "physicsconfig").is_empty());
    }

    // Physics does not need a GraphicsConfig, so a headless world with bodies
    // still gets its config.
    #[test]
    fn a_non_rendering_physics_world_still_gets_the_config() {
        let mut assets = world(&[prop_body()]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        assert_eq!(
            names_of_type(&assets, "physicsconfig"),
            vec![PHYSICS_CONFIG_NAME]
        );
    }

    #[test]
    fn engine_defaults_opts_out_of_the_physics_config() {
        let mut assets = world(&[
            gfx(),
            prop_body(),
            serde_json::json!({"name":"d","type":"EngineDefaults","args":{
                "physics_config": false
            }}),
        ]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        assert!(names_of_type(&assets, "physicsconfig").is_empty());
    }

    #[test]
    fn the_physics_config_name_held_by_another_type_is_an_error() {
        let mut assets = world(&[
            gfx(),
            prop_body(),
            serde_json::json!({"name": PHYSICS_CONFIG_NAME, "type":"Window","args":{}}),
        ]);
        let mut report = ExpandReport::default();
        let err = inject_engine_defaults(&mut assets, &mut report).unwrap_err();
        assert!(err.contains(PHYSICS_CONFIG_NAME), "{err}");
        assert!(err.contains("Window"), "{err}");
    }

    // Running the pass twice (a re-cook of an already expanded list) must not
    // stack a second config.
    #[test]
    fn injecting_twice_yields_one_physics_config() {
        let mut assets = world(&[gfx(), prop_body()]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        assert_eq!(
            names_of_type(&assets, "physicsconfig"),
            vec![PHYSICS_CONFIG_NAME]
        );
    }

    #[test]
    fn synthesized_hud_report_args_include_filled_labels() {
        let mut assets = world(&[
            gfx(),
            serde_json::json!({"name":"menu","type":"MainMenu","args":{}}),
        ]);
        let mut report = ExpandReport::default();
        inject_engine_defaults(&mut assets, &mut report).unwrap();
        let entry = report
            .injected
            .iter()
            .find(|i| i.asset_type == "StatHud")
            .unwrap();
        assert_eq!(entry.args["fps_label"], serde_json::json!("fps_chip"));
    }
}