aboutsummaryrefslogtreecommitdiff
path: root/games/rstnode/rst-client/src/assets.rs
blob: 76556213fc700cedaf4d6d0ab9e0b9fe3a052acb (plain)
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
use crate::{error::LoadError, GameSettings};
use cairo::{Context, Format, ImageSurface, Rectangle};
use ggez::graphics::Image;
use librsvg::{CairoRenderer, Loader};
use std::{
    collections::BTreeMap,
    ffi::OsStr,
    fs::{read_dir, File},
    io::BufWriter,
    io::Read,
    path::{Path, PathBuf},
};
use tempfile::tempdir;

pub type Result<T> = std::result::Result<T, LoadError>;

#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct URI(String, String);

impl From<&'static str> for URI {
    fn from(s: &'static str) -> Self {
        let mut v: Vec<_> = s.split("/").collect();
        Self(v.remove(0).into(), v.remove(0).into())
    }
}

impl From<String> for URI {
    fn from(s: String) -> Self {
        let mut v: Vec<_> = s.split("/").collect();
        Self(v.remove(0).into(), v.remove(0).into())
    }
}

/// Asset loader
#[derive(Debug)]
pub struct Assets {
    inner: BTreeMap<URI, Image>,
}

impl Assets {
    fn new() -> Self {
        Self {
            inner: Default::default(),
        }
    }

    pub fn find<U: Into<URI>>(&self, u: U) -> Option<Image> {
        self.inner.get(&u.into()).map(|i| i.clone())
    }

    /// Load an asset directory path
    fn load_tree(&mut self, ctx: &mut ggez::Context, tmpdir: &Path, p: &Path) -> Result<()> {
        let err: LoadError = p.to_str().unwrap().into();

        read_dir(p)
            .map_err(|_| err)?
            .map(|e| {
                let e = e.unwrap();
                let p = e.path();

                let ext = OsStr::new("svg");

                if p.is_dir() {
                    debug!(
                        "Entering directory {}",
                        p.file_name().unwrap().to_str().unwrap()
                    );
                    self.load_tree(ctx, tmpdir, p.as_path())?;
                } else if p.extension() == Some(ext) {
                    let png = load_svg(tmpdir, p.as_path())?;

                    let basepath = p.with_extension("");

                    let uri_cat = p.parent().unwrap().file_name().unwrap().to_str().unwrap();
                    let name = basepath.file_name().unwrap().to_str().unwrap();
                    let uri = format!("{}/{}", uri_cat, name);
                    let path_str = png.as_path().to_str().unwrap();

                    let mut content = vec![];
                    let mut f = File::open(png.as_path()).map_err(|_| {
                        LoadError::from(format!("No such file: {}", path_str).as_str())
                    })?;

                    f.read_to_end(&mut content).map_err(|e| {
                        LoadError::from(
                            format!("Read error for {}: {}", path_str, e.to_string()).as_str(),
                        )
                    })?;

                    self.inner.insert(
                        uri.into(),
                        Image::from_bytes(ctx, content.as_slice()).map_err(|e| {
                            LoadError::from(
                                format!("Read error for {}: {}", path_str, e.to_string()).as_str(),
                            )
                        })?,
                    );
                }

                Ok(())
            })
            .fold(Ok(()), |acc, res| match (acc, res) {
                (Ok(_), Ok(_)) => Ok(()),
                (Ok(_), Err(e)) => Err(e),
                (Err(e), _) => Err(e),
            })
    }
}

/// Load all game assets into the game
///
/// This function performs three main steps.
///
/// 1. Check that the provided path is a directory
/// 2. Recursively load Directories and files and call
///    [`load_svg`](self::load_svg) on each `.svg` file
/// 3. Re-load newly converted assets into [`Assets`](self::Assets)
pub fn load_tree(ctx: &mut ggez::Context, settings: &GameSettings) -> Result<Assets> {
    let path = match settings.assets.clone() {
        Some(s) => Ok(s),
        None => Err(LoadError::from("No assets path set!")),
    }?;

    debug!(
        "Starting assets loading harness on {}",
        path.to_str().unwrap()
    );

    let tmpdir = tempdir().unwrap();
    let mut assets = Assets::new();
    assets.load_tree(ctx, tmpdir.path(), path.as_path())?;
    info!("Asset loading complete!");
    Ok(assets)
}

/// A utility function to take an SVG and render it to a raster image
/// according to a render spec
pub fn load_svg(tmpdir: &Path, p: &Path) -> Result<PathBuf> {
    let err: LoadError = p.to_str().unwrap().into();

    let handle = Loader::new().read_path(p).map_err(|_| err.clone())?;
    let renderer = CairoRenderer::new(&handle);

    let surf = ImageSurface::create(Format::ARgb32, 256, 256).map_err(|_| err.clone())?;
    let cr = Context::new(&surf);

    renderer
        .render_document(
            &cr,
            &Rectangle {
                x: 0.0,
                y: 0.0,
                width: 256.0,
                height: 256.0,
            },
        )
        .map_err(|_| err.clone())?;

    let png = p.with_extension("png");
    let name = png
        .file_name()
        .map_or_else(|| Err(err.clone()), |name| Ok(name))?;

    let out = tmpdir.join(name.clone());
    let mut file = BufWriter::new(File::create(out.clone()).map_err(|_| err.clone())?);
    surf.write_to_png(&mut file).map_err(|_| err.clone())?;
    Ok(out.to_path_buf())
}