From 856a3f478c8e750f51fb4568f50b3c3d871c41a4 Mon Sep 17 00:00:00 2001 From: Jakob Hellermann Date: Sat, 5 Nov 2022 16:43:15 +0000 Subject: [PATCH] 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::>` 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 --- crates/bevy_core/src/lib.rs | 7 ++----- crates/bevy_reflect/src/type_registry.rs | 4 ++++ 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/crates/bevy_core/src/lib.rs b/crates/bevy_core/src/lib.rs index fb22cbc2ab7323..fbc175c1e3f897 100644 --- a/crates/bevy_core/src/lib.rs +++ b/crates/bevy_core/src/lib.rs @@ -48,11 +48,6 @@ impl Plugin for CorePlugin { ); app.register_type::().register_type::(); - app.register_type::() - .register_type::() - .register_type::>() - .register_type_data::, ReflectSerialize>() - .register_type_data::, ReflectDeserialize>(); register_rust_types(app); register_math_types(app); @@ -63,6 +58,8 @@ impl Plugin for CorePlugin { fn register_rust_types(app: &mut App) { app.register_type::>() + .register_type_data::, ReflectSerialize>() + .register_type_data::, ReflectDeserialize>() .register_type::() .register_type::>() .register_type::>() diff --git a/crates/bevy_reflect/src/type_registry.rs b/crates/bevy_reflect/src/type_registry.rs index fe3302774b7616..00f3bdb2f71774 100644 --- a/crates/bevy_reflect/src/type_registry.rs +++ b/crates/bevy_reflect/src/type_registry.rs @@ -87,6 +87,10 @@ impl TypeRegistry { /// Registers the type described by `registration`. pub fn add_registration(&mut self, registration: TypeRegistration) { + if self.registrations.contains_key(®istration.type_id()) { + return; + } + let short_name = registration.short_name.to_string(); if self.short_name_to_id.contains_key(&short_name) || self.ambiguous_names.contains(&short_name)