From e1178cb2314040f3a4cf8ebce74d0c62876d50a4 Mon Sep 17 00:00:00 2001 From: nothendev Date: Wed, 18 Oct 2023 15:30:32 +0300 Subject: [PATCH] fix: refactor parsing code for new aott --- Cargo.lock | 14 +++++++ Cargo.toml | 5 ++- README.org | 9 ++-- cli/src/cli.rs | 7 ++-- cli/src/error.rs | 67 ++++++++++++++++++++++++++---- protocol/src/model.rs | 2 +- protocol/src/model/packets/play.rs | 2 +- protocol/src/ser.rs | 2 +- proxy/Cargo.toml | 14 +++++++ proxy/src/main.rs | 18 ++++++++ 10 files changed, 119 insertions(+), 21 deletions(-) create mode 100644 proxy/Cargo.toml create mode 100644 proxy/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index d16bd6d..bc12172 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1182,6 +1182,20 @@ dependencies = [ "uuid", ] +[[package]] +name = "oxcr_proxy" +version = "0.1.0" +dependencies = [ + "bevy", + "futures-util", + "oxcr_cli", + "oxcr_protocol", + "tokio", + "tokio-util", + "tracing", + "tracing-subscriber", +] + [[package]] name = "oxcr_server" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 5d8ebfa..5bd7f60 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,8 @@ members = [ "protocol", "server", - "cli" + "cli", + "proxy" ] default-members = [ "protocol", "server" ] resolver = "2" @@ -19,5 +20,5 @@ bevy = { version = "0.11.2", features = ["multi-threaded"], default-features = f rayon = "1" itertools = "0" flate2 = "1" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing-subscriber = { version = "0.3", features = ["env-filter"], default-features = false } tokio-util = { version = "0", features = [ "full" ] } diff --git a/README.org b/README.org index 30bccdc..335f50c 100644 --- a/README.org +++ b/README.org @@ -1,11 +1,8 @@ * OxCraft - an alternative Minecraft server implementation -** Structure -*** ECS -I chose Bevy for the ECS, not because it's better, but because Apecs is dog$hit. - -*** Serialization -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. +** FAQ +*Why?* For fun. +*When release ?!?!* Can't say for certain, but it's very far away. ** Sources - [[https://wiki.vg/index.php?title=Protocol&oldid=18375][Where I get protocol documentation]] (1.20.1) diff --git a/cli/src/cli.rs b/cli/src/cli.rs index cec4b97..782974e 100644 --- a/cli/src/cli.rs +++ b/cli/src/cli.rs @@ -101,12 +101,13 @@ pub fn flag_list(input: &str) -> Vec> { } just("-") .ignore_then(choice(( - slice(filter(|thing: &char| *thing != ' ')) - .map(FlagName::Short) - .repeated(), just("-") .ignore_then(ident) .map(|name| vec![FlagName::Long(name)]), + slice(filter(|ch| *ch != ' ', Expectation::ShortFlag)) + .map(FlagName::Short) + .repeated() + .collect::>(), ))) .parse_with(input) } diff --git a/cli/src/error.rs b/cli/src/error.rs index 4a996c0..f1b43d2 100644 --- a/cli/src/error.rs +++ b/cli/src/error.rs @@ -5,7 +5,7 @@ use oxcr_protocol::{ self, error::{BuiltinLabel, LabelError}, primitive::SeqLabel, - text::{self, CharError, CharLabel}, + text::CharLabel, }, miette::{self, SourceSpan}, ser::any_of, @@ -25,17 +25,17 @@ pub enum ParseError { #[help] help: Option>, }, + #[error("unexpected end of input{}", .expected.as_ref().map(|expectation| format!(", expected {expectation}")).unwrap_or_else(String::new))] - #[diagnostic( - code(aott::error::unexpected_eof), - help("try giving it more input next time, I guess?") - )] + #[diagnostic(code(aott::error::unexpected_eof))] UnexpectedEof { #[label = "here"] at: SourceSpan, #[label = "last data here"] last_data_at: Option, expected: Option, + #[help] + help: Option>, }, #[error("parsing a Level failed: {actual}")] #[diagnostic(code(cli::parse_level_error))] @@ -80,7 +80,7 @@ pub enum ParseError { last_token: Option, }, - #[error("{label}; last token was {last_token:?}")] + #[error("expected {label:?}; last token was {last_token:?}")] Sequence { #[label = "here"] at: SourceSpan, @@ -89,7 +89,7 @@ pub enum ParseError { }, } -#[derive(Debug, thiserror::Error)] +#[derive(Debug, thiserror::Error, Clone)] pub enum Expectation { #[error("{}", any_of(.0))] AnyOf(Vec), @@ -99,6 +99,8 @@ pub enum Expectation { EndOfInput, #[error("a digit with radix {_0}")] Digit(u32), + #[error("a short flag character (anything but a whitespace)")] + ShortFlag, } impl<'a> aott::error::FundamentalError<&'a str> for ParseError { @@ -110,6 +112,7 @@ impl<'a> aott::error::FundamentalError<&'a str> for ParseError { at: span.into(), last_data_at: None, expected: expected.map(Expectation::AnyOf), + help: None, } } @@ -173,3 +176,53 @@ impl<'a> LabelError<&'a str, SeqLabel> for ParseError { } } } + +impl<'a> LabelError<&'a str, Expectation> for ParseError { + fn from_label( + span: Range, + label: Expectation, + last_token: Option<<&'a str as aott::prelude::InputType>::Token>, + ) -> Self { + if let Some(found) = last_token { + Self::Expected { + at: span.into(), + expected: label, + found, + help: None, + } + } else { + let last_data_at = (span.start.saturating_sub(1), span.end.saturating_sub(1)).into(); + Self::UnexpectedEof { + at: span.into(), + last_data_at: Some(last_data_at), + expected: Some(label), + help: None, + } + } + } +} + +impl<'a> LabelError<&'a str, (Expectation, Cow<'static, str>)> for ParseError { + fn from_label( + span: Range, + (label, help): (Expectation, Cow<'static, str>), + last_token: Option<<&'a str as aott::prelude::InputType>::Token>, + ) -> Self { + if let Some(found) = last_token { + Self::Expected { + at: span.into(), + expected: label, + found, + help: Some(help), + } + } else { + let last_data_at = (span.start.saturating_sub(1), span.end.saturating_sub(1)).into(); + Self::UnexpectedEof { + at: span.into(), + last_data_at: Some(last_data_at), + expected: Some(label), + help: Some(help), + } + } + } +} diff --git a/protocol/src/model.rs b/protocol/src/model.rs index e0cbe2f..1564ca1 100644 --- a/protocol/src/model.rs +++ b/protocol/src/model.rs @@ -3,7 +3,7 @@ pub mod packets; pub mod registry; mod varint; use self::registry::RegistryItem; -use aott::primitive::{filter, one_of}; +use aott::primitive::filter; use bytes::BufMut; use std::{ops::RangeInclusive, ptr}; pub use varint::*; diff --git a/protocol/src/model/packets/play.rs b/protocol/src/model/packets/play.rs index a861a20..6270c30 100644 --- a/protocol/src/model/packets/play.rs +++ b/protocol/src/model/packets/play.rs @@ -6,7 +6,7 @@ use crate::{ }; use std::ptr; -use aott::primitive::{filter, one_of}; +use aott::primitive::filter; use bytes::BufMut; use indexmap::IndexMap; diff --git a/protocol/src/ser.rs b/protocol/src/ser.rs index 7fb2e4f..24a3a50 100644 --- a/protocol/src/ser.rs +++ b/protocol/src/ser.rs @@ -21,7 +21,7 @@ use crate::model::VarInt; use ::bytes::{BufMut, Bytes, BytesMut}; pub use aott::prelude::parser; pub use aott::prelude::Parser; -use aott::{error::FundamentalError, iter::IterParser, pfn_type, prelude::*}; +use aott::{error::FundamentalError, iter::IterParser, prelude::*}; use tracing::debug; mod error; diff --git a/proxy/Cargo.toml b/proxy/Cargo.toml new file mode 100644 index 0000000..8cee251 --- /dev/null +++ b/proxy/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "oxcr_proxy" +version = "0.1.0" +edition = "2021" + +[dependencies] +oxcr_protocol.workspace = true +oxcr_cli.workspace = true +tokio.workspace = true +tokio-util.workspace = true +bevy.workspace = true +tracing.workspace = true +tracing-subscriber.workspace = true +futures-util = "0.3.28" diff --git a/proxy/src/main.rs b/proxy/src/main.rs new file mode 100644 index 0000000..f73448e --- /dev/null +++ b/proxy/src/main.rs @@ -0,0 +1,18 @@ +use oxcr_protocol::logging::CraftLayer; +use oxcr_protocol::miette::Report; +use tracing::info; +use tracing_subscriber::{ + prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt, EnvFilter, +}; + +#[tokio::main] +async fn main() -> Result<(), Report> { + tracing_subscriber::registry() + .with(EnvFilter::from_env("OXCR_LOG")) + .with(CraftLayer) + .init(); + + info!("henlo worled me is proxe"); + + Ok(()) +}