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
use crate::world::local::local_entity::RemoteEntity;
use crate::world::local::local_world_manager::LocalWorldManager;
use crate::{
messages::channels::receivers::indexed_message_reader::IndexedMessageReader,
world::host::host_world_manager::SubCommandId, BitReader, ComponentKind, ComponentKinds,
EntityAuthStatus, EntityMessage, EntityMessageType, HostEntity, MessageIndex, OwnedLocalEntity,
Serde, SerdeErr, Tick,
};
/// Stateless helper that deserializes entity update and message payloads from a bit stream into a [`LocalWorldManager`].
pub struct WorldReader;
impl WorldReader {
// Reading
fn read_message_index(
reader: &mut BitReader,
last_index_opt: &mut Option<MessageIndex>,
) -> Result<MessageIndex, SerdeErr> {
// read index
let current_index = IndexedMessageReader::read_message_index(reader, last_index_opt)?;
*last_index_opt = Some(current_index);
Ok(current_index)
}
/// Deserializes both component updates and entity messages from `reader` into `world_manager`.
pub fn read_world_events(
world_manager: &mut LocalWorldManager,
component_kinds: &ComponentKinds,
tick: &Tick,
reader: &mut BitReader,
) -> Result<(), SerdeErr> {
// read entity updates
Self::read_updates(world_manager, component_kinds, tick, reader)?;
// read entity messages
Self::read_messages(world_manager, component_kinds, reader)?;
Ok(())
}
/// Read incoming Entity messages.
fn read_messages(
world_manager: &mut LocalWorldManager,
component_kinds: &ComponentKinds,
reader: &mut BitReader,
) -> Result<(), SerdeErr> {
let mut last_read_id: Option<MessageIndex> = None;
loop {
// read message continue bit
let message_continue = bool::de(reader)?;
if !message_continue {
break;
}
Self::read_message(world_manager, component_kinds, reader, &mut last_read_id)?;
}
Ok(())
}
/// Read the bits corresponding to the EntityMessage and adds the [`EntityMessage`]
/// to an internal buffer.
///
/// We can use a UnorderedReliableReceiver buffer because the messages have already been
/// ordered by the client's jitter buffer
fn read_message(
world_manager: &mut LocalWorldManager,
component_kinds: &ComponentKinds,
reader: &mut BitReader,
last_read_id: &mut Option<MessageIndex>,
) -> Result<(), SerdeErr> {
let message_id = Self::read_message_index(reader, last_read_id)?;
let message_type = EntityMessageType::de(reader)?;
match message_type {
EntityMessageType::Spawn => {
// Count when Spawn message KIND is recognized on wire (before routing)
#[cfg(feature = "e2e_debug")]
{
extern "Rust" {
fn client_saw_spawn_increment();
}
// Safety: client_saw_spawn_increment is defined by the naia-tests harness
// when compiled with feature = "e2e_debug". It atomically increments a
// counter and has no preconditions. The e2e_debug feature is never enabled
// in production builds.
unsafe {
client_saw_spawn_increment();
}
}
// read remote entity
let remote_entity = RemoteEntity::de(reader)?;
world_manager.receiver_buffer_message(
message_id,
EntityMessage::Spawn(remote_entity.copy_to_owned()),
);
}
EntityMessageType::SpawnWithComponents => {
// read entity as full OwnedLocalEntity (carries is_static) then reverse to Remote
let local_entity = OwnedLocalEntity::de(reader)?.to_reversed();
// read component count
let count = u8::de(reader)?;
let mut kinds = Vec::with_capacity(count as usize);
for _ in 0..count {
let converter = world_manager.entity_converter();
let new_component = component_kinds.read(reader, converter)?;
let new_component_kind = new_component.kind();
world_manager.insert_received_component(
&local_entity,
&new_component_kind,
new_component,
);
kinds.push(new_component_kind);
}
world_manager.receiver_buffer_message(
message_id,
EntityMessage::SpawnWithComponents(local_entity, kinds),
);
}
EntityMessageType::Despawn => {
// read local entity
let mut local_entity = OwnedLocalEntity::de(reader)?.to_reversed();
// apply redirect if entity was migrated
local_entity = world_manager.apply_entity_redirect(local_entity);
world_manager
.receiver_buffer_message(message_id, EntityMessage::Despawn(local_entity));
}
EntityMessageType::InsertComponent => {
// read local entity
let mut local_entity = OwnedLocalEntity::de(reader)?.to_reversed();
// apply redirect if entity was migrated
local_entity = world_manager.apply_entity_redirect(local_entity);
// read component
let converter = world_manager.entity_converter();
let new_component = component_kinds.read(reader, converter)?;
let new_component_kind = new_component.kind();
world_manager.receiver_buffer_message(
message_id,
EntityMessage::InsertComponent(local_entity, new_component_kind),
);
world_manager.insert_received_component(
&local_entity,
&new_component_kind,
new_component,
);
}
EntityMessageType::RemoveComponent => {
// read local entity
let mut local_entity = OwnedLocalEntity::de(reader)?.to_reversed();
// apply redirect if entity was migrated
local_entity = world_manager.apply_entity_redirect(local_entity);
// read component kind
let component_kind = ComponentKind::de(component_kinds, reader)?;
world_manager.receiver_buffer_message(
message_id,
EntityMessage::RemoveComponent(local_entity, component_kind),
);
}
EntityMessageType::Publish => {
// read subcommand id
let sub_command_id = SubCommandId::de(reader)?;
// read local entity
let mut local_entity = OwnedLocalEntity::de(reader)?.to_reversed();
// apply redirect if entity was migrated
local_entity = world_manager.apply_entity_redirect(local_entity);
world_manager.receiver_buffer_message(
message_id,
EntityMessage::Publish(sub_command_id, local_entity),
);
}
EntityMessageType::Unpublish => {
// read subcommand id
let sub_command_id = SubCommandId::de(reader)?;
// read local entity
let mut local_entity = OwnedLocalEntity::de(reader)?.to_reversed();
// apply redirect if entity was migrated
local_entity = world_manager.apply_entity_redirect(local_entity);
world_manager.receiver_buffer_message(
message_id,
EntityMessage::Unpublish(sub_command_id, local_entity),
);
}
EntityMessageType::EnableDelegation => {
// read subcommand id
let sub_command_id = SubCommandId::de(reader)?;
// read local entity
let mut local_entity = OwnedLocalEntity::de(reader)?.to_reversed();
// apply redirect if entity was migrated
local_entity = world_manager.apply_entity_redirect(local_entity);
world_manager.receiver_buffer_message(
message_id,
EntityMessage::EnableDelegation(sub_command_id, local_entity),
);
}
EntityMessageType::DisableDelegation => {
// this command is only ever received by clients, regarding server-owned entities
// read subcommand id
let sub_command_id = SubCommandId::de(reader)?;
// read remote entity
let remote_entity = RemoteEntity::de(reader)?;
world_manager.receiver_buffer_message(
message_id,
EntityMessage::DisableDelegation(sub_command_id, remote_entity.copy_to_owned()),
);
}
EntityMessageType::SetAuthority => {
// this command is only ever received by clients, regarding server-owned entities
// Count when SetAuthority message KIND is recognized on wire (before entity mapping)
#[cfg(feature = "e2e_debug")]
{
extern "Rust" {
fn client_saw_set_auth_wire_increment();
}
// Safety: same as client_saw_spawn_increment above — atomic counter defined
// by the test harness; no preconditions; e2e_debug is never active in prod.
unsafe {
client_saw_set_auth_wire_increment();
}
}
// read subcommand id
let sub_command_id = SubCommandId::de(reader)?;
// read remote entity
let remote_entity = RemoteEntity::de(reader)?;
// read auth status
let auth_status = EntityAuthStatus::de(reader)?;
world_manager.receiver_buffer_message(
message_id,
EntityMessage::SetAuthority(
sub_command_id,
remote_entity.copy_to_owned(),
auth_status,
),
);
}
// below are response-type messages
EntityMessageType::RequestAuthority => {
// this command is only read by the server, regarding server-owned entities
// read subcommand id
let sub_command_id = SubCommandId::de(reader)?;
// read host entity
let host_entity = HostEntity::de(reader)?;
world_manager.receiver_buffer_message(
message_id,
EntityMessage::RequestAuthority(sub_command_id, host_entity.copy_to_owned()),
);
}
EntityMessageType::ReleaseAuthority => {
// this command is only read by the server, regarding server-owned entities
// read subcommand id
let sub_command_id = SubCommandId::de(reader)?;
// read local entity
let mut local_entity = OwnedLocalEntity::de(reader)?.to_reversed();
// apply redirect if entity was migrated
local_entity = world_manager.apply_entity_redirect(local_entity);
world_manager.receiver_buffer_message(
message_id,
EntityMessage::ReleaseAuthority(sub_command_id, local_entity),
);
}
EntityMessageType::EnableDelegationResponse => {
// this command is only read by the server, regarding server-owned entities
// read subcommand id
let sub_command_id = SubCommandId::de(reader)?;
// read host entity
let host_entity = HostEntity::de(reader)?;
world_manager.receiver_buffer_message(
message_id,
EntityMessage::EnableDelegationResponse(
sub_command_id,
host_entity.copy_to_owned(),
),
);
}
EntityMessageType::MigrateResponse => {
// this command is only ever received by clients, regarding newly delegated server-owned entities
// read subcommand id
let sub_command_id = SubCommandId::de(reader)?;
// read client's HostEntity (so client can look it up in entity_map!)
let client_host_entity = HostEntity::de(reader)?;
// read new RemoteEntity (what the client will create)
let new_remote_entity = RemoteEntity::de(reader)?;
world_manager.receiver_buffer_message(
message_id,
EntityMessage::MigrateResponse(
sub_command_id,
client_host_entity.copy_to_owned(),
new_remote_entity,
),
);
}
EntityMessageType::Noop => {
world_manager.receiver_buffer_message(message_id, EntityMessage::Noop);
}
}
Ok(())
}
/// Read component updates from raw bits
fn read_updates(
world_manager: &mut LocalWorldManager,
component_kinds: &ComponentKinds,
tick: &Tick,
reader: &mut BitReader,
) -> Result<(), SerdeErr> {
let mut _update_count = 0;
loop {
// read update continue bit
let update_continue = bool::de(reader)?;
if !update_continue {
break;
}
_update_count += 1;
let mut local_entity = OwnedLocalEntity::de(reader)?.to_reversed();
// apply redirect if entity was migrated
local_entity = world_manager.apply_entity_redirect(local_entity);
Self::read_update(world_manager, component_kinds, tick, reader, &local_entity)?;
}
Ok(())
}
/// Read component updates from raw bits for a given entity
fn read_update(
world_manager: &mut LocalWorldManager,
component_kinds: &ComponentKinds,
tick: &Tick,
reader: &mut BitReader,
local_entity: &OwnedLocalEntity,
) -> Result<(), SerdeErr> {
loop {
// read update continue bit
let component_continue = bool::de(reader)?;
if !component_continue {
break;
}
let component_update = component_kinds.read_create_update(reader)?;
// At this point, the WorldChannel/EntityReceiver should guarantee the Entity is in scope, correct?
if world_manager.has_local_entity(local_entity) {
world_manager.insert_received_update(*tick, local_entity, component_update);
}
}
Ok(())
}
}