Skip to main content

rogalik_assets/
lib.rs

1use rogalik_common::{EngineError, ResourceId};
2
3#[cfg(debug_assertions)]
4mod dev_file_store;
5
6#[cfg(not(debug_assertions))]
7mod embedded_store;
8
9#[cfg(debug_assertions)]
10pub use dev_file_store::DevFileStore as AssetStore;
11
12#[cfg(not(debug_assertions))]
13pub use embedded_store::EmbeddedStore as AssetStore;
14
15pub struct Asset {
16    pub state: AssetState,
17    pub data: AssetBytes,
18}
19impl Asset {
20    pub fn owned(bytes: Vec<u8>) -> Self {
21        Self {
22            state: AssetState::Loaded,
23            data: AssetBytes::Owned(bytes),
24        }
25    }
26    pub fn borrowed(bytes: &'static [u8]) -> Self {
27        Self {
28            state: AssetState::Loaded,
29            data: AssetBytes::Borrowed(bytes),
30        }
31    }
32}
33
34pub enum AssetBytes {
35    Borrowed(&'static [u8]),
36    Owned(Vec<u8>),
37}
38impl AssetBytes {
39    pub fn get(&self) -> &[u8] {
40        match self {
41            Self::Borrowed(a) => a,
42            Self::Owned(a) => a,
43        }
44    }
45}
46
47#[derive(PartialEq, Eq)]
48pub enum AssetState {
49    Loaded,
50    Updated,
51}
52
53pub trait AssetContext: Default {
54    fn from_bytes(&mut self, data: &'static [u8]) -> ResourceId;
55    fn load(&mut self, path: &str) -> Result<ResourceId, EngineError>;
56    fn get(&self, asset_id: ResourceId) -> Option<&Asset>;
57    fn mark_read(&mut self, _asset_id: ResourceId) {}
58}