yog-cli 0.49.0

Yog build tool — `yog build` compiles a mod and packages it as .yog.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//! What a content reference (`@<kind>:<id>`) can name, for the designers'
//! pickers — the same three sources `yog build` checks references against:
//! the mod's own content (`content/`), the other mods' ids it declares in
//! `yog.toml` (`[external.<namespace>]`) and the game's (read from the
//! client jar on this machine, when there is one). And the mod's own
//! `.ydef`s of a kind with what they say (an advancement's parent, an
//! effect's color), for the designers that draw several at once.

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use yog_ids::Format;
use yog_sync::{Mutex, OnceLock};

use yog_data::Value;

use crate::design::assets::{Resources, Vanilla};
use crate::design::project::{self, mod_root};

/// Kinds of content defined by files of their own, by extension; any other
/// kind is the `.ydef`s of that `kind`.
const FILE_KINDS: [(&str, &[Format]); 4] = [
    (yog_ids::kind::ITEM, &[Format::Item, Format::Block]),
    (yog_ids::kind::BLOCK, &[Format::Block]),
    (yog_ids::kind::ENTITY, &[Format::Entity]),
    (yog_ids::kind::RECIPE, &[Format::Recipe]),
];

/// Body files (`.ymodel`, `.yanim`): named by their path under `content/`
/// without the extension (`body/wings`), as an entity's `model` names them.
const BODY_FORMATS: [Format; 2] = [Format::Model, Format::Anim];

/// Resources defined by files of their own under `resources/`, named by
/// their path there without the extension (`block/glass`).
const RESOURCE_KINDS: [(&str, Format); 2] = [
    (yog_ids::kind::TEXTURE, Format::Texture),
    (yog_ids::kind::MATERIAL, Format::Material),
];

/// Where the game's ids of a kind are in its client jar: translation keys
/// of its lang (`entity.minecraft.pig` → `pig`; `dotted`: the rest may hold
/// dots, `generic.max_health`), files of its data folders (by kind), or the
/// keys of one of its assets (`sounds.json`: sound events).
enum GameIds {
    /// Keys starting with the game's `<kind>.minecraft.` of each of these kinds.
    Lang {
        kinds: &'static [&'static str],
        dotted: bool,
    },
    /// A prefix of its own (`attribute.name.`).
    LangPrefix(&'static str),
    Data,
    Keys(&'static str),
}

/// The game's sound events: the keys of its `sounds.json`.
const SOUNDS_ASSET: &str = yog_ids::res::SOUNDS_JSON;
/// Attributes' translation keys: `attribute.name.generic.max_health`.
const ATTRIBUTE_LANG_PREFIX: &str = "attribute.name.";
/// The game's potions' item names: `item.minecraft.potion.effect.<name>`, a Yog potion alias.
const POTION_LANG_PREFIX: &str = "potion.effect.";

const GAME_IDS: [(&str, GameIds); 12] = [
    (
        yog_ids::kind::ITEM,
        GameIds::Lang {
            kinds: &[yog_ids::kind::ITEM, yog_ids::kind::BLOCK],
            dotted: false,
        },
    ),
    (
        yog_ids::kind::BLOCK,
        GameIds::Lang {
            kinds: &[yog_ids::kind::BLOCK],
            dotted: false,
        },
    ),
    (
        yog_ids::kind::ENTITY,
        GameIds::Lang {
            kinds: &[yog_ids::kind::ENTITY],
            dotted: false,
        },
    ),
    (
        yog_ids::kind::EFFECT,
        GameIds::Lang {
            kinds: &[yog_ids::kind::EFFECT],
            dotted: false,
        },
    ),
    (
        yog_ids::kind::ATTRIBUTE,
        GameIds::LangPrefix(ATTRIBUTE_LANG_PREFIX),
    ),
    (
        yog_ids::kind::BIOME,
        GameIds::Lang {
            kinds: &[yog_ids::kind::BIOME],
            dotted: false,
        },
    ),
    (
        ENCHANTMENT,
        GameIds::Lang {
            kinds: &[ENCHANTMENT],
            dotted: false,
        },
    ),
    (yog_ids::kind::LOOT_TABLE, GameIds::Data),
    (yog_ids::kind::ADVANCEMENT, GameIds::Data),
    (yog_ids::kind::DAMAGE_TYPE, GameIds::Data),
    (yog_ids::kind::RECIPE, GameIds::Data),
    (SOUND, GameIds::Keys(SOUNDS_ASSET)),
];

/// Kinds of the game's content a reference may name that no Yog file defines.
const ENCHANTMENT: &str = "enchantment";
const SOUND: &str = "sound";

/// The kinds whose game tags a reference may name (`tag:<kind>`).
const GAME_TAG_KINDS: [&str; 6] = [
    yog_ids::kind::ITEM,
    yog_ids::kind::BLOCK,
    yog_ids::kind::ENTITY,
    yog_ids::kind::FLUID,
    yog_ids::kind::BIOME,
    yog_ids::kind::DAMAGE_TYPE,
];

/// One of the mod's own: its id and the file that defines it.
#[derive(Debug, Clone, PartialEq, yog_data::Schema)]
pub struct OwnRef {
    pub id: String,
    pub file: String,
}

/// What a reference of one kind may name, ids with their namespace.
#[derive(Debug, Default, yog_data::Schema)]
pub struct RefChoices {
    pub own: Vec<OwnRef>,
    /// Declared in `yog.toml`, another mod's.
    pub external: Vec<String>,
    /// The game's (`minecraft:…`); empty without a client jar.
    pub game: Vec<String>,
}

/// One of the mod's `.ydef`s: its id, its file and what it says.
#[derive(Debug, yog_data::Schema)]
pub struct OwnDef {
    pub id: String,
    pub file: String,
    pub value: Value,
}

fn files(root: &Path, format: Format) -> Vec<PathBuf> {
    crate::files::of(&root.join(yog_ids::project::CONTENT), format)
}

/// A content file that's one object.
fn read(file: &Path) -> Option<Value> {
    yog_data::text::parse_value(&crate::files::read_source(file).ok()?)
        .ok()
        .filter(|v| matches!(v, Value::Map(_)))
}

/// The choices for `@<kind>:` in the file at `file` (`kind` may also be a
/// body format, `ymodel`/`yanim`, or `tag:<kind>` — the tags of a kind:
/// the mod's own `.ydef` tags of it, the game's).
pub fn refs(file: &Path, kind: &str) -> RefChoices {
    let mut out = RefChoices::default();
    if let Some(root) = mod_root(file) {
        out.own = own(&root, kind);
        out.external = external(&root, kind);
    }
    if body_format(kind).is_none() {
        out.game = game(mod_root(file).as_deref(), kind);
    }
    out
}

/// `kind` as a body format (`ymodel`, `yanim`).
fn body_format(kind: &str) -> Option<Format> {
    Format::of(kind).filter(|f| BODY_FORMATS.contains(f))
}

fn own(root: &Path, kind: &str) -> Vec<OwnRef> {
    let ns = crate::design::project::mod_id(root).unwrap_or_else(|| yog_ids::ns::MINECRAFT.into());
    let mut out = Vec::new();
    let tag_of = format!("{}{}", yog_ids::typed::TAG, yog_ids::sep::NAMESPACE);
    if let Some(of) = kind.strip_prefix(tag_of.as_str()) {
        for d in defs(root, yog_ids::kind::TAG) {
            if d.value
                .get(yog_ids::content_field::OF)
                .and_then(Value::as_str)
                == Some(of)
            {
                out.push(OwnRef {
                    id: d.id,
                    file: d.file,
                });
            }
        }
        return out;
    }
    // Files named by their path: body files under content/, resources under resources/.
    let by_path = body_format(kind)
        .map(|f| (yog_ids::project::CONTENT, f))
        .or_else(|| {
            RESOURCE_KINDS
                .iter()
                .find(|(k, _)| *k == kind)
                .map(|(_, f)| (yog_ids::project::RESOURCES, *f))
        });
    if let Some((dir, format)) = by_path {
        let dir = root.join(dir);
        for f in crate::files::of(&dir, format) {
            let rel = f.strip_prefix(&dir).unwrap_or(&f).with_extension("");
            out.push(OwnRef {
                id: yog_ids::join(
                    &ns,
                    &rel.to_string_lossy().replace('\\', yog_ids::sep::PATH),
                ),
                file: f.to_string_lossy().into_owned(),
            });
        }
        return out;
    }
    if let Some((_, exts)) = FILE_KINDS.iter().find(|(k, _)| *k == kind) {
        for format in *exts {
            for f in files(root, *format) {
                let given = read(&f).and_then(|d| {
                    d.get(yog_ids::content_field::ID)
                        .and_then(Value::as_str)
                        .map(str::to_string)
                });
                let id = given.unwrap_or_else(|| {
                    f.file_stem()
                        .unwrap_or_default()
                        .to_string_lossy()
                        .into_owned()
                });
                out.push(OwnRef {
                    id: yog_ids::qualify_in(&ns, &id),
                    file: f.to_string_lossy().into_owned(),
                });
            }
        }
    } else {
        out = defs(root, kind)
            .into_iter()
            .map(|d| OwnRef {
                id: d.id,
                file: d.file,
            })
            .collect();
    }
    out.sort_by(|a, b| a.id.cmp(&b.id));
    out.dedup_by(|a, b| a.id == b.id);
    out
}

/// The mod's `.ydef`s of `kind`, in file order.
fn defs(root: &Path, kind: &str) -> Vec<OwnDef> {
    let ns = crate::design::project::mod_id(root).unwrap_or_else(|| yog_ids::ns::MINECRAFT.into());
    let mut out = Vec::new();
    for f in files(root, Format::Def) {
        let Some(doc) = read(&f) else { continue };
        if doc
            .get(yog_ids::content_field::KIND)
            .and_then(Value::as_str)
            != Some(kind)
        {
            continue;
        }
        let Some(id) = doc.get(yog_ids::content_field::ID).and_then(Value::as_str) else {
            continue;
        };
        out.push(OwnDef {
            id: yog_ids::qualify_in(&ns, id),
            file: f.to_string_lossy().into_owned(),
            value: doc,
        });
    }
    out
}

/// The mod's `.ydef`s of `kind` beside the file at `file`.
pub fn own_defs(file: &Path, kind: &str) -> Vec<OwnDef> {
    mod_root(file).map(|r| defs(&r, kind)).unwrap_or_default()
}

fn external(root: &Path, kind: &str) -> Vec<String> {
    let Some(kind) = crate::content::refs::RefKind::from_word(kind) else {
        return Vec::new();
    };
    let Ok(manifest) = crate::manifest::YogToml::read(&project::path(root)) else {
        return Vec::new();
    };
    let mut out = Vec::new();
    for external in &manifest.externals {
        for (_, ids) in external.ids.iter().filter(|(k, _)| *k == kind) {
            out.extend(
                ids.iter()
                    .map(|id| yog_ids::qualify_in(&external.namespace, id)),
            );
        }
    }
    out
}

/// The game's ids of `kind`, from the client jar (cached per jar).
fn game(root: Option<&Path>, kind: &str) -> Vec<String> {
    let tag_of = format!("{}{}", yog_ids::typed::TAG, yog_ids::sep::NAMESPACE);
    let tags = kind
        .strip_prefix(tag_of.as_str())
        .filter(|of| GAME_TAG_KINDS.contains(of))
        .map(yog_ids::registry_dirs);
    let how = GAME_IDS.iter().find(|(k, _)| *k == kind).map(|(_, h)| h);
    if tags.is_none() && how.is_none() {
        return Vec::new();
    }
    let Some(vanilla) = Resources::for_mod(root).vanilla else {
        return Vec::new();
    };
    type Cache = Mutex<HashMap<(PathBuf, String), Vec<String>>>;
    static CACHE: OnceLock<Cache> = OnceLock::new();
    let key = (vanilla.jar.clone(), kind.to_string());
    if let Some(hit) = CACHE
        .get_or_init(Default::default)
        .lock()
        .get(&key)
        .cloned()
    {
        return hit;
    }
    let mut out: Vec<String> = match (tags, how) {
        (Some(dirs), _) => data_ids(
            &vanilla,
            dirs.iter()
                .map(|d| format!("{}/{d}", yog_ids::pack::TAGS))
                .collect(),
        ),
        (None, None) => Vec::new(),
        (None, Some(GameIds::Lang { kinds, dotted })) => {
            let prefixes = kinds
                .iter()
                .map(|k| yog_ids::lang_prefix(k, yog_ids::ns::MINECRAFT))
                .collect();
            lang_ids(&vanilla, kind, prefixes, *dotted)
        }
        (None, Some(GameIds::LangPrefix(prefix))) => {
            lang_ids(&vanilla, kind, vec![prefix.to_string()], true)
        }
        (None, Some(GameIds::Data)) => data_ids(&vanilla, yog_ids::registry_dirs(kind)),
        (None, Some(GameIds::Keys(asset))) => {
            asset_keys(&vanilla, &format!("{}/{asset}", yog_ids::ns::MINECRAFT))
                .keys()
                .map(|k| game_id(k))
                .collect()
        }
    };
    out.sort();
    out.dedup();
    CACHE
        .get_or_init(Default::default)
        .lock()
        .insert(key, out.clone());
    out
}

/// `path` as the game's id.
fn game_id(path: &str) -> String {
    yog_ids::join(yog_ids::ns::MINECRAFT, path)
}

/// The keys of the jar's JSON asset `asset` (the first of the jar's copies that reads).
fn asset_keys(vanilla: &Vanilla, asset: &str) -> serde_json::Map<String, serde_json::Value> {
    vanilla
        .read_all(asset)
        .into_iter()
        .find_map(|b| serde_json::from_slice::<serde_json::Map<String, serde_json::Value>>(&b).ok())
        .unwrap_or_default()
}

/// The `.json` files under the game's `data/minecraft/<dir>/`, as ids.
fn data_ids(vanilla: &Vanilla, dirs: Vec<String>) -> Vec<String> {
    dirs.iter()
        .flat_map(|d| {
            vanilla.names_under(&format!(
                "{}/{}/{d}/",
                yog_ids::pack::DATA,
                yog_ids::ns::MINECRAFT
            ))
        })
        .filter_map(|n| n.strip_suffix(yog_ids::res::JSON).map(game_id))
        .collect()
}

/// The ids the game's lang keys under `prefixes` name (`dotted`: the rest
/// may hold dots); for items, a potion's key is Yog's alias of it.
fn lang_ids(vanilla: &Vanilla, kind: &str, prefixes: Vec<String>, dotted: bool) -> Vec<String> {
    let lang = format!(
        "{}/{}",
        yog_ids::ns::MINECRAFT,
        yog_ids::lang_file(yog_ids::game::LOCALE)
    );
    asset_keys(vanilla, &lang)
        .keys()
        .filter_map(|k| prefixes.iter().find_map(|p| k.strip_prefix(p.as_str())))
        .filter(|rest| rest.starts_with(POTION_LANG_PREFIX) || dotted || !rest.contains('.'))
        .map(|rest| {
            match rest
                .strip_prefix(POTION_LANG_PREFIX)
                .filter(|_| kind == yog_ids::kind::ITEM)
            {
                // `item.minecraft.potion.effect.swiftness` → `yog:potion/swiftness`.
                Some(name) => format!("{}{name}", yog_ids::yog::POTION_ALIAS),
                None => game_id(rest),
            }
        })
        .collect()
}

/// The game's kinds of block (`kind: "stairs"` on a `.yblock`): their
/// names, texture slots, the `block` fields they take and their defaults —
/// as `yog-formats` has them.
pub fn block_kinds() -> Value {
    use yog_data::Schema;
    Value::List(
        yog_formats::content::BlockKind::built_ins()
            .iter()
            .map(Schema::write)
            .collect(),
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    fn write(root: &Path, rel: &str, text: &str) {
        let p = root.join(rel);
        std::fs::create_dir_all(p.parent().unwrap()).unwrap();
        std::fs::write(p, text).unwrap();
    }

    #[test]
    fn a_mods_own_content_external_ids_and_defs() {
        let dir = std::env::temp_dir().join(format!("yog-idle-refs-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        write(
            &dir,
            "yog.toml",
            "[mod]\nid = \"mymod\"\n\n[external.create]\nentities = [\"seat\"]\n",
        );
        write(&dir, "content/items/wand.yitem", "{ name: \"Wand\" }");
        write(
            &dir,
            "content/items/potion.yitem",
            "{ item: { potion: { effects: [] } } }",
        );
        write(
            &dir,
            "content/blocks/lamp.yblock",
            "// a lamp\n{ id: \"glow_lamp\" }",
        );
        write(
            &dir,
            "content/mobs/wisp.yentity",
            "{ kind: \"mob\", width: 0.5, height: 0.5 }",
        );
        write(
            &dir,
            "content/body/wisp.ymodel",
            "{ texture: \"\", bones: [] }",
        );
        write(
            &dir,
            "content/fx/glow.ydef",
            "{ kind: \"effect\", id: \"glow\", category: \"beneficial\", color: 255 }",
        );
        write(
            &dir,
            "content/fx/dim.ydef",
            "{ kind: \"attribute\", id: \"dim\", default: 1, min: 0, max: 2 }",
        );
        write(
            &dir,
            "content/tags/plants.ydef",
            "{ kind: \"tag\", of: \"block\", id: \"plants\", values: [] }",
        );
        let probe = dir.join("content/items/wand.yitem");
        let ids = |kind: &str| {
            refs(&probe, kind)
                .own
                .into_iter()
                .map(|r| r.id)
                .collect::<Vec<_>>()
        };
        assert_eq!(
            ids("item"),
            ["mymod:glow_lamp", "mymod:potion", "mymod:wand"],
            "items include potion-only .yitems and a block's item form"
        );
        assert_eq!(ids("block"), ["mymod:glow_lamp"]);
        assert_eq!(ids("entity"), ["mymod:wisp"]);
        assert_eq!(ids("ymodel"), ["mymod:body/wisp"]);
        assert_eq!(ids("effect"), ["mymod:glow"]);
        assert_eq!(refs(&probe, "entity").external, ["create:seat"]);
        assert_eq!(ids("tag:block"), ["mymod:plants"]);
        assert!(ids("tag:item").is_empty());
        let effects = own_defs(&probe, "effect");
        assert_eq!(effects.len(), 1);
        assert_eq!(effects[0].value.get("color"), Some(&Value::Int(255)));
        std::fs::remove_dir_all(&dir).unwrap();
    }

    #[test]
    fn the_games_block_kinds_are_listed() {
        let kinds = block_kinds();
        let names: Vec<&str> = kinds
            .as_list()
            .unwrap()
            .iter()
            .filter_map(|k| k.get("name").and_then(Value::as_str))
            .collect();
        assert!(
            names.contains(&"stairs") && names.contains(&"pressure_plate"),
            "{names:?}"
        );
    }
}