Skip to content

Commit

Permalink
yey
Browse files Browse the repository at this point in the history
  • Loading branch information
nothendev committed Oct 3, 2023
1 parent 8db71c3 commit 64a2ec9
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
7 changes: 6 additions & 1 deletion protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,12 @@ unsafe impl Send for PlayerNet {}
unsafe impl Sync for PlayerNet {}

impl PlayerNet {
pub fn new(mut read: OwnedReadHalf, mut write: OwnedWriteHalf, shit: mpsc::Sender<()>) -> Self {
pub fn new(
mut read: OwnedReadHalf,
mut write: OwnedWriteHalf,
shit: mpsc::Sender<()>,
compression: Option<usize>,
) -> Self {
let peer_addr = read.peer_addr().expect("no peer address");
let local_addr = read.local_addr().expect("no local address");

Expand Down
4 changes: 2 additions & 2 deletions protocol/src/model/packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ pub fn err_with_source(
) -> impl FnOnce(Error) -> Error {
move |e| match e {
Error::Ser(error) => Error::SerSrc(WithSource {
source: BytesSource::new(source(), name),
error,
src: BytesSource::new(source(), name),
errors: vec![error],
}),
e => e,
}
Expand Down
24 changes: 18 additions & 6 deletions protocol/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ pub fn any_of<T: Debug>(things: &[T]) -> String {
}
}

pub fn any_of_display<T: Display>(things: &[T]) -> String {
match things {
[el] => format!("{el}"),
elements => format!(
"any of [{}]",
elements
.iter()
.map(ToString::to_string)
.collect::<Vec<String>>()
.join(", ")
),
}
}

#[derive(thiserror::Error, miette::Diagnostic, Debug)]
pub enum SerializationError<Item: Debug + Display> {
#[error("expected {}, found {found}", any_of(.expected))]
Expand Down Expand Up @@ -114,14 +128,12 @@ pub enum SerializationError<Item: Debug + Display> {
}

#[derive(thiserror::Error, miette::Diagnostic, Debug)]
#[error("{error}")]
#[error("{}", any_of_display(.errors))]
pub struct WithSource<Item: Debug + Display + 'static> {
#[source_code]
pub source: BytesSource,
#[diagnostic(transparent)]
#[source]
#[diagnostic_source]
pub error: SerializationError<Item>,
pub src: BytesSource,
#[related]
pub errors: Vec<SerializationError<Item>>,
}

#[derive(Debug, Clone)]
Expand Down
10 changes: 9 additions & 1 deletion server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,11 @@ fn listen(net: Res<NetNet>, rt: Res<TokioTasksRuntime>) {
let (read, write) = tcp.into_split();
let (shit, mut shit_r) = mpsc::channel(1);

let player = PlayerNet::new(read, write, shit.clone());
let CompressionThreshold(compress) = t
.run_on_main_thread(|tcx| *tcx.world.resource::<CompressionThreshold>())
.await;

let player = PlayerNet::new(read, write, shit.clone(), compress);
let entity = t
.clone()
.run_on_main_thread(move |cx| {
Expand Down Expand Up @@ -372,6 +376,9 @@ fn init_registries(
info!(dimension_types=?dimension_types.0, worldgen_biomes=?worldgen_biomes.0, damage_types=?damage_types.0, "successfully initialized registries.");
}

#[derive(Resource, Debug, Clone, Copy)]
pub struct CompressionThreshold(pub Option<usize>);

#[tokio::main]
async fn main() -> Result<(), Report> {
tracing_subscriber::registry()
Expand Down Expand Up @@ -410,6 +417,7 @@ That was my warning, now I wish you good luck debugging your issue."#
.init_resource::<Registry<DimensionType>>()
.init_resource::<Registry<WorldgenBiome>>()
.init_resource::<Registry<DamageType>>()
.insert_resource(CompressionThreshold(None))
.add_systems(Startup, (init_registries, listen))
.add_systems(Update, on_login)
.run();
Expand Down

0 comments on commit 64a2ec9

Please sign in to comment.