switch out renet netcode with renet_steam
This commit is contained in:
@@ -17,14 +17,12 @@ use bevy_replicon::{
|
||||
};
|
||||
use bevy_replicon_renet::{
|
||||
RenetChannelsExt,
|
||||
netcode::{ClientAuthentication, NetcodeClientTransport, NetcodeError},
|
||||
renet::{ConnectionConfig, RenetClient},
|
||||
steam::SteamClientTransport,
|
||||
};
|
||||
use bevy_steamworks::Client;
|
||||
use bevy_trenchbroom::geometry::Brushes;
|
||||
use std::{
|
||||
net::{Ipv4Addr, UdpSocket},
|
||||
time::SystemTime,
|
||||
};
|
||||
use steamworks::SteamId;
|
||||
|
||||
pub mod audio;
|
||||
pub mod backpack;
|
||||
@@ -55,7 +53,7 @@ pub fn plugin(app: &mut App) {
|
||||
|
||||
app.add_systems(
|
||||
OnEnter(GameState::Connecting),
|
||||
connect_to_server.run_if(|config: Res<NetworkingConfig>| config.server.is_some()),
|
||||
connect_to_server.run_if(|config: Res<NetworkingConfig>| config.connect_to_host()),
|
||||
);
|
||||
app.add_systems(Update, despawn_absent_map_entities);
|
||||
app.add_systems(
|
||||
@@ -91,6 +89,7 @@ fn connect_to_server(
|
||||
mut commands: Commands,
|
||||
config: Res<NetworkingConfig>,
|
||||
channels: Res<RepliconChannels>,
|
||||
steam_client: Res<Client>,
|
||||
) -> Result {
|
||||
let server_channels_config = channels.server_configs();
|
||||
let client_channels_config = channels.client_configs();
|
||||
@@ -101,33 +100,18 @@ 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(client_transport(&config)?);
|
||||
commands.insert_resource(transport);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn client_transport(config: &NetworkingConfig) -> Result<NetcodeClientTransport, NetcodeError> {
|
||||
let current_time = SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.unwrap();
|
||||
let client_id = current_time.as_millis() as u64;
|
||||
let socket = UdpSocket::bind((Ipv4Addr::UNSPECIFIED, 0))?;
|
||||
let server_addr = config
|
||||
.server
|
||||
.flatten()
|
||||
.unwrap_or_else(|| "127.0.0.1:31111".parse().unwrap());
|
||||
let authentication = ClientAuthentication::Unsecure {
|
||||
client_id,
|
||||
protocol_id: 0,
|
||||
server_addr,
|
||||
user_data: None,
|
||||
};
|
||||
|
||||
info!("attempting connection to {server_addr}");
|
||||
NetcodeClientTransport::new(current_time, authentication, socket)
|
||||
}
|
||||
|
||||
#[allow(clippy::type_complexity)]
|
||||
fn migrate_remote_entities(
|
||||
query: Query<(Entity, &TbMapEntityId), (Added<TbMapEntityId>, With<ConfirmHistory>)>,
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use bevy::prelude::*;
|
||||
use clap::Parser;
|
||||
use std::net::SocketAddr;
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
let config = NetworkingConfig::parse();
|
||||
@@ -11,15 +10,17 @@ pub fn plugin(app: &mut App) {
|
||||
#[derive(Resource, Parser, Debug)]
|
||||
#[command(version, about, long_about = None)]
|
||||
pub struct NetworkingConfig {
|
||||
/// The IP/port to connect to.
|
||||
/// If `None`, host a local server.
|
||||
/// If Some(None), connect to the default server (`127.0.0.1:31111`)
|
||||
/// Otherwise, connect to the given server.
|
||||
/// Does nothing on the server.
|
||||
/// Steam id of the host to connect to
|
||||
#[arg(long)]
|
||||
pub server: Option<Option<SocketAddr>>,
|
||||
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.
|
||||
#[arg(long)]
|
||||
pub host: bool,
|
||||
}
|
||||
|
||||
impl NetworkingConfig {
|
||||
pub fn connect_to_host(&self) -> bool {
|
||||
self.steam_id.is_some()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,16 +149,16 @@ pub fn plugin(app: &mut App) {
|
||||
app.add_systems(
|
||||
OnEnter(GameState::Waiting),
|
||||
start_solo_client
|
||||
.run_if(|config: Res<NetworkingConfig>| config.server.is_none() && !config.host),
|
||||
.run_if(|config: Res<NetworkingConfig>| !config.connect_to_host() && !config.host),
|
||||
);
|
||||
app.add_systems(
|
||||
OnEnter(GameState::Waiting),
|
||||
start_listen_server
|
||||
.run_if(|config: Res<NetworkingConfig>| config.server.is_none() && config.host),
|
||||
.run_if(|config: Res<NetworkingConfig>| config.connect_to_host() && config.host),
|
||||
);
|
||||
app.add_systems(
|
||||
OnEnter(GameState::Waiting),
|
||||
start_client.run_if(|config: Res<NetworkingConfig>| config.server.is_some()),
|
||||
start_client.run_if(|config: Res<NetworkingConfig>| config.connect_to_host()),
|
||||
);
|
||||
} else {
|
||||
app.add_systems(OnEnter(GameState::Waiting), start_dedicated_server);
|
||||
|
||||
@@ -16,12 +16,8 @@ use bevy_replicon::{
|
||||
};
|
||||
use bevy_replicon_renet::{
|
||||
RenetChannelsExt,
|
||||
netcode::{NetcodeServerTransport, ServerAuthentication},
|
||||
renet::{ConnectionConfig, RenetServer},
|
||||
};
|
||||
use std::{
|
||||
net::{Ipv4Addr, UdpSocket},
|
||||
time::SystemTime,
|
||||
steam::SteamServerTransport,
|
||||
};
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
@@ -57,6 +53,7 @@ fn open_renet_server(
|
||||
mut commands: Commands,
|
||||
channels: Res<RepliconChannels>,
|
||||
mut next: ResMut<NextState<GameState>>,
|
||||
steam_client: Res<bevy_steamworks::Client>,
|
||||
) -> Result<(), BevyError> {
|
||||
info!("opening server");
|
||||
|
||||
@@ -69,22 +66,18 @@ fn open_renet_server(
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
let current_time = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
|
||||
let port = 31111;
|
||||
let socket = UdpSocket::bind((Ipv4Addr::UNSPECIFIED, port))?;
|
||||
let server_config = bevy_replicon_renet::netcode::ServerConfig {
|
||||
current_time,
|
||||
max_clients: 1,
|
||||
protocol_id: 0,
|
||||
authentication: ServerAuthentication::Unsecure,
|
||||
public_addresses: Default::default(),
|
||||
let steam_config = bevy_replicon_renet::steam::SteamServerConfig {
|
||||
access_permission: bevy_replicon_renet::steam::AccessPermission::FriendsOnly,
|
||||
max_clients: 16,
|
||||
};
|
||||
let transport = NetcodeServerTransport::new(server_config, socket)?;
|
||||
|
||||
let client = (**steam_client).clone();
|
||||
let transport = SteamServerTransport::new(client, steam_config)?;
|
||||
|
||||
commands.insert_resource(server);
|
||||
commands.insert_resource(transport);
|
||||
|
||||
info!("hosting a server on port {port}");
|
||||
info!("hosting a server");
|
||||
|
||||
next.set(GameState::Playing);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user