-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95a5f76
commit 6da2658
Showing
8 changed files
with
162 additions
and
21 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,37 @@ | ||
use common::OutputStream; | ||
use std::any::{Any, TypeId}; | ||
|
||
use common::{OutputStream, VirtualPath}; | ||
|
||
/// An error that can occur when exporting an asset. | ||
#[derive(Debug)] | ||
pub enum AssetExportError {} | ||
|
||
/// Exports assets to a specific format. | ||
pub trait AssetExporter: Send + Sync + 'static { | ||
/// The type of asset processed by this exporter. | ||
type Asset; | ||
|
||
/// Returns whether this export can export the given asset type and path. | ||
fn can_export(&self, _path: VirtualPath) -> bool { | ||
true | ||
} | ||
|
||
/// Exports an asset to the given stream. | ||
fn export(&self, asset: &Self::Asset, stream: &mut dyn OutputStream) -> Result<(), AssetExportError>; | ||
} | ||
|
||
/// A trait for exporting untyped assets. | ||
pub trait UntypedAssetExporter: Send + Sync + 'static { | ||
fn can_export(&self, asset_type: TypeId, path: VirtualPath) -> bool; | ||
fn export(&self, asset: &dyn Any, stream: &mut dyn OutputStream) -> Result<(), AssetExportError>; | ||
} | ||
|
||
/// Allow any typed asset importer to be used as an untyped asset importer. | ||
impl<A: 'static, T: AssetExporter<Asset = A>> UntypedAssetExporter for T { | ||
fn can_export(&self, asset_type: TypeId, path: VirtualPath) -> bool { | ||
asset_type == TypeId::of::<A>() && self.can_export(path) | ||
} | ||
|
||
fn export(&self, asset: &dyn Any, stream: &mut dyn OutputStream) -> Result<(), AssetExportError> { | ||
self.export(asset.downcast_ref::<A>().unwrap(), stream) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,37 @@ | ||
use common::InputStream; | ||
use std::any::{Any, TypeId}; | ||
|
||
use common::{InputStream, VirtualPath}; | ||
|
||
/// An error that can occur when exporting an asset. | ||
#[derive(Debug)] | ||
pub enum AssetImportError {} | ||
|
||
/// Imports assets from a specific format. | ||
pub trait AssetImporter: Send + Sync + 'static { | ||
/// The type of asset processed by this importer. | ||
type Asset; | ||
|
||
/// Returns whether this importer can import the given asset type and path. | ||
fn can_import(&self, _path: VirtualPath) -> bool { | ||
true | ||
} | ||
|
||
/// Imports an asset from the given stream. | ||
fn import(&self, data: &mut dyn InputStream) -> Result<Self::Asset, AssetImportError>; | ||
} | ||
|
||
/// A trait for importing untyped assets. | ||
pub trait UntypedAssetImporter: Send + Sync + 'static { | ||
fn can_import(&self, asset_type: TypeId, path: VirtualPath) -> bool; | ||
fn import(&self, data: &mut dyn InputStream) -> Result<Box<dyn Any>, AssetImportError>; | ||
} | ||
|
||
/// Allow any typed asset importer to be used as an untyped asset importer. | ||
impl<A: 'static, T: AssetImporter<Asset = A>> UntypedAssetImporter for T { | ||
fn can_import(&self, asset_type: TypeId, path: VirtualPath) -> bool { | ||
asset_type == TypeId::of::<A>() && self.can_import(path) | ||
} | ||
|
||
fn import(&self, data: &mut dyn InputStream) -> Result<Box<dyn Any>, AssetImportError> { | ||
Ok(Box::new(self.import(data)?)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use common::{InputStream, VirtualPath}; | ||
use surreal_assets::{AssetDatabase, AssetImporter, AssetImportError}; | ||
|
||
#[test] | ||
pub fn it_should_build_a_valid_asset_database() { | ||
let mut database = AssetDatabase::open("assets").unwrap(); | ||
|
||
database.add_importer(AsepriteFileImporter); | ||
|
||
let test = database.load::<AsepriteFile>("assets/test.ase").unwrap(); | ||
} | ||
|
||
pub struct AsepriteFile; | ||
pub struct AsepriteFileImporter; | ||
|
||
impl AssetImporter for AsepriteFileImporter { | ||
type Asset = AsepriteFile; | ||
|
||
fn can_import(&self, path: VirtualPath) -> bool { | ||
return path.extension().ends_with("aseprite") || path.extension().ends_with("ase"); | ||
} | ||
|
||
fn import(&self, data: &mut dyn InputStream) -> Result<Self::Asset, AssetImportError> { | ||
todo!() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters