yog-cli 0.49.2

Yog build tool — `yog build` compiles a mod and packages it as .yog.
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
//! Compiling a mod's content files: each one read and checked, then what
//! they name checked against everything they define, then the code and the
//! data made of them.

use std::collections::{BTreeMap, HashMap};
use std::path::{Path, PathBuf};
use yog_data::Schema;
use yog_ids::Format;
use yog_ids::ns::MINECRAFT;
use yog_registry::{Brew, PotionDef};

use crate::content::block_item::{compile_file, inventory_screens};
use crate::content::body::{compile_body, compile_entity, compile_font};
use crate::content::book::{BookAgainst, compile_book, recipe_layouts};
use crate::content::ctx::{Deferred, Shared, Sink, Wanted};
use crate::content::ctx::{caveat, problem};
use crate::content::defs::data::{definition_path, loot_path};
use crate::content::defs::ladder::{read_defs, tool_level_tags};
use crate::content::defs::worldgen::dimension_path;
use crate::content::defs::{DefFile, Defs, Made, compile_def};
use crate::content::entry::Entry;
use crate::content::files::{BodyFile, BookFile, EntityFile, FontFile};
use crate::content::outputs::version_overlays;
use crate::content::recipe::registration::Recipe;
use crate::content::recipe::{Against, compile_recipe};
use crate::content::source::{Content, mentions_content_macro, registrations, rust_source};
use crate::content::{
    CONTENT_DIR, Compiled, content_format, datapack, find, looks, refs, textures, valid_namespace,
};
use crate::diagnostics::Diagnostic;
use crate::yres;

/// Ids defined so far, each by the file defining it: a second one is a mistake.
#[derive(Default)]
struct Ids(HashMap<String, String>);

impl Ids {
    /// `id` (a `what`) defined by `file`.
    fn claim(&mut self, errors: &mut Vec<Diagnostic>, file: &str, what: &str, id: &str) {
        if let Some(other) = self.0.insert(id.to_string(), file.to_string()) {
            errors.push(problem(format!("{what} {id} is also defined by {other}")).in_file(file));
        }
    }
}

/// Files read and checked later, once what they name is known: (file, text).
#[derive(Default)]
struct Pending {
    books: Vec<(String, String)>,
    recipes: Vec<(String, String, PathBuf)>,
    defs: Vec<DefFile>,
}

/// Everything checked so far.
struct Build<'a> {
    shared: Shared<'a>,
    /// The mod's namespace, when its id is one.
    mod_ns: Option<&'a str>,
    sink: Sink,
    out: Compiled,
    /// The models the files write, by path: one file each.
    generated: BTreeMap<String, String>,
    entries: Vec<Entry>,
    potions: BTreeMap<String, PotionDef>,
    entities: Vec<EntityFile>,
    bodies: Vec<BodyFile>,
    fonts: Vec<FontFile>,
    recipes: Vec<Recipe>,
    books: Vec<BookFile>,
    /// Blocks', items' and entities' ids, then each other kind's.
    ids: Ids,
    model_ids: Ids,
    anim_ids: Ids,
    font_ids: Ids,
    recipe_ids: Ids,
    book_ids: Ids,
    pending: Pending,
}

/// Compiles the mod's content files (see the module docs). Any error fails
/// the whole thing, listing every problem.
pub fn compile(root: &Path, mod_id: &str, refs: &yres::Refs) -> Result<Compiled, String> {
    let files = find(root);
    if files.is_empty() {
        return Ok(Compiled::default());
    }
    let mod_ns = valid_namespace(mod_id).then_some(mod_id);
    let mut sink = Sink::default();
    let own = textures::compile(root, mod_id, refs, &mut sink.errors);
    let shared = Shared {
        root,
        refs,
        ns: mod_ns.unwrap_or(MINECRAFT),
        own_textures: &own.ids,
    };
    let mut b = Build::new(shared, mod_ns, sink);
    for path in &files {
        b.read(path);
    }
    let mut known = b.known();
    let mut defs = read_defs(&b.pending.defs, b.shared.ns, &mut b.sink.errors);
    let made = b.defs(&known);
    let layouts = b.recipes(&mut known, &made);
    b.check_deferred(&defs, &known);
    b.tool_levels(&defs);
    b.books(&known, &layouts);
    b.inventories();
    b.out.outputs.extend(own.outputs);
    b.out.packed = own.packed;
    b.finish(&mut defs, made, &known)
}

impl<'a> Build<'a> {
    fn new(shared: Shared<'a>, mod_ns: Option<&'a str>, sink: Sink) -> Build<'a> {
        Build {
            shared,
            mod_ns,
            sink,
            out: Compiled::default(),
            generated: BTreeMap::new(),
            entries: Vec::new(),
            potions: BTreeMap::new(),
            entities: Vec::new(),
            bodies: Vec::new(),
            fonts: Vec::new(),
            recipes: Vec::new(),
            books: Vec::new(),
            ids: Ids::default(),
            model_ids: Ids::default(),
            anim_ids: Ids::default(),
            font_ids: Ids::default(),
            recipe_ids: Ids::default(),
            book_ids: Ids::default(),
            pending: Pending::default(),
        }
    }

    /// Reads the content file at `path`: checked now, or kept for when
    /// what it names is known.
    fn read(&mut self, path: &Path) {
        let shown = crate::shown_path(self.shared.root, path);
        let Some(kind) = content_format(path) else {
            return;
        };
        let text = match crate::files::read_source(path) {
            Ok(t) => t,
            Err(e) => {
                return self
                    .sink
                    .errors
                    .push(problem(format!("{e}")).in_file(&shown));
            }
        };
        let errors = &mut self.sink.errors;
        match kind {
            Format::Model | Format::Anim => {
                let Some(b) = compile_body(&self.shared, &shown, &text, kind, path, errors) else {
                    return;
                };
                let ids = if b.model {
                    &mut self.model_ids
                } else {
                    &mut self.anim_ids
                };
                ids.claim(errors, &shown, kind.noun(), &b.id);
                self.bodies.push(b);
            }
            Format::Font => {
                let Some(font) = compile_font(&self.shared, &shown, &text, path, errors) else {
                    return;
                };
                self.font_ids
                    .claim(errors, &shown, yog_ids::kind::FONT, &font.id);
                self.fonts.push(font);
            }
            // Checked once every block, item and recipe is known.
            Format::Book => self.pending.books.push((shown, text)),
            // Checked once every block and item is known.
            Format::Recipe => self.pending.recipes.push((shown, text, path.to_path_buf())),
            Format::Def => {
                let mut cx = self.sink.cx(&self.shared, &shown);
                if let Some(f) = DefFile::read(&mut cx, text) {
                    self.pending.defs.push(f);
                }
            }
            Format::Entity => {
                let mut cx = self.sink.cx(&self.shared, &shown);
                let Some(e) = compile_entity(&mut cx, &text, path, self.mod_ns, &mut self.out)
                else {
                    return;
                };
                self.ids
                    .claim(&mut self.sink.errors, &shown, yog_ids::word::ID, &e.id);
                self.entities.push(e);
            }
            _ => {
                let mut cx = self.sink.cx(&self.shared, &shown);
                let Some(entry) = compile_file(
                    &mut cx,
                    &text,
                    kind,
                    path,
                    self.mod_ns,
                    &mut self.out,
                    &mut self.generated,
                ) else {
                    return;
                };
                self.ids
                    .claim(&mut self.sink.errors, &shown, yog_ids::word::ID, &entry.id);
                match entry.potion.clone() {
                    Some(p) => {
                        self.potions.insert(entry.id, p);
                    }
                    None => self.entries.push(entry),
                }
            }
        }
    }

    /// What the files may name: the mod's content, vanilla's and other mods'
    /// declared ids. A model or clips of this mod an entity names must be
    /// among its `.ymodel`/`.yanim`.
    fn known(&mut self) -> refs::Known {
        let manifest =
            crate::manifest::YogToml::read(&self.shared.root.join(yog_ids::file::MANIFEST)).ok();
        let externals = manifest
            .as_ref()
            .map(|t| t.externals.clone())
            .unwrap_or_default();
        let datapack = manifest.as_ref().and_then(|t| t.datapack.clone());
        let mut known = refs::Known::new(self.shared.ns, &externals, datapack.as_deref());
        for e in &self.entries {
            known.add(
                if e.kind == Format::Block {
                    refs::RefKind::Block
                } else {
                    refs::RefKind::Item
                },
                e.id.clone(),
            );
        }
        for id in self.potions.keys() {
            known.add(refs::RefKind::Item, id.clone());
        }
        for e in &self.entities {
            known.add(refs::RefKind::Entity, e.id.clone());
            for (wanted, kind) in [(&e.model, Format::Model), (&e.animations, Format::Anim)] {
                if let Some(id) = wanted
                    && self.mod_ns.is_some_and(|ns| yog_ids::split(id).0 == ns)
                    && !self
                        .bodies
                        .iter()
                        .any(|b| b.model == (kind == Format::Model) && &b.id == id)
                {
                    self.sink.errors.push(problem(format!(
                        "{}: {} `{id}`: no such file in {CONTENT_DIR}/ (`.{}`)",
                        e.file,
                        kind.noun(),
                        kind.ext()
                    )));
                }
            }
        }
        known
    }

    /// The `.ydef`s checked against what the files name (`known`).
    fn defs(&mut self, known: &refs::Known) -> Made {
        let mut made = Made::default();
        for f in std::mem::take(&mut self.pending.defs) {
            let shown = f.shown.clone();
            let mut cx = self.sink.cx(&self.shared, &shown);
            compile_def(&mut cx, f, known, &mut made, &mut self.out);
        }
        made
    }

    /// The `.yrecipe`s, against the mod's kinds of recipe and how they're
    /// shown (`.yui` `recipe_layout` components, returned); their brewing
    /// recipes onto the potions they make.
    fn recipes(
        &mut self,
        known: &mut refs::Known,
        made: &Made,
    ) -> Vec<yog_formats::book::layout::RecipeLayout> {
        let layouts = recipe_layouts(
            self.shared.root,
            self.shared.ns,
            &made.recipe_kinds,
            known,
            &mut self.sink.errors,
        );
        let mut brews: Vec<(String, Brew)> = Vec::new();
        let mut against = Against {
            mod_ns: self.mod_ns,
            known,
            kinds: &made.recipe_kinds,
            brews: &mut brews,
        };
        for (shown, text, path) in std::mem::take(&mut self.pending.recipes) {
            let mut cx = self.sink.cx(&self.shared, &shown);
            let Some(r) = compile_recipe(&mut cx, &text, &path, &mut against) else {
                continue;
            };
            self.recipe_ids
                .claim(&mut self.sink.errors, &shown, yog_ids::kind::RECIPE, &r.id);
            self.recipes.push(r);
        }
        for r in &self.recipes {
            known.add(refs::RefKind::Recipe, r.id.clone());
        }
        for (to, brew) in brews {
            self.brew(to, brew);
        }
        layouts
    }

    /// A brewing recipe making potion `to`: the mod's potion or a Yog
    /// default potion alias, from one.
    fn brew(&mut self, to: String, brew: Brew) {
        let defined = |p: &BTreeMap<String, PotionDef>, id: &str| {
            p.contains_key(id) || refs::is_vanilla_potion(id)
        };
        if !defined(&self.potions, &brew.from) {
            return self.sink.errors.push(problem(format!("brewing recipe input `{}`: no potion `.yitem` or Yog default potion alias defines it", brew.from)));
        }
        if !defined(&self.potions, &to) {
            return self.sink.errors.push(problem(format!("brewing recipe result `{to}`: no potion `.yitem` or Yog default potion alias defines it")));
        }
        self.potions
            .entry(to.clone())
            .or_insert_with(|| PotionDef {
                id: to,
                name: None,
                effects: Vec::new(),
                brewed: Vec::new(),
            })
            .brewed
            .push(brew);
    }

    /// What blocks and items name (drops, levels), known now.
    fn check_deferred(&mut self, defs: &Defs, known: &refs::Known) {
        for Deferred {
            file,
            line,
            path,
            wanted,
        } in std::mem::take(&mut self.sink.deferred)
        {
            if let Err(e) = check_wanted(&wanted, defs, known) {
                self.sink
                    .errors
                    .push(problem(format!("{path}: {e}")).in_file(file).at_line(line));
            }
        }
    }

    /// The tags that make blocks need their tool level.
    fn tool_levels(&mut self, defs: &Defs) {
        for e in &self.entries {
            let Some((level, line)) = &e.block.tool_level else {
                continue;
            };
            let at = |d: Diagnostic| d.in_file(&e.file).at_line(*line);
            let field = yog_data::field!(yog_formats::content::BlockProps, tool_level);
            match tool_level_tags(&e.id, level, defs, &mut self.out) {
                Ok(None) => {}
                Ok(Some(m)) => self
                    .sink
                    .warnings
                    .push(at(caveat(format!("block.{field}: {m}")))),
                Err(m) => self
                    .sink
                    .errors
                    .push(at(problem(format!("block.{field}: {m}")))),
            }
        }
    }

    /// The `.ybook`s, once every block, item and recipe is known.
    fn books(&mut self, known: &refs::Known, layouts: &[yog_formats::book::layout::RecipeLayout]) {
        let recipe_kinds: HashMap<String, String> = self
            .recipes
            .iter()
            .map(|r| (r.id.clone(), r.ty.clone()))
            .collect();
        // The mod's own kinds of book page (`.yui` components with `book_page`).
        let page_types =
            crate::book_page::find(self.shared.root, self.shared.ns, &mut self.sink.errors);
        let against = BookAgainst {
            known,
            layouts,
            recipe_kinds: &recipe_kinds,
            page_types: &page_types,
        };
        for (shown, text) in std::mem::take(&mut self.pending.books) {
            let mut cx = self.sink.cx(&self.shared, &shown);
            let Some(b) = compile_book(&mut cx, &text, &against) else {
                continue;
            };
            self.book_ids
                .claim(&mut self.sink.errors, &shown, yog_ids::kind::BOOK, &b.id);
            self.books.push(b);
        }
    }

    /// The inventory screens blocks of this mod open must be its screens
    /// (another mod's ids can't be checked here).
    fn inventories(&mut self) {
        let Some(ns) = self.mod_ns else { return };
        let screens = inventory_screens(&self.shared.root.join(yog_ids::project::SRC));
        for (file, line, id) in &self.sink.inventories {
            if yog_ids::split(id).0 == ns && !screens.contains(id) {
                self.sink.errors.push(
                    problem(format!(
                        "block.inventory `{id}`: no .yui <Screen id=\"{id}\"> with <Slot>s in src/"
                    ))
                    .in_file(file)
                    .at_line(*line),
                );
            }
        }
    }

    /// Everything made, unless something was wrong: then every problem.
    fn finish(
        mut self,
        defs: &mut Defs,
        mut made: Made,
        known: &refs::Known,
    ) -> Result<Compiled, String> {
        // Armor's worn look where 1.21.4+ looks for it.
        for m in &defs.armor_materials {
            looks::equipment(
                self.shared.root,
                &mut self.out,
                &m.texture,
                &mut self.sink.warnings,
            );
        }
        self.out.warnings = std::mem::take(&mut self.sink.warnings);
        if !self.sink.errors.is_empty() {
            crate::diagnostics::report_all(&self.sink.errors);
            return Err(format!(
                "invalid blocks/items in {CONTENT_DIR}/:{}{}",
                crate::diagnostics::LIST_SEP,
                crate::diagnostics::listed(&self.sink.errors)
            ));
        }
        let potions = std::mem::take(&mut self.potions);
        made.definitions
            .extend(potions.into_iter().map(|(id, potion)| {
                (
                    yog_ids::kind::POTION,
                    id,
                    yog_data::binary::encode(&potion.write()),
                )
            }));
        self.count();
        self.data_files(&made);
        self.out.registrations =
            registrations(&self.entries, &self.entities, &made.dimensions, defs);
        self.made_into(defs, made);
        let content = Content {
            entries: &self.entries,
            recipes: &self.recipes,
            books: &self.books,
            bodies: &self.bodies,
            entities: &self.entities,
            fonts: &self.fonts,
            defs,
            textures: self.shared.own_textures,
        };
        if content.registers_anything()
            && !mentions_content_macro(&self.shared.root.join(yog_ids::project::SRC))
        {
            self.out.warnings.push(caveat(format!("{CONTENT_DIR} has blocks/items, but src/ never calls `yog_api::content!();` — they get models but aren't registered")));
        }
        self.out.source = Some(rust_source(&content));
        version_overlays(&mut self.out, &known.datapack);
        Ok(self.out)
    }

    /// How many of each the build says it made.
    fn count(&mut self) {
        self.out.blocks = self
            .entries
            .iter()
            .filter(|e| e.kind == Format::Block)
            .count();
        self.out.items = self.entries.len() - self.out.blocks;
        self.out.recipes = self.recipes.len();
        self.out.books = self.books.len();
    }

    /// The data the code includes: books, bodies, fonts, entities, the mod's
    /// own kinds of recipe, dimensions, loot modifiers, definitions.
    fn data_files(&mut self, made: &Made) {
        let files = &mut self.out.data_files;
        files.extend(self.books.iter().map(|b| (b.path(), b.data.clone())));
        files.extend(self.bodies.iter().map(|b| (b.path(), b.data.clone())));
        files.extend(self.fonts.iter().map(|f| (f.path(), f.data.clone())));
        files.extend(self.entities.iter().map(|e| (e.path(), e.data.clone())));
        files.extend(self.recipes.iter().filter_map(Recipe::data_file));
        files.extend(
            made.dimensions
                .iter()
                .map(|(id, data)| (dimension_path(id), data.clone())),
        );
        files.extend(
            made.loot
                .iter()
                .map(|(id, data)| (loot_path(id), data.clone())),
        );
        files.extend(
            made.definitions
                .iter()
                .map(|(kind, id, data)| (definition_path(kind, id), data.clone())),
        );
    }

    /// What the `.ydef`s made, into what the code is generated from; ores'
    /// files and a tool level above the game's tag of what its tools can't mine.
    fn made_into(&mut self, defs: &mut Defs, made: Made) {
        for (files, feature) in made.ores {
            self.out.outputs.extend(files);
            defs.biome_features.push(feature);
        }
        for m in &defs.tool_materials {
            if yog_ids::split(&m.incorrect).0 != MINECRAFT {
                let file = datapack::tag_file(yog_ids::kind::BLOCK, &m.incorrect);
                self.out
                    .tags
                    .entry(datapack::overlay(
                        &yog_registry::ToolLadder::game().incorrect_when,
                        &file,
                    ))
                    .or_default();
            }
        }
        defs.definitions = made
            .definitions
            .into_iter()
            .map(|(kind, id, _)| (kind, id))
            .collect();
        defs.loot_modifiers = made.loot.into_iter().map(|(id, _)| id).collect();
        defs.dimensions = made.dimensions.into_iter().map(|(id, _)| id).collect();
        defs.advancements = made.advancements;
        defs.tables = made.tables;
    }
}

/// Whether what a file names exists: a level, ladder or material of the
/// game's or the `.ydef`s'; a tag of one of its kinds; content.
fn check_wanted(wanted: &Wanted, defs: &Defs, known: &refs::Known) -> Result<(), String> {
    use yog_formats::def::{ArmorMaterial, SoundGroup, ToolMaterial};
    use yog_ids::kind;
    let found = |there: bool, what: &str, id: &str| {
        if there {
            Ok(())
        } else {
            Err(format!("no {what} {id} — the game's or a .ydef's"))
        }
    };
    match wanted {
        Wanted::Tag { kinds, id } => {
            let mut found = kinds.iter().map(|k| known.check_tag(k, id));
            let first = found.next().unwrap_or(Ok(()));
            if first.is_ok() || found.any(|f| f.is_ok()) {
                Ok(())
            } else {
                first
            }
        }
        Wanted::Ref(r) => match r.kind.as_str() {
            kind::LEVEL => found(defs.level(&r.id).is_some(), "level", &r.id),
            kind::LADDER => found(defs.order.contains_key(&r.id), "ladder", &r.id),
            kind::TOOL_MATERIAL => found(
                defs.tool_materials.iter().any(|m| m.id == r.id)
                    || ToolMaterial::built_in(&r.id).is_some(),
                "tool material",
                &r.id,
            ),
            kind::ARMOR_MATERIAL => found(
                defs.armor_materials.iter().any(|m| m.id == r.id)
                    || ArmorMaterial::built_in(&r.id).is_some(),
                "armor material",
                &r.id,
            ),
            kind::SOUND_GROUP => found(
                defs.sound_groups.iter().any(|g| g.id == r.id)
                    || SoundGroup::built_in(&r.id).is_some(),
                "sound group",
                &r.id,
            ),
            other => refs::RefKind::from_word(other).map_or(Ok(()), |k| known.check(k, &r.id)),
        },
    }
}