~starkingdoms/starkingdoms

ref: a232f72c0eb31fd489b8b1c713d21241fb6ee56d starkingdoms/protocol/src/lib.rs -rw-r--r-- 11.0 KiB
a232f72c — ghostlyzsh one time i heard that the base for module manipulation is good 2 years ago
                                                                                
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
use crate::message_c2s::{
    MessageC2SAuthenticateAndBeamOut, MessageC2SChat, MessageC2SGoodbye, MessageC2SHello,
    MessageC2SInput, MessageC2SModuleGrabBegin, MessageC2SModuleGrabEnd, MessageC2SMouseInput, MessageC2SPing,
};
use crate::message_s2c::{
    MessageS2CChat, MessageS2CGoodbye, MessageS2CHello, MessageS2CModulesUpdate,
    MessageS2CModuleAdd, MessageS2CModuleRemove, MessageS2CModuleTreeUpdate,
    MessageS2CPlanetData, MessageS2CPlayersUpdate, MessageS2CPong,
};
use crate::planet::PlanetType;
use crate::starkingdoms_protocol::PacketWrapper;
use protobuf::{Enum, Message};
use std::error::Error;
include!(concat!(env!("OUT_DIR"), "/protos/mod.rs"));

pub const PROTOCOL_VERSION: u32 = 5;

pub mod api;

#[derive(Debug)]
pub enum MessageC2S {
    Hello(MessageC2SHello),
    Goodbye(MessageC2SGoodbye),
    Chat(MessageC2SChat),
    Ping(MessageC2SPing),
    Input(MessageC2SInput),
    AuthenticateAndBeamOut(MessageC2SAuthenticateAndBeamOut),
    MouseInput(MessageC2SMouseInput),
    ModuleGrabBegin(MessageC2SModuleGrabBegin),
    ModuleGrabEnd(MessageC2SModuleGrabEnd),
}

#[derive(Debug)]
pub enum MessageS2C {
    Hello(MessageS2CHello),
    Goodbye(MessageS2CGoodbye),
    Chat(MessageS2CChat),
    Pong(MessageS2CPong),
    PlayersUpdate(MessageS2CPlayersUpdate),
    PlanetData(MessageS2CPlanetData),
    ModulesUpdate(MessageS2CModulesUpdate),
    ModuleTreeUpdate(MessageS2CModuleTreeUpdate),
    ModuleAdd(MessageS2CModuleAdd),
    ModuleRemove(MessageS2CModuleRemove),
}

impl TryFrom<&[u8]> for MessageC2S {
    type Error = Box<dyn Error>;

    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
        let pkt = starkingdoms_protocol::PacketWrapper::parse_from_bytes(value)?;

        let deser_pkt = match pkt.packet_id {
            _id if _id == message_c2s::message_c2shello::Packet_info::type_.value() as i64 => {
                MessageC2S::Hello(MessageC2SHello::parse_from_bytes(&pkt.packet_data)?)
            }
            _id if _id == message_c2s::message_c2sgoodbye::Packet_info::type_.value() as i64 => {
                MessageC2S::Goodbye(MessageC2SGoodbye::parse_from_bytes(&pkt.packet_data)?)
            }
            _id if _id == message_c2s::message_c2schat::Packet_info::type_.value() as i64 => {
                MessageC2S::Chat(MessageC2SChat::parse_from_bytes(&pkt.packet_data)?)
            }
            _id if _id == message_c2s::message_c2sping::Packet_info::type_.value() as i64 => {
                MessageC2S::Ping(MessageC2SPing::parse_from_bytes(&pkt.packet_data)?)
            }
            _id if _id == message_c2s::message_c2sinput::Packet_info::type_.value() as i64 => {
                MessageC2S::Input(MessageC2SInput::parse_from_bytes(&pkt.packet_data)?)
            }
            _id if _id
                == message_c2s::message_c2sauthenticate_and_beam_out::Packet_info::type_.value()
                    as i64 =>
            {
                MessageC2S::AuthenticateAndBeamOut(
                    MessageC2SAuthenticateAndBeamOut::parse_from_bytes(&pkt.packet_data)?,
                )
            }
            _id if _id
                == message_c2s::message_c2smouse_input::Packet_info::type_.value() as i64 =>
            {
                MessageC2S::MouseInput(MessageC2SMouseInput::parse_from_bytes(&pkt.packet_data)?)
            }
            _id if _id
                == message_c2s::message_c2smodule_grab_begin::Packet_info::type_.value() as i64 =>
            {
                MessageC2S::ModuleGrabBegin(MessageC2SModuleGrabBegin::parse_from_bytes(&pkt.packet_data)?)
            }
            _id if _id
                == message_c2s::message_c2smodule_grab_end::Packet_info::type_.value() as i64 =>
            {
                MessageC2S::ModuleGrabEnd(MessageC2SModuleGrabEnd::parse_from_bytes(&pkt.packet_data)?)
            }
            _id => {
                return Err(format!("Unrecognized C2S packet {}", _id).into());
            }
        };

        Ok(deser_pkt)
    }
}

impl TryInto<Vec<u8>> for MessageC2S {
    type Error = Box<dyn Error>;

    fn try_into(self) -> Result<Vec<u8>, Self::Error> {
        let (pkt_id, pkt_bytes) = match self {
            MessageC2S::Hello(p) => (
                message_c2s::message_c2shello::Packet_info::type_.value(),
                p.write_to_bytes()?,
            ),
            MessageC2S::Goodbye(p) => (
                message_c2s::message_c2sgoodbye::Packet_info::type_.value(),
                p.write_to_bytes()?,
            ),
            MessageC2S::Chat(p) => (
                message_c2s::message_c2schat::Packet_info::type_.value(),
                p.write_to_bytes()?,
            ),
            MessageC2S::Ping(p) => (
                message_c2s::message_c2sping::Packet_info::type_.value(),
                p.write_to_bytes()?,
            ),
            MessageC2S::Input(p) => (
                message_c2s::message_c2sping::Packet_info::type_.value(),
                p.write_to_bytes()?,
            ),
            MessageC2S::AuthenticateAndBeamOut(p) => (
                message_c2s::message_c2sauthenticate_and_beam_out::Packet_info::type_.value(),
                p.write_to_bytes()?,
            ),
            MessageC2S::MouseInput(p) => (
                message_c2s::message_c2smouse_input::Packet_info::type_.value(),
                p.write_to_bytes()?,
            ),
            MessageC2S::ModuleGrabBegin(p) => (
                message_c2s::message_c2smodule_grab_begin::Packet_info::type_.value(),
                p.write_to_bytes()?,
            ),
            MessageC2S::ModuleGrabEnd(p) => (
                message_c2s::message_c2smodule_grab_end::Packet_info::type_.value(),
                p.write_to_bytes()?,
            ),
        };

        let pkt = PacketWrapper {
            packet_id: pkt_id as i64,
            packet_data: pkt_bytes,
            special_fields: Default::default(),
        };

        Ok(pkt.write_to_bytes()?)
    }
}

impl TryFrom<&[u8]> for MessageS2C {
    type Error = Box<dyn Error>;

    fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
        let pkt = PacketWrapper::parse_from_bytes(value)?;

        let deser_pkt = match pkt.packet_id {
            _id if _id == message_s2c::message_s2chello::Packet_info::type_.value() as i64 => {
                MessageS2C::Hello(MessageS2CHello::parse_from_bytes(&pkt.packet_data)?)
            }
            _id if _id == message_s2c::message_s2cgoodbye::Packet_info::type_.value() as i64 => {
                MessageS2C::Goodbye(MessageS2CGoodbye::parse_from_bytes(&pkt.packet_data)?)
            }
            _id if _id == message_s2c::message_s2cchat::Packet_info::type_.value() as i64 => {
                MessageS2C::Chat(MessageS2CChat::parse_from_bytes(&pkt.packet_data)?)
            }
            _id if _id == message_s2c::message_s2cpong::Packet_info::type_.value() as i64 => {
                MessageS2C::Pong(MessageS2CPong::parse_from_bytes(&pkt.packet_data)?)
            }
            _id if _id
                == message_s2c::message_s2cplayers_update::Packet_info::type_.value() as i64 =>
            {
                MessageS2C::PlayersUpdate(MessageS2CPlayersUpdate::parse_from_bytes(
                    &pkt.packet_data,
                )?)
            }
            _id if _id
                == message_s2c::message_s2cplanet_data::Packet_info::type_.value() as i64 =>
            {
                MessageS2C::PlanetData(MessageS2CPlanetData::parse_from_bytes(&pkt.packet_data)?)
            }
            _id if _id
                == message_s2c::message_s2cmodules_update::Packet_info::type_.value() as i64 =>
            {
                MessageS2C::ModulesUpdate(MessageS2CModulesUpdate::parse_from_bytes(
                    &pkt.packet_data,
                )?)
            }
            _id if _id
                == message_s2c::message_s2cmodule_tree_update::Packet_info::type_.value() as i64 =>
            {
                MessageS2C::ModuleTreeUpdate(MessageS2CModuleTreeUpdate::parse_from_bytes(
                    &pkt.packet_data,
                )?)
            }
            _id if _id
                == message_s2c::message_s2cmodule_add::Packet_info::type_.value() as i64 =>
            {
                MessageS2C::ModuleAdd(MessageS2CModuleAdd::parse_from_bytes(
                    &pkt.packet_data,
                )?)
            }
            _id if _id
                == message_s2c::message_s2cmodule_remove::Packet_info::type_.value() as i64 =>
            {
                MessageS2C::ModuleRemove(MessageS2CModuleRemove::parse_from_bytes(
                    &pkt.packet_data,
                )?)
            }
            _ => {
                return Err("Not a S2C packet".into());
            }
        };

        Ok(deser_pkt)
    }
}

impl TryInto<Vec<u8>> for MessageS2C {
    type Error = Box<dyn Error>;

    fn try_into(self) -> Result<Vec<u8>, Self::Error> {
        let (pkt_id, pkt_bytes) = match self {
            MessageS2C::Hello(p) => (
                message_s2c::message_s2chello::Packet_info::type_.value(),
                p.write_to_bytes()?,
            ),
            MessageS2C::Goodbye(p) => (
                message_s2c::message_s2cgoodbye::Packet_info::type_.value(),
                p.write_to_bytes()?,
            ),
            MessageS2C::Chat(p) => (
                message_s2c::message_s2cchat::Packet_info::type_.value(),
                p.write_to_bytes()?,
            ),
            MessageS2C::Pong(p) => (
                message_s2c::message_s2cpong::Packet_info::type_.value(),
                p.write_to_bytes()?,
            ),
            MessageS2C::PlayersUpdate(p) => (
                message_s2c::message_s2cplayers_update::Packet_info::type_.value(),
                p.write_to_bytes()?,
            ),
            MessageS2C::PlanetData(p) => (
                message_s2c::message_s2cplanet_data::Packet_info::type_.value(),
                p.write_to_bytes()?,
            ),
            MessageS2C::ModulesUpdate(p) => (
                message_s2c::message_s2cmodules_update::Packet_info::type_.value(),
                p.write_to_bytes()?,
            ),
            MessageS2C::ModuleTreeUpdate(p) => (
                message_s2c::message_s2cmodule_tree_update::Packet_info::type_.value(),
                p.write_to_bytes()?,
            ),
            MessageS2C::ModuleAdd(p) => (
                message_s2c::message_s2cmodule_add::Packet_info::type_.value(),
                p.write_to_bytes()?,
            ),
            MessageS2C::ModuleRemove(p) => (
                message_s2c::message_s2cmodule_remove::Packet_info::type_.value(),
                p.write_to_bytes()?,
            ),
        };

        let pkt = PacketWrapper {
            packet_id: pkt_id as i64,
            packet_data: pkt_bytes,
            special_fields: Default::default(),
        };

        Ok(pkt.write_to_bytes()?)
    }
}

impl planet::PlanetType {
    pub fn as_texture_id(&self) -> String {
        match self {
            PlanetType::Earth => "earth".to_string(),
            PlanetType::Moon => "moon".to_string(),
            PlanetType::UNKNOWN => "missing".to_string(),
        }
    }
}