Skip to content

Commit

Permalink
Add Chat support
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Aug 8, 2024
1 parent 1076cc6 commit fe8cd33
Show file tree
Hide file tree
Showing 18 changed files with 171 additions and 48 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Pumpkin is currently under heavy development.
- [ ] Player Inventory
- [ ] Player Attack
- Server
- [ ] Chat
- [x] Chat
- [x] Commands

Check out our [Github Project](https://github.com/users/Snowiiii/projects/12/views/1) to see current progress
Expand Down
17 changes: 8 additions & 9 deletions pumpkin-protocol/src/bytebuf/serializer.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::fmt::Display;

use serde::{ser, Serialize};
use serde::{
ser::{self},
Serialize,
};
use thiserror::Error;

use crate::VarInt;

use super::ByteBuffer;

pub struct Serializer {
Expand Down Expand Up @@ -126,18 +127,16 @@ impl<'a> ser::Serializer for &'a mut Serializer {
self.output.put_bool(false);
Ok(())
}
fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
if let Some(len) = len {
self.output.put_var_int(&VarInt(len as i32));
}
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
// here is where all arrays/list getting written, usally we prefix the length of every length with an var int. The problem is
// that byte arrays also getting thrown in here, and we don't want to prefix them
Ok(self)
}
fn serialize_some<T>(self, value: &T) -> Result<Self::Ok, Self::Error>
where
T: ?Sized + Serialize,
{
self.output.put_bool(true);
dbg!("aa");
value.serialize(self)
}
fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
Expand Down Expand Up @@ -288,7 +287,7 @@ impl<'a> ser::SerializeTupleVariant for &'a mut Serializer {
}

fn end(self) -> Result<(), Self::Error> {
todo!()
Ok(())
}
}

Expand Down
27 changes: 9 additions & 18 deletions pumpkin-protocol/src/client/login/c_encryption_request.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,33 @@
use pumpkin_macros::packet;
use serde::Serialize;

use crate::{bytebuf::ByteBuffer, ClientPacket, VarInt};
use crate::{RawBytes, VarInt};

#[derive(Serialize)]
#[packet(0x01)]
pub struct CEncryptionRequest<'a> {
server_id: &'a str, // 20
public_key_length: VarInt,
public_key: &'a [u8],
public_key: RawBytes<'a>,
verify_token_length: VarInt,
verify_token: &'a [u8],
verify_token: RawBytes<'a>,
should_authenticate: bool,
}

impl<'a> CEncryptionRequest<'a> {
pub fn new(
server_id: &'a str,
public_key: &'a [u8],
verify_token: &'a [u8],
public_key: RawBytes<'a>,
verify_token: RawBytes<'a>,
should_authenticate: bool,
) -> Self {
Self {
server_id,
public_key_length: public_key.len().into(),
public_key_length: public_key.0.len().into(),
public_key,
verify_token_length: verify_token.len().into(),
verify_token_length: verify_token.0.len().into(),
verify_token,
should_authenticate,
}
}
}

impl<'a> ClientPacket for CEncryptionRequest<'a> {
fn write(&self, bytebuf: &mut ByteBuffer) {
bytebuf.put_string(self.server_id);
bytebuf.put_var_int(&self.public_key_length);
bytebuf.put_slice(self.public_key);
bytebuf.put_var_int(&self.verify_token_length);
bytebuf.put_slice(self.verify_token);
bytebuf.put_bool(self.should_authenticate);
}
}
37 changes: 37 additions & 0 deletions pumpkin-protocol/src/client/play/c_disguised_chat_message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use pumpkin_macros::packet;

use crate::{text::TextComponent, ClientPacket, VarInt};

#[derive(Clone)]
#[packet(0x1E)]
pub struct CDisguisedChatMessage {
message: TextComponent,
chat_type: VarInt,
sender_name: TextComponent,
target_name: Option<TextComponent>,
}

impl CDisguisedChatMessage {
pub fn new(
message: TextComponent,
chat_type: VarInt,
sender_name: TextComponent,
target_name: Option<TextComponent>,
) -> Self {
Self {
message,
chat_type,
sender_name,
target_name,
}
}
}

impl ClientPacket for CDisguisedChatMessage {
fn write(&self, bytebuf: &mut crate::bytebuf::ByteBuffer) {
bytebuf.put_slice(&self.message.encode());
bytebuf.put_var_int(&self.chat_type);
bytebuf.put_slice(&self.sender_name.encode());
bytebuf.put_option(&self.target_name, |p, v| p.put_slice(&v.encode()));
}
}
2 changes: 2 additions & 0 deletions pumpkin-protocol/src/client/play/c_login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::VarInt;
pub struct CLogin {
entity_id: i32,
is_hardcore: bool,
dimension_count: VarInt,
dimension_names: Vec<String>,
max_players: VarInt,
view_distance: VarInt,
Expand Down Expand Up @@ -53,6 +54,7 @@ impl CLogin {
Self {
entity_id,
is_hardcore,
dimension_count: VarInt(dimension_names.len() as i32),
dimension_names,
max_players,
view_distance,
Expand Down
12 changes: 7 additions & 5 deletions pumpkin-protocol/src/client/play/c_player_chat_message.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use num_derive::FromPrimitive;
use num_derive::{FromPrimitive, ToPrimitive};
use pumpkin_macros::packet;
use serde::Serialize;

use crate::{text::TextComponent, VarInt};

#[derive(Serialize)]
pub struct PlayerChatMessage<'a> {
#[derive(Serialize, Clone)]
#[packet(0x39)]
pub struct CPlayerChatMessage<'a> {
sender: uuid::Uuid,
index: VarInt,
message_signature: Option<&'a [u8]>,
Expand All @@ -20,7 +22,7 @@ pub struct PlayerChatMessage<'a> {
target_name: Option<TextComponent>,
}

impl<'a> PlayerChatMessage<'a> {
impl<'a> CPlayerChatMessage<'a> {
#[allow(clippy::too_many_arguments)]
pub fn new(
sender: uuid::Uuid,
Expand Down Expand Up @@ -59,7 +61,7 @@ pub struct PreviousMessage<'a> {
signature: Option<&'a [u8]>,
}

#[derive(FromPrimitive)]
#[derive(FromPrimitive, ToPrimitive)]
pub enum FilterType {
/// Message is not filtered at all
PassThrough,
Expand Down
2 changes: 1 addition & 1 deletion pumpkin-protocol/src/client/play/c_system_chat_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::Serialize;

use crate::text::TextComponent;

#[derive(Serialize)]
#[derive(Serialize, Clone)]
#[packet(0x6C)]
pub struct CSystemChatMessge {
content: TextComponent,
Expand Down
2 changes: 2 additions & 0 deletions pumpkin-protocol/src/client/play/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod c_change_difficulty;
mod c_chunk_data_update_light;
mod c_disguised_chat_message;
mod c_entity_animation;
mod c_entity_metadata;
mod c_game_event;
Expand All @@ -21,6 +22,7 @@ mod player_action;

pub use c_change_difficulty::*;
pub use c_chunk_data_update_light::*;
pub use c_disguised_chat_message::*;
pub use c_entity_animation::*;
pub use c_entity_metadata::*;
pub use c_game_event::*;
Expand Down
10 changes: 10 additions & 0 deletions pumpkin-protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ impl VarInt {
}
}

pub struct RawBytes<'a>(pub &'a [u8]);
impl<'a> Serialize for RawBytes<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_bytes(self.0)
}
}

impl From<i32> for VarInt {
fn from(value: i32) -> Self {
VarInt(value)
Expand Down
2 changes: 2 additions & 0 deletions pumpkin-protocol/src/server/play/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod s_chat_command;
mod s_chat_message;
mod s_confirm_teleport;
mod s_player_command;
mod s_player_position;
Expand All @@ -7,6 +8,7 @@ mod s_player_rotation;
mod s_swing_arm;

pub use s_chat_command::*;
pub use s_chat_message::*;
pub use s_confirm_teleport::*;
pub use s_player_command::*;
pub use s_player_position::*;
Expand Down
29 changes: 29 additions & 0 deletions pumpkin-protocol/src/server/play/s_chat_message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use pumpkin_macros::packet;

use crate::{
bytebuf::{ByteBuffer, DeserializerError},
ServerPacket,
};

// derive(Deserialize)]
#[packet(0x06)]
pub struct SChatMessage {
pub message: String,
pub timestamp: i64,
pub salt: i64,
pub signature: Option<Vec<u8>>,
// pub messagee_count: VarInt,
// acknowledged: BitSet,
}

impl ServerPacket for SChatMessage {
fn read(bytebuf: &mut ByteBuffer) -> Result<Self, DeserializerError> {
Ok(Self {
message: bytebuf.get_string().unwrap(),
timestamp: bytebuf.get_i64(),
salt: bytebuf.get_i64(),
signature: bytebuf.get_option(|v| v.get_slice().to_vec()),
//messagee_count: bytebuf.get_var_int(),
})
}
}
2 changes: 1 addition & 1 deletion pumpkin-protocol/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl serde::Serialize for TextComponent {
where
S: serde::Serializer,
{
serializer.serialize_bytes(&self.encode())
serializer.serialize_bytes(self.encode().as_slice())
}
}

Expand Down
1 change: 0 additions & 1 deletion pumpkin/src/client/authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ pub fn unpack_textures(property: Property, config: &TextureConfig) {
// TODO: no unwrap
let from64 = general_purpose::STANDARD.decode(property.value).unwrap();
let textures: ProfileTextures = serde_json::from_slice(&from64).unwrap();
dbg!(&textures);
for texture in textures.textures {
is_texture_url_valid(Url::parse(&texture.1.url).unwrap(), config);
}
Expand Down
6 changes: 3 additions & 3 deletions pumpkin/src/client/client_packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use pumpkin_protocol::{
login::{SEncryptionResponse, SLoginAcknowledged, SLoginPluginResponse, SLoginStart},
status::{SPingRequest, SStatusRequest},
},
ConnectionState, KnownPack,
ConnectionState, KnownPack, RawBytes,
};
use pumpkin_registry::Registry;
use rsa::Pkcs1v15Encrypt;
Expand Down Expand Up @@ -64,8 +64,8 @@ impl Client {
let public_key_der = &server.public_key_der;
let packet = CEncryptionRequest::new(
"",
public_key_der,
&verify_token,
RawBytes(public_key_der),
RawBytes(&verify_token),
server.base_config.online_mode, // TODO
);
self.send_packet(packet);
Expand Down
16 changes: 11 additions & 5 deletions pumpkin/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use pumpkin_protocol::{
handshake::SHandShake,
login::{SEncryptionResponse, SLoginAcknowledged, SLoginPluginResponse, SLoginStart},
play::{
SChatCommand, SConfirmTeleport, SPlayerCommand, SPlayerPosition,
SChatCommand, SChatMessage, SConfirmTeleport, SPlayerCommand, SPlayerPosition,
SPlayerPositionRotation, SPlayerRotation, SSwingArm,
},
status::{SPingRequest, SStatusRequest},
Expand Down Expand Up @@ -195,10 +195,13 @@ impl Client {
SLoginStart::PACKET_ID => {
self.handle_login_start(server, SLoginStart::read(bytebuf).unwrap())
}
SEncryptionResponse::PACKET_ID => self.handle_encryption_response(
server,
SEncryptionResponse::read(bytebuf).unwrap(),
).await,
SEncryptionResponse::PACKET_ID => {
self.handle_encryption_response(
server,
SEncryptionResponse::read(bytebuf).unwrap(),
)
.await
}
SLoginPluginResponse::PACKET_ID => self
.handle_plugin_response(server, SLoginPluginResponse::read(bytebuf).unwrap()),
SLoginAcknowledged::PACKET_ID => self
Expand Down Expand Up @@ -261,6 +264,9 @@ impl Client {
SSwingArm::PACKET_ID => {
self.handle_swing_arm(server, SSwingArm::read(bytebuf).unwrap())
}
SChatMessage::PACKET_ID => {
self.handle_chat_message(server, SChatMessage::read(bytebuf).unwrap())
}
_ => log::error!("Failed to handle player packet id {}", packet.id.0),
}
}
Expand Down
Loading

0 comments on commit fe8cd33

Please sign in to comment.