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