add renet_steam support (#92)

This commit is contained in:
extrawurst
2025-12-20 19:19:13 +01:00
committed by GitHub
parent 7ea9046414
commit 7d280af821
9 changed files with 214 additions and 73 deletions

View File

@@ -1,6 +1,6 @@
use crate::{
GameState,
config::NetworkingConfig,
config::NetConfig,
protocol::{
ClientEnteredPlaying, TbMapEntityId, TbMapEntityMapping, messages::DespawnTbMapEntity,
},
@@ -17,14 +17,10 @@ use bevy_replicon::{
};
use bevy_replicon_renet::{
RenetChannelsExt,
netcode::{ClientAuthentication, NetcodeClientTransport, NetcodeError},
renet::{ConnectionConfig, RenetClient},
};
use bevy_steamworks::Client;
use bevy_trenchbroom::geometry::Brushes;
use std::{
net::{Ipv4Addr, UdpSocket},
time::SystemTime,
};
pub mod audio;
pub mod backpack;
@@ -55,7 +51,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<NetConfig>| config.is_client()),
);
app.add_systems(Update, despawn_absent_map_entities);
app.add_systems(
@@ -89,8 +85,9 @@ fn on_disconnect() {
fn connect_to_server(
mut commands: Commands,
config: Res<NetworkingConfig>,
config: Res<NetConfig>,
channels: Res<RepliconChannels>,
steam_client: Option<Res<Client>>,
) -> Result {
let server_channels_config = channels.server_configs();
let client_channels_config = channels.client_configs();
@@ -102,32 +99,47 @@ fn connect_to_server(
});
commands.insert_resource(client);
commands.insert_resource(client_transport(&config)?);
if let NetConfig::SteamClient(host_steam_id) = &*config {
let Some(steam_client) = steam_client else {
return Err("Steam client not found".into());
};
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(())
}
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>)>,

View File

@@ -4,10 +4,11 @@ use bevy_pkv::prelude::*;
use crate::{client::audio::SoundSettings, utils::Debounce};
pub fn plugin(app: &mut App) {
#[cfg(not(feature = "dbg"))]
app.insert_resource(PkvStore::new("Rustunit", "HEDZ"));
app.add_systems(Update, persist_settings);
app.add_systems(Startup, load_settings);
app.add_systems(Update, persist_settings.run_if(resource_exists::<PkvStore>));
app.add_systems(Startup, load_settings.run_if(resource_exists::<PkvStore>));
}
fn persist_settings(

View File

@@ -61,6 +61,14 @@ fn test_steam_system(steam_client: Res<Client>) {
},
);
let id = steam_client.user().steam_id();
info!("Steam ID: {:?}", id);
steam_client
.friends()
.set_rich_presence("connect", Some(id.raw().to_string().as_str()));
for friend in steam_client.friends().get_friends(FriendFlags::IMMEDIATE) {
info!(
"Steam Friend: {:?} - {}({:?})",