naia-shared 0.24.0

Common functionality shared between naia-server & naia-client crates
Documentation
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
use std::{
    clone::Clone,
    collections::{HashMap, HashSet, VecDeque},
    hash::Hash,
};

use crate::{
    messages::channels::senders::indexed_message_writer::IndexedMessageWriter,
    sequence_list::SequenceList,
    world::{
        entity::entity_converters::GlobalWorldManagerType, local_world_manager::LocalWorldManager,
    },
    BitWrite, BitWriter, ComponentKind, ComponentKinds, ConstBitLength, EntityAction,
    EntityActionType, EntityAndLocalEntityConverter, EntityConverterMut, HostWorldEvents,
    HostWorldManager, Instant, MessageIndex, PacketIndex, Serde, UnsignedVariableInteger,
    WorldRefType,
};

use super::entity_action_event::EntityActionEvent;

pub type ActionId = MessageIndex;

pub struct HostWorldWriter;

impl HostWorldWriter {
    fn write_action_id(
        writer: &mut dyn BitWrite,
        last_id_opt: &mut Option<ActionId>,
        current_id: &ActionId,
    ) {
        IndexedMessageWriter::write_message_index(writer, last_id_opt, current_id);
        *last_id_opt = Some(*current_id);
    }

    pub fn write_into_packet<E: Copy + Eq + Hash + Send + Sync, W: WorldRefType<E>>(
        component_kinds: &ComponentKinds,
        now: &Instant,
        writer: &mut BitWriter,
        packet_index: &PacketIndex,
        world: &W,
        global_world_manager: &dyn GlobalWorldManagerType<E>,
        local_world_manager: &mut LocalWorldManager<E>,
        has_written: &mut bool,
        host_manager: &mut HostWorldManager<E>,
        world_events: &mut HostWorldEvents<E>,
    ) {
        // write entity updates
        Self::write_updates(
            component_kinds,
            now,
            writer,
            &packet_index,
            world,
            global_world_manager,
            local_world_manager,
            has_written,
            host_manager,
            &mut world_events.next_send_updates,
        );

        // write entity actions
        Self::write_actions(
            component_kinds,
            now,
            writer,
            &packet_index,
            world,
            global_world_manager,
            local_world_manager,
            has_written,
            host_manager,
            &mut world_events.next_send_actions,
        );
    }

    fn write_actions<E: Copy + Eq + Hash + Send + Sync, W: WorldRefType<E>>(
        component_kinds: &ComponentKinds,
        now: &Instant,
        writer: &mut BitWriter,
        packet_index: &PacketIndex,
        world: &W,
        global_world_manager: &dyn GlobalWorldManagerType<E>,
        local_world_manager: &mut LocalWorldManager<E>,
        has_written: &mut bool,
        host_manager: &mut HostWorldManager<E>,
        next_send_actions: &mut VecDeque<(ActionId, EntityActionEvent<E>)>,
    ) {
        let mut last_counted_id: Option<MessageIndex> = None;
        let mut last_written_id: Option<MessageIndex> = None;

        loop {
            if next_send_actions.is_empty() {
                break;
            }

            // check that we can write the next message
            let mut counter = writer.counter();
            // write ActionContinue bit
            true.ser(&mut counter);
            // write data
            Self::write_action(
                component_kinds,
                world,
                global_world_manager,
                local_world_manager,
                packet_index,
                &mut counter,
                &mut last_counted_id,
                false,
                host_manager,
                next_send_actions,
            );
            if counter.overflowed() {
                // if nothing useful has been written in this packet yet,
                // send warning about size of component being too big
                if !*has_written {
                    Self::warn_overflow_action(
                        component_kinds,
                        counter.bits_needed(),
                        writer.bits_free(),
                        next_send_actions,
                    );
                }
                break;
            }

            *has_written = true;

            // optimization
            if !host_manager
                .sent_action_packets
                .contains_scan_from_back(packet_index)
            {
                host_manager
                    .sent_action_packets
                    .insert_scan_from_back(*packet_index, (now.clone(), Vec::new()));
            }

            // write ActionContinue bit
            true.ser(writer);
            // write data
            Self::write_action(
                component_kinds,
                world,
                global_world_manager,
                local_world_manager,
                packet_index,
                writer,
                &mut last_written_id,
                true,
                host_manager,
                next_send_actions,
            );

            // pop action we've written
            next_send_actions.pop_front();
        }

        // Finish actions by writing false ActionContinue bit
        writer.release_bits(1);
        false.ser(writer);
    }

    #[allow(clippy::too_many_arguments)]
    fn write_action<E: Copy + Eq + Hash + Send + Sync, W: WorldRefType<E>>(
        component_kinds: &ComponentKinds,
        world: &W,
        global_world_manager: &dyn GlobalWorldManagerType<E>,
        local_world_manager: &mut LocalWorldManager<E>,
        packet_index: &PacketIndex,
        writer: &mut dyn BitWrite,
        last_written_id: &mut Option<ActionId>,
        is_writing: bool,
        host_manager: &mut HostWorldManager<E>,
        next_send_actions: &mut VecDeque<(ActionId, EntityActionEvent<E>)>,
    ) {
        let (action_id, action) = next_send_actions.front().unwrap();

        // write message id
        Self::write_action_id(writer, last_written_id, action_id);

        match action {
            EntityActionEvent::SpawnEntity(world_entity, component_kind_list) => {
                EntityActionType::SpawnEntity.ser(writer);

                // write net entity
                local_world_manager
                    .entity_to_host_entity(world_entity)
                    .unwrap()
                    .ser(writer);

                // write number of components
                let components_num =
                    UnsignedVariableInteger::<3>::new(component_kind_list.len() as i128);
                components_num.ser(writer);

                for component_kind in component_kind_list {
                    let mut converter =
                        EntityConverterMut::new(global_world_manager, local_world_manager);

                    // write component payload
                    world
                        .component_of_kind(world_entity, component_kind)
                        .expect("Component does not exist in World")
                        .write(component_kinds, writer, &mut converter);
                }

                // if we are writing to this packet, add it to record
                if is_writing {
                    Self::record_action_written(
                        &mut host_manager.sent_action_packets,
                        packet_index,
                        action_id,
                        EntityAction::SpawnEntity(*world_entity, component_kind_list.clone()),
                    );
                }
            }
            EntityActionEvent::DespawnEntity(world_entity) => {
                EntityActionType::DespawnEntity.ser(writer);

                // write net entity
                local_world_manager
                    .entity_to_host_entity(world_entity)
                    .unwrap()
                    .ser(writer);

                // if we are writing to this packet, add it to record
                if is_writing {
                    Self::record_action_written(
                        &mut host_manager.sent_action_packets,
                        packet_index,
                        action_id,
                        EntityAction::DespawnEntity(*world_entity),
                    );
                }
            }
            EntityActionEvent::InsertComponent(world_entity, component) => {
                if !host_manager
                    .world_channel
                    .entity_channel_is_open(world_entity)
                    || !world.has_component_of_kind(world_entity, component)
                {
                    EntityActionType::Noop.ser(writer);

                    // if we are actually writing this packet
                    if is_writing {
                        // add it to action record
                        Self::record_action_written(
                            &mut host_manager.sent_action_packets,
                            packet_index,
                            action_id,
                            EntityAction::Noop,
                        );
                    }
                } else {
                    EntityActionType::InsertComponent.ser(writer);

                    // write net entity
                    local_world_manager
                        .entity_to_host_entity(world_entity)
                        .unwrap()
                        .ser(writer);

                    let mut converter =
                        EntityConverterMut::new(global_world_manager, local_world_manager);

                    // write component payload
                    world
                        .component_of_kind(world_entity, component)
                        .expect("Component does not exist in World")
                        .write(component_kinds, writer, &mut converter);

                    // if we are actually writing this packet
                    if is_writing {
                        // add it to action record
                        Self::record_action_written(
                            &mut host_manager.sent_action_packets,
                            packet_index,
                            action_id,
                            EntityAction::InsertComponent(*world_entity, *component),
                        );
                    }
                }
            }
            EntityActionEvent::RemoveComponent(world_entity, component_kind) => {
                if !host_manager
                    .world_channel
                    .entity_channel_is_open(world_entity)
                {
                    EntityActionType::Noop.ser(writer);

                    // if we are actually writing this packet
                    if is_writing {
                        // add it to action record
                        Self::record_action_written(
                            &mut host_manager.sent_action_packets,
                            packet_index,
                            action_id,
                            EntityAction::Noop,
                        );
                    }
                } else {
                    EntityActionType::RemoveComponent.ser(writer);

                    // write net entity
                    local_world_manager
                        .entity_to_host_entity(world_entity)
                        .unwrap()
                        .ser(writer);

                    // write component kind
                    component_kind.ser(component_kinds, writer);

                    // if we are writing to this packet, add it to record
                    if is_writing {
                        Self::record_action_written(
                            &mut host_manager.sent_action_packets,
                            packet_index,
                            action_id,
                            EntityAction::RemoveComponent(*world_entity, *component_kind),
                        );
                    }
                }
            }
        }
    }

    #[allow(clippy::type_complexity)]
    fn record_action_written<E: Copy + Eq + Hash + Send + Sync>(
        sent_actions: &mut SequenceList<(Instant, Vec<(ActionId, EntityAction<E>)>)>,
        packet_index: &PacketIndex,
        action_id: &ActionId,
        action_record: EntityAction<E>,
    ) {
        let (_, sent_actions_list) = sent_actions.get_mut_scan_from_back(packet_index).unwrap();
        sent_actions_list.push((*action_id, action_record));
    }

    fn warn_overflow_action<E: Copy + Eq + Hash + Send + Sync>(
        component_kinds: &ComponentKinds,
        bits_needed: u32,
        bits_free: u32,
        next_send_actions: &VecDeque<(ActionId, EntityActionEvent<E>)>,
    ) {
        let (_action_id, action) = next_send_actions.front().unwrap();

        match action {
            EntityActionEvent::SpawnEntity(_entity, component_kind_list) => {
                let mut component_names = "".to_owned();
                let mut added = false;

                for component_kind in component_kind_list {
                    if added {
                        component_names.push(',');
                    } else {
                        added = true;
                    }
                    let name = component_kinds.kind_to_name(component_kind);
                    component_names.push_str(&name);
                }
                panic!(
                    "Packet Write Error: Blocking overflow detected! Entity Spawn message with Components `{component_names}` requires {bits_needed} bits, but packet only has {bits_free} bits available! Recommend slimming down these Components."
                )
            }
            EntityActionEvent::InsertComponent(_entity, component_kind) => {
                let component_name = component_kinds.kind_to_name(component_kind);
                panic!(
                    "Packet Write Error: Blocking overflow detected! Component Insertion message of type `{component_name}` requires {bits_needed} bits, but packet only has {bits_free} bits available! This condition should never be reached, as large Messages should be Fragmented in the Reliable channel"
                )
            }
            _ => {
                panic!(
                    "Packet Write Error: Blocking overflow detected! Action requires {bits_needed} bits, but packet only has {bits_free} bits available! This message should never display..."
                )
            }
        }
    }

    fn write_updates<E: Copy + Eq + Hash + Send + Sync, W: WorldRefType<E>>(
        component_kinds: &ComponentKinds,
        now: &Instant,
        writer: &mut BitWriter,
        packet_index: &PacketIndex,
        world: &W,
        global_world_manager: &dyn GlobalWorldManagerType<E>,
        local_world_manager: &mut LocalWorldManager<E>,
        has_written: &mut bool,
        host_manager: &mut HostWorldManager<E>,
        next_send_updates: &mut HashMap<E, HashSet<ComponentKind>>,
    ) {
        let all_update_entities: Vec<E> = next_send_updates.keys().copied().collect();

        for entity in all_update_entities {
            // get LocalEntity
            let host_entity = local_world_manager.entity_to_host_entity(&entity).unwrap();

            // check that we can at least write a LocalEntity and a ComponentContinue bit
            let mut counter = writer.counter();
            // reserve ComponentContinue bit
            counter.write_bit(true);
            // write UpdateContinue bit
            counter.write_bit(true);
            // write LocalEntity
            host_entity.ser(&mut counter);
            if counter.overflowed() {
                break;
            }

            // reserve ComponentContinue bit
            writer.reserve_bits(1);
            // write UpdateContinue bit
            true.ser(writer);
            // write HostEntity
            host_entity.ser(writer);
            // write Components
            Self::write_update(
                component_kinds,
                now,
                world,
                global_world_manager,
                local_world_manager,
                packet_index,
                writer,
                &entity,
                has_written,
                host_manager,
                next_send_updates,
            );

            // write ComponentContinue finish bit, release
            writer.release_bits(1);
            false.ser(writer);
        }

        // write EntityContinue finish bit, release
        writer.release_bits(1);
        false.ser(writer);
    }

    /// For a given entity, write component value updates into a packet
    /// Only component values that changed in the internal (naia's) host world will be written
    fn write_update<E: Copy + Eq + Hash + Send + Sync, W: WorldRefType<E>>(
        component_kinds: &ComponentKinds,
        now: &Instant,
        world: &W,
        global_world_manager: &dyn GlobalWorldManagerType<E>,
        local_world_manager: &mut LocalWorldManager<E>,
        packet_index: &PacketIndex,
        writer: &mut BitWriter,
        entity: &E,
        has_written: &mut bool,
        host_manager: &mut HostWorldManager<E>,
        next_send_updates: &mut HashMap<E, HashSet<ComponentKind>>,
    ) {
        let mut written_component_kinds = Vec::new();
        let component_kind_set = next_send_updates.get(entity).unwrap();
        for component_kind in component_kind_set {
            // get diff mask
            let diff_mask = host_manager
                .world_channel
                .diff_handler
                .diff_mask(entity, component_kind)
                .clone();

            let mut converter = EntityConverterMut::new(global_world_manager, local_world_manager);

            // check that we can write the next component update
            let mut counter = writer.counter();
            // write ComponentContinue bit
            true.ser(&mut counter);
            // write component kind
            counter.count_bits(<ComponentKind as ConstBitLength>::const_bit_length());
            // write data
            world
                .component_of_kind(entity, component_kind)
                .expect("Component does not exist in World")
                .write_update(&diff_mask, &mut counter, &mut converter);
            if counter.overflowed() {
                // if nothing useful has been written in this packet yet,
                // send warning about size of component being too big
                if !*has_written {
                    let component_name = component_kinds.kind_to_name(component_kind);
                    Self::warn_overflow_update(
                        component_name,
                        counter.bits_needed(),
                        writer.bits_free(),
                    );
                }

                break;
            }

            *has_written = true;

            // write ComponentContinue bit
            true.ser(writer);
            // write component kind
            component_kind.ser(component_kinds, writer);
            // write data
            world
                .component_of_kind(entity, component_kind)
                .expect("Component does not exist in World")
                .write_update(&diff_mask, writer, &mut converter);

            written_component_kinds.push(*component_kind);

            // place diff mask in a special transmission record - like map
            host_manager.last_update_packet_index = *packet_index;

            if !host_manager.sent_updates.contains_key(packet_index) {
                host_manager
                    .sent_updates
                    .insert(*packet_index, (now.clone(), HashMap::new()));
            }
            let (_, sent_updates_map) = host_manager.sent_updates.get_mut(packet_index).unwrap();
            sent_updates_map.insert((*entity, *component_kind), diff_mask);

            // having copied the diff mask for this update, clear the component
            host_manager
                .world_channel
                .diff_handler
                .clear_diff_mask(entity, component_kind);
        }

        let update_kinds = next_send_updates.get_mut(entity).unwrap();
        for component_kind in &written_component_kinds {
            update_kinds.remove(component_kind);
        }
        if update_kinds.is_empty() {
            next_send_updates.remove(entity);
        }
    }

    fn warn_overflow_update(component_name: String, bits_needed: u32, bits_free: u32) {
        panic!(
            "Packet Write Error: Blocking overflow detected! Data update of Component `{component_name}` requires {bits_needed} bits, but packet only has {bits_free} bits available! Recommended to slim down this Component"
        )
    }
}