Skip to main content

Crate extendable_assets

Crate extendable_assets 

Source
Expand description

§Extendable Assets

An asset framework for graphics and games that provides flexible asset management, loading, and saving capabilities.

§Example

use std::sync::Arc;
use extendable_assets::*;

// Define your asset data type
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub struct TextureData {
    pub width: u32,
    pub height: u32,
    pub format: String,
    pub pixels: Vec<u8>,
}

// Implement the AssetData trait
impl AssetData for TextureData {}

// Create an asset type that defines how to load/save your assets
pub struct TextureAssetType;

impl AssetType for TextureAssetType {
    fn name(&self) -> &str {
        "Texture"
    }
     
    fn loader(&self) -> Box<dyn AssetDataLoader> {
        struct TextureLoader;
        impl AssetDataLoader for TextureLoader {
            fn asset_from_bytes(
                &self,
                bytes: &[u8],
                _context: Option<Arc<dyn AssetManagerContext>>,
            ) -> Result<Box<dyn AssetData>, AssetLoadError> {
                let data: TextureData = serde_json::from_slice(bytes)
                    .map_err(|e| AssetLoadError::Deserialization(anyhow::Error::new(e)))?;
                Ok(Box::new(data))
            }
        }
        Box::new(TextureLoader)
    }
     
    fn saver(&self) -> Box<dyn AssetDataSaver> {
        struct TextureSaver;
        impl AssetDataSaver for TextureSaver {
            fn asset_to_bytes(
                &self,
                asset: &dyn AssetData,
                _context: Option<Arc<dyn AssetManagerContext>>,
            ) -> Result<Vec<u8>, AssetSaveError> {
                let data = asset
                    .downcast_ref::<TextureData>()
                    .ok_or(AssetSaveError::UnsupportedType)?;
                serde_json::to_vec(&data)
                    .map_err(|e| AssetSaveError::Serialization(anyhow::Error::new(e)))
            }
        }
        Box::new(TextureSaver)
    }
}

// Create asset manager with native filesystem
let mut manager = AssetManager::new(Arc::new(NativeFilesystem::new("assets")));
 
// Set JSON serialization backend
manager.set_serialization_backend(Box::new(JsonAssetSerializationBackend));
 
// Register your asset type
manager.register_asset_type(Arc::new(TextureAssetType));
 
// Create and register an asset manually
let texture_data = TextureData {
    width: 256,
    height: 256,
    format: "RGBA8".to_string(),
    pixels: vec![255; 256 * 256 * 4],
};
 
let asset_type = manager.asset_type_by_name("Texture").unwrap();
let asset = Asset::new(asset_type, Box::new(texture_data));
let asset_id = manager.register_asset("my_texture", asset);

// Get the asset back
let retrieved_asset = manager.asset_by_id(asset_id).unwrap();
if let Some(texture) = retrieved_asset.data().downcast_ref::<TextureData>() {
    assert_eq!(texture.width, 256);
    assert_eq!(texture.height, 256);
}

Modules§

third_party
Third-party re-exports for external crates used by this library.

Structs§

Asset
An asset containing type information and data.
AssetId
AssetManager
Central manager for assets in the system.
EmbedFilesystem
A filesystem implementation that reads from embedded files.
FallbackFilesystem
A filesystem implementation that tries multiple fallback filesystems in order.
JsonAssetSerializationBackend
JSON-based asset serialization backend.
MsgpackAssetSerializationBackend
MessagePack-based asset serialization backend.
NativeFilesystem
A filesystem implementation that reads from the native OS filesystem.
NullSerializationBackend
A null implementation of AssetSerializationBackend that always returns errors.
SerializedAsset
A serialized representation of an asset for storage or transmission.

Enums§

AssetError
Errors that can occur during asset operations.
AssetLoadError
Errors that can occur during asset loading.
AssetSaveError
Errors that can occur during asset saving.
CompressionMode
Specifies the compression algorithm used for compressed asset data.
FilesystemError
Errors that can occur during filesystem operations.
SerializedData
Represents serialized asset data that can be either compressed or uncompressed.

Traits§

AssetData
Trait for asset data that can be stored in an asset.
AssetDataLoader
Trait for loading asset data from raw byte data.
AssetDataSaver
Trait for saving asset data to raw byte data.
AssetManagerContext
Trait for providing additional context to the asset manager.
AssetSerializationBackend
Trait for implementing different asset serialization backends.
AssetType
Defines how a specific type of asset should be loaded and saved.
EmbedFilesystemProvider
Trait for providing access to embedded files.
Filesystem
Abstraction for reading files from different storage backends.