1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use std::sync::Arc;
use amethyst_core::specs::storage::UnprotectedStorage;
use {ErrorKind, Handle, Reload, Result, ResultExt, SingleFile, Source};
pub trait Asset: Send + Sync + 'static {
const NAME: &'static str;
type Data: Send + Sync + 'static;
type HandleStorage: UnprotectedStorage<Handle<Self>> + Send + Sync;
}
pub trait Format<A: Asset>: Send + 'static {
const NAME: &'static str;
type Options: Send + 'static;
fn import(
&self,
name: String,
source: Arc<Source>,
options: Self::Options,
create_reload: bool,
) -> Result<FormatValue<A>>;
}
pub struct FormatValue<A: Asset> {
pub data: A::Data,
pub reload: Option<Box<Reload<A>>>,
}
impl<A: Asset> FormatValue<A> {
pub fn data(data: A::Data) -> Self {
FormatValue { data, reload: None }
}
}
pub trait SimpleFormat<A: Asset> {
const NAME: &'static str;
type Options: Clone + Send + Sync + 'static;
fn import(&self, bytes: Vec<u8>, options: Self::Options) -> Result<A::Data>;
}
impl<A, T> Format<A> for T
where
A: Asset,
T: SimpleFormat<A> + Clone + Send + Sync + 'static,
{
const NAME: &'static str = T::NAME;
type Options = T::Options;
fn import(
&self,
name: String,
source: Arc<Source>,
options: Self::Options,
create_reload: bool,
) -> Result<FormatValue<A>> {
#[cfg(feature = "profiler")]
profile_scope!("import_asset");
if create_reload {
let (b, m) = source
.load_with_metadata(&name)
.chain_err(|| ErrorKind::Source)?;
let data = T::import(&self, b, options.clone())?;
let reload = SingleFile::new(self.clone(), m, options, name, source);
let reload = Some(Box::new(reload) as Box<Reload<A>>);
Ok(FormatValue { data, reload })
} else {
let b = source.load(&name).chain_err(|| ErrorKind::Source)?;
let data = T::import(&self, b, options)?;
Ok(FormatValue::data(data))
}
}
}