Skip to content

Commit 5eba736

Browse files
committed
Initialize channels in App::finish instead of Startup
This allows users to access channels earlier if necessary. Requires adding a few `finish` calls in tests where they were previously missing.
1 parent e1586e2 commit 5eba736

16 files changed

+174
-125
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
- Rename `replication_registry::despawn_recursive` into `replication_registry::despawn`.
2121
- `ReplicationRule` now stores `Vec<ComponentRule>` instead of `Vec<(ComponentId, FnsId)>`
2222
- `RuleFns` now available from prelude.
23+
- Initialize channels in `App::finish` instead of `Startup`. It's called automatically on `App::run`, but in tests you need to call `App::finish` manually.
2324
- Rules created with the same priority now evaluated in their creation order.
2425
- Component removals and insertions for an entity are now buffered and applied as bundles to avoid triggering observers without all components being inserted or removed. This also significantly improves performance by avoiding extra archetype moves and lookups.
2526
- The `Replicated` component is no longer automatically inserted into non-replicated entities spawned from replicated components.

bevy_replicon_example_backend/tests/backend.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ fn connect_disconnect() {
1717
..Default::default()
1818
}),
1919
RepliconExampleBackendPlugins,
20-
));
20+
))
21+
.finish();
2122
}
2223

2324
setup(&mut server_app, &mut client_app).unwrap();
@@ -62,7 +63,8 @@ fn replication() {
6263
..Default::default()
6364
}),
6465
RepliconExampleBackendPlugins,
65-
));
66+
))
67+
.finish();
6668
}
6769

6870
setup(&mut server_app, &mut client_app).unwrap();

src/client.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ impl Plugin for ClientPlugin {
6464
PostUpdate,
6565
(ClientSet::Send, ClientSet::SendPackets).chain(),
6666
)
67-
.add_systems(Startup, setup_channels)
6867
.add_systems(
6968
PreUpdate,
7069
receive_replication
@@ -78,11 +77,13 @@ impl Plugin for ClientPlugin {
7877
if **app.world().resource::<TrackMutateMessages>() {
7978
app.init_resource::<ServerMutateTicks>();
8079
}
81-
}
82-
}
8380

84-
fn setup_channels(mut client: ResMut<RepliconClient>, channels: Res<RepliconChannels>) {
85-
client.setup_server_channels(channels.server_channels().len());
81+
app.world_mut()
82+
.resource_scope(|world, mut client: Mut<RepliconClient>| {
83+
let channels = world.resource::<RepliconChannels>();
84+
client.setup_server_channels(channels.server_channels().len());
85+
});
86+
}
8687
}
8788

8889
/// Receives and applies replication messages from the server.

src/server.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ impl Plugin for ServerPlugin {
110110
.add_observer(handle_connects)
111111
.add_observer(handle_disconnects)
112112
.add_observer(buffer_despawns)
113-
.add_systems(Startup, setup_channels)
114113
.add_systems(
115114
PreUpdate,
116115
(
@@ -175,10 +174,14 @@ impl Plugin for ServerPlugin {
175174
app.register_required_components::<ConnectedClient, ReplicatedClient>();
176175
}
177176
}
178-
}
179177

180-
fn setup_channels(mut server: ResMut<RepliconServer>, channels: Res<RepliconChannels>) {
181-
server.setup_client_channels(channels.client_channels().len());
178+
fn finish(&self, app: &mut App) {
179+
app.world_mut()
180+
.resource_scope(|world, mut server: Mut<RepliconServer>| {
181+
let channels = world.resource::<RepliconChannels>();
182+
server.setup_client_channels(channels.client_channels().len());
183+
});
184+
}
182185
}
183186

184187
/// Increments current server tick which causes the server to replicate this frame.

src/shared/backend/replicon_channels.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ use log::debug;
33

44
/// A resource with all channels used by Replicon.
55
///
6+
/// Initialized in [`ClientPlugin::finish`](crate::client::ClientPlugin) and
7+
/// [`ServerPlugin::finish`](crate::server::ServerPlugin).
8+
///
69
/// Channel IDs are represented by [`usize`], but backends may limit the number of channels.
710
///
811
/// The first two channels are used for replication. For more details, see [`ReplicationChannel`].

src/test_app.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ for app in [&mut server_app, &mut client_app] {
2525
tick_policy: TickPolicy::EveryFrame, // To tick each app update.
2626
..Default::default()
2727
}),
28-
));
28+
))
29+
.finish(); // Don't forget to call `finish`.
2930
}
3031
3132
// Simulate connection between two apps:

tests/connection.rs

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ fn client_to_server() {
1414
let mut server_app = App::new();
1515
let mut client_app = App::new();
1616
for app in [&mut server_app, &mut client_app] {
17-
app.add_plugins((MinimalPlugins, RepliconPlugins));
18-
app.update();
17+
app.add_plugins((MinimalPlugins, RepliconPlugins)).finish();
1918
}
2019

2120
const MESSAGES: &[&[u8]] = &[&[0], &[1]];
@@ -44,8 +43,7 @@ fn server_to_client() {
4443
let mut server_app = App::new();
4544
let mut client_app = App::new();
4645
for app in [&mut server_app, &mut client_app] {
47-
app.add_plugins((MinimalPlugins, RepliconPlugins));
48-
app.update();
46+
app.add_plugins((MinimalPlugins, RepliconPlugins)).finish();
4947
}
5048

5149
const MESSAGES: &[&[u8]] = &[&[0], &[1]];
@@ -71,13 +69,7 @@ fn connect_disconnect() {
7169
let mut server_app = App::new();
7270
let mut client_app = App::new();
7371
for app in [&mut server_app, &mut client_app] {
74-
app.add_plugins((
75-
MinimalPlugins,
76-
RepliconPlugins.set(ServerPlugin {
77-
tick_policy: TickPolicy::EveryFrame,
78-
..Default::default()
79-
}),
80-
));
72+
app.add_plugins((MinimalPlugins, RepliconPlugins)).finish();
8173
}
8274

8375
server_app.connect_client(&mut client_app);
@@ -102,15 +94,7 @@ fn connect_disconnect() {
10294
#[test]
10395
fn client_cleanup_on_disconnect() {
10496
let mut app = App::new();
105-
app.add_plugins((
106-
MinimalPlugins,
107-
RepliconPlugins.set(ServerPlugin {
108-
tick_policy: TickPolicy::EveryFrame,
109-
..Default::default()
110-
}),
111-
));
112-
113-
app.update();
97+
app.add_plugins((MinimalPlugins, RepliconPlugins)).finish();
11498

11599
let mut client = app.world_mut().resource_mut::<RepliconClient>();
116100
client.set_status(RepliconClientStatus::Connected);
@@ -129,15 +113,7 @@ fn client_cleanup_on_disconnect() {
129113
#[test]
130114
fn server_cleanup_on_stop() {
131115
let mut app = App::new();
132-
app.add_plugins((
133-
MinimalPlugins,
134-
RepliconPlugins.set(ServerPlugin {
135-
tick_policy: TickPolicy::EveryFrame,
136-
..Default::default()
137-
}),
138-
));
139-
140-
app.update();
116+
app.add_plugins((MinimalPlugins, RepliconPlugins)).finish();
141117

142118
let mut server = app.world_mut().resource_mut::<RepliconServer>();
143119
server.set_running(true);
@@ -158,13 +134,7 @@ fn server_cleanup_on_stop() {
158134
#[test]
159135
fn client_disconnected() {
160136
let mut app = App::new();
161-
app.add_plugins((
162-
MinimalPlugins,
163-
RepliconPlugins.set(ServerPlugin {
164-
tick_policy: TickPolicy::EveryFrame,
165-
..Default::default()
166-
}),
167-
));
137+
app.add_plugins((MinimalPlugins, RepliconPlugins)).finish();
168138

169139
app.update();
170140

@@ -188,7 +158,8 @@ fn server_inactive() {
188158
tick_policy: TickPolicy::EveryFrame,
189159
..Default::default()
190160
}),
191-
));
161+
))
162+
.finish();
192163

193164
app.update();
194165

@@ -217,7 +188,8 @@ fn deferred_replication() {
217188
replicate_after_connect: false,
218189
..Default::default()
219190
}),
220-
));
191+
))
192+
.finish();
221193
}
222194

223195
server_app.connect_client(&mut client_app);

tests/despawn.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ fn single() {
1515
tick_policy: TickPolicy::EveryFrame,
1616
..Default::default()
1717
}),
18-
));
18+
))
19+
.finish();
1920
}
2021

2122
server_app.connect_client(&mut client_app);
@@ -52,7 +53,8 @@ fn with_hierarchy() {
5253
tick_policy: TickPolicy::EveryFrame,
5354
..Default::default()
5455
}),
55-
));
56+
))
57+
.finish();
5658
}
5759

5860
server_app.connect_client(&mut client_app);
@@ -98,7 +100,8 @@ fn after_spawn() {
98100
..Default::default()
99101
}),
100102
))
101-
.replicate::<TestComponent>();
103+
.replicate::<TestComponent>()
104+
.finish();
102105
}
103106

104107
server_app.connect_client(&mut client_app);
@@ -129,7 +132,8 @@ fn hidden() {
129132
visibility_policy: VisibilityPolicy::Whitelist, // Hide all spawned entities by default.
130133
..Default::default()
131134
}),
132-
));
135+
))
136+
.finish();
133137
}
134138

135139
server_app.connect_client(&mut client_app);

tests/insertion.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ fn table_storage() {
2727
..Default::default()
2828
}),
2929
))
30-
.replicate::<TableComponent>();
30+
.replicate::<TableComponent>()
31+
.finish();
3132
}
3233

3334
server_app.connect_client(&mut client_app);
@@ -64,7 +65,8 @@ fn sparse_set_storage() {
6465
..Default::default()
6566
}),
6667
))
67-
.replicate::<SparseSetComponent>();
68+
.replicate::<SparseSetComponent>()
69+
.finish();
6870
}
6971

7072
server_app.connect_client(&mut client_app);
@@ -101,7 +103,8 @@ fn immutable() {
101103
..Default::default()
102104
}),
103105
))
104-
.replicate::<ImmutableComponent>();
106+
.replicate::<ImmutableComponent>()
107+
.finish();
105108
}
106109

107110
server_app.connect_client(&mut client_app);
@@ -151,7 +154,8 @@ fn mapped_existing_entity() {
151154
..Default::default()
152155
}),
153156
))
154-
.replicate::<MappedComponent>();
157+
.replicate::<MappedComponent>()
158+
.finish();
155159
}
156160

157161
server_app.connect_client(&mut client_app);
@@ -200,7 +204,8 @@ fn mapped_new_entity() {
200204
..Default::default()
201205
}),
202206
))
203-
.replicate::<MappedComponent>();
207+
.replicate::<MappedComponent>()
208+
.finish();
204209
}
205210

206211
server_app.connect_client(&mut client_app);
@@ -246,7 +251,8 @@ fn multiple_components() {
246251
}),
247252
))
248253
.replicate::<ComponentA>()
249-
.replicate::<ComponentB>();
254+
.replicate::<ComponentB>()
255+
.finish();
250256
}
251257

252258
server_app.connect_client(&mut client_app);
@@ -291,7 +297,8 @@ fn command_fns() {
291297
}),
292298
))
293299
.replicate::<OriginalComponent>()
294-
.set_command_fns(replace, command_fns::default_remove::<ReplacedComponent>);
300+
.set_command_fns(replace, command_fns::default_remove::<ReplacedComponent>)
301+
.finish();
295302
}
296303

297304
server_app.connect_client(&mut client_app);
@@ -335,7 +342,8 @@ fn marker() {
335342
.set_marker_fns::<ReplaceMarker, _>(
336343
replace,
337344
command_fns::default_remove::<ReplacedComponent>,
338-
);
345+
)
346+
.finish();
339347
}
340348

341349
server_app.connect_client(&mut client_app);
@@ -382,7 +390,8 @@ fn group() {
382390
..Default::default()
383391
}),
384392
))
385-
.replicate_bundle::<(ComponentA, ComponentB)>();
393+
.replicate_bundle::<(ComponentA, ComponentB)>()
394+
.finish();
386395
}
387396

388397
server_app.connect_client(&mut client_app);
@@ -418,7 +427,8 @@ fn not_replicated() {
418427
tick_policy: TickPolicy::EveryFrame,
419428
..Default::default()
420429
}),
421-
));
430+
))
431+
.finish();
422432
}
423433

424434
server_app.connect_client(&mut client_app);
@@ -455,7 +465,8 @@ fn after_removal() {
455465
..Default::default()
456466
}),
457467
))
458-
.replicate::<TestComponent>();
468+
.replicate::<TestComponent>()
469+
.finish();
459470
}
460471

461472
server_app.connect_client(&mut client_app);
@@ -507,7 +518,8 @@ fn before_started_replication() {
507518
..Default::default()
508519
}),
509520
))
510-
.replicate::<TestComponent>();
521+
.replicate::<TestComponent>()
522+
.finish();
511523
}
512524

513525
server_app.connect_client(&mut client_app);
@@ -553,7 +565,8 @@ fn after_started_replication() {
553565
..Default::default()
554566
}),
555567
))
556-
.replicate::<TestComponent>();
568+
.replicate::<TestComponent>()
569+
.finish();
557570
}
558571

559572
server_app.connect_client(&mut client_app);
@@ -592,7 +605,8 @@ fn confirm_history() {
592605
..Default::default()
593606
}),
594607
))
595-
.replicate::<TestComponent>();
608+
.replicate::<TestComponent>()
609+
.finish();
596610
}
597611

598612
server_app.connect_client(&mut client_app);

0 commit comments

Comments
 (0)