diff --git a/kubi-server/src/client.rs b/kubi-server/src/client.rs index cbba8fc..0757ebb 100644 --- a/kubi-server/src/client.rs +++ b/kubi-server/src/client.rs @@ -1,4 +1,4 @@ -use shipyard::{Component, EntityId, Unique, Workload, AllStoragesView}; +use shipyard::{Component, EntityId, Unique, AllStoragesView}; use hashbrown::HashMap; use nohash_hasher::BuildNoHashHasher; use std::net::SocketAddr; diff --git a/kubi-server/src/main.rs b/kubi-server/src/main.rs index 3878e8e..a5f7e02 100644 --- a/kubi-server/src/main.rs +++ b/kubi-server/src/main.rs @@ -1,7 +1,6 @@ use shipyard::{World, Workload, IntoWorkload}; use std::{thread, time::Duration}; -mod util; mod config; mod server; mod client; diff --git a/kubi-server/src/util.rs b/kubi-server/src/util.rs deleted file mode 100644 index 2438d9e..0000000 --- a/kubi-server/src/util.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub fn log_error(error: anyhow::Error) { - log::error!("{}", error); -} diff --git a/kubi/src/networking.rs b/kubi/src/networking.rs index 0ef3ee5..286cd59 100644 --- a/kubi/src/networking.rs +++ b/kubi/src/networking.rs @@ -1,24 +1,27 @@ use shipyard::{Unique, AllStoragesView, UniqueView, UniqueViewMut, Workload, IntoWorkload, EntitiesViewMut, Component, ViewMut, SystemModificator, View, IntoIter, WorkloadModificator}; use glium::glutin::event_loop::ControlFlow; use std::net::SocketAddr; -use uflow::{client::{Client, Config as ClientConfig, Event as ClientEvent}, SendMode}; -use lz4_flex::decompress_size_prepended; -use anyhow::{Result, Context}; -use kubi_shared::{ - networking::{ - messages::{ClientToServerMessage, ServerToClientMessage, S_SERVER_HELLO, S_CHUNK_RESPONSE, S_QUEUE_BLOCK}, - state::ClientJoinState, - channels::{CHANNEL_AUTH, CHANNEL_BLOCK}, - }, - queue::QueuedBlock +use uflow::client::{Client, Config as ClientConfig, Event as ClientEvent}; +use kubi_shared::networking::{ + messages::{ClientToServerMessage, ServerToClientMessage, S_SERVER_HELLO}, + state::ClientJoinState, + channels::CHANNEL_AUTH, }; use crate::{ - events::{EventComponent, player_actions::PlayerActionEvent}, + events::EventComponent, control_flow::SetControlFlow, - world::{tasks::{ChunkTaskResponse, ChunkTaskManager}, queue::BlockUpdateQueue}, + world::tasks::ChunkTaskManager, state::is_ingame_or_loading }; +mod world; + +use world::{ + inject_network_responses_into_manager_queue, + send_block_place_events, + recv_block_place_events, +}; + #[derive(Unique, Clone, Copy, PartialEq, Eq)] pub enum GameType { Singleplayer, @@ -124,78 +127,6 @@ fn check_server_hello_response( } } -//TODO multithreaded decompression -fn decompress_chunk_packet(data: &Box<[u8]>) -> Result { - let mut decompressed = decompress_size_prepended(&data[1..])?; - decompressed.insert(0, data[0]); - Ok(postcard::from_bytes(&decompressed).ok().context("Deserialization failed")?) -} - -//TODO get rid of this, this is awfulll -fn inject_network_responses_into_manager_queue( - manager: UniqueView, - events: View -) { - for event in events.iter() { - if event.is_message_of_type::() { - let NetworkEvent(ClientEvent::Receive(data)) = &event else { unreachable!() }; - let packet = decompress_chunk_packet(data).expect("Chunk decode failed"); - let ServerToClientMessage::ChunkResponse { - chunk, data, queued - } = packet else { unreachable!() }; - manager.add_sussy_response(ChunkTaskResponse::LoadedChunk { - position: chunk, - chunk_data: data, - queued - }); - } - } -} - -fn send_block_place_events( - action_events: View, - mut client: UniqueViewMut, -) { - for event in action_events.iter() { - let PlayerActionEvent::UpdatedBlock { position, block } = event else { - continue - }; - client.0.send( - postcard::to_allocvec(&ClientToServerMessage::QueueBlock { - item: QueuedBlock { - position: *position, - block_type: *block, - soft: false - } - }).unwrap().into_boxed_slice(), - CHANNEL_BLOCK, - SendMode::Reliable, - ); - } -} - -fn recv_block_place_events( - mut queue: UniqueViewMut, - network_events: View, -) { - for event in network_events.iter() { - let ClientEvent::Receive(data) = &event.0 else { - continue - }; - if !event.is_message_of_type::() { - continue - } - let Ok(parsed_message) = postcard::from_bytes(data) else { - log::error!("Malformed message"); - continue - }; - let ServerToClientMessage::QueueBlock { item } = parsed_message else { - unreachable!() - }; - queue.push(item); - } -} - pub fn update_networking() -> Workload { ( connect_client.run_if_missing_unique::(), diff --git a/kubi/src/networking/world.rs b/kubi/src/networking/world.rs new file mode 100644 index 0000000..56cab52 --- /dev/null +++ b/kubi/src/networking/world.rs @@ -0,0 +1,91 @@ +use shipyard::{UniqueView, UniqueViewMut, View, IntoIter}; +use uflow::{client::Event as ClientEvent, SendMode}; +use lz4_flex::decompress_size_prepended; +use anyhow::{Result, Context}; +use kubi_shared::{ + networking::{ + messages::{ClientToServerMessage, ServerToClientMessage, S_CHUNK_RESPONSE, S_QUEUE_BLOCK}, + channels::CHANNEL_BLOCK, + }, + queue::QueuedBlock +}; +use crate::{ + events::player_actions::PlayerActionEvent, + world::{ + tasks::{ChunkTaskResponse, ChunkTaskManager}, + queue::BlockUpdateQueue + }, +}; +use super::{NetworkEvent, UdpClient}; + +//TODO multithreaded decompression +fn decompress_chunk_packet(data: &Box<[u8]>) -> Result { + let mut decompressed = decompress_size_prepended(&data[1..])?; + decompressed.insert(0, data[0]); + Ok(postcard::from_bytes(&decompressed).ok().context("Deserialization failed")?) +} + +//TODO get rid of this, this is awfulll +pub fn inject_network_responses_into_manager_queue( + manager: UniqueView, + events: View +) { + for event in events.iter() { + if event.is_message_of_type::() { + let NetworkEvent(ClientEvent::Receive(data)) = &event else { unreachable!() }; + let packet = decompress_chunk_packet(data).expect("Chunk decode failed"); + let ServerToClientMessage::ChunkResponse { + chunk, data, queued + } = packet else { unreachable!() }; + manager.add_sussy_response(ChunkTaskResponse::LoadedChunk { + position: chunk, + chunk_data: data, + queued + }); + } + } +} + +pub fn send_block_place_events( + action_events: View, + mut client: UniqueViewMut, +) { + for event in action_events.iter() { + let PlayerActionEvent::UpdatedBlock { position, block } = event else { + continue + }; + client.0.send( + postcard::to_allocvec(&ClientToServerMessage::QueueBlock { + item: QueuedBlock { + position: *position, + block_type: *block, + soft: false + } + }).unwrap().into_boxed_slice(), + CHANNEL_BLOCK, + SendMode::Reliable, + ); + } +} + +pub fn recv_block_place_events( + mut queue: UniqueViewMut, + network_events: View, +) { + for event in network_events.iter() { + let ClientEvent::Receive(data) = &event.0 else { + continue + }; + if !event.is_message_of_type::() { + continue + } + let Ok(parsed_message) = postcard::from_bytes(data) else { + log::error!("Malformed message"); + continue + }; + let ServerToClientMessage::QueueBlock { item } = parsed_message else { + unreachable!() + }; + queue.push(item); + } +} diff --git a/kubi/src/player.rs b/kubi/src/player.rs index 0ae0a9a..705d0b6 100644 --- a/kubi/src/player.rs +++ b/kubi/src/player.rs @@ -1,4 +1,4 @@ -use shipyard::{Component, AllStoragesViewMut, View, IntoIter}; +use shipyard::{Component, AllStoragesViewMut}; use crate::{ transform::Transform, camera::Camera, @@ -15,7 +15,7 @@ pub fn spawn_player ( mut storages: AllStoragesViewMut ) { log::info!("spawning player"); - let entity_id = storages.add_entity(( + storages.add_entity(( Player, MainPlayer, Transform::default(), diff --git a/kubi/src/prefabs.rs b/kubi/src/prefabs.rs index 118b93c..3598eb2 100644 --- a/kubi/src/prefabs.rs +++ b/kubi/src/prefabs.rs @@ -1,6 +1,6 @@ use shipyard::{NonSendSync, UniqueView, Unique, AllStoragesView}; use glium::{texture::{SrgbTexture2dArray, MipmapsOption}, Program}; -use kubi_shared::block::{Block, BlockTexture}; +use kubi_shared::block::BlockTexture; use crate::rendering::Renderer; mod texture; diff --git a/kubi/src/world/loading.rs b/kubi/src/world/loading.rs index 0441649..6e4a9ce 100644 --- a/kubi/src/world/loading.rs +++ b/kubi/src/world/loading.rs @@ -2,7 +2,6 @@ use glam::{IVec3, ivec3}; use glium::{VertexBuffer, IndexBuffer, index::PrimitiveType}; use kubi_shared::networking::messages::ClientToServerMessage; use shipyard::{View, UniqueView, UniqueViewMut, IntoIter, Workload, IntoWorkload, NonSendSync, track}; -use kubi_shared::queue::QueuedBlock; use uflow::SendMode; use crate::{ player::MainPlayer,