Skip to main content

yog_cli/design/
font_copy.rs

1//! A game font made the mod's own to edit: Yog's alias of it
2//! (`yog:font/alt`) → its copy, `content/font/alt.yfont`, made by
3//! `yog import font` unless it's there already.
4
5use std::path::{Path, PathBuf};
6
7use yog_ids::Format;
8
9use super::project;
10
11/// The mod's copy of a game font: its id, and its file.
12#[derive(yog_data::Schema)]
13pub struct Copy {
14    pub id: String,
15    pub file: PathBuf,
16}
17
18/// The mod (of `file`)'s copy of the game font `alias` names, made first
19/// if it has none.
20pub fn copy(file: &Path, alias: &str) -> Result<Copy, String> {
21    let root = project::mod_root(file).ok_or("not in a mod (no yog.toml)")?;
22    let mod_id = project::mod_id(&root).ok_or("the mod has no id")?;
23    let game = yog_ids::game_font_of(alias)
24        .ok_or_else(|| format!("{alias} isn't Yog's alias of a game font"))?;
25    let path = crate::content::font_copy_path(yog_ids::split(&game).1);
26    let copy = Copy {
27        id: yog_ids::join(&mod_id, &path),
28        file: root
29            .join(yog_ids::project::CONTENT)
30            .join(Format::Font.file(&path)),
31    };
32    if copy.file.is_file() {
33        return Ok(copy);
34    }
35    let out = super::this_cli()
36        .current_dir(&root)
37        .args(["import", yog_ids::kind::FONT, alias])
38        .output()
39        .map_err(|e| format!("failed to run yog: {e}"))?;
40    if !out.status.success() {
41        return Err(String::from_utf8_lossy(&out.stderr)
42            .replace("yog: error: ", "")
43            .trim()
44            .to_string());
45    }
46    Ok(copy)
47}