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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
use std::any::{Any, TypeId};
use bevy_ecs::world::{unsafe_world_cell::UnsafeWorldCell, World};
use bevy_reflect::{FromReflect, FromType, Reflect, Uuid};
use crate::{Asset, Assets, Handle, HandleId, HandleUntyped};
#[derive(Clone)]
pub struct ReflectAsset {
type_uuid: Uuid,
handle_type_id: TypeId,
assets_resource_type_id: TypeId,
get: fn(&World, HandleUntyped) -> Option<&dyn Reflect>,
get_unchecked_mut: unsafe fn(UnsafeWorldCell<'_>, HandleUntyped) -> Option<&mut dyn Reflect>,
add: fn(&mut World, &dyn Reflect) -> HandleUntyped,
set: fn(&mut World, HandleUntyped, &dyn Reflect) -> HandleUntyped,
len: fn(&World) -> usize,
ids: for<'w> fn(&'w World) -> Box<dyn Iterator<Item = HandleId> + 'w>,
remove: fn(&mut World, HandleUntyped) -> Option<Box<dyn Reflect>>,
}
impl ReflectAsset {
pub fn type_uuid(&self) -> Uuid {
self.type_uuid
}
pub fn handle_type_id(&self) -> TypeId {
self.handle_type_id
}
pub fn assets_resource_type_id(&self) -> TypeId {
self.assets_resource_type_id
}
pub fn get<'w>(&self, world: &'w World, handle: HandleUntyped) -> Option<&'w dyn Reflect> {
(self.get)(world, handle)
}
pub fn get_mut<'w>(
&self,
world: &'w mut World,
handle: HandleUntyped,
) -> Option<&'w mut dyn Reflect> {
unsafe { (self.get_unchecked_mut)(world.as_unsafe_world_cell(), handle) }
}
pub unsafe fn get_unchecked_mut<'w>(
&self,
world: UnsafeWorldCell<'w>,
handle: HandleUntyped,
) -> Option<&'w mut dyn Reflect> {
(self.get_unchecked_mut)(world, handle)
}
pub fn add(&self, world: &mut World, value: &dyn Reflect) -> HandleUntyped {
(self.add)(world, value)
}
pub fn set(
&self,
world: &mut World,
handle: HandleUntyped,
value: &dyn Reflect,
) -> HandleUntyped {
(self.set)(world, handle, value)
}
pub fn remove(&self, world: &mut World, handle: HandleUntyped) -> Option<Box<dyn Reflect>> {
(self.remove)(world, handle)
}
#[allow(clippy::len_without_is_empty)] pub fn len(&self, world: &World) -> usize {
(self.len)(world)
}
pub fn is_empty(&self, world: &World) -> bool {
self.len(world) == 0
}
pub fn ids<'w>(&self, world: &'w World) -> impl Iterator<Item = HandleId> + 'w {
(self.ids)(world)
}
}
impl<A: Asset + FromReflect> FromType<A> for ReflectAsset {
fn from_type() -> Self {
ReflectAsset {
type_uuid: A::TYPE_UUID,
handle_type_id: TypeId::of::<Handle<A>>(),
assets_resource_type_id: TypeId::of::<Assets<A>>(),
get: |world, handle| {
let assets = world.resource::<Assets<A>>();
let asset = assets.get(&handle.typed());
asset.map(|asset| asset as &dyn Reflect)
},
get_unchecked_mut: |world, handle| {
let assets = unsafe { world.get_resource_mut::<Assets<A>>().unwrap().into_inner() };
let asset = assets.get_mut(&handle.typed());
asset.map(|asset| asset as &mut dyn Reflect)
},
add: |world, value| {
let mut assets = world.resource_mut::<Assets<A>>();
let value: A = FromReflect::from_reflect(value)
.expect("could not call `FromReflect::from_reflect` in `ReflectAsset::add`");
assets.add(value).into()
},
set: |world, handle, value| {
let mut assets = world.resource_mut::<Assets<A>>();
let value: A = FromReflect::from_reflect(value)
.expect("could not call `FromReflect::from_reflect` in `ReflectAsset::set`");
assets.set(handle, value).into()
},
len: |world| {
let assets = world.resource::<Assets<A>>();
assets.len()
},
ids: |world| {
let assets = world.resource::<Assets<A>>();
Box::new(assets.ids())
},
remove: |world, handle| {
let mut assets = world.resource_mut::<Assets<A>>();
let value = assets.remove(handle);
value.map(|value| Box::new(value) as Box<dyn Reflect>)
},
}
}
}
#[derive(Clone)]
pub struct ReflectHandle {
type_uuid: Uuid,
asset_type_id: TypeId,
downcast_handle_untyped: fn(&dyn Any) -> Option<HandleUntyped>,
typed: fn(HandleUntyped) -> Box<dyn Reflect>,
}
impl ReflectHandle {
pub fn type_uuid(&self) -> Uuid {
self.type_uuid
}
pub fn asset_type_id(&self) -> TypeId {
self.asset_type_id
}
pub fn downcast_handle_untyped(&self, handle: &dyn Any) -> Option<HandleUntyped> {
(self.downcast_handle_untyped)(handle)
}
pub fn typed(&self, handle: HandleUntyped) -> Box<dyn Reflect> {
(self.typed)(handle)
}
}
impl<A: Asset> FromType<Handle<A>> for ReflectHandle {
fn from_type() -> Self {
ReflectHandle {
type_uuid: A::TYPE_UUID,
asset_type_id: TypeId::of::<A>(),
downcast_handle_untyped: |handle: &dyn Any| {
handle
.downcast_ref::<Handle<A>>()
.map(|handle| handle.clone_untyped())
},
typed: |handle: HandleUntyped| Box::new(handle.typed::<A>()),
}
}
}
#[cfg(test)]
mod tests {
use std::any::TypeId;
use bevy_app::{App, AppTypeRegistry};
use bevy_reflect::{FromReflect, Reflect, ReflectMut, TypeUuid};
use crate::{AddAsset, AssetPlugin, HandleUntyped, ReflectAsset};
#[derive(Reflect, FromReflect, TypeUuid)]
#[uuid = "09191350-1238-4736-9a89-46f04bda6966"]
struct AssetType {
field: String,
}
#[test]
fn test_reflect_asset_operations() {
let mut app = App::new();
app.add_plugin(AssetPlugin::default())
.add_asset::<AssetType>()
.register_asset_reflect::<AssetType>();
let reflect_asset = {
let type_registry = app.world.resource::<AppTypeRegistry>();
let type_registry = type_registry.read();
type_registry
.get_type_data::<ReflectAsset>(TypeId::of::<AssetType>())
.unwrap()
.clone()
};
let value = AssetType {
field: "test".into(),
};
let handle = reflect_asset.add(&mut app.world, &value);
let strukt = match reflect_asset
.get_mut(&mut app.world, handle)
.unwrap()
.reflect_mut()
{
ReflectMut::Struct(s) => s,
_ => unreachable!(),
};
strukt
.field_mut("field")
.unwrap()
.apply(&String::from("edited"));
assert_eq!(reflect_asset.len(&app.world), 1);
let ids: Vec<_> = reflect_asset.ids(&app.world).collect();
assert_eq!(ids.len(), 1);
let fetched_handle = HandleUntyped::weak(ids[0]);
let asset = reflect_asset
.get(&app.world, fetched_handle.clone_weak())
.unwrap();
assert_eq!(asset.downcast_ref::<AssetType>().unwrap().field, "edited");
reflect_asset
.remove(&mut app.world, fetched_handle)
.unwrap();
assert_eq!(reflect_asset.len(&app.world), 0);
}
}