Skip to content

Commit

Permalink
update gone wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
nothendev committed Oct 1, 2023
1 parent a318e37 commit 4cc1624
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 173 deletions.
48 changes: 19 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion README.org
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ I chose Bevy for the ECS, not because it's better, but because Apecs is dog$hit.
I chose AOTT for serialization, becuase serde is too much for me and implementing a serializer AND deserializer is just an awful process and I'm lazy.

** Sources

- [[https://wiki.vg/index.php?title=Protocol&oldid=18375][Where I get protocol documentation]] (1.20.1)
** Hacking / building & running from source
First - clone the repo (~gh repo clone Implodent/oxcraft~ or if you don't have Github's CLI (the ~gh~), ~git clone https://github.com/Implodent/oxcraft.git~) or ~git pull~ (to update the tree)
Expand All @@ -22,5 +21,7 @@ Small detail - this server implements the Minecraft protocol version 763 (1.20.1
** Configuration
*** Runtime flags
You could set the log level as an environment variable ~OXCR_LOG~. The format is the format for the ~tracing-subscriber~'s [[https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#directives][EnvFilter]].
If you are a developer and want to inspect the contents of the packets, I recommend you set the variable to ~debug,oxcr_protocol=trace~.
This sets everything except ~oxcr_protocol~ (the protocol library which ~trace!~'s the packet sending and receiving) to ~DEBUG~, while setting ~oxcr_protocol~ to ~TRACE~.
*** Build-time flags (~#[cfg(feature = "...")]~'es)
Right now there are no features. Soon there will be.
41 changes: 20 additions & 21 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
use oxcr_protocol::{
aott::{self, prelude::Parser},
bytes::Bytes,
error::Error,
logging::CraftLayer,
miette::{bail, IntoDiagnostic, Report},
model::{
packets::{play::LoginPlay, Packet, SerializedPacket},
packets::{
err_with_source, play::LoginPlay, Packet, SerializedPacket, SerializedPacketCompressed,
},
VarInt,
},
nbt::Nbt,
ser::{BytesSource, Deserialize, WithSource},
ser::Deserialize,
tracing::debug,
};
use tracing_subscriber::{
Expand Down Expand Up @@ -47,8 +48,13 @@ fn run(_path: String, args: &str) -> Result<(), Report> {
CliCommand::Help => help(),
CliCommand::Decode(inp) => {
let bytes = read_byte_input(inp)?;
debug!("{bytes:?}");
let spack = SerializedPacket::deserialize.parse(&bytes)?;

// let spack = SerializedPacket::deserialize.parse(&bytes)?;
let spack = SerializedPacket {
length: bytes.len() + LoginPlay::ID.length_of(),
id: LoginPlay::ID,
data: bytes,
};
let deserialized: LoginPlay = spack.try_deserialize(LoginPlay::STATE)?;

println!("{:#?}", deserialized);
Expand All @@ -61,36 +67,29 @@ fn run(_path: String, args: &str) -> Result<(), Report> {
VarInt::<i64>::deserialize
.then_ignore(aott::prelude::end)
.parse(&bytes)
.map_err(reconcile(bytes))?
.map_err(err_with_source(|| bytes, Some("varint.bin".to_string())))?
);
}
CliCommand::Nbt(tag, inp) => {
let bytes = read_byte_input(inp)?;
let nbt = Nbt::single
.parse_with_context(&bytes, tag)
.map_err(reconcile(bytes))?;
.map_err(err_with_source(|| bytes, Some("nbt.bin".to_string())))?;

println!("{nbt:#?}");
}
CliCommand::Decompress(_inp) => todo!(),
CliCommand::Decompress(inp) => {
let bytes = read_byte_input(inp)?;
println!(
"{:#x?}",
SerializedPacketCompressed::deserialize.parse(&bytes)?
);
}
};

Ok(())
}

fn reconcile(
bytes: Bytes,
) -> impl FnOnce(oxcr_protocol::error::Error) -> oxcr_protocol::error::Error {
move |error| match error {
Error::Ser(ser) => Error::SerSrc(WithSource {
kind: ser.kind,
span: ser.span,
source: BytesSource::new(bytes, Some("thing.bin".into())),
}),
a => a,
}
}

fn read_byte_input(inp: ByteInput) -> Result<Bytes, Report> {
try {
match inp {
Expand Down
16 changes: 9 additions & 7 deletions protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,10 @@ impl PlayerNet {
pub fn new(mut read: OwnedReadHalf, mut write: OwnedWriteHalf, shit: mpsc::Sender<()>) -> Self {
let peer_addr = read.peer_addr().expect("no peer address");
let local_addr = read.local_addr().expect("no local address");

let (s_recv, recv) = flume::unbounded();
let (send, r_send) = flume::unbounded();

let send_task = tokio::spawn(async move {
let Err::<!, _>(e) = async {
loop {
Expand Down Expand Up @@ -215,32 +217,32 @@ impl PlayerNet {
) -> Result<T> {
let tnov = std::any::type_name::<T>();
if self.recv.is_disconnected() {
debug!(packet=tnov, addr=%self.peer_addr, "receiving packet failed - disconnected");
trace!(packet=tnov, addr=%self.peer_addr, "receiving packet failed - disconnected");
return Err(crate::error::Error::ConnectionEnded);
}
let packet = self.recv.recv_async().await?;
let state = *self.state.read().await;
debug!(%self.peer_addr, ?packet, ?state, "Received packet");
trace!(%self.peer_addr, ?packet, ?state, "Received packet");
let result = packet.try_deserialize(state);
debug!(?result, %self.peer_addr, "Deserialized packet");
trace!(?result, %self.peer_addr, "Deserialized packet");
result
}

/// Writes a packet.
pub fn send_packet<T: Packet + Serialize + Debug>(&self, packet: T) -> Result<()> {
if self.send.is_disconnected() {
debug!(?packet, addr=%self.peer_addr, "sending packet failed - disconnected");
trace!(?packet, addr=%self.peer_addr, "sending packet failed - disconnected");
return Err(crate::error::Error::ConnectionEnded);
}
let spack = SerializedPacket::new_ref(&packet)?;
debug!(?packet, addr=%self.peer_addr, ?spack, "Sending packet");
trace!(?packet, addr=%self.peer_addr, ?spack, "Sending packet");
Ok(self.send.send(spack)?)
}

/// Sends a plugin message.
/// Equivalent to `self.send_packet(PluginMessage { channel, data: data.serialize() })`
pub fn plugin_message<T: Serialize + Debug>(&self, channel: Identifier, data: T) -> Result<()> {
debug!(?channel, ?data, %self.peer_addr, "Sending plugin message");
trace!(?channel, ?data, %self.peer_addr, "Sending plugin message");
self.send_packet(PluginMessage {
channel,
data: data.serialize()?,
Expand All @@ -254,7 +256,7 @@ impl PlayerNet {
) -> Result<Result<T, (State, SerializedPacket)>> {
let packet = self.recv.recv_async().await?;
let state = *self.state.read().await;
debug!(%self.peer_addr, ?packet, ?state, "trying to receive packet");
trace!(%self.peer_addr, ?packet, ?state, "trying to receive packet");

match packet.try_deserialize(state) {
Ok(deserialized) => {
Expand Down
26 changes: 2 additions & 24 deletions protocol/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ impl<S: tracing::Subscriber> Layer<S> for CraftLayer {
Level::TRACE => Color::Purple,
})
.bold()
.paint(format!(" {} ", event.metadata().level().as_str()));

let pad_level = " ".repeat(5 - event.metadata().level().as_str().len());
.paint(format!(" {:<5} ", event.metadata().level().as_str()));

let mut visitor = CraftVisitor {
message: None,
Expand Down Expand Up @@ -74,7 +72,7 @@ impl<S: tracing::Subscriber> Layer<S> for CraftLayer {
.intersperse_with(|| Color::LightRed.paint("::").to_string())
.collect::<String>();

println!("{level}{pad_level} {target}{message} {other_fields}");
println!("{level} {target}{message} {other_fields}");
}
}

Expand All @@ -94,26 +92,6 @@ impl CraftVisitor {
}

impl tracing::field::Visit for CraftVisitor {
fn record_f64(&mut self, field: &tracing::field::Field, value: f64) {
self.record(field, value.to_string())
}

fn record_i64(&mut self, field: &tracing::field::Field, value: i64) {
self.record(field, value.to_string())
}

fn record_u64(&mut self, field: &tracing::field::Field, value: u64) {
self.record(field, value.to_string())
}

fn record_bool(&mut self, field: &tracing::field::Field, value: bool) {
self.record(field, value.to_string())
}

fn record_str(&mut self, field: &tracing::field::Field, value: &str) {
self.record(field, value.to_string())
}

fn record_error(
&mut self,
field: &tracing::field::Field,
Expand Down
2 changes: 1 addition & 1 deletion protocol/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl DimensionType {
logical_height: 384,
min_y: -64,
monster_spawn_block_light_limit: 0,
monster_spawn_light_level: MonsterSpawnLightLevel::Range(0..=7),
monster_spawn_light_level: MonsterSpawnLightLevel::Level(0),
natural: true,
piglin_safe: false,
respawn_anchor_works: false,
Expand Down
Loading

0 comments on commit 4cc1624

Please sign in to comment.