allow to choosing renet_netcode vs steam

This commit is contained in:
2025-12-20 11:33:23 -05:00
parent 930753170f
commit 12c3cdc87b
7 changed files with 318 additions and 61 deletions

View File

@@ -1,6 +1,6 @@
use crate::{
GameState,
config::NetworkingConfig,
config::NetConfig,
protocol::{
ClientEnteredPlaying, TbMapEntityId, TbMapEntityMapping, messages::DespawnTbMapEntity,
},
@@ -18,11 +18,9 @@ use bevy_replicon::{
use bevy_replicon_renet::{
RenetChannelsExt,
renet::{ConnectionConfig, RenetClient},
steam::SteamClientTransport,
};
use bevy_steamworks::Client;
use bevy_trenchbroom::geometry::Brushes;
use steamworks::SteamId;
pub mod audio;
pub mod backpack;
@@ -53,7 +51,7 @@ pub fn plugin(app: &mut App) {
app.add_systems(
OnEnter(GameState::Connecting),
connect_to_server.run_if(|config: Res<NetworkingConfig>| config.connect_to_host()),
connect_to_server.run_if(|config: Res<NetConfig>| config.is_client()),
);
app.add_systems(Update, despawn_absent_map_entities);
app.add_systems(
@@ -87,7 +85,7 @@ fn on_disconnect() {
fn connect_to_server(
mut commands: Commands,
config: Res<NetworkingConfig>,
config: Res<NetConfig>,
channels: Res<RepliconChannels>,
steam_client: Res<Client>,
) -> Result {
@@ -100,14 +98,40 @@ fn connect_to_server(
..Default::default()
});
let steam_id: u64 = config.steam_id.clone().unwrap().parse().unwrap();
let steam_id = SteamId::from_raw(steam_id);
info!("attempting connection to {steam_id:?}");
let transport = SteamClientTransport::new((**steam_client).clone(), &steam_id)?;
commands.insert_resource(client);
commands.insert_resource(transport);
if let NetConfig::SteamClient(host_steam_id) = &*config {
info!("connecting to steam host: {host_steam_id:?}");
let transport = bevy_replicon_renet::steam::SteamClientTransport::new(
(**steam_client).clone(),
host_steam_id,
)?;
commands.insert_resource(transport);
} else if let NetConfig::NetcodeClient(host_addr) = &*config {
use std::time::SystemTime;
info!("connecting to netcode host: {host_addr:?}");
let current_time = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap();
let client_id = current_time.as_millis() as u64;
let socket = std::net::UdpSocket::bind((std::net::Ipv4Addr::UNSPECIFIED, 0))?;
let authentication = bevy_replicon_renet::netcode::ClientAuthentication::Unsecure {
client_id,
protocol_id: 0,
server_addr: host_addr.clone(),
user_data: None,
};
let transport = bevy_replicon_renet::netcode::NetcodeClientTransport::new(
current_time,
authentication,
socket,
)?;
commands.insert_resource(transport);
}
Ok(())
}

View File

@@ -1,26 +1,92 @@
use std::net::SocketAddr;
use bevy::prelude::*;
use clap::Parser;
use steamworks::SteamId;
pub fn plugin(app: &mut App) {
let config = NetworkingConfig::parse();
let config: NetConfig = config.into();
info!("net config: {:?}", config);
app.insert_resource(config);
}
#[derive(Resource, Parser, Debug)]
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct NetworkingConfig {
struct NetworkingConfig {
/// Steam id of the host to connect to
#[arg(long)]
pub steam_id: Option<String>,
/// Whether or not to open a port when opening the client, for other clients
/// to connect. Does nothing if `server` is set.
pub steam_host_id: Option<String>,
/// Act as steam host
#[arg(long)]
pub host: bool,
pub steam_host: bool,
/// Act as host using netcode, so we have to define our port
#[arg(long)]
pub netcode_host: Option<Option<u16>>,
/// Host address we connect to as a client
#[arg(long)]
pub netcode_client: Option<Option<String>>,
}
impl NetworkingConfig {
pub fn connect_to_host(&self) -> bool {
self.steam_id.is_some()
#[derive(Resource, Debug)]
pub enum NetConfig {
Singleplayer,
SteamHost,
NetcodeHost { port: u16 },
SteamClient(SteamId),
NetcodeClient(SocketAddr),
}
impl NetConfig {
pub fn is_client(&self) -> bool {
matches!(
self,
NetConfig::SteamClient(_) | NetConfig::NetcodeClient(_)
)
}
pub fn is_host(&self) -> bool {
matches!(self, NetConfig::SteamHost | NetConfig::NetcodeHost { .. })
}
pub fn is_singleplayer(&self) -> bool {
!self.is_client() && !self.is_host()
}
}
impl From<NetworkingConfig> for NetConfig {
fn from(config: NetworkingConfig) -> Self {
match (
config.steam_host,
config.steam_host_id,
config.netcode_host,
config.netcode_client,
) {
(false, None, None, None) => Self::Singleplayer,
(true, None, None, None) => Self::SteamHost,
(false, Some(id), None, None) => Self::SteamClient(parse_steam_id(id)),
(false, None, Some(port), None) => Self::NetcodeHost {
port: port.unwrap_or(31111),
},
(false, None, None, Some(addr)) => Self::NetcodeClient(parse_addr(addr)),
_ => panic!("Invalid configuration"),
}
}
}
fn parse_addr(addr: Option<String>) -> SocketAddr {
addr.map(|addr| addr.parse().ok())
.flatten()
.unwrap_or_else(|| "127.0.0.1:31111".parse().unwrap())
}
fn parse_steam_id(id: String) -> SteamId {
let id: u64 = id.parse().unwrap();
SteamId::from_raw(id)
}

View File

@@ -34,7 +34,7 @@ pub mod utils;
pub mod water;
use crate::{
config::NetworkingConfig,
config::NetConfig,
heads_database::{HeadDatabaseAsset, HeadsDatabase},
protocol::{PlayerIdCounter, messages::AssignClientPlayer},
tb_entities::SpawnPoint,
@@ -52,7 +52,6 @@ use bevy_trenchbroom::{
TrenchBroomPlugins, config::TrenchBroomConfig, prelude::TrenchBroomPhysicsPlugin,
};
use bevy_trenchbroom_avian::AvianPhysicsBackend;
use steamworks::{CallbackResult, P2PSessionRequest};
use utils::{billboards, squish_animation};
pub const HEDZ_GREEN: Srgba = Srgba::rgb(0.0, 1.0, 0.0);
@@ -150,23 +149,21 @@ pub fn plugin(app: &mut App) {
if cfg!(feature = "client") {
app.add_systems(
OnEnter(GameState::Waiting),
start_solo_client
.run_if(|config: Res<NetworkingConfig>| !config.connect_to_host() && !config.host),
start_solo_client.run_if(|config: Res<NetConfig>| config.is_singleplayer()),
);
app.add_systems(
OnEnter(GameState::Waiting),
start_listen_server
.run_if(|config: Res<NetworkingConfig>| !config.connect_to_host() && config.host),
start_listen_server.run_if(|config: Res<NetConfig>| config.is_host()),
);
app.add_systems(
OnEnter(GameState::Waiting),
start_client.run_if(|config: Res<NetworkingConfig>| config.connect_to_host()),
start_client.run_if(|config: Res<NetConfig>| config.is_client()),
);
} else {
app.add_systems(OnEnter(GameState::Waiting), start_dedicated_server);
}
app.add_systems(Update, accept_p2p_sessions);
app.add_systems(Update, log_steam_events);
}
#[derive(Resource, Reflect, Debug)]
@@ -196,20 +193,14 @@ pub enum GameState {
Playing,
}
fn accept_p2p_sessions(
mut events: MessageReader<SteamworksEvent>,
steam_client: Res<bevy_steamworks::Client>,
) {
fn log_steam_events(events: Option<MessageReader<SteamworksEvent>>) {
let Some(mut events) = events else {
return;
};
for event in events.read() {
if let SteamworksEvent::CallbackResult(CallbackResult::P2PSessionRequest(
P2PSessionRequest { remote },
)) = event
{
info!("Accepting P2P session from: {:?}", remote);
steam_client.networking().accept_p2p_session(*remote);
} else {
info!("steamworks event: {:?}", event);
}
let SteamworksEvent::CallbackResult(result) = event;
info!("steam: {:?}", result);
}
}

View File

@@ -1,5 +1,7 @@
use crate::{
GameState, global_observer,
GameState,
config::NetConfig,
global_observer,
heads_database::HeadsDatabase,
player::ClientPlayerId,
protocol::{ClientEnteredPlaying, PlayerIdCounter, SetGameTick, messages::AssignClientPlayer},
@@ -55,7 +57,8 @@ fn open_renet_server(
mut commands: Commands,
channels: Res<RepliconChannels>,
mut next: ResMut<NextState<GameState>>,
steam_client: Res<bevy_steamworks::Client>,
steam_client: Option<Res<bevy_steamworks::Client>>,
config: Res<NetConfig>,
) -> Result<(), BevyError> {
info!("opening server");
@@ -68,18 +71,46 @@ fn open_renet_server(
..Default::default()
});
let steam_config = bevy_replicon_renet::steam::SteamServerConfig {
access_permission: bevy_replicon_renet::steam::AccessPermission::FriendsOnly,
max_clients: 16,
};
if let NetConfig::SteamHost = *config {
let Some(steam_client) = steam_client else {
return Err("Steam client not found".into());
};
let client = (**steam_client).clone();
let transport = SteamServerTransport::new(client, steam_config)?;
let steam_config = bevy_replicon_renet::steam::SteamServerConfig {
access_permission: bevy_replicon_renet::steam::AccessPermission::FriendsOnly,
max_clients: 16,
};
commands.queue(|w: &mut World| {
w.insert_resource(server);
w.insert_non_send_resource(transport);
});
let client = (**steam_client).clone();
let transport = SteamServerTransport::new(client, steam_config)?;
commands.queue(|w: &mut World| {
w.insert_resource(server);
w.insert_non_send_resource(transport);
});
info!("hosting server: steam");
} else if let NetConfig::NetcodeHost { port } = *config {
use std::time::SystemTime;
let current_time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
let socket = std::net::UdpSocket::bind((std::net::Ipv4Addr::UNSPECIFIED, port))?;
let server_config = bevy_replicon_renet::netcode::ServerConfig {
current_time,
max_clients: 1,
protocol_id: 0,
authentication: bevy_replicon_renet::netcode::ServerAuthentication::Unsecure,
public_addresses: Default::default(),
};
let transport =
bevy_replicon_renet::netcode::NetcodeServerTransport::new(server_config, socket)?;
commands.insert_resource(server);
commands.insert_resource(transport);
info!("hosting server: netcode on port {port}");
}
next.set(GameState::Playing);