Random ports settings

This commit is contained in:
IQuant 2024-11-19 21:20:39 +03:00
parent a30cdfe2a2
commit f454e6919c
9 changed files with 66 additions and 16 deletions

View file

@ -149,3 +149,6 @@ ip_wait_for_connection = Connecting to ip...
info_stress_tests = We're doing public lobbies (a.k.a stress tests) every saturday, 18:00 UTC. Join our discord for more info.
Info = Info
## Local settings
connect_settings_random_ports = Don't use a predetermined port. Makes things a bit more robust and allows multiple proxies to be launched on the same computer, but Noita will have to be launched through the proxy.

View file

@ -138,3 +138,6 @@ ip_wait_for_connection = Connecting to ip...
info_stress_tests = We're doing public lobbies (a.k.a stress tests) every saturday, 18:00 UTC. Join our discord for more info.
Info = Info
## Local settings
connect_settings_random_ports = Don't use a predetermined port. Makes things a bit more robust and allows multiple proxies to be launched on the same computer, but Noita will have to be launched through the proxy.

View file

@ -149,3 +149,6 @@ ip_wait_for_connection = Conectando ao ip...
info_stress_tests = Estamos fazendo lobbies públicos (vulgo testes de estresse) todo sábado, 18:00 UTC. Entre em nosso discord para mais informação.
Info = Informação
## Local settings
connect_settings_random_ports = Don't use a predetermined port. Makes things a bit more robust and allows multiple proxies to be launched on the same computer, but Noita will have to be launched through the proxy.

View file

@ -147,3 +147,6 @@ ip_wait_for_connection = Подключение к ip...
info_stress_tests = We're doing public lobbies (a.k.a stress tests) every saturday, 18:00 UTC. Join our discord for more info.
Info = Info
## Local settings
connect_settings_random_ports = Don't use a predetermined port. Makes things a bit more robust and allows multiple proxies to be launched on the same computer, but Noita will have to be launched through the proxy.

View file

@ -147,3 +147,6 @@ ip_wait_for_connection = 连接至IP...
info_stress_tests = 我们将在每周六 18:00 UTC 进行公共大厅(也称为压力测试)。加入我们的discord以获取更多信息。
Info = 资讯
## Local settings
connect_settings_random_ports = Don't use a predetermined port. Makes things a bit more robust and allows multiple proxies to be launched on the same computer, but Noita will have to be launched through the proxy.

View file

@ -197,7 +197,8 @@ fn steam_intall_path(steamapps_path: &Path) -> Option<PathBuf> {
}
impl LaunchToken<'_> {
pub fn start_game(&mut self) {
pub fn start_game(&mut self, port: u16) {
let addr_env = format!("127.0.0.1:{port}");
let start_cmd = self.0.start_args.as_ref().unwrap();
let child = if let Some(game_path) = &start_cmd.noita_install {
let steam_install = start_cmd.steam_install.clone().unwrap();
@ -211,11 +212,13 @@ impl LaunchToken<'_> {
Command::new(&start_cmd.executable)
.env("STEAM_COMPAT_CLIENT_INSTALL_PATH", steam_install)
.env("STEAM_COMPAT_DATA_PATH", compat_data)
.env("NP_NOITA_ADDR", &addr_env)
.current_dir(&self.0.game_dir_path)
.args(&start_cmd.args)
.spawn()
} else {
Command::new(&start_cmd.executable)
.env("NP_NOITA_ADDR", &addr_env)
.current_dir(&self.0.game_dir_path)
.args(&start_cmd.args)
.spawn()

View file

@ -421,6 +421,7 @@ struct AppSavedState {
#[serde(default)]
record_all: bool,
spacewars: bool,
random_ports: bool,
}
impl Default for AppSavedState {
@ -435,6 +436,7 @@ impl Default for AppSavedState {
show_extra_debug_stuff: false,
record_all: false,
spacewars: false,
random_ports: false,
}
}
}
@ -662,6 +664,12 @@ impl App {
cosmetics.2 = false
}
}
let noita_port = if self.app_saved_state.random_ports {
0
} else {
21251
};
NetManagerInit {
my_nickname,
save_state: self.run_save_state.clone(),
@ -674,6 +682,7 @@ impl App {
cosmetics: cosmetics.into(),
colors: self.appearance.player_color,
},
noita_port,
}
}
@ -972,6 +981,10 @@ impl App {
&mut self.app_saved_state.spacewars,
tr("connect_settings_spacewars"),
);
ui.checkbox(
&mut self.app_saved_state.random_ports,
tr("connect_settings_random_ports"),
);
ui.add_space(20.0);
if self.player_image.width() == 1 {
self.player_image = image::open(player_path(self.modmanager_settings.mod_path()))
@ -1179,7 +1192,9 @@ impl App {
let mut old_settings = netman.settings.lock().unwrap().clone();
old_settings.progress.clear();
old_settings.seed = new_settings.seed;
netman.dirty.store(old_settings != new_settings, Ordering::Relaxed)
netman
.dirty
.store(old_settings != new_settings, Ordering::Relaxed)
}
ui.add_space(ui.available_width() - 56.0);
if ui.button("Back out").clicked() {
@ -1216,7 +1231,9 @@ impl App {
&& self.app_saved_state.start_game_automatically;
if start_auto || ui.button(tr("launcher_start_game")).clicked() {
info!("Starting the game now");
token.start_game();
token.start_game(
netman.actual_noita_port.load(Ordering::Relaxed),
);
self.can_start_automatically = false;
}
}
@ -1241,7 +1258,11 @@ impl App {
.small()
.fill(Color32::LIGHT_RED);
if !self.end_run_confirmation
&& if dirty { ui.add(button).clicked() } else { ui.button(tr("launcher_end_run")).clicked() }
&& if dirty {
ui.add(button).clicked()
} else {
ui.button(tr("launcher_end_run")).clicked()
}
{
self.end_run_confirmation = true
} else if self.end_run_confirmation
@ -1637,6 +1658,7 @@ fn cli_setup() -> (steam_helper::SteamState, NetManagerInit) {
cosmetics: cosmetics.into(),
colors: PlayerColor::default(),
},
noita_port: 21251,
};
(state, netmaninit)
}

View file

@ -5,6 +5,8 @@ use proxy_opt::ProxyOpt;
use socket2::{Domain, Socket, Type};
use std::fs::{create_dir, remove_dir_all, File};
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, AtomicI32, AtomicU16, Ordering};
use std::sync::{Arc, Mutex};
use std::{
env,
fmt::Display,
@ -13,8 +15,6 @@ use std::{
thread::{self, JoinHandle},
time::{Duration, Instant},
};
use std::sync::atomic::{AtomicBool, AtomicI32, Ordering};
use std::sync::{Arc, Mutex};
use world::{world_info::WorldInfo, NoitaWorldUpdate, WorldManager};
use tangled::Reliability;
@ -110,6 +110,7 @@ pub struct NetManagerInit {
pub player_path: PathBuf,
pub modmanager_settings: ModmanagerSettings,
pub player_png_desc: PlayerPngDesc,
pub noita_port: u16,
}
pub struct NetManager {
@ -133,6 +134,7 @@ pub struct NetManager {
pub no_more_players: AtomicBool,
dont_kick: Mutex<Vec<OmniPeerId>>,
pub dirty: AtomicBool,
pub actual_noita_port: AtomicU16,
}
impl NetManager {
@ -158,6 +160,7 @@ impl NetManager {
no_more_players: AtomicBool::new(false),
dont_kick: Default::default(),
dirty: AtomicBool::new(false),
actual_noita_port: AtomicU16::new(0),
}
.into()
}
@ -215,12 +218,19 @@ impl NetManager {
let address: SocketAddr = env::var("NP_NOITA_ADDR")
.ok()
.and_then(|x| x.parse().ok())
.unwrap_or_else(|| "127.0.0.1:21251".parse().unwrap());
.unwrap_or_else(|| {
SocketAddr::new("127.0.0.1".parse().unwrap(), self.init_settings.noita_port)
});
info!("Listening for noita connection on {}", address);
let address = address.into();
socket.bind(&address)?;
socket.listen(1)?;
socket.set_nonblocking(true)?;
let actual_port = socket.local_addr()?.as_socket().unwrap().port();
self.actual_noita_port.store(actual_port, Ordering::Relaxed);
info!("Actual Noita port: {actual_port}");
let local_server: TcpListener = socket.into();
let is_host = self.is_host();

View file

@ -210,7 +210,7 @@ pub fn replay_file(path: PathBuf) {
let loop_thread = thread::spawn(|| replay_loop(path));
token.start_game();
token.start_game(21251);
loop_thread.join().unwrap();
}