bevy_ecs/entity/clone_entities.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
use alloc::{borrow::ToOwned, boxed::Box, collections::VecDeque, vec::Vec};
use bevy_platform::collections::{HashMap, HashSet};
use bevy_ptr::{Ptr, PtrMut};
use bumpalo::Bump;
use core::any::TypeId;
use crate::{
bundle::Bundle,
component::{Component, ComponentCloneBehavior, ComponentCloneFn, ComponentId, ComponentInfo},
entity::{hash_map::EntityHashMap, Entities, Entity, EntityMapper},
query::DebugCheckedUnwrap,
relationship::RelationshipHookMode,
world::World,
};
/// Provides read access to the source component (the component being cloned) in a [`ComponentCloneFn`].
pub struct SourceComponent<'a> {
ptr: Ptr<'a>,
info: &'a ComponentInfo,
}
impl<'a> SourceComponent<'a> {
/// Returns a reference to the component on the source entity.
///
/// Will return `None` if `ComponentId` of requested component does not match `ComponentId` of source component
pub fn read<C: Component>(&self) -> Option<&C> {
if self
.info
.type_id()
.is_some_and(|id| id == TypeId::of::<C>())
{
// SAFETY:
// - Components and ComponentId are from the same world
// - source_component_ptr holds valid data of the type referenced by ComponentId
unsafe { Some(self.ptr.deref::<C>()) }
} else {
None
}
}
/// Returns the "raw" pointer to the source component.
pub fn ptr(&self) -> Ptr<'a> {
self.ptr
}
/// Returns a reference to the component on the source entity as [`&dyn Reflect`](bevy_reflect::Reflect).
///
/// Will return `None` if:
/// - World does not have [`AppTypeRegistry`](`crate::reflect::AppTypeRegistry`).
/// - Component does not implement [`ReflectFromPtr`](bevy_reflect::ReflectFromPtr).
/// - Component is not registered.
/// - Component does not have [`TypeId`]
/// - Registered [`ReflectFromPtr`](bevy_reflect::ReflectFromPtr)'s [`TypeId`] does not match component's [`TypeId`]
#[cfg(feature = "bevy_reflect")]
pub fn read_reflect(
&self,
registry: &bevy_reflect::TypeRegistry,
) -> Option<&dyn bevy_reflect::Reflect> {
let type_id = self.info.type_id()?;
let reflect_from_ptr = registry.get_type_data::<bevy_reflect::ReflectFromPtr>(type_id)?;
if reflect_from_ptr.type_id() != type_id {
return None;
}
// SAFETY: `source_component_ptr` stores data represented by `component_id`, which we used to get `ReflectFromPtr`.
unsafe { Some(reflect_from_ptr.as_reflect(self.ptr)) }
}
}
/// Context for component clone handlers.
///
/// Provides fast access to useful resources like [`AppTypeRegistry`](crate::reflect::AppTypeRegistry)
/// and allows component clone handler to get information about component being cloned.
pub struct ComponentCloneCtx<'a, 'b> {
component_id: ComponentId,
target_component_written: bool,
bundle_scratch: &'a mut BundleScratch<'b>,
bundle_scratch_allocator: &'b Bump,
entities: &'a Entities,
source: Entity,
target: Entity,
component_info: &'a ComponentInfo,
entity_cloner: &'a mut EntityCloner,
mapper: &'a mut dyn EntityMapper,
#[cfg(feature = "bevy_reflect")]
type_registry: Option<&'a crate::reflect::AppTypeRegistry>,
#[cfg(not(feature = "bevy_reflect"))]
#[expect(dead_code)]
type_registry: Option<&'a ()>,
}
impl<'a, 'b> ComponentCloneCtx<'a, 'b> {
/// Create a new instance of `ComponentCloneCtx` that can be passed to component clone handlers.
///
/// # Safety
/// Caller must ensure that:
/// - `component_info` corresponds to the `component_id` in the same world,.
/// - `source_component_ptr` points to a valid component of type represented by `component_id`.
unsafe fn new(
component_id: ComponentId,
source: Entity,
target: Entity,
bundle_scratch_allocator: &'b Bump,
bundle_scratch: &'a mut BundleScratch<'b>,
entities: &'a Entities,
component_info: &'a ComponentInfo,
entity_cloner: &'a mut EntityCloner,
mapper: &'a mut dyn EntityMapper,
#[cfg(feature = "bevy_reflect")] type_registry: Option<&'a crate::reflect::AppTypeRegistry>,
#[cfg(not(feature = "bevy_reflect"))] type_registry: Option<&'a ()>,
) -> Self {
Self {
component_id,
source,
target,
bundle_scratch,
target_component_written: false,
bundle_scratch_allocator,
entities,
mapper,
component_info,
entity_cloner,
type_registry,
}
}
/// Returns true if [`write_target_component`](`Self::write_target_component`) was called before.
pub fn target_component_written(&self) -> bool {
self.target_component_written
}
/// Returns the current source entity.
pub fn source(&self) -> Entity {
self.source
}
/// Returns the current target entity.
pub fn target(&self) -> Entity {
self.target
}
/// Returns the [`ComponentId`] of the component being cloned.
pub fn component_id(&self) -> ComponentId {
self.component_id
}
/// Returns the [`ComponentInfo`] of the component being cloned.
pub fn component_info(&self) -> &ComponentInfo {
self.component_info
}
/// Returns true if the [`EntityCloner`] is configured to recursively clone entities. When this is enabled,
/// entities stored in a cloned entity's [`RelationshipTarget`](crate::relationship::RelationshipTarget) component with
/// [`RelationshipTarget::LINKED_SPAWN`](crate::relationship::RelationshipTarget::LINKED_SPAWN) will also be cloned.
#[inline]
pub fn linked_cloning(&self) -> bool {
self.entity_cloner.linked_cloning
}
/// Returns this context's [`EntityMapper`].
pub fn entity_mapper(&mut self) -> &mut dyn EntityMapper {
self.mapper
}
/// Writes component data to target entity.
///
/// # Panics
/// This will panic if:
/// - Component has already been written once.
/// - Component being written is not registered in the world.
/// - `ComponentId` of component being written does not match expected `ComponentId`.
pub fn write_target_component<C: Component>(&mut self, mut component: C) {
C::map_entities(&mut component, &mut self.mapper);
let short_name = disqualified::ShortName::of::<C>();
if self.target_component_written {
panic!("Trying to write component '{short_name}' multiple times")
}
if self
.component_info
.type_id()
.is_none_or(|id| id != TypeId::of::<C>())
{
panic!("TypeId of component '{short_name}' does not match source component TypeId")
};
// SAFETY: the TypeId of self.component_id has been checked to ensure it matches `C`
unsafe {
self.bundle_scratch
.push(self.bundle_scratch_allocator, self.component_id, component);
};
self.target_component_written = true;
}
/// Writes component data to target entity by providing a pointer to source component data.
///
/// # Safety
/// Caller must ensure that the passed in `ptr` references data that corresponds to the type of the source / target [`ComponentId`].
/// `ptr` must also contain data that the written component can "own" (for example, this should not directly copy non-Copy data).
///
/// # Panics
/// This will panic if component has already been written once.
pub unsafe fn write_target_component_ptr(&mut self, ptr: Ptr) {
if self.target_component_written {
panic!("Trying to write component multiple times")
}
let layout = self.component_info.layout();
let target_ptr = self.bundle_scratch_allocator.alloc_layout(layout);
core::ptr::copy_nonoverlapping(ptr.as_ptr(), target_ptr.as_ptr(), layout.size());
self.bundle_scratch
.push_ptr(self.component_id, PtrMut::new(target_ptr));
self.target_component_written = true;
}
/// Writes component data to target entity.
///
/// # Panics
/// This will panic if:
/// - World does not have [`AppTypeRegistry`](`crate::reflect::AppTypeRegistry`).
/// - Component does not implement [`ReflectFromPtr`](bevy_reflect::ReflectFromPtr).
/// - Source component does not have [`TypeId`].
/// - Passed component's [`TypeId`] does not match source component [`TypeId`].
/// - Component has already been written once.
#[cfg(feature = "bevy_reflect")]
pub fn write_target_component_reflect(&mut self, component: Box<dyn bevy_reflect::Reflect>) {
if self.target_component_written {
panic!("Trying to write component multiple times")
}
let source_type_id = self
.component_info
.type_id()
.expect("Source component must have TypeId");
let component_type_id = component.type_id();
if source_type_id != component_type_id {
panic!("Passed component TypeId does not match source component TypeId")
}
let component_layout = self.component_info.layout();
let component_data_ptr = Box::into_raw(component).cast::<u8>();
let target_component_data_ptr =
self.bundle_scratch_allocator.alloc_layout(component_layout);
// SAFETY:
// - target_component_data_ptr and component_data have the same data type.
// - component_data_ptr has layout of component_layout
unsafe {
core::ptr::copy_nonoverlapping(
component_data_ptr,
target_component_data_ptr.as_ptr(),
component_layout.size(),
);
self.bundle_scratch
.push_ptr(self.component_id, PtrMut::new(target_component_data_ptr));
if component_layout.size() > 0 {
// Ensure we don't attempt to deallocate zero-sized components
alloc::alloc::dealloc(component_data_ptr, component_layout);
}
}
self.target_component_written = true;
}
/// Returns [`AppTypeRegistry`](`crate::reflect::AppTypeRegistry`) if it exists in the world.
///
/// NOTE: Prefer this method instead of manually reading the resource from the world.
#[cfg(feature = "bevy_reflect")]
pub fn type_registry(&self) -> Option<&crate::reflect::AppTypeRegistry> {
self.type_registry
}
/// Queues the `entity` to be cloned by the current [`EntityCloner`]
pub fn queue_entity_clone(&mut self, entity: Entity) {
let target = self.entities.reserve_entity();
self.mapper.set_mapped(entity, target);
self.entity_cloner.clone_queue.push_back(entity);
}
/// Queues a deferred clone operation, which will run with exclusive [`World`] access immediately after calling the clone handler for each component on an entity.
/// This exists, despite its similarity to [`Commands`](crate::system::Commands), to provide access to the entity mapper in the current context.
pub fn queue_deferred(
&mut self,
deferred: impl FnOnce(&mut World, &mut dyn EntityMapper) + 'static,
) {
self.entity_cloner
.deferred_commands
.push_back(Box::new(deferred));
}
}
/// A configuration determining how to clone entities. This can be built using [`EntityCloner::build`], which
/// returns an [`EntityClonerBuilder`].
///
/// After configuration is complete an entity can be cloned using [`Self::clone_entity`].
///
///```
/// use bevy_ecs::prelude::*;
/// use bevy_ecs::entity::EntityCloner;
///
/// #[derive(Component, Clone, PartialEq, Eq)]
/// struct A {
/// field: usize,
/// }
///
/// let mut world = World::default();
///
/// let component = A { field: 5 };
///
/// let entity = world.spawn(component.clone()).id();
/// let entity_clone = world.spawn_empty().id();
///
/// EntityCloner::build(&mut world).clone_entity(entity, entity_clone);
///
/// assert!(world.get::<A>(entity_clone).is_some_and(|c| *c == component));
///```
///
/// # Default cloning strategy
/// By default, all types that derive [`Component`] and implement either [`Clone`] or `Reflect` (with `ReflectComponent`) will be cloned
/// (with `Clone`-based implementation preferred in case component implements both).
///
/// It should be noted that if `Component` is implemented manually or if `Clone` implementation is conditional
/// (like when deriving `Clone` for a type with a generic parameter without `Clone` bound),
/// the component will be cloned using the [default cloning strategy](crate::component::ComponentCloneBehavior::global_default_fn).
/// To use `Clone`-based handler ([`ComponentCloneBehavior::clone`]) in this case it should be set manually using one
/// of the methods mentioned in the [Clone Behaviors](#Clone-Behaviors) section
///
/// Here's an example of how to do it using [`clone_behavior`](Component::clone_behavior):
/// ```
/// # use bevy_ecs::prelude::*;
/// # use bevy_ecs::component::{StorageType, ComponentCloneBehavior, Mutable};
/// #[derive(Clone)]
/// struct SomeComponent;
///
/// impl Component for SomeComponent {
/// const STORAGE_TYPE: StorageType = StorageType::Table;
/// type Mutability = Mutable;
/// fn clone_behavior() -> ComponentCloneBehavior {
/// ComponentCloneBehavior::clone::<Self>()
/// }
/// }
/// ```
///
/// # Clone Behaviors
/// [`EntityCloner`] clones entities by cloning components using [`ComponentCloneBehavior`], and there are multiple layers
/// to decide which handler to use for which component. The overall hierarchy looks like this (priority from most to least):
/// 1. local overrides using [`EntityClonerBuilder::override_clone_behavior`]
/// 2. component-defined handler using [`Component::clone_behavior`]
/// 3. default handler override using [`EntityClonerBuilder::with_default_clone_fn`].
/// 4. reflect-based or noop default clone handler depending on if `bevy_reflect` feature is enabled or not.
pub struct EntityCloner {
filter_allows_components: bool,
filter: HashSet<ComponentId>,
clone_behavior_overrides: HashMap<ComponentId, ComponentCloneBehavior>,
move_components: bool,
linked_cloning: bool,
default_clone_fn: ComponentCloneFn,
clone_queue: VecDeque<Entity>,
deferred_commands: VecDeque<Box<dyn FnOnce(&mut World, &mut dyn EntityMapper)>>,
}
impl Default for EntityCloner {
fn default() -> Self {
Self {
filter_allows_components: false,
move_components: false,
linked_cloning: false,
default_clone_fn: ComponentCloneBehavior::global_default_fn(),
filter: Default::default(),
clone_behavior_overrides: Default::default(),
clone_queue: Default::default(),
deferred_commands: Default::default(),
}
}
}
/// An expandable scratch space for defining a dynamic bundle.
struct BundleScratch<'a> {
component_ids: Vec<ComponentId>,
component_ptrs: Vec<PtrMut<'a>>,
}
impl<'a> BundleScratch<'a> {
pub(crate) fn with_capacity(capacity: usize) -> Self {
Self {
component_ids: Vec::with_capacity(capacity),
component_ptrs: Vec::with_capacity(capacity),
}
}
/// Pushes the `ptr` component onto this storage with the given `id` [`ComponentId`].
///
/// # Safety
/// The `id` [`ComponentId`] must match the component `ptr` for whatever [`World`] this scratch will
/// be written to. `ptr` must contain valid uniquely-owned data that matches the type of component referenced
/// in `id`.
pub(crate) unsafe fn push_ptr(&mut self, id: ComponentId, ptr: PtrMut<'a>) {
self.component_ids.push(id);
self.component_ptrs.push(ptr);
}
/// Pushes the `C` component onto this storage with the given `id` [`ComponentId`], using the given `bump` allocator.
///
/// # Safety
/// The `id` [`ComponentId`] must match the component `C` for whatever [`World`] this scratch will
/// be written to.
pub(crate) unsafe fn push<C: Component>(
&mut self,
allocator: &'a Bump,
id: ComponentId,
component: C,
) {
let component_ref = allocator.alloc(component);
self.component_ids.push(id);
self.component_ptrs.push(PtrMut::from(component_ref));
}
/// Writes the scratch components to the given entity in the given world.
///
/// # Safety
/// All [`ComponentId`] values in this instance must come from `world`.
pub(crate) unsafe fn write(
self,
world: &mut World,
entity: Entity,
relationship_hook_insert_mode: RelationshipHookMode,
) {
// SAFETY:
// - All `component_ids` are from the same world as `target` entity
// - All `component_data_ptrs` are valid types represented by `component_ids`
unsafe {
world.entity_mut(entity).insert_by_ids_internal(
&self.component_ids,
self.component_ptrs.into_iter().map(|ptr| ptr.promote()),
relationship_hook_insert_mode,
);
}
}
}
impl EntityCloner {
/// Returns a new [`EntityClonerBuilder`] using the given `world`.
pub fn build(world: &mut World) -> EntityClonerBuilder {
EntityClonerBuilder {
world,
attach_required_components: true,
entity_cloner: EntityCloner::default(),
}
}
/// Returns `true` if this cloner is configured to clone entities referenced in cloned components via [`RelationshipTarget::LINKED_SPAWN`](crate::relationship::RelationshipTarget::LINKED_SPAWN).
/// This will produce "deep" / recursive clones of relationship trees that have "linked spawn".
#[inline]
pub fn linked_cloning(&self) -> bool {
self.linked_cloning
}
/// Clones and inserts components from the `source` entity into the entity mapped by `mapper` from `source` using the stored configuration.
fn clone_entity_internal(
&mut self,
world: &mut World,
source: Entity,
mapper: &mut dyn EntityMapper,
relationship_hook_insert_mode: RelationshipHookMode,
) -> Entity {
let target = mapper.get_mapped(source);
// PERF: reusing allocated space across clones would be more efficient. Consider an allocation model similar to `Commands`.
let bundle_scratch_allocator = Bump::new();
let mut bundle_scratch: BundleScratch;
{
let world = world.as_unsafe_world_cell();
let source_entity = world.get_entity(source).expect("Source entity must exist");
#[cfg(feature = "bevy_reflect")]
// SAFETY: we have unique access to `world`, nothing else accesses the registry at this moment, and we clone
// the registry, which prevents future conflicts.
let app_registry = unsafe {
world
.get_resource::<crate::reflect::AppTypeRegistry>()
.cloned()
};
#[cfg(not(feature = "bevy_reflect"))]
let app_registry = Option::<()>::None;
let archetype = source_entity.archetype();
bundle_scratch = BundleScratch::with_capacity(archetype.component_count());
for component in archetype.components() {
if !self.is_cloning_allowed(&component) {
continue;
}
let handler = match self.clone_behavior_overrides.get(&component) {
Some(clone_behavior) => clone_behavior.resolve(self.default_clone_fn),
None => world
.components()
.get_info(component)
.map(|info| info.clone_behavior().resolve(self.default_clone_fn))
.unwrap_or(self.default_clone_fn),
};
// SAFETY: This component exists because it is present on the archetype.
let info = unsafe { world.components().get_info_unchecked(component) };
// SAFETY:
// - There are no other mutable references to source entity.
// - `component` is from `source_entity`'s archetype
let source_component_ptr =
unsafe { source_entity.get_by_id(component).debug_checked_unwrap() };
let source_component = SourceComponent {
info,
ptr: source_component_ptr,
};
// SAFETY:
// - `components` and `component` are from the same world
// - `source_component_ptr` is valid and points to the same type as represented by `component`
let mut ctx = unsafe {
ComponentCloneCtx::new(
component,
source,
target,
&bundle_scratch_allocator,
&mut bundle_scratch,
world.entities(),
info,
self,
mapper,
app_registry.as_ref(),
)
};
(handler)(&source_component, &mut ctx);
}
}
world.flush();
for deferred in self.deferred_commands.drain(..) {
(deferred)(world, mapper);
}
if !world.entities.contains(target) {
panic!("Target entity does not exist");
}
if self.move_components {
world
.entity_mut(source)
.remove_by_ids(&bundle_scratch.component_ids);
}
// SAFETY:
// - All `component_ids` are from the same world as `target` entity
// - All `component_data_ptrs` are valid types represented by `component_ids`
unsafe { bundle_scratch.write(world, target, relationship_hook_insert_mode) };
target
}
/// Clones and inserts components from the `source` entity into `target` entity using the stored configuration.
/// If this [`EntityCloner`] has [`EntityCloner::linked_cloning`], then it will recursively spawn entities as defined
/// by [`RelationshipTarget`](crate::relationship::RelationshipTarget) components with
/// [`RelationshipTarget::LINKED_SPAWN`](crate::relationship::RelationshipTarget::LINKED_SPAWN)
#[track_caller]
pub fn clone_entity(&mut self, world: &mut World, source: Entity, target: Entity) {
let mut map = EntityHashMap::<Entity>::new();
map.set_mapped(source, target);
self.clone_entity_mapped(world, source, &mut map);
}
/// Clones and inserts components from the `source` entity into a newly spawned entity using the stored configuration.
/// If this [`EntityCloner`] has [`EntityCloner::linked_cloning`], then it will recursively spawn entities as defined
/// by [`RelationshipTarget`](crate::relationship::RelationshipTarget) components with
/// [`RelationshipTarget::LINKED_SPAWN`](crate::relationship::RelationshipTarget::LINKED_SPAWN)
#[track_caller]
pub fn spawn_clone(&mut self, world: &mut World, source: Entity) -> Entity {
let target = world.spawn_empty().id();
self.clone_entity(world, source, target);
target
}
/// Clones the entity into whatever entity `mapper` chooses for it.
#[track_caller]
pub fn clone_entity_mapped(
&mut self,
world: &mut World,
source: Entity,
mapper: &mut dyn EntityMapper,
) -> Entity {
// All relationships on the root should have their hooks run
let target = self.clone_entity_internal(world, source, mapper, RelationshipHookMode::Run);
let child_hook_insert_mode = if self.linked_cloning {
// When spawning "linked relationships", we want to ignore hooks for relationships we are spawning, while
// still registering with original relationship targets that are "not linked" to the current recursive spawn.
RelationshipHookMode::RunIfNotLinked
} else {
// If we are not cloning "linked relationships" recursively, then we want any cloned relationship components to
// register themselves with their original relationship target.
RelationshipHookMode::Run
};
loop {
let queued = self.clone_queue.pop_front();
if let Some(queued) = queued {
self.clone_entity_internal(world, queued, mapper, child_hook_insert_mode);
} else {
break;
}
}
target
}
fn is_cloning_allowed(&self, component: &ComponentId) -> bool {
(self.filter_allows_components && self.filter.contains(component))
|| (!self.filter_allows_components && !self.filter.contains(component))
}
}
/// A builder for configuring [`EntityCloner`]. See [`EntityCloner`] for more information.
pub struct EntityClonerBuilder<'w> {
world: &'w mut World,
entity_cloner: EntityCloner,
attach_required_components: bool,
}
impl<'w> EntityClonerBuilder<'w> {
/// Internally calls [`EntityCloner::clone_entity`] on the builder's [`World`].
pub fn clone_entity(&mut self, source: Entity, target: Entity) -> &mut Self {
self.entity_cloner.clone_entity(self.world, source, target);
self
}
/// Finishes configuring [`EntityCloner`] returns it.
pub fn finish(self) -> EntityCloner {
self.entity_cloner
}
/// By default, any components allowed/denied through the filter will automatically
/// allow/deny all of their required components.
///
/// This method allows for a scoped mode where any changes to the filter
/// will not involve required components.
pub fn without_required_components(
&mut self,
builder: impl FnOnce(&mut EntityClonerBuilder),
) -> &mut Self {
self.attach_required_components = false;
builder(self);
self.attach_required_components = true;
self
}
/// Sets the default clone function to use.
pub fn with_default_clone_fn(&mut self, clone_fn: ComponentCloneFn) -> &mut Self {
self.entity_cloner.default_clone_fn = clone_fn;
self
}
/// Sets whether the cloner should remove any components that were cloned,
/// effectively moving them from the source entity to the target.
///
/// This is disabled by default.
///
/// The setting only applies to components that are allowed through the filter
/// at the time [`EntityClonerBuilder::clone_entity`] is called.
pub fn move_components(&mut self, enable: bool) -> &mut Self {
self.entity_cloner.move_components = enable;
self
}
/// Adds all components of the bundle to the list of components to clone.
///
/// Note that all components are allowed by default, to clone only explicitly allowed components make sure to call
/// [`deny_all`](`Self::deny_all`) before calling any of the `allow` methods.
pub fn allow<T: Bundle>(&mut self) -> &mut Self {
let bundle = self.world.register_bundle::<T>();
let ids = bundle.explicit_components().to_owned();
for id in ids {
self.filter_allow(id);
}
self
}
/// Extends the list of components to clone.
///
/// Note that all components are allowed by default, to clone only explicitly allowed components make sure to call
/// [`deny_all`](`Self::deny_all`) before calling any of the `allow` methods.
pub fn allow_by_ids(&mut self, ids: impl IntoIterator<Item = ComponentId>) -> &mut Self {
for id in ids {
self.filter_allow(id);
}
self
}
/// Extends the list of components to clone using [`TypeId`]s.
///
/// Note that all components are allowed by default, to clone only explicitly allowed components make sure to call
/// [`deny_all`](`Self::deny_all`) before calling any of the `allow` methods.
pub fn allow_by_type_ids(&mut self, ids: impl IntoIterator<Item = TypeId>) -> &mut Self {
for type_id in ids {
if let Some(id) = self.world.components().get_id(type_id) {
self.filter_allow(id);
}
}
self
}
/// Resets the filter to allow all components to be cloned.
pub fn allow_all(&mut self) -> &mut Self {
self.entity_cloner.filter_allows_components = false;
self.entity_cloner.filter.clear();
self
}
/// Disallows all components of the bundle from being cloned.
pub fn deny<T: Bundle>(&mut self) -> &mut Self {
let bundle = self.world.register_bundle::<T>();
let ids = bundle.explicit_components().to_owned();
for id in ids {
self.filter_deny(id);
}
self
}
/// Extends the list of components that shouldn't be cloned.
pub fn deny_by_ids(&mut self, ids: impl IntoIterator<Item = ComponentId>) -> &mut Self {
for id in ids {
self.filter_deny(id);
}
self
}
/// Extends the list of components that shouldn't be cloned by type ids.
pub fn deny_by_type_ids(&mut self, ids: impl IntoIterator<Item = TypeId>) -> &mut Self {
for type_id in ids {
if let Some(id) = self.world.components().get_id(type_id) {
self.filter_deny(id);
}
}
self
}
/// Sets the filter to deny all components.
pub fn deny_all(&mut self) -> &mut Self {
self.entity_cloner.filter_allows_components = true;
self.entity_cloner.filter.clear();
self
}
/// Overrides the [`ComponentCloneBehavior`] for a component in this builder.
/// This handler will be used to clone the component instead of the global one defined by the [`EntityCloner`].
///
/// See [Handlers section of `EntityClonerBuilder`](EntityClonerBuilder#handlers) to understand how this affects handler priority.
pub fn override_clone_behavior<T: Component>(
&mut self,
clone_behavior: ComponentCloneBehavior,
) -> &mut Self {
if let Some(id) = self.world.components().component_id::<T>() {
self.entity_cloner
.clone_behavior_overrides
.insert(id, clone_behavior);
}
self
}
/// Overrides the [`ComponentCloneBehavior`] for a component with the given `component_id` in this builder.
/// This handler will be used to clone the component instead of the global one defined by the [`EntityCloner`].
///
/// See [Handlers section of `EntityClonerBuilder`](EntityClonerBuilder#handlers) to understand how this affects handler priority.
pub fn override_clone_behavior_with_id(
&mut self,
component_id: ComponentId,
clone_behavior: ComponentCloneBehavior,
) -> &mut Self {
self.entity_cloner
.clone_behavior_overrides
.insert(component_id, clone_behavior);
self
}
/// Removes a previously set override of [`ComponentCloneBehavior`] for a component in this builder.
pub fn remove_clone_behavior_override<T: Component>(&mut self) -> &mut Self {
if let Some(id) = self.world.components().component_id::<T>() {
self.entity_cloner.clone_behavior_overrides.remove(&id);
}
self
}
/// Removes a previously set override of [`ComponentCloneBehavior`] for a given `component_id` in this builder.
pub fn remove_clone_behavior_override_with_id(
&mut self,
component_id: ComponentId,
) -> &mut Self {
self.entity_cloner
.clone_behavior_overrides
.remove(&component_id);
self
}
/// When true this cloner will be configured to clone entities referenced in cloned components via [`RelationshipTarget::LINKED_SPAWN`](crate::relationship::RelationshipTarget::LINKED_SPAWN).
/// This will produce "deep" / recursive clones of relationship trees that have "linked spawn".
pub fn linked_cloning(&mut self, linked_cloning: bool) -> &mut Self {
self.entity_cloner.linked_cloning = linked_cloning;
self
}
/// Helper function that allows a component through the filter.
fn filter_allow(&mut self, id: ComponentId) {
if self.entity_cloner.filter_allows_components {
self.entity_cloner.filter.insert(id);
} else {
self.entity_cloner.filter.remove(&id);
}
if self.attach_required_components {
if let Some(info) = self.world.components().get_info(id) {
for required_id in info.required_components().iter_ids() {
if self.entity_cloner.filter_allows_components {
self.entity_cloner.filter.insert(required_id);
} else {
self.entity_cloner.filter.remove(&required_id);
}
}
}
}
}
/// Helper function that disallows a component through the filter.
fn filter_deny(&mut self, id: ComponentId) {
if self.entity_cloner.filter_allows_components {
self.entity_cloner.filter.remove(&id);
} else {
self.entity_cloner.filter.insert(id);
}
if self.attach_required_components {
if let Some(info) = self.world.components().get_info(id) {
for required_id in info.required_components().iter_ids() {
if self.entity_cloner.filter_allows_components {
self.entity_cloner.filter.remove(&required_id);
} else {
self.entity_cloner.filter.insert(required_id);
}
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::ComponentCloneCtx;
use crate::{
component::{Component, ComponentCloneBehavior, ComponentDescriptor, StorageType},
entity::{Entity, EntityCloner, EntityHashMap, SourceComponent},
prelude::{ChildOf, Children, Resource},
reflect::{AppTypeRegistry, ReflectComponent, ReflectFromWorld},
world::{FromWorld, World},
};
use alloc::vec::Vec;
use bevy_ptr::OwningPtr;
use bevy_reflect::Reflect;
use core::marker::PhantomData;
use core::{alloc::Layout, ops::Deref};
#[cfg(feature = "bevy_reflect")]
mod reflect {
use super::*;
use crate::{
component::{Component, ComponentCloneBehavior},
entity::{EntityCloner, SourceComponent},
reflect::{AppTypeRegistry, ReflectComponent, ReflectFromWorld},
};
use alloc::vec;
use bevy_reflect::{std_traits::ReflectDefault, FromType, Reflect, ReflectFromPtr};
#[test]
fn clone_entity_using_reflect() {
#[derive(Component, Reflect, Clone, PartialEq, Eq)]
#[reflect(Component)]
struct A {
field: usize,
}
let mut world = World::default();
world.init_resource::<AppTypeRegistry>();
let registry = world.get_resource::<AppTypeRegistry>().unwrap();
registry.write().register::<A>();
world.register_component::<A>();
let component = A { field: 5 };
let e = world.spawn(component.clone()).id();
let e_clone = world.spawn_empty().id();
EntityCloner::build(&mut world)
.override_clone_behavior::<A>(ComponentCloneBehavior::reflect())
.clone_entity(e, e_clone);
assert!(world.get::<A>(e_clone).is_some_and(|c| *c == component));
}
#[test]
fn clone_entity_using_reflect_all_paths() {
#[derive(PartialEq, Eq, Default, Debug)]
struct NotClone;
// `reflect_clone`-based fast path
#[derive(Component, Reflect, PartialEq, Eq, Default, Debug)]
#[reflect(from_reflect = false)]
struct A {
field: usize,
field2: Vec<usize>,
}
// `ReflectDefault`-based fast path
#[derive(Component, Reflect, PartialEq, Eq, Default, Debug)]
#[reflect(Default)]
#[reflect(from_reflect = false)]
struct B {
field: usize,
field2: Vec<usize>,
#[reflect(ignore)]
ignored: NotClone,
}
// `ReflectFromReflect`-based fast path
#[derive(Component, Reflect, PartialEq, Eq, Default, Debug)]
struct C {
field: usize,
field2: Vec<usize>,
#[reflect(ignore)]
ignored: NotClone,
}
// `ReflectFromWorld`-based fast path
#[derive(Component, Reflect, PartialEq, Eq, Default, Debug)]
#[reflect(FromWorld)]
#[reflect(from_reflect = false)]
struct D {
field: usize,
field2: Vec<usize>,
#[reflect(ignore)]
ignored: NotClone,
}
let mut world = World::default();
world.init_resource::<AppTypeRegistry>();
let registry = world.get_resource::<AppTypeRegistry>().unwrap();
registry.write().register::<(A, B, C, D)>();
let a_id = world.register_component::<A>();
let b_id = world.register_component::<B>();
let c_id = world.register_component::<C>();
let d_id = world.register_component::<D>();
let component_a = A {
field: 5,
field2: vec![1, 2, 3, 4, 5],
};
let component_b = B {
field: 5,
field2: vec![1, 2, 3, 4, 5],
ignored: NotClone,
};
let component_c = C {
field: 6,
field2: vec![1, 2, 3, 4, 5],
ignored: NotClone,
};
let component_d = D {
field: 7,
field2: vec![1, 2, 3, 4, 5],
ignored: NotClone,
};
let e = world
.spawn((component_a, component_b, component_c, component_d))
.id();
let e_clone = world.spawn_empty().id();
EntityCloner::build(&mut world)
.override_clone_behavior_with_id(a_id, ComponentCloneBehavior::reflect())
.override_clone_behavior_with_id(b_id, ComponentCloneBehavior::reflect())
.override_clone_behavior_with_id(c_id, ComponentCloneBehavior::reflect())
.override_clone_behavior_with_id(d_id, ComponentCloneBehavior::reflect())
.clone_entity(e, e_clone);
assert_eq!(world.get::<A>(e_clone), Some(world.get::<A>(e).unwrap()));
assert_eq!(world.get::<B>(e_clone), Some(world.get::<B>(e).unwrap()));
assert_eq!(world.get::<C>(e_clone), Some(world.get::<C>(e).unwrap()));
assert_eq!(world.get::<D>(e_clone), Some(world.get::<D>(e).unwrap()));
}
#[test]
fn read_source_component_reflect_should_return_none_on_invalid_reflect_from_ptr() {
#[derive(Component, Reflect)]
struct A;
#[derive(Component, Reflect)]
struct B;
fn test_handler(source: &SourceComponent, ctx: &mut ComponentCloneCtx) {
let registry = ctx.type_registry().unwrap();
assert!(source.read_reflect(®istry.read()).is_none());
}
let mut world = World::default();
world.init_resource::<AppTypeRegistry>();
let registry = world.get_resource::<AppTypeRegistry>().unwrap();
{
let mut registry = registry.write();
registry.register::<A>();
registry
.get_mut(core::any::TypeId::of::<A>())
.unwrap()
.insert(<ReflectFromPtr as FromType<B>>::from_type());
}
let e = world.spawn(A).id();
let e_clone = world.spawn_empty().id();
EntityCloner::build(&mut world)
.override_clone_behavior::<A>(ComponentCloneBehavior::Custom(test_handler))
.clone_entity(e, e_clone);
}
#[test]
fn clone_entity_specialization() {
#[derive(Component, Reflect, PartialEq, Eq)]
#[reflect(Component)]
struct A {
field: usize,
}
impl Clone for A {
fn clone(&self) -> Self {
Self { field: 10 }
}
}
let mut world = World::default();
world.init_resource::<AppTypeRegistry>();
let registry = world.get_resource::<AppTypeRegistry>().unwrap();
registry.write().register::<A>();
let component = A { field: 5 };
let e = world.spawn(component.clone()).id();
let e_clone = world.spawn_empty().id();
EntityCloner::build(&mut world).clone_entity(e, e_clone);
assert!(world
.get::<A>(e_clone)
.is_some_and(|comp| *comp == A { field: 10 }));
}
#[test]
fn clone_entity_using_reflect_should_skip_without_panic() {
// Not reflected
#[derive(Component, PartialEq, Eq, Default, Debug)]
struct A;
// No valid type data and not `reflect_clone`-able
#[derive(Component, Reflect, PartialEq, Eq, Default, Debug)]
#[reflect(Component)]
#[reflect(from_reflect = false)]
struct B(#[reflect(ignore)] PhantomData<()>);
let mut world = World::default();
// No AppTypeRegistry
let e = world.spawn((A, B(Default::default()))).id();
let e_clone = world.spawn_empty().id();
EntityCloner::build(&mut world)
.override_clone_behavior::<A>(ComponentCloneBehavior::reflect())
.override_clone_behavior::<B>(ComponentCloneBehavior::reflect())
.clone_entity(e, e_clone);
assert_eq!(world.get::<A>(e_clone), None);
assert_eq!(world.get::<B>(e_clone), None);
// With AppTypeRegistry
world.init_resource::<AppTypeRegistry>();
let registry = world.get_resource::<AppTypeRegistry>().unwrap();
registry.write().register::<B>();
let e = world.spawn((A, B(Default::default()))).id();
let e_clone = world.spawn_empty().id();
EntityCloner::build(&mut world).clone_entity(e, e_clone);
assert_eq!(world.get::<A>(e_clone), None);
assert_eq!(world.get::<B>(e_clone), None);
}
}
#[test]
fn clone_entity_using_clone() {
#[derive(Component, Clone, PartialEq, Eq)]
struct A {
field: usize,
}
let mut world = World::default();
let component = A { field: 5 };
let e = world.spawn(component.clone()).id();
let e_clone = world.spawn_empty().id();
EntityCloner::build(&mut world).clone_entity(e, e_clone);
assert!(world.get::<A>(e_clone).is_some_and(|c| *c == component));
}
#[test]
fn clone_entity_with_allow_filter() {
#[derive(Component, Clone, PartialEq, Eq)]
struct A {
field: usize,
}
#[derive(Component, Clone)]
struct B;
let mut world = World::default();
let component = A { field: 5 };
let e = world.spawn((component.clone(), B)).id();
let e_clone = world.spawn_empty().id();
EntityCloner::build(&mut world)
.deny_all()
.allow::<A>()
.clone_entity(e, e_clone);
assert!(world.get::<A>(e_clone).is_some_and(|c| *c == component));
assert!(world.get::<B>(e_clone).is_none());
}
#[test]
fn clone_entity_with_deny_filter() {
#[derive(Component, Clone, PartialEq, Eq)]
struct A {
field: usize,
}
#[derive(Component, Clone)]
struct B;
#[derive(Component, Clone)]
struct C;
let mut world = World::default();
let component = A { field: 5 };
let e = world.spawn((component.clone(), B, C)).id();
let e_clone = world.spawn_empty().id();
EntityCloner::build(&mut world)
.deny::<B>()
.clone_entity(e, e_clone);
assert!(world.get::<A>(e_clone).is_some_and(|c| *c == component));
assert!(world.get::<B>(e_clone).is_none());
assert!(world.get::<C>(e_clone).is_some());
}
#[test]
fn clone_entity_with_override_allow_filter() {
#[derive(Component, Clone, PartialEq, Eq)]
struct A {
field: usize,
}
#[derive(Component, Clone)]
struct B;
#[derive(Component, Clone)]
struct C;
let mut world = World::default();
let component = A { field: 5 };
let e = world.spawn((component.clone(), B, C)).id();
let e_clone = world.spawn_empty().id();
EntityCloner::build(&mut world)
.deny_all()
.allow::<A>()
.allow::<B>()
.allow::<C>()
.deny::<B>()
.clone_entity(e, e_clone);
assert!(world.get::<A>(e_clone).is_some_and(|c| *c == component));
assert!(world.get::<B>(e_clone).is_none());
assert!(world.get::<C>(e_clone).is_some());
}
#[test]
fn clone_entity_with_override_bundle() {
#[derive(Component, Clone, PartialEq, Eq)]
struct A {
field: usize,
}
#[derive(Component, Clone)]
struct B;
#[derive(Component, Clone)]
struct C;
let mut world = World::default();
let component = A { field: 5 };
let e = world.spawn((component.clone(), B, C)).id();
let e_clone = world.spawn_empty().id();
EntityCloner::build(&mut world)
.deny_all()
.allow::<(A, B, C)>()
.deny::<(B, C)>()
.clone_entity(e, e_clone);
assert!(world.get::<A>(e_clone).is_some_and(|c| *c == component));
assert!(world.get::<B>(e_clone).is_none());
assert!(world.get::<C>(e_clone).is_none());
}
#[test]
fn clone_entity_with_required_components() {
#[derive(Component, Clone, PartialEq, Debug)]
#[require(B)]
struct A;
#[derive(Component, Clone, PartialEq, Debug, Default)]
#[require(C(5))]
struct B;
#[derive(Component, Clone, PartialEq, Debug)]
struct C(u32);
let mut world = World::default();
let e = world.spawn(A).id();
let e_clone = world.spawn_empty().id();
EntityCloner::build(&mut world)
.deny_all()
.allow::<B>()
.clone_entity(e, e_clone);
assert_eq!(world.entity(e_clone).get::<A>(), None);
assert_eq!(world.entity(e_clone).get::<B>(), Some(&B));
assert_eq!(world.entity(e_clone).get::<C>(), Some(&C(5)));
}
#[test]
fn clone_entity_with_default_required_components() {
#[derive(Component, Clone, PartialEq, Debug)]
#[require(B)]
struct A;
#[derive(Component, Clone, PartialEq, Debug, Default)]
#[require(C(5))]
struct B;
#[derive(Component, Clone, PartialEq, Debug)]
struct C(u32);
let mut world = World::default();
let e = world.spawn((A, C(0))).id();
let e_clone = world.spawn_empty().id();
EntityCloner::build(&mut world)
.deny_all()
.without_required_components(|builder| {
builder.allow::<A>();
})
.clone_entity(e, e_clone);
assert_eq!(world.entity(e_clone).get::<A>(), Some(&A));
assert_eq!(world.entity(e_clone).get::<B>(), Some(&B));
assert_eq!(world.entity(e_clone).get::<C>(), Some(&C(5)));
}
#[test]
fn clone_entity_with_dynamic_components() {
const COMPONENT_SIZE: usize = 10;
fn test_handler(source: &SourceComponent, ctx: &mut ComponentCloneCtx) {
// SAFETY: the passed in ptr corresponds to copy-able data that matches the type of the source / target component
unsafe {
ctx.write_target_component_ptr(source.ptr());
}
}
let mut world = World::default();
let layout = Layout::array::<u8>(COMPONENT_SIZE).unwrap();
// SAFETY:
// - No drop command is required
// - The component will store [u8; COMPONENT_SIZE], which is Send + Sync
let descriptor = unsafe {
ComponentDescriptor::new_with_layout(
"DynamicComp",
StorageType::Table,
layout,
None,
true,
ComponentCloneBehavior::Custom(test_handler),
)
};
let component_id = world.register_component_with_descriptor(descriptor);
let mut entity = world.spawn_empty();
let data = [5u8; COMPONENT_SIZE];
// SAFETY:
// - ptr points to data represented by component_id ([u8; COMPONENT_SIZE])
// - component_id is from the same world as entity
OwningPtr::make(data, |ptr| unsafe {
entity.insert_by_id(component_id, ptr);
});
let entity = entity.id();
let entity_clone = world.spawn_empty().id();
EntityCloner::build(&mut world).clone_entity(entity, entity_clone);
let ptr = world.get_by_id(entity, component_id).unwrap();
let clone_ptr = world.get_by_id(entity_clone, component_id).unwrap();
// SAFETY: ptr and clone_ptr store component represented by [u8; COMPONENT_SIZE]
unsafe {
assert_eq!(
core::slice::from_raw_parts(ptr.as_ptr(), COMPONENT_SIZE),
core::slice::from_raw_parts(clone_ptr.as_ptr(), COMPONENT_SIZE),
);
}
}
#[test]
fn recursive_clone() {
let mut world = World::new();
let root = world.spawn_empty().id();
let child1 = world.spawn(ChildOf(root)).id();
let grandchild = world.spawn(ChildOf(child1)).id();
let child2 = world.spawn(ChildOf(root)).id();
let clone_root = world.spawn_empty().id();
EntityCloner::build(&mut world)
.linked_cloning(true)
.clone_entity(root, clone_root);
let root_children = world
.entity(clone_root)
.get::<Children>()
.unwrap()
.iter()
.cloned()
.collect::<Vec<_>>();
assert!(root_children.iter().all(|e| *e != child1 && *e != child2));
assert_eq!(root_children.len(), 2);
let child1_children = world.entity(root_children[0]).get::<Children>().unwrap();
assert_eq!(child1_children.len(), 1);
assert_ne!(child1_children[0], grandchild);
assert!(world.entity(root_children[1]).get::<Children>().is_none());
assert_eq!(
world.entity(root).get::<Children>().unwrap().deref(),
&[child1, child2]
);
}
#[test]
fn clone_with_reflect_from_world() {
#[derive(Component, Reflect, PartialEq, Eq, Debug)]
#[reflect(Component, FromWorld, from_reflect = false)]
struct SomeRef(
#[entities] Entity,
// We add an ignored field here to ensure `reflect_clone` fails and `FromWorld` is used
#[reflect(ignore)] PhantomData<()>,
);
#[derive(Resource)]
struct FromWorldCalled(bool);
impl FromWorld for SomeRef {
fn from_world(world: &mut World) -> Self {
world.insert_resource(FromWorldCalled(true));
SomeRef(Entity::PLACEHOLDER, Default::default())
}
}
let mut world = World::new();
let registry = AppTypeRegistry::default();
registry.write().register::<SomeRef>();
world.insert_resource(registry);
let a = world.spawn_empty().id();
let b = world.spawn_empty().id();
let c = world.spawn(SomeRef(a, Default::default())).id();
let d = world.spawn_empty().id();
let mut map = EntityHashMap::<Entity>::new();
map.insert(a, b);
map.insert(c, d);
let cloned = EntityCloner::default().clone_entity_mapped(&mut world, c, &mut map);
assert_eq!(
*world.entity(cloned).get::<SomeRef>().unwrap(),
SomeRef(b, Default::default())
);
assert!(world.resource::<FromWorldCalled>().0);
}
}