mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-12-25 05:08:21 -06:00
separate networking, fix warnings
This commit is contained in:
parent
6375b397e9
commit
4361eff76f
|
@ -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;
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use shipyard::{World, Workload, IntoWorkload};
|
||||
use std::{thread, time::Duration};
|
||||
|
||||
mod util;
|
||||
mod config;
|
||||
mod server;
|
||||
mod client;
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
pub fn log_error(error: anyhow::Error) {
|
||||
log::error!("{}", error);
|
||||
}
|
|
@ -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},
|
||||
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, CHANNEL_BLOCK},
|
||||
},
|
||||
queue::QueuedBlock
|
||||
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<ServerToClientMessage> {
|
||||
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<ChunkTaskManager>,
|
||||
events: View<NetworkEvent>
|
||||
) {
|
||||
for event in events.iter() {
|
||||
if event.is_message_of_type::<S_CHUNK_RESPONSE>() {
|
||||
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<PlayerActionEvent>,
|
||||
mut client: UniqueViewMut<UdpClient>,
|
||||
) {
|
||||
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<BlockUpdateQueue>,
|
||||
network_events: View<NetworkEvent>,
|
||||
) {
|
||||
for event in network_events.iter() {
|
||||
let ClientEvent::Receive(data) = &event.0 else {
|
||||
continue
|
||||
};
|
||||
if !event.is_message_of_type::<S_QUEUE_BLOCK>() {
|
||||
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::<UdpClient>(),
|
||||
|
|
91
kubi/src/networking/world.rs
Normal file
91
kubi/src/networking/world.rs
Normal file
|
@ -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<ServerToClientMessage> {
|
||||
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<ChunkTaskManager>,
|
||||
events: View<NetworkEvent>
|
||||
) {
|
||||
for event in events.iter() {
|
||||
if event.is_message_of_type::<S_CHUNK_RESPONSE>() {
|
||||
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<PlayerActionEvent>,
|
||||
mut client: UniqueViewMut<UdpClient>,
|
||||
) {
|
||||
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<BlockUpdateQueue>,
|
||||
network_events: View<NetworkEvent>,
|
||||
) {
|
||||
for event in network_events.iter() {
|
||||
let ClientEvent::Receive(data) = &event.0 else {
|
||||
continue
|
||||
};
|
||||
if !event.is_message_of_type::<S_QUEUE_BLOCK>() {
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -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(),
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue