oxygengine_core/assets/
protocol.rs

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