make cli host work

This commit is contained in:
bgkillas 2024-09-28 11:12:27 -04:00
parent 83cae6ce67
commit 33a2a2ce2c
5 changed files with 55 additions and 11 deletions

View file

@ -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

View file

@ -1187,8 +1187,7 @@ fn peer_role(peer: net::omni::OmniPeerId, netman: &Arc<net::NetManager>) -> 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();
}

View file

@ -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::<u16>().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()

View file

@ -168,7 +168,7 @@ impl NetManager {
create_dir(tmp).unwrap();
}
pub(crate) fn start_inner(self: Arc<NetManager>, player_path: PathBuf) -> io::Result<()> {
pub(crate) fn start_inner(self: Arc<NetManager>, 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<NetManager>, 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");
}
}
}
}

View file

@ -17,4 +17,7 @@ pub struct Args {
/// steam lobby code.
#[argh(option)]
pub lobby: Option<String>,
/// host either steam or ip.
#[argh(option)]
pub host: Option<String>,
}