naia_hecs_shared/
protocol.rs

1use std::time::Duration;
2
3use hecs::World;
4
5use naia_shared::{
6    Channel, ChannelDirection, ChannelMode, ComponentKind, CompressionConfig,
7    LinkConditionerConfig, Message, Protocol as InnerProtocol, ProtocolPlugin, Replicate,
8    SocketConfig,
9};
10
11use crate::{WorldData, WorldWrapper};
12
13pub struct Protocol {
14    inner: InnerProtocol,
15    world_data: Option<WorldData>,
16}
17
18impl Default for Protocol {
19    fn default() -> Self {
20        Protocol {
21            inner: InnerProtocol::default(),
22            world_data: Some(WorldData::new()),
23        }
24    }
25}
26
27impl Into<InnerProtocol> for Protocol {
28    fn into(self) -> InnerProtocol {
29        self.inner
30    }
31}
32
33impl Protocol {
34    pub fn builder() -> Self {
35        Self::default()
36    }
37
38    pub fn wrap_world(&mut self, hecs_world: World) -> WorldWrapper {
39        WorldWrapper::wrap(self, hecs_world)
40    }
41
42    pub fn world_data(&mut self) -> WorldData {
43        self.world_data.take().expect("should only call this once")
44    }
45
46    pub fn add_plugin<P: ProtocolPlugin>(&mut self, plugin: P) -> &mut Self {
47        self.inner.add_plugin(plugin);
48        self
49    }
50
51    pub fn link_condition(&mut self, config: LinkConditionerConfig) -> &mut Self {
52        self.inner.link_condition(config);
53        self
54    }
55
56    pub fn rtc_endpoint(&mut self, path: String) -> &mut Self {
57        self.inner.rtc_endpoint(path);
58        self
59    }
60
61    pub fn tick_interval(&mut self, duration: Duration) -> &mut Self {
62        self.inner.tick_interval(duration);
63        self
64    }
65
66    pub fn compression(&mut self, config: CompressionConfig) -> &mut Self {
67        self.inner.compression(config);
68        self
69    }
70
71    pub fn add_default_channels(&mut self) -> &mut Self {
72        self.inner.add_default_channels();
73        self
74    }
75
76    pub fn add_channel<C: Channel>(
77        &mut self,
78        direction: ChannelDirection,
79        mode: ChannelMode,
80    ) -> &mut Self {
81        self.inner.add_channel::<C>(direction, mode);
82        self
83    }
84
85    pub fn add_message<M: Message>(&mut self) -> &mut Self {
86        self.inner.add_message::<M>();
87        self
88    }
89
90    pub fn add_component<C: Replicate>(&mut self) -> &mut Self {
91        self.inner.add_component::<C>();
92        self.world_data
93            .as_mut()
94            .expect("shouldn't happen")
95            .put_kind::<C>(&ComponentKind::of::<C>());
96        self
97    }
98
99    pub fn lock(&mut self) {
100        self.inner.lock();
101    }
102
103    pub fn build(&mut self) -> Self {
104        std::mem::take(self)
105    }
106
107    pub fn socket_config(&self) -> &SocketConfig {
108        &self.inner.socket
109    }
110}