aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol/src/mc_buf
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2022-05-15 01:46:11 +0000
committerGitHub <noreply@github.com>2022-05-15 01:46:11 +0000
commitd0ac62d85276bc48e4f8e0e60afdc35840681622 (patch)
treeff4996b89d6f34c7c452d1b2950e53d512bce3c1 /azalea-protocol/src/mc_buf
parentef3cbe27f2a7eed5c635924d6fa0401dd04eae77 (diff)
parentc16e958d0be671a17edf060aee9850faccbcfe14 (diff)
downloadazalea-drasl-d0ac62d85276bc48e4f8e0e60afdc35840681622.tar.xz
Merge pull request #6 from mat-1/chunk-decoding
Chunk decoding
Diffstat (limited to 'azalea-protocol/src/mc_buf')
-rw-r--r--azalea-protocol/src/mc_buf/definitions.rs33
-rw-r--r--[-rwxr-xr-x]azalea-protocol/src/mc_buf/mod.rs6
-rw-r--r--[-rwxr-xr-x]azalea-protocol/src/mc_buf/read.rs20
-rw-r--r--[-rwxr-xr-x]azalea-protocol/src/mc_buf/write.rs21
4 files changed, 55 insertions, 25 deletions
diff --git a/azalea-protocol/src/mc_buf/definitions.rs b/azalea-protocol/src/mc_buf/definitions.rs
index a59fc574..3867b3fc 100644
--- a/azalea-protocol/src/mc_buf/definitions.rs
+++ b/azalea-protocol/src/mc_buf/definitions.rs
@@ -1,8 +1,9 @@
use crate::mc_buf::read::{McBufReadable, Readable};
use crate::mc_buf::write::{McBufWritable, Writable};
+use crate::mc_buf::McBufVarReadable;
use azalea_chat::component::Component;
use azalea_core::{BlockPos, Direction, Slot};
-use packet_macros::{McBufReadable, McBufWritable};
+use packet_macros::McBuf;
use std::io::{Read, Write};
use std::ops::Deref;
use uuid::Uuid;
@@ -32,7 +33,7 @@ impl From<&str> for UnsizedByteArray {
}
/// Represents Java's BitSet, a list of bits.
-#[derive(Debug, Clone, PartialEq, Eq, Hash, McBufReadable, McBufWritable)]
+#[derive(Debug, Clone, PartialEq, Eq, Hash, McBuf)]
pub struct BitSet {
data: Vec<u64>,
}
@@ -159,7 +160,7 @@ impl McBufWritable for EntityDataValue {
}
}
-#[derive(Clone, Debug, Copy, McBufReadable, McBufWritable)]
+#[derive(Clone, Debug, Copy, McBuf)]
pub enum Pose {
Standing = 0,
FallFlying = 1,
@@ -171,7 +172,7 @@ pub enum Pose {
Dying = 7,
}
-#[derive(Debug, Clone, McBufReadable, McBufWritable)]
+#[derive(Debug, Clone, McBuf)]
pub struct VillagerData {
#[var]
type_: u32,
@@ -181,7 +182,7 @@ pub struct VillagerData {
level: u32,
}
-#[derive(Debug, Clone, McBufReadable, McBufWritable)]
+#[derive(Debug, Clone, McBuf)]
pub struct Particle {
#[var]
pub id: i32,
@@ -280,12 +281,12 @@ pub enum ParticleData {
Scrape,
}
-#[derive(Debug, Clone, McBufReadable, McBufWritable)]
+#[derive(Debug, Clone, McBuf)]
pub struct BlockParticle {
#[var]
pub block_state: i32,
}
-#[derive(Debug, Clone, McBufReadable, McBufWritable)]
+#[derive(Debug, Clone, McBuf)]
pub struct DustParticle {
/// Red value, 0-1
pub red: f32,
@@ -297,7 +298,7 @@ pub struct DustParticle {
pub scale: f32,
}
-#[derive(Debug, Clone, McBufReadable, McBufWritable)]
+#[derive(Debug, Clone, McBuf)]
pub struct DustColorTransitionParticle {
/// Red value, 0-1
pub from_red: f32,
@@ -315,12 +316,12 @@ pub struct DustColorTransitionParticle {
pub to_blue: f32,
}
-#[derive(Debug, Clone, McBufReadable, McBufWritable)]
+#[derive(Debug, Clone, McBuf)]
pub struct ItemParticle {
pub item: Slot,
}
-#[derive(Debug, Clone, McBufReadable, McBufWritable)]
+#[derive(Debug, Clone, McBuf)]
pub struct VibrationParticle {
pub origin: BlockPos,
pub position_type: String,
@@ -331,9 +332,8 @@ pub struct VibrationParticle {
pub ticks: u32,
}
-impl McBufReadable for ParticleData {
- fn read_into(buf: &mut impl Read) -> Result<Self, String> {
- let id = buf.read_varint()?;
+impl ParticleData {
+ pub fn read_from_particle_id(buf: &mut impl Read, id: u32) -> Result<Self, String> {
Ok(match id {
0 => ParticleData::AmbientEntityEffect,
1 => ParticleData::AngryVillager,
@@ -428,6 +428,13 @@ impl McBufReadable for ParticleData {
}
}
+impl McBufReadable for ParticleData {
+ fn read_into(buf: &mut impl Read) -> Result<Self, String> {
+ let id = u32::var_read_into(buf)?;
+ ParticleData::read_from_particle_id(buf, id)
+ }
+}
+
impl McBufWritable for ParticleData {
fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
todo!()
diff --git a/azalea-protocol/src/mc_buf/mod.rs b/azalea-protocol/src/mc_buf/mod.rs
index bee269c9..548ba7c2 100755..100644
--- a/azalea-protocol/src/mc_buf/mod.rs
+++ b/azalea-protocol/src/mc_buf/mod.rs
@@ -4,16 +4,16 @@ mod definitions;
mod read;
mod write;
-pub use definitions::{BitSet, EntityMetadata, UnsizedByteArray};
-use packet_macros::{McBufReadable, McBufWritable};
+pub use definitions::{BitSet, EntityMetadata, ParticleData, UnsizedByteArray};
pub use read::{read_varint_async, McBufReadable, McBufVarReadable, Readable};
-use std::ops::Deref;
pub use write::{McBufVarWritable, McBufWritable, Writable};
// const DEFAULT_NBT_QUOTA: u32 = 2097152;
const MAX_STRING_LENGTH: u16 = 32767;
// const MAX_COMPONENT_STRING_LENGTH: u32 = 262144;
+// TODO: maybe get rid of the readable/writable traits so there's not two ways to do the same thing and improve McBufReadable/McBufWritable
+
// TODO: have a definitions.rs in mc_buf that contains UnsizedByteArray and BitSet
#[cfg(test)]
diff --git a/azalea-protocol/src/mc_buf/read.rs b/azalea-protocol/src/mc_buf/read.rs
index 98a3ee53..1c4fbd6f 100755..100644
--- a/azalea-protocol/src/mc_buf/read.rs
+++ b/azalea-protocol/src/mc_buf/read.rs
@@ -1,10 +1,10 @@
-use super::{BitSet, UnsizedByteArray, MAX_STRING_LENGTH};
+use super::{UnsizedByteArray, MAX_STRING_LENGTH};
use azalea_chat::component::Component;
use azalea_core::{
difficulty::Difficulty, game_type::GameType, resource_location::ResourceLocation,
- serializable_uuid::SerializableUuid, BlockPos, Direction, Slot, SlotData,
+ serializable_uuid::SerializableUuid, BlockPos, ChunkSectionPos, Direction, Slot, SlotData,
};
-use byteorder::{ReadBytesExt, WriteBytesExt, BE};
+use byteorder::{ReadBytesExt, BE};
use serde::Deserialize;
use std::{collections::HashMap, hash::Hash, io::Read};
use tokio::io::{AsyncRead, AsyncReadExt};
@@ -466,7 +466,7 @@ impl McBufReadable for Component {
fn read_into(buf: &mut impl Read) -> Result<Self, String> {
let string = buf.read_utf()?;
let json: serde_json::Value = serde_json::from_str(string.as_str())
- .map_err(|e| "Component isn't valid JSON".to_string())?;
+ .map_err(|_| "Component isn't valid JSON".to_string())?;
let component = Component::deserialize(json).map_err(|e| e.to_string())?;
Ok(component)
}
@@ -518,3 +518,15 @@ impl McBufReadable for Direction {
}
}
}
+
+// ChunkSectionPos
+impl McBufReadable for ChunkSectionPos {
+ fn read_into(buf: &mut impl Read) -> Result<Self, String> {
+ let long = i64::read_into(buf)?;
+ Ok(ChunkSectionPos {
+ x: (long >> 42) as i32,
+ y: (long << 44 >> 44) as i32,
+ z: (long << 22 >> 42) as i32,
+ })
+ }
+}
diff --git a/azalea-protocol/src/mc_buf/write.rs b/azalea-protocol/src/mc_buf/write.rs
index e3e7a2be..c46297a6 100755..100644
--- a/azalea-protocol/src/mc_buf/write.rs
+++ b/azalea-protocol/src/mc_buf/write.rs
@@ -2,7 +2,7 @@ use super::{UnsizedByteArray, MAX_STRING_LENGTH};
use azalea_chat::component::Component;
use azalea_core::{
difficulty::Difficulty, game_type::GameType, resource_location::ResourceLocation,
- serializable_uuid::SerializableUuid, BlockPos, Direction, Slot,
+ serializable_uuid::SerializableUuid, BlockPos, ChunkSectionPos, Direction, Slot,
};
use byteorder::{BigEndian, WriteBytesExt};
use std::{collections::HashMap, io::Write};
@@ -20,8 +20,8 @@ pub trait Writable: Write {
Ok(())
}
- fn write_int_id_list(&mut self, list: &Vec<i32>) -> Result<(), std::io::Error> {
- self.write_list(&list, |buf, n| buf.write_varint(*n))
+ fn write_int_id_list(&mut self, list: &[i32]) -> Result<(), std::io::Error> {
+ self.write_list(list, |buf, n| buf.write_varint(*n))
}
fn write_map<KF, VF, KT, VT>(
@@ -47,7 +47,7 @@ pub trait Writable: Write {
}
fn write_bytes(&mut self, bytes: &[u8]) -> Result<(), std::io::Error> {
- self.write_all(bytes);
+ self.write_all(bytes)?;
Ok(())
}
@@ -377,7 +377,7 @@ impl McBufWritable for Component {
// let component = Component::deserialize(json).map_err(|e| e.to_string())?;
// Ok(component)
// }
- fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
+ fn write_into(&self, _buf: &mut impl Write) -> Result<(), std::io::Error> {
// component doesn't have serialize implemented yet
todo!()
}
@@ -425,3 +425,14 @@ impl McBufWritable for Direction {
buf.write_varint(*self as i32)
}
}
+
+// ChunkSectionPos
+impl McBufWritable for ChunkSectionPos {
+ fn write_into(&self, buf: &mut impl Write) -> Result<(), std::io::Error> {
+ let long = (((self.x & 0x3FFFFF) as i64) << 42)
+ | (self.y & 0xFFFFF) as i64
+ | (((self.z & 0x3FFFFF) as i64) << 20);
+ long.write_into(buf)?;
+ Ok(())
+ }
+}