This commit is contained in:
griffi-gh 2023-02-12 00:37:24 +01:00
parent 209dc15f81
commit f0f17bdf74
9 changed files with 81 additions and 32 deletions

View file

@ -1,4 +1,3 @@
[server] [server]
address = "0.0.0.0" address = "0.0.0.0:1234"
port = 1234
max_clients = 254 max_clients = 254

View file

@ -11,3 +11,8 @@ log = "*"
shipyard = "0.6" shipyard = "0.6"
serde = "1.0" serde = "1.0"
toml = "0.7" toml = "0.7"
glam = { version = "0.22", features = ["debug-glam-assert", "fast-math"] }
[features]
default = []
unstable = ["glam/core-simd"]

View file

@ -0,0 +1,4 @@
use shipyard::Component;
#[derive(Component)]
pub struct Client;

23
kubi-server/src/config.rs Normal file
View file

@ -0,0 +1,23 @@
use shipyard::{AllStoragesView, Unique};
use serde::{Serialize, Deserialize};
use std::{fs, net::SocketAddr};
#[derive(Serialize, Deserialize)]
pub struct ConfigTableServer {
pub address: SocketAddr,
pub max_clients: usize,
}
#[derive(Unique, Serialize, Deserialize)]
pub struct ConfigTable {
pub server: ConfigTableServer
}
pub fn read_config(
storages: AllStoragesView,
) {
log::info!("Reading config...");
let config_str = fs::read_to_string("Server.toml").expect("No config file found");
let config: ConfigTable = toml::from_str(&config_str).expect("Invalid configuration file");
storages.add_unique(config);
}

View file

@ -1,38 +1,17 @@
use shipyard::{World, AllStoragesView, Unique, Workload, IntoWorkload, UniqueView, UniqueViewMut}; use shipyard::{World, Workload, IntoWorkload};
use kubi_udp::server::{Server, ServerConfig}; use std::{thread, time::Duration};
use kubi_shared::networking::messages::{ClientToServerMessage, ServerToClientMessage};
use std::{thread, time::Duration, net::SocketAddr};
#[derive(Unique)] pub(crate) mod config;
#[repr(transparent)] pub(crate) mod server;
pub struct ServerAddr(SocketAddr); pub(crate) mod client;
pub(crate) mod transform;
#[derive(Unique)] use config::read_config;
#[repr(transparent)] use server::{bind_server, update_server};
pub struct UdpServer(Server<ServerToClientMessage, ClientToServerMessage>);
fn bind_server(
storages: AllStoragesView,
) {
log::info!("Creating server...");
let addr = storages.borrow::<UniqueView<SocketAddr>>().expect("No server addr found");
let server: Server<ServerToClientMessage, ClientToServerMessage> = Server::bind(
"0.0.0.0:1234".parse().unwrap(),
ServerConfig::default()
).unwrap();
storages.add_unique(UdpServer(server));
}
fn update_server(
mut server: UniqueViewMut<UdpServer>
) {
if let Err(error) = server.0.update() {
log::error!("Server error: {error:?}")
}
}
fn initialize() -> Workload { fn initialize() -> Workload {
( (
read_config,
bind_server, bind_server,
).into_workload() ).into_workload()
} }

28
kubi-server/src/server.rs Normal file
View file

@ -0,0 +1,28 @@
use shipyard::{AllStoragesView, Unique, UniqueView, UniqueViewMut};
use kubi_udp::server::{Server, ServerConfig};
use kubi_shared::networking::messages::{ClientToServerMessage, ServerToClientMessage};
use crate::config::ConfigTable;
#[derive(Unique)]
#[repr(transparent)]
pub struct UdpServer(Server<ServerToClientMessage, ClientToServerMessage>);
pub fn bind_server(
storages: AllStoragesView,
) {
log::info!("Creating server...");
let config = storages.borrow::<UniqueView<ConfigTable>>().unwrap();
let server: Server<ServerToClientMessage, ClientToServerMessage> = Server::bind(
config.server.address,
ServerConfig { max_clients: config.server.max_clients }
).unwrap();
storages.add_unique(UdpServer(server));
}
pub fn update_server(
mut server: UniqueViewMut<UdpServer>
) {
if let Err(error) = server.0.update() {
log::error!("Server error: {error:?}")
}
}

View file

@ -0,0 +1,6 @@
use shipyard::Component;
use glam::Mat4;
#[derive(Component, Clone, Copy, Debug, Default)]
#[track(All)]
pub struct Transform(pub Mat4);

View file

@ -22,3 +22,7 @@ gilrs = { version = "0.10", default_features = false, features = ["xinput"] }
[target.'cfg(target_os = "windows")'.dependencies] [target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "0.3" } winapi = { version = "0.3" }
[features]
default = []
unstable = ["glam/core-simd"]

View file

@ -35,6 +35,7 @@ use crate::{
use super::{RenderTarget, primitives::cube::CubePrimitive}; use super::{RenderTarget, primitives::cube::CubePrimitive};
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
#[repr(C)]
pub struct ChunkVertex { pub struct ChunkVertex {
pub position: [f32; 3], pub position: [f32; 3],
pub normal: [f32; 3], pub normal: [f32; 3],