naia_shared/world/component/
component_kinds.rs

1use std::{any::TypeId, collections::HashMap};
2
3use naia_serde::{BitReader, BitWrite, ConstBitLength, Serde, SerdeErr};
4
5use crate::{
6    ComponentFieldUpdate, ComponentUpdate, LocalEntityAndGlobalEntityConverter, RemoteEntity,
7    Replicate, ReplicateBuilder,
8};
9
10type NetId = u16;
11
12/// ComponentKind - should be one unique value for each type of Component
13#[derive(Eq, Hash, Copy, Clone, PartialEq, Debug)]
14pub struct ComponentKind {
15    type_id: TypeId,
16}
17
18impl From<TypeId> for ComponentKind {
19    fn from(type_id: TypeId) -> Self {
20        Self { type_id }
21    }
22}
23impl Into<TypeId> for ComponentKind {
24    fn into(self) -> TypeId {
25        self.type_id
26    }
27}
28
29impl ComponentKind {
30    pub fn of<C: Replicate>() -> Self {
31        Self {
32            type_id: TypeId::of::<C>(),
33        }
34    }
35
36    pub fn ser(&self, component_kinds: &ComponentKinds, writer: &mut dyn BitWrite) {
37        component_kinds.kind_to_net_id(self).ser(writer);
38    }
39
40    pub fn de(component_kinds: &ComponentKinds, reader: &mut BitReader) -> Result<Self, SerdeErr> {
41        let net_id: NetId = NetId::de(reader)?;
42        Ok(component_kinds.net_id_to_kind(&net_id))
43    }
44}
45
46impl ConstBitLength for ComponentKind {
47    fn const_bit_length() -> u32 {
48        <NetId as ConstBitLength>::const_bit_length()
49    }
50}
51
52/// A map to hold all component types
53pub struct ComponentKinds {
54    current_net_id: NetId,
55    kind_map: HashMap<ComponentKind, (NetId, Box<dyn ReplicateBuilder>)>,
56    net_id_map: HashMap<NetId, ComponentKind>,
57}
58
59impl ComponentKinds {
60    pub fn new() -> Self {
61        Self {
62            current_net_id: 0,
63            kind_map: HashMap::new(),
64            net_id_map: HashMap::new(),
65        }
66    }
67
68    pub fn add_component<C: Replicate>(&mut self) {
69        let component_kind = ComponentKind::of::<C>();
70
71        let net_id = self.current_net_id;
72        self.kind_map
73            .insert(component_kind, (net_id, C::create_builder()));
74        self.net_id_map.insert(net_id, component_kind);
75        self.current_net_id += 1;
76        //TODO: check for current_id overflow?
77    }
78
79    pub fn read(
80        &self,
81        reader: &mut BitReader,
82        converter: &dyn LocalEntityAndGlobalEntityConverter,
83    ) -> Result<Box<dyn Replicate>, SerdeErr> {
84        let component_kind: ComponentKind = ComponentKind::de(self, reader)?;
85        return self
86            .kind_to_builder(&component_kind)
87            .read(reader, converter);
88    }
89
90    pub fn read_create_update(&self, reader: &mut BitReader) -> Result<ComponentUpdate, SerdeErr> {
91        let component_kind: ComponentKind = ComponentKind::de(self, reader)?;
92        return self
93            .kind_to_builder(&component_kind)
94            .read_create_update(reader);
95    }
96
97    pub fn split_update(
98        &self,
99        converter: &dyn LocalEntityAndGlobalEntityConverter,
100        component_kind: &ComponentKind,
101        update: ComponentUpdate,
102    ) -> Result<
103        (
104            Option<Vec<(RemoteEntity, ComponentFieldUpdate)>>,
105            Option<ComponentUpdate>,
106        ),
107        SerdeErr,
108    > {
109        return self
110            .kind_to_builder(component_kind)
111            .split_update(converter, update);
112    }
113
114    pub fn kind_to_name(&self, component_kind: &ComponentKind) -> String {
115        return self.kind_to_builder(component_kind).name();
116    }
117
118    fn net_id_to_kind(&self, net_id: &NetId) -> ComponentKind {
119        return *self.net_id_map.get(net_id).expect(
120            "Must properly initialize Component with Protocol via `add_component()` function!",
121        );
122    }
123
124    fn kind_to_net_id(&self, component_kind: &ComponentKind) -> NetId {
125        return self
126            .kind_map
127            .get(component_kind)
128            .expect(
129                "Must properly initialize Component with Protocol via `add_component()` function!",
130            )
131            .0;
132    }
133
134    fn kind_to_builder(&self, component_kind: &ComponentKind) -> &Box<dyn ReplicateBuilder> {
135        return &self
136            .kind_map
137            .get(component_kind)
138            .expect(
139                "Must properly initialize Component with Protocol via `add_component()` function!",
140            )
141            .1;
142    }
143}