From e14bef9119155b46a0bb04a052117b478283ddc9 Mon Sep 17 00:00:00 2001 From: Hennadii Chernyshchyk Date: Sun, 1 Dec 2024 19:38:12 +0200 Subject: [PATCH 1/5] Use is_visible for checking Does the exact same thing. --- src/server.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/server.rs b/src/server.rs index 5c2750d0..cc6131f4 100644 --- a/src/server.rs +++ b/src/server.rs @@ -420,8 +420,7 @@ fn collect_despawns( for entity in despawn_buffer.drain(..) { let entity_range = serialized.write_entity(entity)?; for ((message, _), client) in messages.iter_mut().zip(replicated_clients.iter_mut()) { - let visibility = client.visibility().state(entity); - if visibility != Visibility::Hidden { + if client.visibility().is_visible(entity) { message.add_despawn(entity_range.clone()); } client.remove_despawned(entity); From 4102fb9aa7126f866acf4218259da3e88d42cca2 Mon Sep 17 00:00:00 2001 From: Hennadii Chernyshchyk Date: Sun, 1 Dec 2024 19:38:37 +0200 Subject: [PATCH 2/5] Add a test for despawning hidden entity --- tests/despawn.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/despawn.rs b/tests/despawn.rs index 1d6e29fd..25b2b642 100644 --- a/tests/despawn.rs +++ b/tests/despawn.rs @@ -118,5 +118,42 @@ fn after_spawn() { assert!(client_app.world().entities().is_empty()); } +#[test] +fn hidden() { + let mut server_app = App::new(); + let mut client_app = App::new(); + for app in [&mut server_app, &mut client_app] { + app.add_plugins(( + MinimalPlugins, + RepliconPlugins.set(ServerPlugin { + tick_policy: TickPolicy::EveryFrame, + visibility_policy: VisibilityPolicy::Whitelist, // Hide all spawned entities by default. + ..Default::default() + }), + )); + } + + server_app.connect_client(&mut client_app); + + let server_entity = server_app.world_mut().spawn(Replicated).id(); + + server_app.update(); + server_app.exchange_with_client(&mut client_app); + client_app.update(); + server_app.exchange_with_client(&mut client_app); + + server_app.world_mut().despawn(server_entity); + + server_app.update(); + server_app.exchange_with_client(&mut client_app); + client_app.update(); + + assert_eq!( + client_app.world().entities().total_count(), + 0, + "client shouldn't spawn or despawn hidden entity" + ); +} + #[derive(Component, Deserialize, Serialize)] struct DummyComponent; From 5d807de8e5877a206458da3b4fb3d95cbb3791d4 Mon Sep 17 00:00:00 2001 From: Hennadii Chernyshchyk Date: Sun, 1 Dec 2024 19:39:05 +0200 Subject: [PATCH 3/5] Do not send removals for hidden entities --- src/server.rs | 16 ++++++++++++---- tests/removal.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/server.rs b/src/server.rs index cc6131f4..df920484 100644 --- a/src/server.rs +++ b/src/server.rs @@ -294,7 +294,12 @@ impl ServerPlugin { &mut replicated_clients, &mut set.p5(), )?; - collect_removals(&mut messages, &mut serialized, &removal_buffer)?; + collect_removals( + &mut messages, + &mut serialized, + &replicated_clients, + &removal_buffer, + )?; collect_changes( &mut messages, &mut serialized, @@ -441,14 +446,17 @@ fn collect_despawns( fn collect_removals( messages: &mut ReplicationMessages, serialized: &mut SerializedData, + replicated_clients: &ReplicatedClients, removal_buffer: &RemovalBuffer, ) -> bincode::Result<()> { for (&entity, remove_ids) in removal_buffer.iter() { - let entity = serialized.write_entity(entity)?; + let entity_range = serialized.write_entity(entity)?; let ids_len = remove_ids.len(); let fn_ids = serialized.write_fn_ids(remove_ids.iter().map(|&(_, fns_id)| fns_id))?; - for (message, _) in messages.iter_mut() { - message.add_removals(entity.clone(), ids_len, fn_ids.clone()); + for ((message, _), client) in messages.iter_mut().zip(replicated_clients.iter()) { + if client.visibility().is_visible(entity) { + message.add_removals(entity_range.clone(), ids_len, fn_ids.clone()); + } } } diff --git a/tests/removal.rs b/tests/removal.rs index 1880d1b8..a7dcd22c 100644 --- a/tests/removal.rs +++ b/tests/removal.rs @@ -378,6 +378,49 @@ fn with_despawn() { assert!(client_app.world().entities().is_empty()); } +#[test] +fn hidden() { + let mut server_app = App::new(); + let mut client_app = App::new(); + for app in [&mut server_app, &mut client_app] { + app.add_plugins(( + MinimalPlugins, + RepliconPlugins.set(ServerPlugin { + tick_policy: TickPolicy::EveryFrame, + visibility_policy: VisibilityPolicy::Whitelist, // Hide all spawned entities by default. + ..Default::default() + }), + )) + .replicate::(); + } + + server_app.connect_client(&mut client_app); + + let server_entity = server_app + .world_mut() + .spawn((Replicated, DummyComponent)) + .id(); + + server_app.update(); + server_app.exchange_with_client(&mut client_app); + client_app.update(); + server_app.exchange_with_client(&mut client_app); + + server_app + .world_mut() + .entity_mut(server_entity) + .remove::(); + + server_app.update(); + server_app.exchange_with_client(&mut client_app); + client_app.update(); + + assert!( + client_app.world().entities().is_empty(), + "client shouldn't know about hidden entity" + ); +} + #[derive(Component, Deserialize, Serialize)] struct DummyComponent; From 588422e78eb3dbb77520d52c29807beeea2fe7f8 Mon Sep 17 00:00:00 2001 From: Hennadii Chernyshchyk Date: Sun, 1 Dec 2024 19:39:10 +0200 Subject: [PATCH 4/5] Update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1689ef9e..7a189e41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - RTT, bytes per second and packet loss information for `RepliconClient` and `ConnectedClients`. - `ClientSet::Diagnostics` for systems that collect client diagnostics. +### Fixed + +- Sending removals and despawns for hidden entities. + ### Changed - Make `core::replication::replication_rules::ReplicationRules` public. From 04b619af84bcc51d955bccc008a6620438c8136e Mon Sep 17 00:00:00 2001 From: Hennadii Chernyshchyk Date: Mon, 2 Dec 2024 00:26:25 +0200 Subject: [PATCH 5/5] Missing newlinew --- tests/removal.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/removal.rs b/tests/removal.rs index 1d4cb97f..f2ee99f6 100644 --- a/tests/removal.rs +++ b/tests/removal.rs @@ -489,6 +489,7 @@ fn hidden() { "client shouldn't know about hidden entity" ); } + #[derive(Component, Deserialize, Serialize)] struct DummyComponent;