yog_cli/content.rs
1//! `.yblock` / `.yitem` content files — a mod's blocks and items: their
2//! properties, 3D model, block states and item form, in one JSON file each,
3//! edited in Yog-IDLE's 3D model editor — and `.yrecipe` files, one recipe
4//! each (crafting, shapeless, smelting), edited in its recipe editor. See
5//! Yog-IDLE's `docs/blocks-items.md` for the formats.
6//!
7//! `yog build` reads `content/**/*.yblock` and `content/**/*.yitem` and
8//!
9//! - writes Minecraft's JSON for them: block models, blockstates, item
10//! models, 1.21.4+ item definitions (`assets/<ns>/items/`), block loot
11//! tables, the `mineable/<tool>` and `needs_<level>_tool` block tags;
12//! - puts their names and tooltips into the lang files (a `@<Set>.<name>`
13//! text of a `.yres` file under the key Minecraft looks up, a literal into
14//! `en_us`);
15//! - builds the mod's textures (`.ytexture`, see `textures`);
16//! - generates `.yog-build/generated/yog-content.g.rs` — `Blocks`/`Items`
17//! with an id constant and a `BlockDef`/`ItemDef` builder per entry and
18//! a `register` function — pulled in by `yog_api::content!();`, which
19//! also registers them (unless an entry says `"register": "manual"`) and
20//! every recipe.
21
22pub(crate) mod formats;
23pub(crate) mod media;
24pub(crate) mod pipeline;
25
26mod import;
27pub(crate) use import::font_copy_path;
28pub(crate) mod refs;
29mod vocab;
30
31pub use import::{Imported, import_command};
32pub use pipeline::compile::compile;
33pub use pipeline::outputs::{OutputSet, TAGS_FILE, TagLists, merge_outputs, read_tags};
34pub use pipeline::source::{GENERATED_PARTS_DIR, Source, write_source};
35pub(crate) use vocab::TEMPLATES;
36
37use serde_json::Value;
38use std::collections::{BTreeMap, BTreeSet};
39use std::path::{Path, PathBuf};
40use yog_ids::Format;
41
42/// Where content files live, under the mod root.
43pub const CONTENT_DIR: &str = yog_ids::project::CONTENT;
44/// The generated Rust code, under the mod root.
45pub const GENERATED_FILE: &str = ".yog-build/generated/yog-content.g.rs";
46
47/// A generated file: path inside the `.yog` and its bytes.
48pub(crate) type Output = (String, Vec<u8>);
49
50/// The formats a content file under `content/` is written in.
51const CONTENT: [Format; 9] = [
52 Format::Block,
53 Format::Item,
54 Format::Recipe,
55 Format::Book,
56 Format::Def,
57 Format::Model,
58 Format::Anim,
59 Format::Entity,
60 Format::Font,
61];
62
63/// The content format of the file at `path`.
64fn content_format(path: &Path) -> Option<Format> {
65 Format::of_path(path).filter(|f| CONTENT.contains(f))
66}
67
68/// Everything `yog build` makes of a mod's content files.
69#[derive(Debug, Default)]
70pub struct Compiled {
71 /// Models, blockstates, item definitions, loot tables, textures.
72 pub outputs: Vec<Output>,
73 /// Lang entries: `(namespace, locale)` → key → text, merged into the
74 /// lang files by [`merge_outputs`].
75 lang: Lang,
76 /// `.yres` text keys to copy under another key: (from, to).
77 lang_aliases: Vec<(String, String)>,
78 /// Tag path (`data/minecraft/tags/block/...json`) → its entries.
79 tags: BTreeMap<String, formats::defs::data::TagEntries>,
80 /// The generated Rust code; `None` when the mod has no content files.
81 pub source: Option<Source>,
82 /// Data the code includes (`include_bytes!`), by path under
83 /// `.yog-build/generated/`: the books, as Yog Data.
84 pub data_files: Vec<(String, Vec<u8>)>,
85 /// What's suspect but builds.
86 pub warnings: Vec<crate::diagnostics::Diagnostic>,
87 /// [`yog_registry::REGISTRATIONS_FILE`]: what the mod registers at the
88 /// game's start — packaged by dev builds only, for hot reload.
89 pub registrations: String,
90 /// The pictures (`.yres` images' files) an atlas took in: left out of the build.
91 pub packed: BTreeSet<String>,
92 /// Textures the game loads as files of their own as well (a tab's
93 /// background): their pictures stay in the build though packed.
94 loose: BTreeSet<String>,
95 /// How many blocks, items, recipes and books it compiled.
96 pub blocks: usize,
97 /// See [`Compiled::blocks`].
98 pub items: usize,
99 /// See [`Compiled::blocks`].
100 pub recipes: usize,
101 /// See [`Compiled::blocks`].
102 pub books: usize,
103}
104
105pub(crate) use yog_ids::{valid_id, valid_namespace, valid_path};
106
107/// Every content file (`CONTENT`) under `content/` of the mod at `root`, sorted.
108pub(crate) fn find(root: &Path) -> Vec<PathBuf> {
109 yog_analysis::files::under(&root.join(CONTENT_DIR), &|p| content_format(p).is_some())
110}
111
112fn pretty(v: &Value) -> Vec<u8> {
113 let mut s = serde_json::to_string_pretty(v).unwrap_or_default();
114 s.push('\n');
115 s.into_bytes()
116}
117
118/// The mod's lang: (namespace, locale) → key → text.
119type Lang = BTreeMap<(String, String), BTreeMap<String, String>>;
120
121#[cfg(test)]
122mod tests;
123
124/// Whole numbers below this are written as integers.
125const WHOLE_BELOW: f64 = 1e15;
126
127/// A number in the game's JSON: integral values without a fraction (`16`, not `16.0`).
128pub(crate) fn num_value(n: f64) -> serde_json::Value {
129 if n.fract() == 0.0 && n.abs() < WHOLE_BELOW {
130 serde_json::Value::from(n as i64)
131 } else {
132 serde_json::Number::from_f64(n)
133 .map(serde_json::Value::Number)
134 .unwrap_or(serde_json::Value::Null)
135 }
136}