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