make register on TypeRegistry idempotent (#6487)

# Objective

- adding a new `.register` should not overwrite old type data
- separate crates should both be able to register the same type

I ran into this while debugging why `register::<Handle<T>>` removed the `ReflectHandle` type data from a prior `register_asset_reflect`.


## Solution

- make `register` do nothing if called again for the same type
- I also removed some unnecessary duplicate registrations
This commit is contained in:
Jakob Hellermann 2022-11-05 16:43:15 +00:00
parent 3d64acd0c3
commit 5ae94750a1
2 changed files with 6 additions and 5 deletions

View file

@ -48,11 +48,6 @@ impl Plugin for CorePlugin {
); );
app.register_type::<Entity>().register_type::<Name>(); app.register_type::<Entity>().register_type::<Name>();
app.register_type::<Entity>()
.register_type::<Name>()
.register_type::<Range<f32>>()
.register_type_data::<Range<f32>, ReflectSerialize>()
.register_type_data::<Range<f32>, ReflectDeserialize>();
register_rust_types(app); register_rust_types(app);
register_math_types(app); register_math_types(app);
@ -63,6 +58,8 @@ impl Plugin for CorePlugin {
fn register_rust_types(app: &mut App) { fn register_rust_types(app: &mut App) {
app.register_type::<Range<f32>>() app.register_type::<Range<f32>>()
.register_type_data::<Range<f32>, ReflectSerialize>()
.register_type_data::<Range<f32>, ReflectDeserialize>()
.register_type::<String>() .register_type::<String>()
.register_type::<HashSet<String>>() .register_type::<HashSet<String>>()
.register_type::<Option<String>>() .register_type::<Option<String>>()

View file

@ -87,6 +87,10 @@ impl TypeRegistry {
/// Registers the type described by `registration`. /// Registers the type described by `registration`.
pub fn add_registration(&mut self, registration: TypeRegistration) { pub fn add_registration(&mut self, registration: TypeRegistration) {
if self.registrations.contains_key(&registration.type_id()) {
return;
}
let short_name = registration.short_name.to_string(); let short_name = registration.short_name.to_string();
if self.short_name_to_id.contains_key(&short_name) if self.short_name_to_id.contains_key(&short_name)
|| self.ambiguous_names.contains(&short_name) || self.ambiguous_names.contains(&short_name)