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