summaryrefslogtreecommitdiff
path: root/src/gfx/media.rs
blob: ac5d1584e4844fc5b0fb06ddf38c814d93641ce7 (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
use std::collections::HashMap;

#[derive(rust_embed::RustEmbed)]
#[folder = "assets/textures"]
pub struct BaseFolder; // copied from github.com/minetest/minetest

pub struct MediaMgr {
    packs: Vec<HashMap<String, Vec<u8>>>,
    srv_idx: usize,
}

impl MediaMgr {
    pub fn new() -> Self {
        Self {
            packs: [
                BaseFolder::iter()
                    .map(|file| {
                        (
                            file.to_string(),
                            BaseFolder::get(&file).unwrap().data.into_owned(),
                        )
                    })
                    .collect(),
                HashMap::new(),
            ]
            .into(),
            srv_idx: 1,
        }
    }

    pub fn add_server_media(&mut self, files: HashMap<String, Vec<u8>>) {
        self.packs[self.srv_idx].extend(files.into_iter());
    }

    pub fn get(&self, file: &str) -> Option<&[u8]> {
        self.packs
            .iter()
            .rev()
            .find_map(|pack| pack.get(file))
            .map(Vec::as_slice)
    }
}