Skip to main content

yog_registry/
atlases.rs

1//! The game's texture atlases as data (`assets/atlases.ydata`): what a
2//! `.ytexture` can be packed into.
3
4use yog_sync::LazyLock;
5
6/// One of the game's atlases.
7#[derive(yog_data::Schema, Debug, Clone, PartialEq)]
8pub struct Atlas {
9    /// `minecraft:blocks`: its definition is `assets/minecraft/atlases/blocks.json`.
10    pub id: String,
11    pub description: String,
12    /// The first version that has it (every version Yog supports if none).
13    pub since: Option<String>,
14    /// The texture folders the game puts in it whole.
15    #[data(default)]
16    pub dirs: Vec<String>,
17    /// Its sprites are named without their folder: `mob_effect/speed` is sprite `speed`.
18    #[data(default)]
19    pub bare: bool,
20}
21
22static ATLASES: LazyLock<Vec<Atlas>> = LazyLock::new(|| {
23    yog_data::built_in::<Vec<Atlas>>(
24        "yog-registry's atlases",
25        include_str!("../assets/atlases.ydata"),
26    )
27});
28
29impl Atlas {
30    /// The game's.
31    pub fn game() -> &'static [Atlas] {
32        &ATLASES
33    }
34
35    /// The atlas a texture at `path` (`block/ruby`) belongs to by its folder.
36    pub fn of_path(path: &str) -> Option<&'static Atlas> {
37        ATLASES.iter().find(|a| a.dir_of(path).is_some())
38    }
39
40    /// Which of its folders `path` is in, and the rest of it.
41    fn dir_of<'p>(&self, path: &'p str) -> Option<&'p str> {
42        self.dirs.iter().find_map(|d| {
43            path.strip_prefix(d.as_str())?
44                .strip_prefix(yog_ids::sep::PATH)
45        })
46    }
47
48    /// The sprite texture `path` is in this atlas: the path itself, or — a
49    /// bare atlas's folder — the rest of it (`mob_effect/speed` → `speed`).
50    pub fn sprite<'p>(&self, path: &'p str) -> &'p str {
51        if self.bare {
52            self.dir_of(path).unwrap_or(path)
53        } else {
54            path
55        }
56    }
57
58    /// One of the game's, by id (`blocks` = `minecraft:blocks`).
59    pub fn find(id: &str) -> Option<&'static Atlas> {
60        let id = yog_ids::qualify(id);
61        ATLASES.iter().find(|a| a.id == id)
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    #[test]
68    fn the_games_atlases_read() {
69        assert!(super::Atlas::find("blocks").is_some());
70        assert_eq!(
71            super::Atlas::find("minecraft:gui").and_then(|a| a.since.as_deref()),
72            Some("1.20.2")
73        );
74        assert_eq!(
75            super::Atlas::of_path("item/staff/oak").map(|a| a.id.as_str()),
76            Some("minecraft:blocks")
77        );
78        assert!(super::Atlas::of_path("entity/wisp").is_none());
79        let effects = super::Atlas::of_path("mob_effect/clarity").unwrap();
80        assert_eq!(effects.sprite("mob_effect/clarity"), "clarity");
81        assert_eq!(
82            super::Atlas::find("blocks").unwrap().sprite("block/ruby"),
83            "block/ruby"
84        );
85        assert_eq!(
86            super::Atlas::of_path("gui/sprites/hud/x").map(|a| a.id.as_str()),
87            Some("minecraft:gui")
88        );
89    }
90}