1
2
Fork 0
mirror of https://github.com/mat-1/azalea.git synced 2025-08-02 14:26:04 +00:00

serverbound chat packets

This commit is contained in:
mat 2022-10-30 19:59:38 -05:00
parent 68a21a79fb
commit 430d812dd7
5 changed files with 98 additions and 10 deletions

View file

@ -1,6 +1,6 @@
use std::io::{Cursor, Read};
use std::io::{Cursor, Read, Write};
use azalea_buf::{BufReadError, McBuf};
use azalea_buf::{BufReadError, McBuf, McBufReadable, McBufWritable};
/// Represents Java's BitSet, a list of bits.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, McBuf)]
@ -128,6 +128,58 @@ impl From<Vec<u8>> for BitSet {
}
}
/// A list of bits with a known fixed size.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FixedBitSet<const N: usize>
where
[u64; N.div_ceil(64)]: Sized,
{
data: [u64; N.div_ceil(64)],
}
impl<const N: usize> FixedBitSet<N>
where
[u64; N.div_ceil(64)]: Sized,
{
pub fn new() -> Self {
FixedBitSet {
data: [0; N.div_ceil(64)],
}
}
pub fn index(&self, index: usize) -> bool {
(self.data[index / 64] & (1u64 << (index % 64))) != 0
}
pub fn set(&mut self, bit_index: usize) {
self.data[bit_index / 64] |= 1u64 << (bit_index % 64);
}
}
impl<const N: usize> McBufReadable for FixedBitSet<N>
where
[u64; N.div_ceil(64)]: Sized,
{
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let mut data = [0; N.div_ceil(64)];
for i in 0..N.div_ceil(64) {
data[i] = u64::read_from(buf)?;
}
Ok(FixedBitSet { data })
}
}
impl<const N: usize> McBufWritable for FixedBitSet<N>
where
[u64; N.div_ceil(64)]: Sized,
{
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
for i in 0..N.div_ceil(64) {
self.data[i].write_into(buf)?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;

View file

@ -1,6 +1,7 @@
//! Random miscellaneous things like UUIDs that don't deserve their own crate.
#![feature(int_roundings)]
#![feature(generic_const_exprs)]
mod difficulty;
pub use difficulty::*;

View file

@ -59,7 +59,7 @@ pub struct UpdateDisplayNameAction {
impl McBufReadable for ClientboundPlayerInfoUpdatePacket {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let actions = ActionEnumSet::read_from(buf)?;
let entries = vec![];
let mut entries = vec![];
let entry_count = u32::var_read_from(buf)?;
for _ in 0..entry_count {
@ -110,14 +110,14 @@ impl McBufWritable for ClientboundPlayerInfoUpdatePacket {
if self.actions.add_player {
AddPlayerAction {
name: entry.profile.name,
properties: entry.profile.properties,
name: entry.profile.name.clone(),
properties: entry.profile.properties.clone(),
}
.write_into(buf)?;
}
if self.actions.initialize_chat {
InitializeChatAction {
chat_session: entry.chat_session,
chat_session: entry.chat_session.clone(),
}
.write_into(buf)?;
}
@ -141,7 +141,7 @@ impl McBufWritable for ClientboundPlayerInfoUpdatePacket {
}
if self.actions.update_display_name {
UpdateDisplayNameAction {
display_name: entry.display_name,
display_name: entry.display_name.clone(),
}
.write_into(buf)?;
}
@ -163,7 +163,7 @@ pub struct ActionEnumSet {
impl McBufReadable for ActionEnumSet {
fn read_from(buf: &mut Cursor<&[u8]>) -> Result<Self, BufReadError> {
let mut set = BitSet::read_fixed(buf, 6)?;
let set = BitSet::read_fixed(buf, 6)?;
Ok(ActionEnumSet {
add_player: set.index(0),
initialize_chat: set.index(1),

View file

@ -1,5 +1,25 @@
use super::serverbound_chat_packet::LastSeenMessagesUpdate;
use azalea_buf::McBuf;
use azalea_core::FixedBitSet;
use azalea_crypto::MessageSignature;
use azalea_protocol_macros::ServerboundGamePacket;
#[derive(Clone, Debug, McBuf, ServerboundGamePacket)]
pub struct ServerboundChatCommandPacket {}
pub struct ServerboundChatCommandPacket {
pub command: String,
pub timestamp: u64,
pub salt: u64,
pub argument_signatures: ArgumentSignatures,
pub last_seen_messages: LastSeenMessagesUpdate,
}
#[derive(Clone, Debug, McBuf)]
pub struct ArgumentSignatures {
pub entries: Vec<ArgumentSignature>,
}
#[derive(Clone, Debug, McBuf)]
pub struct ArgumentSignature {
pub name: String,
pub signature: MessageSignature,
}

View file

@ -1,5 +1,20 @@
use azalea_buf::McBuf;
use azalea_core::FixedBitSet;
use azalea_crypto::MessageSignature;
use azalea_protocol_macros::ServerboundGamePacket;
#[derive(Clone, Debug, McBuf, ServerboundGamePacket)]
pub struct ServerboundChatPacket {}
pub struct ServerboundChatPacket {
pub message: String,
pub timestamp: u64,
pub salt: u64,
pub signature: Option<MessageSignature>,
pub last_seen_messages: LastSeenMessagesUpdate,
}
#[derive(Clone, Debug, McBuf)]
pub struct LastSeenMessagesUpdate {
#[var]
pub offset: u32,
pub acknowledged: FixedBitSet<20>,
}