Skip to content

Commit cd021cf

Browse files
committed
Use map for scene components
1 parent beab0bd commit cd021cf

File tree

2 files changed

+64
-49
lines changed

2 files changed

+64
-49
lines changed

assets/scenes/load_scene_example.scn.ron

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

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},
@@ -100,12 +100,12 @@ impl<'a> Serialize for ComponentsSerializer<'a> {
100100
where
101101
S: serde::Serializer,
102102
{
103-
let mut state = serializer.serialize_seq(Some(self.components.len()))?;
103+
let mut state = serializer.serialize_map(Some(self.components.len()))?;
104104
for component in self.components {
105-
state.serialize_element(&ReflectSerializer::new(
106-
&**component,
107-
&*self.registry.read(),
108-
))?;
105+
state.serialize_entry(
106+
component.type_name(),
107+
&TypedReflectSerializer::new(&**component, &*self.registry.read()),
108+
)?;
109109
}
110110
state.end()
111111
}
@@ -287,7 +287,7 @@ impl<'a, 'de> Visitor<'de> for SceneEntityVisitor<'a> {
287287
return Err(Error::duplicate_field(ENTITY_FIELD_COMPONENTS));
288288
}
289289

290-
components = Some(map.next_value_seed(ComponentVecDeserializer {
290+
components = Some(map.next_value_seed(ComponentDeserializer {
291291
registry: self.registry,
292292
})?);
293293
}
@@ -308,32 +308,55 @@ impl<'a, 'de> Visitor<'de> for SceneEntityVisitor<'a> {
308308
}
309309
}
310310

311-
pub struct ComponentVecDeserializer<'a> {
311+
pub struct ComponentDeserializer<'a> {
312312
pub registry: &'a TypeRegistry,
313313
}
314314

315-
impl<'a, 'de> DeserializeSeed<'de> for ComponentVecDeserializer<'a> {
315+
impl<'a, 'de> DeserializeSeed<'de> for ComponentDeserializer<'a> {
316316
type Value = Vec<Box<dyn Reflect>>;
317317

318318
fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
319319
where
320320
D: serde::Deserializer<'de>,
321321
{
322-
deserializer.deserialize_seq(ComponentSeqVisitor {
322+
deserializer.deserialize_map(ComponentVisitor {
323323
registry: self.registry,
324324
})
325325
}
326326
}
327327

328-
struct ComponentSeqVisitor<'a> {
328+
struct ComponentVisitor<'a> {
329329
pub registry: &'a TypeRegistry,
330330
}
331331

332-
impl<'a, 'de> Visitor<'de> for ComponentSeqVisitor<'a> {
332+
impl<'a, 'de> Visitor<'de> for ComponentVisitor<'a> {
333333
type Value = Vec<Box<dyn Reflect>>;
334334

335335
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
336-
formatter.write_str("list of components")
336+
formatter.write_str("map of components")
337+
}
338+
339+
fn visit_map<A>(self, mut map: A) -> std::result::Result<Self::Value, A::Error>
340+
where
341+
A: MapAccess<'de>,
342+
{
343+
let mut added = HashSet::new();
344+
let mut components = Vec::new();
345+
while let Some(key) = map.next_key::<&str>()? {
346+
if !added.insert(key) {
347+
return Err(Error::custom(format!("duplicate component: `{}`", key)));
348+
}
349+
350+
let registration = self
351+
.registry
352+
.get_with_name(key)
353+
.ok_or_else(|| Error::custom(format!("no registration found for `{}`", key)))?;
354+
components.push(
355+
map.next_value_seed(TypedReflectDeserializer::new(registration, self.registry))?,
356+
);
357+
}
358+
359+
Ok(components)
337360
}
338361

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

0 commit comments

Comments
 (0)