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