Skip to content

Commit

Permalink
hehe end
Browse files Browse the repository at this point in the history
  • Loading branch information
nothendev committed Oct 8, 2023
1 parent e15b617 commit b0ad104
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 26 deletions.
17 changes: 7 additions & 10 deletions protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub use indexmap;
pub use miette;
pub use nu_ansi_term as ansi;
pub use tracing;

/// A macro similar to `vec![$elem; $size]` which returns a boxed array.
///
/// ```rust
Expand All @@ -55,10 +56,6 @@ macro_rules! box_array {
}};
}

pub async fn rwlock_set<T: 'static>(rwlock: &RwLock<T>, value: T) {
rwlock.set(value).await
}

pub trait AsyncGet<T: ?Sized + 'static> {
type Ptr<'a, D: ?Sized + 'static>: Deref<Target = D> + 'a;

Expand Down Expand Up @@ -133,10 +130,7 @@ use std::{
fmt::Debug,
net::SocketAddr,
ops::{Deref, DerefMut},
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
sync::Arc,
time::Duration,
};

Expand Down Expand Up @@ -218,14 +212,17 @@ impl PlayerNet {
let mut buf = box_array![0u8; model::MAX_PACKET_DATA];

loop {
let read_bytes = read.read_buf(&mut buf.as_mut().as_mut()).await?;
debug!("three two one explode");
let read_bytes = read.read(&mut buf.as_mut().as_mut()).await?;
debug!(%read_bytes, "didn't explode");
if read_bytes == 0 {
return Ok::<(), crate::error::Error>(());
}
let compres = compressing__.get_copy().await;
let spack = if let Some(cmp) = compression.filter(|_| compres) {
trace!("[recv]compressing");
let sp: SerializedPacket = SerializedPacketCompressed::deserialize
.then_ignore(aott::prelude::end)
.parse(buf.as_ref())?
.into();
if sp.length < cmp {
Expand All @@ -236,7 +233,7 @@ impl PlayerNet {
trace!("[recv]not compressing");
SerializedPacket::deserialize.parse(buf.as_ref())?
};
trace!(buf=?buf[..spack.length], ?spack, "recving packet");
trace!(buf=%format!("{:#x?}", &buf[..read_bytes]), ?spack, "recving packet");
s_recv.send_async(spack).await?;
unsafe { std::ptr::write_bytes(buf.as_mut().as_mut_ptr(), 0u8, buf.len()) };
}
Expand Down
16 changes: 6 additions & 10 deletions protocol/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@ use std::collections::HashMap;
use itertools::Itertools;
use nu_ansi_term::{Color, Style};
use tracing::{field::Field, Level};
use tracing_subscriber::Layer;
use tracing_subscriber::{registry::LookupSpan, Layer};

pub struct CraftLayer;

impl<S: tracing::Subscriber> Layer<S> for CraftLayer {
fn on_event(
&self,
event: &tracing::Event<'_>,
_context: tracing_subscriber::layer::Context<'_, S>,
) {
impl<S: tracing::Subscriber + for<'lo> LookupSpan<'lo>> Layer<S> for CraftLayer {
fn on_event(&self, event: &tracing::Event<'_>, cx: tracing_subscriber::layer::Context<'_, S>) {
// inspiration taken from pnpm
let level = Color::Black
.on(match *event.metadata().level() {
Expand All @@ -24,7 +20,7 @@ impl<S: tracing::Subscriber> Layer<S> for CraftLayer {
Level::TRACE => Color::Purple,
})
.bold()
.paint(format!(" {:<5} ", event.metadata().level().as_str()));
.paint(format!(" {} ", event.metadata().level().as_str()));

let mut visitor = CraftVisitor {
message: None,
Expand All @@ -45,8 +41,8 @@ impl<S: tracing::Subscriber> Layer<S> for CraftLayer {
String::new()
} else {
let padding = " ".repeat(
// max length of level
7
// length of level
event.metadata().level().as_str().len()
// space after level before target
+ 1,
);
Expand Down
10 changes: 4 additions & 6 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,11 @@ use oxcr_protocol::{
},
nbt::{nbt_serde, Nbt, NbtList, NbtTagType},
nsfr::when_the_miette,
rwlock_set,
ser::{Array, Identifier, Json, Namespace, Position},
uuid::Uuid,
AsyncSet, PlayerN, PlayerNet, ProtocolPlugin,
};
use std::{
net::SocketAddr,
sync::{atomic::Ordering, Arc},
};
use std::{net::SocketAddr, sync::Arc};
use tokio::net::TcpListener;
use tokio_util::sync::CancellationToken;
use tracing::instrument;
Expand Down Expand Up @@ -85,9 +81,11 @@ async fn login(net: Arc<PlayerNet>, cx: Arc<TaskContext>, ent_id: Entity) -> Res

if let Some(threshold) = net.compression {
debug!(%threshold, ?net, "compressing");

net.send_packet(SetCompression {
threshold: VarInt::<i32>(threshold.try_into().unwrap()),
})?;

net.compressing.set(true).await;
}

Expand Down Expand Up @@ -206,7 +204,7 @@ async fn login(net: Arc<PlayerNet>, cx: Arc<TaskContext>, ent_id: Entity) -> Res
}

async fn status(net: Arc<PlayerNet>) -> Result<()> {
rwlock_set(&net.state, State::Status).await;
net.state.set(State::Status).await;

let _: StatusRequest = net.recv_packet().await?;
net.send_packet(StatusResponse {
Expand Down

0 comments on commit b0ad104

Please sign in to comment.