naia_shared/world/update/
global_diff_handler.rs1use std::{collections::HashMap, net::SocketAddr};
2
3use crate::{ComponentKind, ComponentKinds, GlobalEntity, GlobalWorldManagerType};
4
5use crate::world::update::mut_channel::{MutChannel, MutReceiver, MutReceiverBuilder, MutSender};
6
7pub struct GlobalDiffHandler {
9 mut_receiver_builders: HashMap<(GlobalEntity, ComponentKind), MutReceiverBuilder>,
10 kind_bits: HashMap<ComponentKind, u16>,
18 max_kind_count: u16,
23}
24
25#[cfg(feature = "test_utils")]
26impl GlobalDiffHandler {
27 #[doc(hidden)]
28 pub fn receiver_count(&self) -> usize {
29 self.mut_receiver_builders.len()
30 }
31
32 #[doc(hidden)]
33 pub fn receiver_count_by_kind(&self) -> HashMap<ComponentKind, usize> {
34 let mut map = HashMap::new();
35 for &(_, kind) in self.mut_receiver_builders.keys() {
36 *map.entry(kind).or_insert(0) += 1;
37 }
38 map
39 }
40}
41
42impl Default for GlobalDiffHandler {
43 fn default() -> Self {
44 Self::new()
45 }
46}
47
48impl GlobalDiffHandler {
49 pub fn new() -> Self {
51 Self {
52 mut_receiver_builders: HashMap::new(),
53 kind_bits: HashMap::new(),
54 max_kind_count: 0,
55 }
56 }
57
58 pub fn kind_bit(&self, component_kind: &ComponentKind) -> Option<u16> {
62 self.kind_bits.get(component_kind).copied()
63 }
64
65 pub fn kind_count(&self) -> u16 {
69 self.max_kind_count
70 }
71
72 pub fn has_component(&self, global_entity: &GlobalEntity, component_kind: &ComponentKind) -> bool {
74 self.mut_receiver_builders.contains_key(&(*global_entity, *component_kind))
75 }
76
77 pub fn register_component(
79 &mut self,
80 component_kinds: &ComponentKinds,
81 global_world_manager: &dyn GlobalWorldManagerType,
82 global_entity: &GlobalEntity,
83 component_kind: &ComponentKind,
84 diff_mask_length: u8,
85 ) -> MutSender {
86 let name = component_kinds.kind_to_name(component_kind);
87
88 if self
89 .mut_receiver_builders
90 .contains_key(&(*global_entity, *component_kind))
91 {
92 panic!(
93 "GlobalDiffHandler: For Entity {:?}, Component {} cannot Register more than once!",
94 global_entity, name
95 );
96 }
97
98 let (sender, builder) = MutChannel::new_channel(global_world_manager, diff_mask_length);
99
100 self.mut_receiver_builders
101 .insert((*global_entity, *component_kind), builder);
102
103 if let std::collections::hash_map::Entry::Vacant(entry) =
104 self.kind_bits.entry(*component_kind)
105 {
106 if let Some(net_id) = component_kinds.net_id_of(component_kind) {
107 entry.insert(net_id);
108 if net_id + 1 > self.max_kind_count {
109 self.max_kind_count = net_id + 1;
110 }
111 }
112 }
113
114 sender
115 }
116
117 pub fn deregister_component(&mut self, entity: &GlobalEntity, component_kind: &ComponentKind) {
119 self.mut_receiver_builders
120 .remove(&(*entity, *component_kind));
121 }
122
123 pub fn receiver(
125 &self,
126 address: &Option<SocketAddr>,
127 entity: &GlobalEntity,
128 component_kind: &ComponentKind,
129 ) -> Option<MutReceiver> {
130 if let Some(builder) = self.mut_receiver_builders.get(&(*entity, *component_kind)) {
131 return builder.build(address);
132 }
133 None
134 }
135}