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
use crate::assets::{
    asset::{Asset, AssetID},
    protocol::{AssetLoadResult, AssetProtocol, AssetVariant, Meta},
};
use std::str::from_utf8;

pub struct SetAsset {
    paths: Vec<String>,
    ids: Vec<AssetID>,
}

impl SetAsset {
    pub fn paths(&self) -> &[String] {
        &self.paths
    }

    pub fn ids(&self) -> &[AssetID] {
        &self.ids
    }
}

pub struct SetAssetProtocol;

impl AssetProtocol for SetAssetProtocol {
    fn name(&self) -> &str {
        "set"
    }

    fn on_load(&mut self, data: Vec<u8>) -> AssetLoadResult {
        let data = from_utf8(&data).unwrap().to_owned();
        let list = data
            .lines()
            .enumerate()
            .filter_map(|(i, line)| {
                let path = line.trim();
                if path.is_empty() || path.starts_with('#') {
                    None
                } else {
                    Some((i.to_string(), path.to_owned()))
                }
            })
            .collect::<Vec<_>>();
        AssetLoadResult::Yield(None, list)
    }

    fn on_resume(&mut self, _: Meta, list: &[(&str, &Asset)]) -> AssetLoadResult {
        let paths = list
            .iter()
            .map(|(_, asset)| asset.to_full_path())
            .collect::<Vec<_>>();
        let ids = list.iter().map(|(_, asset)| asset.id()).collect::<Vec<_>>();
        AssetLoadResult::Data(Box::new(SetAsset { paths, ids }))
    }

    fn on_unload(&mut self, asset: &Asset) -> Option<Vec<AssetVariant>> {
        if let Some(asset) = asset.get::<SetAsset>() {
            Some(asset.ids().iter().map(|id| AssetVariant::Id(*id)).collect())
        } else {
            None
        }
    }
}