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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
//! Server-received world messages.
pub mod mail;

use std::io::{Read, Write};
use std::io::Result as Res;

use endio::{Deserialize, LERead, LEWrite, Serialize};
use endio::LittleEndian as LE;
use endio_bit::{BEBitReader, BEBitWriter};
use lu_packets_derive::VariantTests;

use crate::common::{ObjId, LuVarWString, LuWString33, LuWString42, ServiceId};
use crate::chat::ChatChannel;
use crate::chat::server::ChatMessage;
use crate::raknet::client::replica::controllable_physics::FrameStats;
use super::ZoneId;
use super::gm::server::SubjectGameMessage;
use self::mail::Mail;

pub use crate::general::server::GeneralMessage;

/// All messages that can be received by a world server.
pub type Message = crate::raknet::server::Message<LuMessage>;

/// All LU messages that can be received by a world server.
#[derive(Debug, Deserialize, PartialEq, Serialize, VariantTests)]
#[repr(u16)]
pub enum LuMessage {
	General(GeneralMessage) = ServiceId::General as u16,
	World(WorldMessage) = ServiceId::World as u16,
}

/// All server-received world messages.
#[derive(Debug, Deserialize, PartialEq, Serialize, VariantTests)]
#[post_disc_padding = 1]
#[repr(u32)]
pub enum WorldMessage {
	ClientValidation(ClientValidation) = 1,
	CharacterListRequest = 2,
	CharacterCreateRequest(CharacterCreateRequest) = 3,
	CharacterLoginRequest(CharacterLoginRequest) = 4,
	SubjectGameMessage(SubjectGameMessage) = 5,
	CharacterDeleteRequest(CharacterDeleteRequest) = 6,
	GeneralChatMessage(GeneralChatMessage) = 14,
	LevelLoadComplete(LevelLoadComplete) = 19,
	RouteMessage(RouteMessage) = 21,
	PositionUpdate(PositionUpdate) = 22,
	Mail(Mail) = 23,
	StringCheck(StringCheck) = 25,
	RequestFreeTrialRefresh = 32,
	Top5IssuesRequest(Top5IssuesRequest) = 91,
	UgcDownloadFailed(UgcDownloadFailed) = 120,
}

/**
	Provides session info for authentication.

	### Trigger
	Receipt of [Server handshake](crate::general::client::Handshake).

	### Handling
	Verify with your auth server that the `(username, session_key)` combination is valid. If not, immediately disconnect the client, ideally with a [`DisconnectNotify::InvalidSessionKey`](crate::general::client::DisconnectNotify::InvalidSessionKey).

	If you are concerned about players modding their client DB, also check the [`fdb_checksum`](Self::fdb_checksum). Note that players can still change their client to send a fake checksum, but this requires exe modding, which most players are presumably not familiar with.

	If all validation checks pass, store the connection -> username association, as this is the only packet that references the username.

	### Response
	The client does not require a fixed response to this packet. However, world servers (with the exception of a dedicated char server) will usually want to respond to this with [`LoadStaticZone`](super::client::LoadStaticZone).

	### Notes
	**Important**: Do **not** handle any other packets from clients that have not yet been validated. Handling other packets before validation can lead to errors because the connection has not yet been associated with a username, and can lead to security vulnerabilities if session keys are not validated properly.
*/
#[derive(Debug, PartialEq)]
pub struct ClientValidation {
	/// Account username.
	pub username: LuWString33,
	/// [Session key from auth's login response](crate::auth::client::LoginResponse::Ok::session_key).
	pub session_key: LuWString33,
	/// MD5 hash of null-terminated cdclient.fdb file contents.
	pub fdb_checksum: [u8; 32],
}

impl<R: Read + LERead> Deserialize<LE, R> for ClientValidation
where
	u8: Deserialize<LE, R>,
	LuWString33: Deserialize<LE, R>,
{
	#[rustfmt::skip]
	fn deserialize(reader: &mut R) -> Res<Self> {
		let username         = LERead::read(reader)?;
		let session_key      = LERead::read(reader)?;
		let mut fdb_checksum = [0; 32];
		std::io::Read::read(reader, &mut fdb_checksum)?;
		// garbage byte because the devs messed up the null terminator
		let _ : u8           =  LERead::read(reader)?;
		Ok(Self {
			username,
			session_key,
			fdb_checksum,
		})
	}
}

impl<'a, W: Write + LEWrite> Serialize<LE, W> for &'a ClientValidation
where
	u8: Serialize<LE, W>,
	&'a LuWString33: Serialize<LE, W>,
{
	fn serialize(self, writer: &mut W) -> Res<()> {
		LEWrite::write(writer, &self.username)?;
		LEWrite::write(writer, &self.session_key)?;
		Write::write(writer, &self.fdb_checksum)?;
		// garbage byte because the devs messed up the null terminator
		LEWrite::write(writer, 0u8)
	}
}

/**
	Requests a new character to be created.

	### Trigger
	The player creating a new character and submitting it to the server.

	### Handling
	Check if the predefined name is available. If the custom name is set, check if it is available as well. If your server has a name policy, check if the custom name is known to be unacceptable.

	If all checks pass, create the character and save it to the database using the information specified in this message.

	### Response
	Respond with [`CharacterCreateResponse`](super::client::CharacterCreateResponse), using the appropriate variant to indicate the result. If the character creation is successful, additionally send a [`CharacterListResponse`](super::client::CharacterListResponse) afterwards with the new character included.
*/
#[derive(Debug, Deserialize, PartialEq, Serialize)]
#[trailing_padding = 1]
pub struct CharacterCreateRequest {
	/// The custom name, or blank if the predefined name is to be used.
	pub char_name: LuWString33,
	/// First part of the predefined name.
	pub predef_name_id_1: u32, // todo: enum
	/// Second part of the predefined name.
	pub predef_name_id_2: u32, // todo: enum
	/// Third part of the predefined name.
	pub predef_name_id_3: u32, // todo: enum
	#[padding = 9]
	/// Chosen torso color.
	pub torso_color: u32, // todo: enum
	#[padding = 4]
	/// Chosen legs color.
	pub legs_color: u32, // todo: enum
	/// Chosen hair style.
	pub hair_style: u32, // todo: enum
	/// Chosen hair color.
	pub hair_color: u32, // todo: enum
	#[padding = 8]
	/// Chosen eyebrow style.
	pub eyebrows_style: u32, // todo: enum
	/// Chosen eye style.
	pub eyes_style: u32, // todo: enum
	/// Chosen mouth style.
	pub mouth_style: u32, // todo: enum
}

/**
	Indicates that the player has chosen a character to play with.

	### Trigger
	The player selecting the character in the character selection screen and pressing play.

	### Handling
	Do what's necessary to let the character join the world it was last in. In the common case of the world server instance being different from the current instance, redirect the client to the world instance.

	### Response
	Respond with [`LoadStaticZone`](super::client::LoadStaticZone) if you're not switching instances, or [`TransferToWorld`](super::client::TransferToWorld) if you do.
*/
#[derive(Debug, Deserialize, PartialEq, Serialize)]
pub struct CharacterLoginRequest {
	/// The object ID of the chosen character.
	pub char_id: ObjId,
}

/**
	Requests a character to be deleted.

	### Trigger
	The player deleting the character and confirming with their password.

	### Handling
	Delete the character from the database, with appropriate cascading deletes, such as deleting the characters from any friends lists they're in.

	### Response
	Respond with [`CharacterDeleteResponse`](super::client::CharacterDeleteResponse) indicating whether deletion was successful.
*/
#[derive(Debug, Deserialize, PartialEq, Serialize)]
pub struct CharacterDeleteRequest {
	/// The object ID of the chosen character.
	pub char_id: ObjId,
}

#[derive(Debug, PartialEq)]
pub struct GeneralChatMessage {
	pub chat_channel: ChatChannel,
	pub source_id: u16,
	pub message: LuVarWString<u32>,
}

impl<R: Read + LERead> Deserialize<LE, R> for GeneralChatMessage
where
	u8: Deserialize<LE, R>,
	LuWString33: Deserialize<LE, R>,
{
	fn deserialize(reader: &mut R) -> Res<Self> {
		let chat_channel = LERead::read(reader)?;
		let source_id = LERead::read(reader)?;
		let mut str_len: u32 = LERead::read(reader)?;
		str_len -= 1;
		let message = LuVarWString::deser_content(reader, str_len)?;
		let _: u16 = LERead::read(reader)?;
		Ok(Self { chat_channel, source_id, message })
	}
}

impl<'a, W: Write + LEWrite> Serialize<LE, W> for &'a GeneralChatMessage {
	fn serialize(self, writer: &mut W) -> Res<()> {
		LEWrite::write(writer, &self.chat_channel)?;
		LEWrite::write(writer, self.source_id)?;
		let mut str_len = self.message.len();
		str_len += 1;
		LEWrite::write(writer, str_len as u32)?;
		self.message.ser_content(writer)?;
		LEWrite::write(writer, 0u16)
	}
}

/**
	Reports to the server that client-side loading has finished.

	### Trigger
	The client finishing a zone load initiated by [`LoadStaticZone`](super::client::LoadStaticZone).

	### Handling / Response
	Respond with [`CreateCharacter`](super::client::CreateCharacter) containing details about the player's character. Add the client to your server's replica manager, so that existing objects in range are replicated using [`ReplicaConstruction`](crate::raknet::client::replica::ReplicaConstruction). Create the character's replica object and and let the replica manager broadcast its construction to all clients in range. Finally, send [`ServerDoneLoadingAllObjects`](crate::world::gm::client::GameMessage::ServerDoneLoadingAllObjects) from the character object to the client.
*/
#[derive(Debug, Deserialize, PartialEq, Serialize)]
pub struct LevelLoadComplete {
	/// The ID of the zone that was loaded. Servers should not trust this, as a player could use it to get into zones they don't belong.
	pub zone_id: ZoneId,
}

#[derive(Debug, Deserialize, PartialEq, Serialize)]
#[pre_disc_padding = 4]
#[repr(u16)]
pub enum RouteMessage {
	Chat(ChatMessage) = ServiceId::Chat as u16,
}

#[derive(Debug, PartialEq)]
pub struct PositionUpdate {
	pub frame_stats: FrameStats,
}

impl<R: Read> Deserialize<LE, R> for PositionUpdate {
	fn deserialize(reader: &mut R) -> Res<Self> {
		let mut reader = BEBitReader::new(reader);
		let frame_stats = LERead::read(&mut reader)?;
		Ok(Self { frame_stats })
	}
}

impl<'a, W: Write> Serialize<LE, W> for &'a PositionUpdate {
	fn serialize(self, writer: &mut W) -> Res<()> {
		let mut writer = BEBitWriter::new(writer);
		LEWrite::write(&mut writer, &self.frame_stats)
	}
}

/**
	Asks the server whether a string the player entered is acceptable.

	### Trigger
	The player entering a string in the chat box. This message is sent as the player is typing, before pressing enter.

	### Handling
	Check whether the [`string`](Self::string) is acceptable per the server's moderation policy, taking into account the player's chat mode and best friend status with possible recipient.

	### Response
	Respond with [`ChatModerationString`](super::client::ChatModerationString), indicating whether the string is ok, or if not, the spans that are not acceptable.

	### Notes
	This message is only for quick player feedback on acceptability. Final string submissions by the player will be sent in different messages (e.g. [`GeneralChatMessage`] or `Mail` (todo)). Those messages will need to be checked for moderation as well. This means that there's no harm in trusting the client to provide accurate context ([`chat_mode`](Self::chat_mode), [`recipient_name`](Self::recipient_name) in this message.
*/
#[derive(Debug, Deserialize, PartialEq, Serialize)]
pub struct StringCheck {
	pub chat_mode: u8, // todo: type?
	pub request_id: u8,
	pub recipient_name: LuWString42,
	/// The string to be checked.
	pub string: LuVarWString<u16>,
}

#[derive(Debug, Deserialize, PartialEq, Serialize)]
#[repr(u32)]
#[allow(non_camel_case_types)]
pub enum Language {
	en_US,
	pl_US,
	de_DE,
	en_GB,
}

#[derive(Debug, Deserialize, PartialEq, Serialize)]
pub struct Top5IssuesRequest {
	pub language: Language,
}

#[derive(Debug, Deserialize, PartialEq, Serialize)]
#[repr(u32)]
pub enum UgcResType {
	Lxfml,
	Nif,
	Hkx,
	Dds,
}

#[derive(Debug, Deserialize, PartialEq, Serialize)]
pub struct UgcDownloadFailed {
	pub res_type: UgcResType,
	pub blueprint_id: ObjId,
	pub status_code: u32,
	pub char_id: ObjId,
}