Skip to content

Commit 1f71756

Browse files
committed
Use map for scene components
1 parent 7989cb2 commit 1f71756

File tree

2 files changed

+64
-49
lines changed

2 files changed

+64
-49
lines changed
Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,36 @@
11
[
22
(
33
entity: 0,
4-
components: [
5-
{
6-
"bevy_transform::components::transform::Transform": (
7-
translation: (
8-
x: 0.0,
9-
y: 0.0,
10-
z: 0.0
11-
),
12-
rotation: (0.0, 0.0, 0.0, 1.0),
13-
scale: (
14-
x: 1.0,
15-
y: 1.0,
16-
z: 1.0
17-
),
4+
components: {
5+
"bevy_transform::components::transform::Transform": (
6+
translation: (
7+
x: 0.0,
8+
y: 0.0,
9+
z: 0.0
1810
),
19-
},
20-
{
21-
"scene::ComponentB": (
22-
value: "hello",
23-
),
24-
},
25-
{
26-
"scene::ComponentA": (
11+
rotation: (0.0, 0.0, 0.0, 1.0),
12+
scale: (
2713
x: 1.0,
28-
y: 2.0,
14+
y: 1.0,
15+
z: 1.0
2916
),
30-
},
31-
],
17+
),
18+
"scene::ComponentB": (
19+
value: "hello",
20+
),
21+
"scene::ComponentA": (
22+
x: 1.0,
23+
y: 2.0,
24+
),
25+
},
3226
),
3327
(
3428
entity: 1,
35-
components: [
36-
{
37-
"scene::ComponentA": (
38-
x: 3.0,
39-
y: 4.0,
40-
),
41-
},
42-
],
29+
components: {
30+
"scene::ComponentA": (
31+
x: 3.0,
32+
y: 4.0,
33+
),
34+
},
4335
),
4436
]

crates/bevy_scene/src/serde.rs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::{DynamicEntity, DynamicScene};
22
use anyhow::Result;
3-
use bevy_reflect::{
4-
serde::{ReflectSerializer, UntypedReflectDeserializer},
5-
Reflect, TypeRegistry, TypeRegistryArc,
6-
};
3+
use bevy_reflect::serde::{TypedReflectDeserializer, TypedReflectSerializer};
4+
use bevy_reflect::{serde::UntypedReflectDeserializer, Reflect, TypeRegistry, TypeRegistryArc};
5+
use bevy_utils::HashSet;
6+
use serde::ser::SerializeMap;
77
use serde::{
88
de::{DeserializeSeed, Error, MapAccess, SeqAccess, Visitor},
99
ser::{SerializeSeq, SerializeStruct},
@@ -70,12 +70,12 @@ impl<'a> Serialize for ComponentsSerializer<'a> {
7070
where
7171
S: serde::Serializer,
7272
{
73-
let mut state = serializer.serialize_seq(Some(self.components.len()))?;
73+
let mut state = serializer.serialize_map(Some(self.components.len()))?;
7474
for component in self.components {
75-
state.serialize_element(&ReflectSerializer::new(
76-
&**component,
77-
&*self.registry.read(),
78-
))?;
75+
state.serialize_entry(
76+
component.type_name(),
77+
&TypedReflectSerializer::new(&**component, &*self.registry.read()),
78+
)?;
7979
}
8080
state.end()
8181
}
@@ -188,7 +188,7 @@ impl<'a, 'de> Visitor<'de> for SceneEntityVisitor<'a> {
188188
return Err(Error::duplicate_field(ENTITY_FIELD_COMPONENTS));
189189
}
190190

191-
components = Some(map.next_value_seed(ComponentVecDeserializer {
191+
components = Some(map.next_value_seed(ComponentDeserializer {
192192
registry: self.registry,
193193
})?);
194194
}
@@ -209,32 +209,55 @@ impl<'a, 'de> Visitor<'de> for SceneEntityVisitor<'a> {
209209
}
210210
}
211211

212-
pub struct ComponentVecDeserializer<'a> {
212+
pub struct ComponentDeserializer<'a> {
213213
pub registry: &'a TypeRegistry,
214214
}
215215

216-
impl<'a, 'de> DeserializeSeed<'de> for ComponentVecDeserializer<'a> {
216+
impl<'a, 'de> DeserializeSeed<'de> for ComponentDeserializer<'a> {
217217
type Value = Vec<Box<dyn Reflect>>;
218218

219219
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
220220
where
221221
D: serde::Deserializer<'de>,
222222
{
223-
deserializer.deserialize_seq(ComponentSeqVisitor {
223+
deserializer.deserialize_map(ComponentVisitor {
224224
registry: self.registry,
225225
})
226226
}
227227
}
228228

229-
struct ComponentSeqVisitor<'a> {
229+
struct ComponentVisitor<'a> {
230230
pub registry: &'a TypeRegistry,
231231
}
232232

233-
impl<'a, 'de> Visitor<'de> for ComponentSeqVisitor<'a> {
233+
impl<'a, 'de> Visitor<'de> for ComponentVisitor<'a> {
234234
type Value = Vec<Box<dyn Reflect>>;
235235

236236
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
237-
formatter.write_str("list of components")
237+
formatter.write_str("map of components")
238+
}
239+
240+
fn visit_map<A>(self, mut map: A) -> std::result::Result<Self::Value, A::Error>
241+
where
242+
A: MapAccess<'de>,
243+
{
244+
let mut added = HashSet::new();
245+
let mut components = Vec::new();
246+
while let Some(key) = map.next_key::<&str>()? {
247+
if !added.insert(key) {
248+
return Err(Error::custom(format!("duplicate component: `{}`", key)));
249+
}
250+
251+
let registration = self
252+
.registry
253+
.get_with_name(key)
254+
.ok_or_else(|| Error::custom(format!("no registration found for `{}`", key)))?;
255+
components.push(
256+
map.next_value_seed(TypedReflectDeserializer::new(registration, self.registry))?,
257+
);
258+
}
259+
260+
Ok(components)
238261
}
239262

240263
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>

0 commit comments

Comments
 (0)