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 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 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    pub warnings: Vec<String>,
100    /// [`yog_registry::REGISTRATIONS_FILE`]: what the mod registers at the
101    /// game's start — packaged by dev builds only, for hot reload.
102    pub registrations: String,
103    /// The pictures (`.yres` images' files) an atlas took in: left out of the build.
104    pub packed: BTreeSet<String>,
105    pub blocks: usize,
106    pub items: usize,
107    pub recipes: usize,
108    pub books: usize,
109}
110
111pub use yog_ids::{valid_id, valid_namespace, valid_path};
112
113/// Every content file (`CONTENT`) under `content/` of the mod at `root`, sorted.
114pub fn find(root: &Path) -> Vec<PathBuf> {
115    crate::files::under(&root.join(CONTENT_DIR), &|p| content_format(p).is_some())
116}
117
118fn pretty(v: &Value) -> Vec<u8> {
119    let mut s = serde_json::to_string_pretty(v).unwrap_or_default();
120    s.push('\n');
121    s.into_bytes()
122}
123
124/// The mod's lang: (namespace, locale) → key → text.
125type Lang = BTreeMap<(String, String), BTreeMap<String, String>>;
126
127#[cfg(test)]
128mod tests;
129
130/// Whole numbers below this are written as integers.
131const WHOLE_BELOW: f64 = 1e15;
132
133/// A number in the game's JSON: integral values without a fraction (`16`, not `16.0`).
134pub fn num_value(n: f64) -> serde_json::Value {
135    if n.fract() == 0.0 && n.abs() < WHOLE_BELOW {
136        serde_json::Value::from(n as i64)
137    } else {
138        serde_json::Number::from_f64(n)
139            .map(serde_json::Value::Number)
140            .unwrap_or(serde_json::Value::Null)
141    }
142}