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
use std::{any::TypeId, collections::HashMap};
use naia_serde::{BitReader, BitWrite, ConstBitLength, Serde, SerdeErr};
use crate::messages::channels::channel::{Channel, ChannelSettings};
type NetId = u16;
#[derive(Eq, Hash, Copy, Clone, PartialEq)]
pub struct ChannelKind {
type_id: TypeId,
}
impl ChannelKind {
pub fn of<C: Channel>() -> Self {
Self {
type_id: TypeId::of::<C>(),
}
}
pub fn ser(&self, channel_kinds: &ChannelKinds, writer: &mut dyn BitWrite) {
channel_kinds.kind_to_net_id(self).ser(writer);
}
pub fn de(channel_kinds: &ChannelKinds, reader: &mut BitReader) -> Result<Self, SerdeErr> {
let net_id: NetId = NetId::de(reader)?;
Ok(channel_kinds.net_id_to_kind(&net_id))
}
}
impl ConstBitLength for ChannelKind {
fn const_bit_length() -> u32 {
<NetId as ConstBitLength>::const_bit_length()
}
}
pub struct ChannelKinds {
current_net_id: NetId,
kind_map: HashMap<ChannelKind, (NetId, ChannelSettings)>,
net_id_map: HashMap<NetId, ChannelKind>,
}
impl ChannelKinds {
pub fn new() -> Self {
Self {
current_net_id: 0,
kind_map: HashMap::new(),
net_id_map: HashMap::new(),
}
}
pub fn add_channel<C: Channel>(&mut self, settings: ChannelSettings) {
let channel_kind = ChannelKind::of::<C>();
let net_id = self.current_net_id;
self.kind_map.insert(channel_kind, (net_id, settings));
self.net_id_map.insert(net_id, channel_kind);
self.current_net_id += 1;
}
pub fn channels(&self) -> Vec<(ChannelKind, ChannelSettings)> {
let mut output = Vec::new();
for (kind, (_, settings)) in &self.kind_map {
output.push((*kind, settings.clone()));
}
output
}
pub fn channel(&self, kind: &ChannelKind) -> ChannelSettings {
let (_, settings) = self.kind_map.get(kind).unwrap();
settings.clone()
}
fn net_id_to_kind(&self, net_id: &NetId) -> ChannelKind {
return *self.net_id_map.get(net_id).expect(
"Must properly initialize Channel with Protocol via `add_channel()` function!",
);
}
fn kind_to_net_id(&self, channel_kind: &ChannelKind) -> NetId {
return self
.kind_map
.get(channel_kind)
.expect(
"Must properly initialize Component with Protocol via `add_channel()` function!",
)
.0;
}
}