hassium_core/assets/
protocol.rs

1use crate::assets::asset::{Asset, AssetID};
2use std::any::Any;
3
4pub type Meta = Option<Box<dyn Any + Send + Sync>>;
5
6#[derive(Debug, Clone)]
7pub enum AssetVariant {
8    Id(AssetID),
9    Path(String),
10}
11
12impl From<AssetID> for AssetVariant {
13    fn from(id: AssetID) -> Self {
14        AssetVariant::Id(id)
15    }
16}
17
18impl From<&str> for AssetVariant {
19    fn from(path: &str) -> Self {
20        AssetVariant::Path(path.to_owned())
21    }
22}
23
24impl From<String> for AssetVariant {
25    fn from(path: String) -> Self {
26        AssetVariant::Path(path)
27    }
28}
29
30impl From<&String> for AssetVariant {
31    fn from(path: &String) -> Self {
32        AssetVariant::Path(path.clone())
33    }
34}
35
36pub enum AssetLoadResult {
37    None,
38    Data(Box<dyn Any + Send + Sync>),
39    /// (meta, [(key, path to load)])
40    Yield(Meta, Vec<(String, String)>),
41}
42
43pub trait AssetProtocol: Send + Sync {
44    fn name(&self) -> &str;
45
46    fn on_load(&mut self, data: Vec<u8>) -> AssetLoadResult;
47
48    fn on_resume(&mut self, _meta: Meta, _list: &[(&str, &Asset)]) -> AssetLoadResult {
49        AssetLoadResult::None
50    }
51
52    fn on_unload(&mut self, _asset: &Asset) -> Option<Vec<AssetVariant>> {
53        None
54    }
55
56    fn on_register(&mut self) {}
57
58    fn on_unregister(&mut self) {}
59}