1use yog_sync::LazyLock;
5
6#[derive(yog_data::Schema, Debug, Clone, PartialEq)]
8pub struct Atlas {
9 pub id: String,
11 pub description: String,
12 pub since: Option<String>,
14 #[data(default)]
16 pub dirs: Vec<String>,
17 #[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 pub fn game() -> &'static [Atlas] {
32 &ATLASES
33 }
34
35 pub fn of_path(path: &str) -> Option<&'static Atlas> {
37 ATLASES.iter().find(|a| a.dir_of(path).is_some())
38 }
39
40 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 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 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}