Skip to main content

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