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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
use std::fmt::Debug;
use super::Resource;
use super::RESOURCE_COUNT;
use crate::EngineError;
#[cfg_attr(not(feature = "nightly"), repr(transparent))]
pub struct ResourceTable([Resource; RESOURCE_COUNT as usize]);
macro_rules! get_store {
($type: ty, $enum_type: ident) => {
impl GetStore<$type> for ResourceTable {
fn get<U: Into<usize> + Debug + Copy>(&self, id: U) -> Result<&$type, EngineError> {
self.index_check(id.into())?;
match &self.0[id.into()] {
Resource::$enum_type(value) => Ok(&value),
Resource::None => Err(EngineError::ResourceMissing(format!(
"Could not find requested resource #{}",
id.into(),
))),
_ => Err(EngineError::ResourceType(format!(
"Wrong resource type, required \"{}\"",
stringify!($type),
))),
}
}
fn store(&mut self, data: $type, id: usize) -> Result<(), EngineError> {
self.index_check(id)?;
Ok(self.0[id] = Resource::$enum_type(data))
}
}
};
}
pub trait GetStore<T> {
fn get<U: Into<usize> + Debug + Copy>(&self, id: U) -> Result<&T, EngineError>;
fn store(&mut self, data: T, id: usize) -> Result<(), EngineError>;
}
pub trait GetTextures {
fn get_textures<U: Into<usize> + Debug + Copy>(
&self,
id: U,
) -> Result<Vec<(u64, &super::Texture)>, EngineError>;
fn get_texture<U: Into<usize> + Debug + Copy>(
&self,
id: U,
sid: u64,
) -> Result<&super::Texture, EngineError>;
}
impl Default for ResourceTable {
fn default() -> Self {
Self::new()
}
}
impl ResourceTable {
#[cfg(feature = "nightly")]
pub const fn new() -> Self {
Self([Resource::None; RESOURCE_COUNT as usize])
}
#[cfg(not(feature = "nightly"))]
pub fn new() -> Self {
let bytes = [0u8; std::mem::size_of::<Self>()];
unsafe { std::mem::transmute(bytes) }
}
fn index_check(&self, id: usize) -> Result<(), EngineError> {
if id >= self.0.len() {
return Err(EngineError::ResourceIndex(format!(
"The requested resource index: {} is out ouf range, the max id is {}",
id,
self.0.len() - 1
)));
}
Ok(())
}
}
get_store!(super::Texture, Texture);
get_store!(super::Sound, Sound);
get_store!(Box<super::Character>, Character);
impl GetTextures for ResourceTable {
fn get_textures<U: Into<usize> + Debug + Copy>(
&self,
id: U,
) -> Result<Vec<(u64, &super::Texture)>, EngineError> {
self.index_check(id.into())?;
match &self.0[id.into()] {
Resource::Texture(value) => Ok(vec![(0, value)]),
Resource::Character(value) => {
Ok(value.atlas().iter().map(|(id, t)| (*id, t)).collect())
}
Resource::None => Err(EngineError::ResourceMissing(format!(
"Could not find requested resource #{}",
id.into(),
))),
_ => Err(EngineError::ResourceType(format!(
"Wrong resource type, required \"{}\"",
stringify!($type),
))),
}
}
fn get_texture<U: Into<usize> + Debug + Copy>(
&self,
id: U,
sid: u64,
) -> Result<&super::Texture, EngineError> {
self.index_check(id.into())?;
match &self.0[id.into()] {
Resource::Texture(value) => Ok(value),
Resource::Character(value) => value.atlas().get(&sid).ok_or_else(|| {
EngineError::ResourceIndex(format!(
"Invalid subtexture id #{} of texture #{:?}",
sid, id
))
}),
Resource::None => Err(EngineError::ResourceMissing(format!(
"Could not find requested resource #{}",
id.into(),
))),
_ => Err(EngineError::ResourceType(format!(
"Wrong resource type, required \"{}\"",
stringify!($type),
))),
}
}
}