From 33a2a2ce2c0000e2188784e1176b063f5f68c76c Mon Sep 17 00:00:00 2001 From: bgkillas Date: Sat, 28 Sep 2024 11:12:27 -0400 Subject: [PATCH] make cli host work --- README.md | 5 +++++ noita-proxy/src/lib.rs | 32 ++++++++++++++++++++++++++++---- noita-proxy/src/main.rs | 14 ++++++++++---- noita-proxy/src/net.rs | 12 +++++++++--- noita-proxy/src/util/args.rs | 3 +++ 5 files changed, 55 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index d21fe5e6..0fc35e56 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,11 @@ After that, just start a new Noita game on everyone's PCs, and you should be in can also connect via cli, just run `noita_proxy --lobby [steam_code/ip and port]` + +## Cli host + +can also host via cli, just run `noita_proxy --host [steam/port]`, "--host steam" will host a steam game and "--host 5123" or any port will host via ip at that port + ## Connecting via steam without steam version of game to connect via steam without the steam version of game, since its more stable you can do the following diff --git a/noita-proxy/src/lib.rs b/noita-proxy/src/lib.rs index c5a951eb..27efbfef 100644 --- a/noita-proxy/src/lib.rs +++ b/noita-proxy/src/lib.rs @@ -1187,8 +1187,7 @@ fn peer_role(peer: net::omni::OmniPeerId, netman: &Arc) -> Stri } } -pub fn start_cli(lobby: String) -{ +fn cli_setup() -> (steam_helper::SteamState, NetManagerInit) { let mut state = steam_helper::SteamState::new().unwrap(); let my_nickname = Some(state.get_user_name(state.get_my_id())); let mut mod_manager = ModmanagerSettings{game_exe_path: PathBuf::new(), game_save_path: Some(PathBuf::new())}; @@ -1222,8 +1221,13 @@ pub fn start_cli(lobby: String) player_color: PlayerColor::default(), cosmetics, mod_path: mod_manager.mod_path(), - player_path: player_path.clone(), + player_path, }; + (state, netmaninit) +} + +pub fn connect_cli(lobby: String) { + let (state, netmaninit) = cli_setup(); let varient = if lobby.contains('.') { PeerVariant::Tangled(Peer::connect(SocketAddr::from_str(&lobby).unwrap(), None).unwrap()) @@ -1234,6 +1238,26 @@ pub fn start_cli(lobby: String) ); PeerVariant::Steam(peer) }; + let player_path = netmaninit.player_path.clone(); let netman = net::NetManager::new(varient, netmaninit); - netman.clone().start_inner(player_path).unwrap(); + netman.clone().start_inner(player_path, true).unwrap(); +} + +pub fn host_cli(port: u16) { + let (state, netmaninit) = cli_setup(); + let varient = if port != 0 + { + let bind_addr = SocketAddr::new("0.0.0.0".parse().unwrap(), port); + let peer = Peer::host(bind_addr, None).unwrap(); + PeerVariant::Tangled(peer) + } else { + let peer = net::steam_networking::SteamPeer::new_host( + steamworks::LobbyType::Private, + state.client, + ); + PeerVariant::Steam(peer) + }; + let player_path = netmaninit.player_path.clone(); + let netman = net::NetManager::new(varient, netmaninit); + netman.clone().start_inner(player_path, true).unwrap(); } \ No newline at end of file diff --git a/noita-proxy/src/main.rs b/noita-proxy/src/main.rs index 07fc6f0c..035ef0c9 100644 --- a/noita-proxy/src/main.rs +++ b/noita-proxy/src/main.rs @@ -2,7 +2,7 @@ use eframe::{ egui::{IconData, ViewportBuilder}, NativeOptions, }; -use noita_proxy::{args::Args, recorder::replay_file, start_cli, App}; +use noita_proxy::{args::Args, recorder::replay_file, connect_cli, host_cli, App}; use tracing::{info, level_filters::LevelFilter}; use tracing_subscriber::EnvFilter; @@ -24,9 +24,15 @@ async fn main() { if let Some(replay) = args.replay_folder { replay_file(replay) } - else if let Some(lobby) = args.lobby - { - start_cli(lobby) + else if let Some(host) = args.host { + let port = if host.to_ascii_lowercase() == "steam" { + 0 + } else { + host.parse::().unwrap_or(5123) + }; + host_cli(port) + } else if let Some(lobby) = args.lobby { + connect_cli(lobby) } else { let icon = image::load_from_memory(include_bytes!("../assets/icon.png")) .unwrap() diff --git a/noita-proxy/src/net.rs b/noita-proxy/src/net.rs index 1ef73418..a9c40095 100644 --- a/noita-proxy/src/net.rs +++ b/noita-proxy/src/net.rs @@ -168,7 +168,7 @@ impl NetManager { create_dir(tmp).unwrap(); } - pub(crate) fn start_inner(self: Arc, player_path: PathBuf) -> io::Result<()> { + pub(crate) fn start_inner(self: Arc, player_path: PathBuf, mut cli: bool) -> io::Result<()> { Self::clean_dir(player_path.clone()); if !self.init_settings.cosmetics.0 { File::create(player_path.parent().unwrap().join("tmp/no_crown"))?; @@ -235,6 +235,12 @@ impl NetManager { ), ); while self.continue_running.load(atomic::Ordering::Relaxed) { + if cli { + if let Some(n) = self.peer.lobby_id() { + println!("Lobby ID: {}", n.raw()); + cli = false + } + } if self.end_run.load(atomic::Ordering::Relaxed) { for id in self.peer.iter_peer_ids() { self.send(id, &NetMsg::EndRun, Reliability::Reliable); @@ -501,7 +507,7 @@ impl NetManager { pub fn start(self: Arc, player_path: PathBuf) -> JoinHandle<()> { info!("Starting netmanager"); thread::spawn(move || { - let result = self.clone().start_inner(player_path); + let result = self.clone().start_inner(player_path, false); if let Err(err) = result { error!("Error in netmanager: {}", err); *self.error.lock().unwrap() = Some(err); @@ -601,4 +607,4 @@ impl Drop for NetManager { info!("Skip saving run info: not a host"); } } -} +} \ No newline at end of file diff --git a/noita-proxy/src/util/args.rs b/noita-proxy/src/util/args.rs index d2cc7e81..e412c4e0 100644 --- a/noita-proxy/src/util/args.rs +++ b/noita-proxy/src/util/args.rs @@ -17,4 +17,7 @@ pub struct Args { /// steam lobby code. #[argh(option)] pub lobby: Option, + /// host either steam or ip. + #[argh(option)] + pub host: Option, }