oxygengine_core/assets/protocols/
pack.rs1use crate::{
2 assets::protocol::{AssetLoadResult, AssetProtocol},
3 fetch::engines::map::MapFetchEngine,
4};
5use std::collections::HashMap;
6
7pub struct PackAsset(HashMap<String, Vec<u8>>);
8
9impl PackAsset {
10 pub fn get_asset_data(&self, path: &str) -> Option<&[u8]> {
11 self.0.get(path).map(|d| d.as_ref())
12 }
13
14 pub fn make_fetch_engine(&self) -> MapFetchEngine {
15 MapFetchEngine::new(self.0.clone())
16 }
17}
18
19pub struct PackAssetProtocol;
20
21impl AssetProtocol for PackAssetProtocol {
22 fn name(&self) -> &str {
23 "pack"
24 }
25
26 fn on_load(&mut self, data: Vec<u8>) -> AssetLoadResult {
27 match bincode::deserialize(&data) {
28 Ok(data) => AssetLoadResult::Data(Box::new(PackAsset(data))),
29 Err(error) => AssetLoadResult::Error(format!("Error loading pack asset: {:?}", error)),
30 }
31 }
32}