naia_shared/world/component/
component_kinds.rs1use std::{any::TypeId, collections::HashMap};
2
3use naia_serde::{BitReader, BitWrite, Serde, SerdeErr};
4
5use crate::{
6 ComponentUpdate, LocalEntityAndGlobalEntityConverter,
7 Replicate, ReplicateBuilder,
8};
9use crate::world::component::replicate::SplitUpdateResult;
10
11type NetId = u16;
12
13fn bit_width_for_kind_count(count: NetId) -> u8 {
38 if count < 2 {
41 0
42 } else {
43 (count as u32).next_power_of_two().trailing_zeros() as u8
44 }
45}
46
47#[derive(Eq, Hash, Copy, Clone, PartialEq, Debug)]
49pub struct ComponentKind {
50 type_id: TypeId,
51}
52
53impl From<TypeId> for ComponentKind {
54 fn from(type_id: TypeId) -> Self {
55 Self { type_id }
56 }
57}
58impl From<ComponentKind> for TypeId {
59 fn from(val: ComponentKind) -> Self {
60 val.type_id
61 }
62}
63
64impl ComponentKind {
65 pub fn of<C: Replicate>() -> Self {
67 Self {
68 type_id: TypeId::of::<C>(),
69 }
70 }
71
72 pub fn ser(&self, component_kinds: &ComponentKinds, writer: &mut dyn BitWrite) {
74 let net_id = component_kinds.kind_to_net_id(self);
75 let bits = component_kinds.kind_bit_width;
76 for i in 0..bits {
77 writer.write_bit((net_id >> i) & 1 != 0);
78 }
79 }
80
81 pub fn de(component_kinds: &ComponentKinds, reader: &mut BitReader) -> Result<Self, SerdeErr> {
83 let bits = component_kinds.kind_bit_width;
84 let mut net_id: NetId = 0;
85 for i in 0..bits {
86 if bool::de(reader)? {
87 net_id |= 1 << i;
88 }
89 }
90 Ok(component_kinds.net_id_to_kind(&net_id))
91 }
92}
93
94pub struct ComponentKinds {
96 current_net_id: NetId,
97 kind_bit_width: u8,
101 kind_map: HashMap<ComponentKind, (NetId, Box<dyn ReplicateBuilder>, String)>,
102 net_id_map: HashMap<NetId, ComponentKind>,
103}
104
105impl Clone for ComponentKinds {
106 fn clone(&self) -> Self {
107 let current_net_id = self.current_net_id;
108 let kind_bit_width = self.kind_bit_width;
109 let net_id_map = self.net_id_map.clone();
110
111 let mut kind_map = HashMap::new();
112 for (key, value) in self.kind_map.iter() {
113 kind_map.insert(*key, (value.0, value.1.box_clone(), value.2.clone()));
114 }
115
116 Self {
117 current_net_id,
118 kind_bit_width,
119 kind_map,
120 net_id_map,
121 }
122 }
123}
124
125impl Default for ComponentKinds {
126 fn default() -> Self {
127 Self::new()
128 }
129}
130
131impl ComponentKinds {
132 pub fn new() -> Self {
134 Self {
135 current_net_id: 0,
136 kind_bit_width: 0,
137 kind_map: HashMap::new(),
138 net_id_map: HashMap::new(),
139 }
140 }
141
142 pub fn add_component<C: Replicate>(&mut self) {
144 let component_kind = ComponentKind::of::<C>();
145
146 let net_id = self.current_net_id;
147 self.kind_map.insert(
155 component_kind,
156 (net_id, C::create_builder(), C::protocol_name().to_string()),
157 );
158 self.net_id_map.insert(net_id, component_kind);
159 self.current_net_id += 1;
160 self.kind_bit_width = bit_width_for_kind_count(self.current_net_id);
161 }
162
163 pub fn kind_count(&self) -> u16 {
167 self.current_net_id
168 }
169
170 pub fn read(
172 &self,
173 reader: &mut BitReader,
174 converter: &dyn LocalEntityAndGlobalEntityConverter,
175 ) -> Result<Box<dyn Replicate>, SerdeErr> {
176 let component_kind: ComponentKind = ComponentKind::de(self, reader)?;
177 self
178 .kind_to_builder(&component_kind)
179 .read(reader, converter)
180 }
181
182 pub fn read_create_update(&self, reader: &mut BitReader) -> Result<ComponentUpdate, SerdeErr> {
184 let component_kind: ComponentKind = ComponentKind::de(self, reader)?;
185 self
186 .kind_to_builder(&component_kind)
187 .read_create_update(reader)
188 }
189
190 pub fn split_update(
192 &self,
193 converter: &dyn LocalEntityAndGlobalEntityConverter,
194 component_kind: &ComponentKind,
195 update: ComponentUpdate,
196 ) -> SplitUpdateResult {
197 self
198 .kind_to_builder(component_kind)
199 .split_update(converter, update)
200 }
201
202 pub fn kind_to_name(&self, component_kind: &ComponentKind) -> String {
204 self
205 .kind_map
206 .get(component_kind)
207 .expect(
208 "Must properly initialize Component with Protocol via `add_component()` function!",
209 )
210 .2
211 .clone()
212 }
213
214 fn net_id_to_kind(&self, net_id: &NetId) -> ComponentKind {
215 *self.net_id_map.get(net_id).expect(
216 "Must properly initialize Component with Protocol via `add_component()` function!",
217 )
218 }
219
220 fn kind_to_net_id(&self, component_kind: &ComponentKind) -> NetId {
221 self
222 .kind_map
223 .get(component_kind)
224 .expect(
225 "Must properly initialize Component with Protocol via `add_component()` function!",
226 )
227 .0
228 }
229
230 pub fn net_id_of(&self, component_kind: &ComponentKind) -> Option<u16> {
234 self.kind_map.get(component_kind).map(|(net_id, _, _)| *net_id)
235 }
236
237 fn kind_to_builder(&self, component_kind: &ComponentKind) -> &dyn ReplicateBuilder {
238 self
239 .kind_map
240 .get(component_kind)
241 .expect(
242 "Must properly initialize Component with Protocol via `add_component()` function!",
243 )
244 .1
245 .as_ref()
246 }
247
248 pub fn kind_is_immutable(&self, component_kind: &ComponentKind) -> bool {
250 self.kind_map
251 .get(component_kind)
252 .map(|(_, builder, _)| builder.is_immutable())
253 .unwrap_or(false)
254 }
255
256 pub fn all_names(&self) -> Vec<String> {
258 let mut output = Vec::new();
259 for (_, _, name) in self.kind_map.values() {
260 output.push(name.clone());
261 }
262 output.sort();
263 output
264 }
265}