add renet_steam support (#92)
This commit is contained in:
@@ -1,25 +1,92 @@
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use bevy::prelude::*;
|
||||
use clap::Parser;
|
||||
use std::net::SocketAddr;
|
||||
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 {
|
||||
/// 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.
|
||||
struct NetworkingConfig {
|
||||
/// Steam id of the host to connect to
|
||||
#[arg(long)]
|
||||
pub server: Option<Option<SocketAddr>>,
|
||||
/// 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>>,
|
||||
}
|
||||
|
||||
#[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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user