yog-cli 0.51.2

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
//! A mod's media as the tools see them: its `.yres` pictures (bytes and
//! all), its `.ytexture`s, and the files the build makes of them — so the
//! IDE previews and edits what the build will make, converting nothing
//! itself. Reading is cached until a file changes.

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::time::SystemTime;
use yog_sync::{Arc, Mutex, OnceLock};

use yog_data::Res;
use yog_formats::texture::{Animation, TextureFile};
use yog_formats::yres::{Entry, Kind, Value, split_file_name};
use yog_ids::Format;

/// One picture of a `.yres` set.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Picture {
    /// Its set (`Images` for `Images.yres`) and name: `@Images.gui.logo`.
    pub(crate) set: String,
    pub(crate) name: String,
    /// The `.yres` file it's in.
    pub(crate) file: PathBuf,
    /// The picture, as stored.
    pub(crate) bytes: Arc<Vec<u8>>,
}

impl Picture {
    /// Its path under the mod's textures, as the build writes it: `gui.logo` → `gui/logo`.
    pub(crate) fn path(&self) -> String {
        self.name.replace('.', yog_ids::sep::PATH)
    }
}

/// One of the mod's textures.
#[derive(Debug, Clone)]
pub(crate) struct Texture {
    pub(crate) id: String,
    /// Its `.ytexture`.
    pub(crate) file: PathBuf,
    pub(crate) texture: TextureFile,
}

impl Texture {
    /// Its picture, when its `.yres` has it.
    pub(crate) fn picture(&self, root: &Path) -> Option<Picture> {
        picture(root, &self.texture.image.set, &self.texture.image.key)
    }
}

/// The id of the mod at `root` (its manifest's).
pub(crate) fn mod_id(root: &Path) -> Option<String> {
    crate::manifest::YogToml::read(&root.join(yog_ids::file::MANIFEST))
        .ok()
        .map(|m| m.id)
}

type Cache = Mutex<HashMap<PathBuf, (SystemTime, Arc<Vec<Picture>>)>>;
static PICTURES: OnceLock<Cache> = OnceLock::new();

/// The pictures of the `.yres` file at `file` (a set's neutral one).
fn pictures_of(file: &Path, set: &str) -> Arc<Vec<Picture>> {
    let modified = std::fs::metadata(file)
        .and_then(|m| m.modified())
        .unwrap_or(SystemTime::UNIX_EPOCH);
    let cache = PICTURES.get_or_init(Default::default);
    if let Some((at, pictures)) = cache.lock().get(file).cloned()
        && at == modified
    {
        return pictures;
    }
    let entries = crate::files::read_source(file)
        .ok()
        .and_then(|t| yog_formats::yres::parse(&t).ok())
        .unwrap_or_default();
    let pictures: Arc<Vec<Picture>> = Arc::new(
        entries
            .into_iter()
            .filter_map(|e| match (e.kind, e.value) {
                (Kind::Image, Value::Bytes(b)) => Some(Picture {
                    set: set.to_string(),
                    name: e.name,
                    file: file.to_path_buf(),
                    bytes: Arc::new(b),
                }),
                _ => None,
            })
            .collect(),
    );
    let mut c = cache.lock();
    c.insert(file.to_path_buf(), (modified, pictures.clone()));
    pictures
}

/// Every picture of the mod at `root`.
pub(crate) fn pictures(root: &Path) -> Vec<Picture> {
    let mut out = Vec::new();
    for file in crate::files::with_ext(&root.join(yog_ids::project::RESOURCES), Format::Res.ext()) {
        // A culture variant (`Images.ru_ru.yres`) has no pictures of its own.
        let Some((set, None)) = file
            .file_name()
            .and_then(|n| n.to_str())
            .and_then(split_file_name)
        else {
            continue;
        };
        out.extend(pictures_of(&file, set).iter().cloned());
    }
    out
}

/// Picture `@<set>.<name>` of the mod at `root`.
pub(crate) fn picture(root: &Path, set: &str, name: &str) -> Option<Picture> {
    let file = root
        .join(yog_ids::project::RESOURCES)
        .join(Format::Res.file(set));
    pictures_of(&file, set)
        .iter()
        .find(|p| p.name == name)
        .cloned()
}

/// Sound `@<set>.<name>` of the mod at `root`, as stored (any format the build converts).
pub(crate) fn sound(root: &Path, set: &str, name: &str) -> Option<Vec<u8>> {
    stored(root, set, name, Kind::Sound)
}

/// Video `@<set>.<name>` of the mod at `root`, as stored (any format ffmpeg reads).
pub(crate) fn video(root: &Path, set: &str, name: &str) -> Option<Vec<u8>> {
    stored(root, set, name, Kind::Video)
}

/// The bytes of entry `@<set>.<name>` of `kind` of the mod at `root`.
fn stored(root: &Path, set: &str, name: &str, kind: Kind) -> Option<Vec<u8>> {
    let file = root
        .join(yog_ids::project::RESOURCES)
        .join(Format::Res.file(set));
    let text = crate::files::read_source(&file).ok()?;
    yog_formats::yres::parse(&text)
        .ok()?
        .into_iter()
        .find(|e| e.name == name && e.kind == kind)
        .and_then(|e| match e.value {
            Value::Bytes(b) => Some(b),
            _ => None,
        })
}

/// Every sound of the mod at `root`, as files name them: `@Sounds.abacus1`.
pub(crate) fn sounds(root: &Path) -> Vec<String> {
    entries_of(root, Kind::Sound)
}

/// Every picture of the mod at `root`, as files name them: `@Images.logo`.
pub(crate) fn images(root: &Path) -> Vec<String> {
    entries_of(root, Kind::Image)
}

/// Every entry of `kind` of the mod at `root`, as files name them.
fn entries_of(root: &Path, kind: Kind) -> Vec<String> {
    resources_of(root, kind)
        .iter()
        .map(Res::to_string)
        .collect()
}

/// Every entry of `kind` of the mod at `root`, sorted.
pub(crate) fn resources_of(root: &Path, kind: Kind) -> Vec<Res> {
    let mut out = Vec::new();
    for file in crate::files::with_ext(&root.join(yog_ids::project::RESOURCES), Format::Res.ext()) {
        let Some((set, None)) = file
            .file_name()
            .and_then(|n| n.to_str())
            .and_then(split_file_name)
        else {
            continue;
        };
        let entries = crate::files::read_source(&file)
            .ok()
            .and_then(|t| yog_formats::yres::parse(&t).ok())
            .unwrap_or_default();
        out.extend(entries.into_iter().filter(|e| e.kind == kind).map(|e| Res {
            set: set.to_owned(),
            key: e.name,
        }));
    }
    out.sort();
    out
}

/// Every sound file the game has (`minecraft:dig/sand1`), from its assets on this machine.
pub(crate) fn game_sounds() -> Vec<String> {
    let prefix = format!(
        "{}{}{}",
        yog_ids::ns::MINECRAFT,
        yog_ids::sep::PATH,
        yog_ids::res::SOUNDS
    );
    crate::design::assets::vanilla(&[])
        .map(|v| v.object_names(&prefix))
        .unwrap_or_default()
        .iter()
        .filter_map(|k| {
            k.strip_prefix(&prefix)?
                .strip_suffix(yog_ids::res::OGG)
                .map(|p| yog_ids::join(yog_ids::ns::MINECRAFT, p))
        })
        .collect()
}

/// The game's sound file `id` (`minecraft:dig/sand1`, no namespace: the
/// game's), from its assets on this machine.
pub(crate) fn game_sound(id: &str) -> Option<Vec<u8>> {
    let (ns, path) = yog_ids::split(id);
    let key = format!(
        "{ns}{}{}{path}{}",
        yog_ids::sep::PATH,
        yog_ids::res::SOUNDS,
        yog_ids::res::OGG
    );
    crate::design::assets::vanilla(&[]).and_then(|v| v.read_all(&key).pop())
}

/// Writes `bytes` as picture `@<set>.<name>` of the mod at `root` (the
/// set's file made when there's none); the file written.
pub(crate) fn save_picture(
    root: &Path,
    set: &str,
    name: &str,
    bytes: Vec<u8>,
) -> Result<PathBuf, String> {
    let file = root
        .join(yog_ids::project::RESOURCES)
        .join(Format::Res.file(set));
    let mut entries = match crate::files::read_source(&file) {
        Ok(text) => {
            yog_formats::yres::parse(&text).map_err(|e| format!("{}: {e}", file.display()))?
        }
        Err(_) => Vec::new(),
    };
    match entries.iter_mut().find(|e| e.name == name) {
        Some(e) => e.value = Value::Bytes(bytes),
        None => entries.push(Entry::picture(name, bytes)),
    }
    if let Some(dir) = file.parent() {
        std::fs::create_dir_all(dir).map_err(|e| format!("{}: {e}", dir.display()))?;
    }
    crate::files::write_source(&file, &yog_formats::yres::write(&entries))?;
    Ok(file)
}

/// Where texture `id` of the mod at `root` is described, whether it is yet or not.
fn texture_file(root: &Path, id: &str) -> Option<PathBuf> {
    let (ns, path) = yog_ids::split(id);
    (mod_id(root)? == ns).then(|| {
        root.join(yog_ids::project::RESOURCES)
            .join(Format::Texture.file(path))
    })
}

/// Texture `id` of the mod at `root`, when it has a `.ytexture`.
pub(crate) fn texture(root: &Path, id: &str) -> Option<Texture> {
    let file = texture_file(root, id)?;
    let texture = yog_data::from_text::<TextureFile>(&crate::files::read_source(&file).ok()?)
        .ok()?
        .value;
    Some(Texture {
        id: yog_ids::qualify(id),
        file,
        texture,
    })
}

/// Every texture of the mod at `root`.
pub(crate) fn textures(root: &Path) -> Vec<Texture> {
    let Some(ns) = mod_id(root) else {
        return Vec::new();
    };
    let resources = root.join(yog_ids::project::RESOURCES);
    crate::files::with_ext(&resources, Format::Texture.ext())
        .into_iter()
        .filter_map(|f| {
            let rel = f
                .strip_prefix(&resources)
                .ok()?
                .to_string_lossy()
                .replace('\\', yog_ids::sep::PATH);
            texture(root, &yog_formats::texture::id_of(&ns, &rel)?)
        })
        .collect()
}

/// Whether texture `id` is the mod's own (it can be painted in place, or added).
pub(crate) fn is_own_texture(root: &Path, id: &str) -> bool {
    texture_file(root, id).is_some()
}

/// Writes `png` as texture `id`'s picture: into the `.yres` picture its
/// `.ytexture` names, or — a new texture — into the set its folder's
/// pictures go in, with a `.ytexture` naming it. The `.yres` written.
pub(crate) fn save_texture_picture(root: &Path, id: &str, png: Vec<u8>) -> Result<PathBuf, String> {
    let file = texture_file(root, id).ok_or_else(|| format!("{id} isn't a texture of this mod"))?;
    if let Some(own) = texture(root, id) {
        return save_picture(root, &own.texture.image.set, &own.texture.image.key, png);
    }
    let path = yog_ids::split(id).1;
    let image = Res {
        set: yog_formats::texture::picture_set(path),
        key: path.replace(yog_ids::sep::PATH, "."),
    };
    let written = save_picture(root, &image.set, &image.key, png)?;
    let texture = TextureFile {
        image,
        atlas: yog_registry::Atlas::of_path(path).map(|a| a.id.clone()),
        animation: None,
        blur: false,
        clamp: false,
    };
    write_texture(&file, &texture)?;
    Ok(written)
}

/// Sets texture `id`'s animation (`None`: still); the `.ytexture` written.
pub(crate) fn save_texture_animation(
    root: &Path,
    id: &str,
    animation: Option<Animation>,
) -> Result<PathBuf, String> {
    let mut own = texture(root, id)
        .ok_or_else(|| format!("{id} has no .{} in this mod", Format::Texture.ext()))?;
    own.texture.animation = animation;
    write_texture(&own.file, &own.texture)?;
    Ok(own.file)
}

fn write_texture(file: &Path, texture: &TextureFile) -> Result<(), String> {
    if let Some(dir) = file.parent() {
        std::fs::create_dir_all(dir).map_err(|e| format!("{}: {e}", dir.display()))?;
    }
    std::fs::write(file, yog_data::to_bytes(texture))
        .map_err(|e| format!("{}: {e}", file.display()))
}

/// The file the build makes at `assets/<ns>/<path>` from the mod at `root`
/// — a texture's PNG or `.png.mcmeta` (a `.ytexture`'s picture and
/// animation, else a `.yres` picture of that path) — `None` when the build
/// makes none there.
pub(crate) fn built_asset(root: &Path, ns: &str, path: &str) -> Option<Vec<u8>> {
    if mod_id(root)? != ns {
        return None;
    }
    let file = path.strip_prefix(yog_ids::res::TEXTURES)?;
    if let Some(stem) = file
        .strip_suffix(yog_ids::res::MCMETA)
        .and_then(|f| f.strip_suffix(yog_ids::res::PNG))
    {
        let meta = texture(root, &yog_ids::join(ns, stem))?
            .texture
            .game_mcmeta()?;
        return serde_json::to_vec(&meta).ok();
    }
    let stem = file.strip_suffix(yog_ids::res::PNG)?;
    let picture = match texture(root, &yog_ids::join(ns, stem)) {
        Some(own) => own.picture(root)?,
        None => pictures(root).into_iter().find(|p| p.path() == stem)?,
    };
    // The game takes PNG; a picture stored as anything else is converted, as the build does.
    let image = yog_media::image::decode(&picture.bytes).ok()?;
    yog_media::image::encode_png(&image).ok()
}

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

    fn png() -> Vec<u8> {
        yog_media::image::encode_png(&yog_media::image::Image {
            width: 1,
            height: 1,
            rgba: vec![9, 9, 9, 255],
        })
        .unwrap()
    }

    #[test]
    fn a_painted_texture_becomes_a_picture_and_a_ytexture() {
        let dir = std::env::temp_dir().join(format!("yog-media-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(dir.join(yog_ids::file::MANIFEST), "[mod]\nid = \"mymod\"\n").unwrap();
        save_texture_picture(&dir, "mymod:block/ruby", png()).unwrap();
        let own = texture(&dir, "mymod:block/ruby").unwrap();
        assert_eq!(own.texture.atlas.as_deref(), Some("minecraft:blocks"));
        assert_eq!(
            own.picture(&dir).unwrap().bytes.as_slice(),
            png().as_slice()
        );
        assert_eq!(
            built_asset(&dir, "mymod", "textures/block/ruby.png"),
            Some(png())
        );
        save_texture_animation(
            &dir,
            "mymod:block/ruby",
            Some(Animation {
                frame_time: Some(4),
                ..Animation::default()
            }),
        )
        .unwrap();
        assert!(built_asset(&dir, "mymod", "textures/block/ruby.png.mcmeta").is_some());
        assert_eq!(textures(&dir).len(), 1);
        assert!(!is_own_texture(&dir, "minecraft:block/stone"));
        assert!(pictures(&dir).iter().any(|p| p.path() == "block/ruby"));
        let _ = std::fs::remove_dir_all(&dir);
    }
}