diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..ca4be8b --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,6 @@ +[target.x86_64-unknown-linux-gnu] +linker = "clang" +rustflags = ["-C", "link-arg=-fuse-ld=lld"] + +[target.x86_64-pc-windows-msvc] +linker = "rust-lld.exe" diff --git a/Cargo.lock b/Cargo.lock index 31bcd38..4cbe485 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -197,6 +197,55 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.6.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23a1e53f0f5d86382dafe1cf314783b2044280f406e7e1506368220ad11b1338" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8365de52b16c035ff4fcafe0092ba9390540e3e352870ac09933bebcaa2c8c56" + +[[package]] +name = "anstyle-parse" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b2d16507662817a6a20a9ea92df6652ee4f94f914589377d69f3b21bc5798a9" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79947af37f4177cfead1110013d678905c37501914fba0efea834c3fe9a8d60c" +dependencies = [ + "windows-sys 0.59.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2109dbce0e72be3ec00bed26e6a7479ca384ad226efdd66db8fa2e3a38c83125" +dependencies = [ + "anstyle", + "windows-sys 0.59.0", +] + [[package]] name = "approx" version = "0.5.1" @@ -1460,6 +1509,46 @@ dependencies = [ "libloading 0.8.5", ] +[[package]] +name = "clap" +version = "4.5.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b97f376d85a664d5837dbae44bf546e6477a679ff6610010f17276f686d867e8" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19bc80abd44e4bed93ca373a0704ccbd1b710dc5749406201bb018272808dc54" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.85", +] + +[[package]] +name = "clap_lex" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" + [[package]] name = "codespan-reporting" version = "0.11.1" @@ -1470,6 +1559,12 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "colorchoice" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" + [[package]] name = "com" version = "0.6.0" @@ -1669,6 +1764,8 @@ name = "crooks" version = "0.1.0" dependencies = [ "bevy", + "clap", + "lazy_static", "lightyear", "serde", ] @@ -2356,6 +2453,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hermit-abi" version = "0.4.0" @@ -2477,6 +2580,12 @@ dependencies = [ "mach2", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + [[package]] name = "itertools" version = "0.13.0" @@ -3814,6 +3923,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "subtle" version = "2.6.1" @@ -4102,6 +4217,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "uuid" version = "1.11.0" diff --git a/Cargo.toml b/Cargo.toml index aabff94..5eb54c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,5 +5,10 @@ edition = "2021" [dependencies] bevy = "0.14.2" +clap = { version = "4.5.20", features = ["derive"] } +lazy_static = "1.5.0" lightyear = "0.17.1" serde = { version = "1.0.213", features = ["derive"] } + +[profile.dev.package."*"] +opt-level = 3 diff --git a/src/main.rs b/src/main.rs index f48d399..a2a66ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,46 @@ +use bevy::log::LogPlugin; use bevy::prelude::*; -use protocol::ProtocolPlugin; +use bevy::state::app::StatesPlugin; +use clap::Parser; +use lightyear::prelude::client::ClientPlugins; +use lightyear::prelude::server::ServerPlugins; pub mod protocol; pub mod shared_net; -struct ClientServerPlugin; -impl Plugin for ClientServerPlugin { - fn build(&self, app: &mut App) {} +lazy_static::lazy_static! { + pub static ref CONFIG: Cli = { + Cli::parse() + }; } +pub struct ClientOrServerPlugin; +impl Plugin for ClientOrServerPlugin { + fn build(&self, app: &mut App) { + if CONFIG.client { + app.add_plugins(DefaultPlugins) + .add_plugins(ClientPlugins::new(shared_net::client_config())); + } + if CONFIG.server { + app.add_plugins((MinimalPlugins, StatesPlugin, LogPlugin::default())) + .add_plugins(ServerPlugins::new(shared_net::server_config())); + } + } +} + +#[derive(Parser)] +#[command(version, about, long_about = None)] +pub struct Cli { + /// Start as a client. + #[arg(short, long)] + client: bool, + /// Start as a server. + #[arg(short, long)] + server: bool, +} fn main() { - bevy::app::new() - .add_plugins(DefaultPlugins) - .add_plugins(ClientServerPlugin) - .add_plugins(ProtocolPlugin) + App::new() + .add_plugins(ClientOrServerPlugin) + .add_plugins(protocol::ProtocolPlugin) .run(); } diff --git a/src/shared_net.rs b/src/shared_net.rs index ec207d5..c9d3960 100644 --- a/src/shared_net.rs +++ b/src/shared_net.rs @@ -1,33 +1,12 @@ use client::{Authentication, ClientConfig}; use lightyear::prelude::*; -use server::ServerConfig; +use lightyear::server::config::ServerConfig; use std::time::Duration; -pub const PRIVIATE_KEY: Key = [ - 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, - 12, 12, 12, 12, 12, 12, 12, 12, -]; +pub const PRIVIATE_KEY: Key = [12; 32]; pub const FIXED_TIMESTEP_HZ: f64 = 64.0; pub const REPLICATION_INTERVAL: Duration = Duration::from_millis(100); -pub fn server_config() -> ServerConfig { - let netcode_config = server::NetcodeConfig::default(); - let io_config = server::IoConfig { - transport: server::ServerTransport::UdpSocket("127.0.0.1:7000".parse().unwrap()), - conditioner: None, - compression: CompressionConfig::default(), - }; - let net = server::NetConfig::Netcode { - config: netcode_config, - io: io_config, - }; - ServerConfig { - shared: shared_config(Mode::Separate), - net: vec![net], - ..Default::default() - } -} - pub fn client_config() -> ClientConfig { let auth = Authentication::Manual { server_addr: "127.0.0.1:7000".parse().unwrap(), @@ -53,6 +32,28 @@ pub fn client_config() -> ClientConfig { } } +pub fn server_config() -> ServerConfig { + let netcode_config = server::NetcodeConfig::default(); + let io_config = server::IoConfig { + transport: server::ServerTransport::UdpSocket("127.0.0.1:7000".parse().unwrap()), + conditioner: None, + compression: CompressionConfig::default(), + }; + let net = server::NetConfig::Netcode { + config: netcode_config, + io: io_config, + }; + ServerConfig { + shared: shared_config(Mode::Separate), + net: vec![net], + replication: ReplicationConfig { + send_interval: REPLICATION_INTERVAL, + ..Default::default() + }, + ..Default::default() + } +} + /// The [`SharedConfig`] must be shared between the `ClientConfig` and `ServerConfig` pub fn shared_config(mode: Mode) -> SharedConfig { SharedConfig {