Skip to content

Commit 58bc666

Browse files
committed
additional refactoring
1 parent 9ddd607 commit 58bc666

File tree

3 files changed

+62
-69
lines changed

3 files changed

+62
-69
lines changed

crates/bevy_ecs/src/component/mod.rs

Lines changed: 59 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -121,25 +121,29 @@ impl SparseSetIndex for RelationshipId {
121121
}
122122
}
123123

124-
pub mod default_relationship_kinds {
124+
pub mod relationship_kinds {
125125
pub struct HasComponent;
126126
pub struct HasResource;
127127
}
128128

129129
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
130-
pub enum ComponentIdOrEntity {
130+
pub enum EntityOrDummyId {
131131
Entity(crate::entity::Entity),
132-
ComponentId(ComponentId),
132+
/// DummyId is used so that we can fit regular components into the relationship model i.e.
133+
/// HasComponent Position
134+
/// however we dont want to implicitly spawn entities for use as the relationship target
135+
/// so we use a dummy id
136+
DummyId(DummyId),
133137
}
134138

135139
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
136140
pub struct Relship {
137141
kind: RelationshipKindId,
138-
target: ComponentIdOrEntity,
142+
target: EntityOrDummyId,
139143
}
140144

141145
impl Relship {
142-
pub fn new(kind: RelationshipKindId, target: ComponentIdOrEntity) -> Self {
146+
pub fn new(kind: RelationshipKindId, target: EntityOrDummyId) -> Self {
143147
Self { kind, target }
144148
}
145149
}
@@ -169,25 +173,26 @@ pub struct RelationshipKindInfo {
169173
}
170174

171175
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
172-
pub struct ComponentInfo {
176+
pub struct DummyInfo {
173177
static_type: Option<TypeId>,
174-
id: ComponentId,
178+
id: DummyId,
175179
}
176180
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
177-
pub struct ComponentId(usize);
181+
pub struct DummyId(usize);
178182

179183
#[derive(Debug)]
180184
pub struct Relationships {
181185
relationships: Vec<RelationshipInfo>,
182186
relationship_indices: HashMap<Relship, RelationshipId, fxhash::FxBuildHasher>,
183187

184188
kinds: Vec<RelationshipKindInfo>,
189+
kind_indices: HashMap<DummyId, RelationshipKindId, fxhash::FxBuildHasher>,
185190

186-
components: Vec<ComponentInfo>,
187-
// FIXME(Relationships) we use TypeId here because bevy needs to do this conversion *somewhere*
188-
// scripting stuff can directly use ComponentId/RelationshipKindId somehow
189-
typeid_to_component_id: HashMap<TypeId, ComponentId, fxhash::FxBuildHasher>,
190-
kind_indices: HashMap<TypeId, RelationshipKindId, fxhash::FxBuildHasher>,
191+
// These two fields are only use for bevy to map TypeId -> DummyId, scripting/modding
192+
// support does not need to care about these two fields and should directly use DummyId's
193+
// and roll their own mapping of ScriptingId -> DummyId if necessary
194+
dummy_id_to_type_id: Vec<DummyInfo>,
195+
type_id_to_dummy_id: HashMap<TypeId, DummyId, fxhash::FxBuildHasher>,
191196
}
192197

193198
impl Default for Relationships {
@@ -197,16 +202,19 @@ impl Default for Relationships {
197202
relationship_indices: Default::default(),
198203

199204
kinds: Default::default(),
200-
201-
components: Default::default(),
202-
typeid_to_component_id: Default::default(),
203205
kind_indices: Default::default(),
206+
207+
dummy_id_to_type_id: Default::default(),
208+
type_id_to_dummy_id: Default::default(),
204209
};
205210

206-
this.new_relationship_kind(Some(
207-
TypeId::of::<default_relationship_kinds::HasComponent>(),
208-
));
209-
this.new_relationship_kind(Some(TypeId::of::<default_relationship_kinds::HasResource>()));
211+
let has_component_id =
212+
this.new_dummy_id(Some(TypeId::of::<relationship_kinds::HasComponent>()));
213+
this.new_relationship_kind(has_component_id);
214+
215+
let has_resource_id =
216+
this.new_dummy_id(Some(TypeId::of::<relationship_kinds::HasResource>()));
217+
this.new_relationship_kind(has_resource_id);
210218

211219
this
212220
}
@@ -220,80 +228,65 @@ pub enum ComponentsError {
220228

221229
impl Relationships {
222230
pub fn relkind_of_has_component(&self) -> RelationshipKindId {
223-
self.kind_indices[&TypeId::of::<default_relationship_kinds::HasComponent>()]
231+
let has_component_id =
232+
self.type_id_to_dummy_id[&TypeId::of::<relationship_kinds::HasComponent>()];
233+
self.kind_indices[&has_component_id]
224234
}
225235

226236
pub fn relkind_of_has_resource(&self) -> RelationshipKindId {
227-
self.kind_indices[&TypeId::of::<default_relationship_kinds::HasResource>()]
237+
let has_resource_id =
238+
self.type_id_to_dummy_id[&TypeId::of::<relationship_kinds::HasResource>()];
239+
self.kind_indices[&has_resource_id]
228240
}
229241

230-
/// TypeId is used for bevy to map from relationship kind structs -> RelationshipKindId
231-
/// scripting/untyped use of this should pass in None
232-
pub fn new_relationship_kind(&mut self, type_id: Option<TypeId>) -> RelationshipKindId {
242+
pub fn new_relationship_kind(&mut self, dummy_id: DummyId) -> RelationshipKindId {
233243
let id = RelationshipKindId(self.kinds.len());
234-
235-
if let Some(type_id) = type_id {
236-
let previously_inserted = self
237-
.kind_indices
238-
.insert(type_id, RelationshipKindId(self.kinds.len()));
239-
assert!(previously_inserted.is_none());
240-
}
241-
244+
self.kind_indices.insert(dummy_id, id);
242245
self.kinds.push(RelationshipKindInfo { id });
243246
id
244247
}
245248

246-
/// TypeId is used by bevy to map from component type -> component id
247-
/// scripting/untyped use of this should pass in None
248-
pub(crate) fn new_component_id(&mut self, type_id: Option<TypeId>) -> ComponentId {
249-
let component_id = ComponentId(self.components.len());
250-
self.components.push(ComponentInfo {
249+
/// TypeId is used by bevy to register a mapping from typeid -> dummyid
250+
/// dynamic component use of this should pass in None or else it could
251+
/// interfere with bevy's use of this `Relationships` struct
252+
pub(crate) fn new_dummy_id(&mut self, type_id: Option<TypeId>) -> DummyId {
253+
let dummy_id = DummyId(self.dummy_id_to_type_id.len());
254+
self.dummy_id_to_type_id.push(DummyInfo {
251255
static_type: type_id,
252-
id: component_id,
256+
id: dummy_id,
253257
});
254258

255259
if let Some(type_id) = type_id {
256-
let previously_inserted = self.typeid_to_component_id.insert(type_id, component_id);
260+
let previously_inserted = self.type_id_to_dummy_id.insert(type_id, dummy_id);
257261
assert!(previously_inserted.is_none());
258262
}
259-
component_id
263+
dummy_id
260264
}
261265

262-
pub(crate) fn type_id_to_component_id(&self, type_id: TypeId) -> Option<ComponentId> {
263-
self.typeid_to_component_id.get(&type_id).copied()
266+
pub(crate) fn type_id_to_dummy_id(&self, type_id: TypeId) -> Option<DummyId> {
267+
self.type_id_to_dummy_id.get(&type_id).copied()
264268
}
265269

266270
pub(crate) fn register_relationship(
267271
&mut self,
268272
relationship: Relship,
269273
comp_descriptor: ComponentDescriptor,
270274
) -> Result<&RelationshipInfo, ComponentsError> {
271-
let new_id = RelationshipId(self.relationships.len());
275+
let rel_id = RelationshipId(self.relationships.len());
272276

273277
if let Entry::Occupied(_) = self.relationship_indices.entry(relationship) {
274278
return Err(ComponentsError::ComponentAlreadyExists(relationship));
275279
}
276280

277-
if let Some(type_id) = comp_descriptor.type_id {
278-
if let ComponentIdOrEntity::ComponentId(id) = relationship.target {
279-
if let Some(stored_type_id) = self.components[id.0].static_type {
280-
assert!(stored_type_id == type_id);
281-
}
282-
283-
let component_id = self.typeid_to_component_id[&type_id];
284-
assert!(ComponentIdOrEntity::ComponentId(component_id) == relationship.target);
285-
}
286-
}
287-
288-
self.relationship_indices.insert(relationship, new_id);
281+
self.relationship_indices.insert(relationship, rel_id);
289282
self.relationships.push(RelationshipInfo {
290-
id: new_id,
283+
id: rel_id,
291284
relationship,
292285
data: comp_descriptor,
293286
});
294287

295288
// Safety: Just inserted ^^^
296-
unsafe { Ok(self.get_relationship_info_unchecked(new_id)) }
289+
unsafe { Ok(self.get_relationship_info_unchecked(rel_id)) }
297290
}
298291

299292
#[inline]
@@ -310,7 +303,7 @@ impl Relationships {
310303
pub fn get_resource_id(&self, type_id: TypeId) -> Option<RelationshipId> {
311304
self.get_relationship_id(Relship {
312305
kind: self.relkind_of_has_resource(),
313-
target: ComponentIdOrEntity::ComponentId(self.type_id_to_component_id(type_id)?),
306+
target: EntityOrDummyId::DummyId(self.type_id_to_dummy_id(type_id)?),
314307
})
315308
}
316309
#[inline]
@@ -330,15 +323,15 @@ impl Relationships {
330323
type_id: TypeId,
331324
data_layout: impl FnOnce() -> TypeInfo,
332325
) -> &RelationshipInfo {
333-
let component_id = match self.type_id_to_component_id(type_id) {
326+
let component_id = match self.type_id_to_dummy_id(type_id) {
334327
Some(id) => id,
335-
None => self.new_component_id(Some(type_id)),
328+
None => self.new_dummy_id(Some(type_id)),
336329
};
337330

338331
self.get_relationship_info_or_insert_with(
339332
Relship {
340333
kind: self.relkind_of_has_resource(),
341-
target: ComponentIdOrEntity::ComponentId(component_id),
334+
target: EntityOrDummyId::DummyId(component_id),
342335
},
343336
data_layout,
344337
)
@@ -348,7 +341,7 @@ impl Relationships {
348341
pub fn get_component_id(&self, type_id: TypeId) -> Option<RelationshipId> {
349342
self.get_relationship_id(Relship {
350343
kind: self.relkind_of_has_component(),
351-
target: ComponentIdOrEntity::ComponentId(self.type_id_to_component_id(type_id)?),
344+
target: EntityOrDummyId::DummyId(self.type_id_to_dummy_id(type_id)?),
352345
})
353346
}
354347
#[inline]
@@ -361,15 +354,15 @@ impl Relationships {
361354
type_id: TypeId,
362355
data_layout: impl FnOnce() -> TypeInfo,
363356
) -> &RelationshipInfo {
364-
let component_id = match self.type_id_to_component_id(type_id) {
357+
let component_id = match self.type_id_to_dummy_id(type_id) {
365358
Some(id) => id,
366-
None => self.new_component_id(Some(type_id)),
359+
None => self.new_dummy_id(Some(type_id)),
367360
};
368361

369362
self.get_relationship_info_or_insert_with(
370363
Relship {
371364
kind: self.relkind_of_has_component(),
372-
target: ComponentIdOrEntity::ComponentId(component_id),
365+
target: EntityOrDummyId::DummyId(component_id),
373366
},
374367
data_layout,
375368
)

crates/bevy_ecs/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub mod prelude {
1515
pub use crate::reflect::ReflectComponent;
1616
pub use crate::{
1717
bundle::Bundle,
18-
component::default_relationship_kinds::*,
18+
component::relationship_kinds::*,
1919
entity::Entity,
2020
query::{Added, Changed, Flags, Mutated, Or, QueryState, With, WithBundle, Without},
2121
schedule::{

crates/bevy_ecs/src/world/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,11 @@ impl World {
126126
) -> Result<RelationshipId, ComponentsError> {
127127
let storage_type = descriptor.storage_type();
128128

129-
let component_id = self.relationships.new_component_id(descriptor.type_id());
129+
let component_id = self.relationships.new_dummy_id(descriptor.type_id());
130130
let relationship_info = self.relationships.register_relationship(
131131
crate::component::Relship::new(
132132
self.relationships.relkind_of_has_component(),
133-
crate::component::ComponentIdOrEntity::ComponentId(component_id),
133+
crate::component::EntityOrDummyId::DummyId(component_id),
134134
),
135135
descriptor,
136136
)?;

0 commit comments

Comments
 (0)