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
22mod block_item;
23mod body;
24mod book;
25mod compile;
26mod ctx;
27mod datapack;
28mod defs;
29mod entry;
30mod file;
31mod files;
32mod import;
33pub(crate) use import::font_copy_path;
34mod looks;
35pub(crate) mod material;
36mod model;
37mod outputs;
38mod particles;
39mod recipe;
40pub(crate) mod refs;
41mod sounds;
42mod source;
43mod table;
44mod textures;
45mod vocab;
46
47pub use compile::compile;
48pub use import::{Imported, import_command};
49pub use outputs::{OutputSet, TAGS_FILE, TagLists, merge_outputs, read_tags};
50pub use source::{GENERATED_PARTS_DIR, Source, write_source};
51pub(crate) use vocab::TEMPLATES;
52
53use serde_json::Value;
54use std::collections::{BTreeMap, BTreeSet};
55use std::path::{Path, PathBuf};
56use yog_ids::Format;
57
58/// Where content files live, under the mod root.
59pub const CONTENT_DIR: &str = yog_ids::project::CONTENT;
60/// The generated Rust code, under the mod root.
61pub const GENERATED_FILE: &str = ".yog-build/generated/yog-content.g.rs";
62
63/// A generated file: path inside the `.yog` and its bytes.
64pub(crate) type Output = (String, Vec<u8>);
65
66/// The formats a content file under `content/` is written in.
67const CONTENT: [Format; 9] = [
68 Format::Block,
69 Format::Item,
70 Format::Recipe,
71 Format::Book,
72 Format::Def,
73 Format::Model,
74 Format::Anim,
75 Format::Entity,
76 Format::Font,
77];
78
79/// The content format of the file at `path`.
80fn content_format(path: &Path) -> Option<Format> {
81 Format::of_path(path).filter(|f| CONTENT.contains(f))
82}
83
84/// Everything `yog build` makes of a mod's content files.
85#[derive(Debug, Default)]
86pub struct Compiled {
87 /// Models, blockstates, item definitions, loot tables, textures.
88 pub outputs: Vec<Output>,
89 /// Lang entries: `(namespace, locale)` → key → text, merged into the
90 /// lang files by [`merge_outputs`].
91 lang: Lang,
92 /// `.yres` text keys to copy under another key: (from, to).
93 lang_aliases: Vec<(String, String)>,
94 /// Tag path (`data/minecraft/tags/block/...json`) → its entries.
95 tags: BTreeMap<String, defs::data::TagEntries>,
96 /// The generated Rust code; `None` when the mod has no content files.
97 pub source: Option<Source>,
98 /// Data the code includes (`include_bytes!`), by path under
99 /// `.yog-build/generated/`: the books, as Yog Data.
100 pub data_files: Vec<(String, Vec<u8>)>,
101 /// What's suspect but builds.
102 pub warnings: Vec<crate::diagnostics::Diagnostic>,
103 /// [`yog_registry::REGISTRATIONS_FILE`]: what the mod registers at the
104 /// game's start — packaged by dev builds only, for hot reload.
105 pub registrations: String,
106 /// The pictures (`.yres` images' files) an atlas took in: left out of the build.
107 pub packed: BTreeSet<String>,
108 /// Textures the game loads as files of their own as well (a tab's
109 /// background): their pictures stay in the build though packed.
110 loose: BTreeSet<String>,
111 /// How many blocks, items, recipes and books it compiled.
112 pub blocks: usize,
113 /// See [`Compiled::blocks`].
114 pub items: usize,
115 /// See [`Compiled::blocks`].
116 pub recipes: usize,
117 /// See [`Compiled::blocks`].
118 pub books: usize,
119}
120
121pub(crate) use yog_ids::{valid_id, valid_namespace, valid_path};
122
123/// Every content file (`CONTENT`) under `content/` of the mod at `root`, sorted.
124pub(crate) fn find(root: &Path) -> Vec<PathBuf> {
125 crate::files::under(&root.join(CONTENT_DIR), &|p| content_format(p).is_some())
126}
127
128fn pretty(v: &Value) -> Vec<u8> {
129 let mut s = serde_json::to_string_pretty(v).unwrap_or_default();
130 s.push('\n');
131 s.into_bytes()
132}
133
134/// The mod's lang: (namespace, locale) → key → text.
135type Lang = BTreeMap<(String, String), BTreeMap<String, String>>;
136
137#[cfg(test)]
138mod tests;
139mod videos;
140
141/// Whole numbers below this are written as integers.
142const WHOLE_BELOW: f64 = 1e15;
143
144/// A number in the game's JSON: integral values without a fraction (`16`, not `16.0`).
145pub(crate) fn num_value(n: f64) -> serde_json::Value {
146 if n.fract() == 0.0 && n.abs() < WHOLE_BELOW {
147 serde_json::Value::from(n as i64)
148 } else {
149 serde_json::Number::from_f64(n)
150 .map(serde_json::Value::Number)
151 .unwrap_or(serde_json::Value::Null)
152 }
153}