From 3e1dd1eb2fcd215f5cc8311d8f38aedd5b8d6f8d Mon Sep 17 00:00:00 2001 From: Sofia Date: Tue, 28 Jul 2026 21:36:09 +0300 Subject: [PATCH] Use DNS for joining server --- godot/scenes/main.tscn | 31 +++++++++++++++++++++++++------ rust/src/cli_parser.rs | 2 +- rust/src/game_settings.rs | 10 ++++++---- rust/src/net/network_manager.rs | 23 ++++++++++++++++++++--- rust/src/net/protocol.rs | 4 ---- rust/src/ui/main_menu.rs | 27 +++++++++++++++++---------- 6 files changed, 69 insertions(+), 28 deletions(-) diff --git a/godot/scenes/main.tscn b/godot/scenes/main.tscn index 2627ddc..bf2fca1 100644 --- a/godot/scenes/main.tscn +++ b/godot/scenes/main.tscn @@ -4,7 +4,7 @@ [ext_resource type="PackedScene" uid="uid://dsxu0y8pjbw3k" path="res://scenes/ui/credits.tscn" id="1_sugp2"] [ext_resource type="PackedScene" uid="uid://n3u0jr4wiib1" path="res://scenes/ui/settings.tscn" id="2_0wfyh"] -[node name="menu" type="MainMenu" unique_id=2036763876 node_paths=PackedStringArray("camera", "main_position", "host_position", "main_panel", "host_panel", "join_panel", "host_port", "join_addr", "recent_servers_button", "popup_panel", "popup_title", "popup_text", "popup_button", "open_replay_dialog")] +[node name="menu" type="MainMenu" unique_id=2036763876 node_paths=PackedStringArray("camera", "main_position", "host_position", "main_panel", "host_panel", "join_panel", "host_port", "join_addr", "join_port", "recent_servers_button", "popup_panel", "popup_title", "popup_text", "popup_button", "open_replay_dialog")] camera = NodePath("camera_3d") main_position = NodePath("main_pos") host_position = NodePath("host_pos") @@ -13,16 +13,18 @@ host_panel = NodePath("host_panel") join_panel = NodePath("join_panel") host_port = NodePath("host_panel/v_box_container/host_port") join_addr = NodePath("join_panel/v_box_container/address_text") +join_port = NodePath("join_panel/v_box_container/port_text") recent_servers_button = NodePath("join_panel/v_box_container/recent_servers") popup_panel = NodePath("popup_panel") popup_title = NodePath("popup_panel/v_box_container/title") popup_text = NodePath("popup_panel/v_box_container/text") popup_button = NodePath("popup_panel/v_box_container/ok") +current_menu = 2 credits = ExtResource("1_sugp2") open_replay_dialog = NodePath("open_replay_dialog") [node name="camera_3d" type="Camera3D" parent="." unique_id=1513321931] -transform = Transform3D(0.98629165, -0.031186381, 0.16203775, -1.4054481e-08, 0.981978, 0.1889952, -0.16501158, -0.18640438, 0.9685167, 1.0974052, 1.7166122, 1.4909106) +transform = Transform3D(0.39605948, -0.17354012, 0.9016766, -1.9851193e-08, 0.981978, 0.18899524, -0.9182249, -0.074853376, 0.3889217, -1.0602231, 1.7166122, 3.7597804) current = true [node name="directional_light_3d" type="DirectionalLight3D" parent="." unique_id=75522463] @@ -38,6 +40,7 @@ transform = Transform3D(0.9823497, 0, 0.18705386, 0, 1, 0, -0.18705386, 0, 0.982 transform = Transform3D(-0.26696783, 0, 0.9637054, 0, 1, 0, -0.9637054, 0, -0.26696783, -3.5550833, -2.3841858e-07, 2.7302406) [node name="main_panel" type="Panel" parent="." unique_id=2078435804] +visible = false offset_left = 139.0 offset_top = 167.0 offset_right = 336.0 @@ -144,7 +147,6 @@ layout_mode = 2 text = "Back" [node name="join_panel" type="Panel" parent="." unique_id=1630672771] -visible = false offset_left = 139.0 offset_top = 167.0 offset_right = 336.0 @@ -172,18 +174,35 @@ text = "Join Game" horizontal_alignment = 1 metadata/_edit_lock_ = true -[node name="address_label" type="Label" parent="join_panel/v_box_container" unique_id=425811384] +[node name="recent_servers_label" type="Label" parent="join_panel/v_box_container" unique_id=683871668] layout_mode = 2 -text = "Address" +text = "Recent Servers" horizontal_alignment = 1 [node name="recent_servers" type="OptionButton" parent="join_panel/v_box_container" unique_id=486398131] layout_mode = 2 +[node name="address_label" type="Label" parent="join_panel/v_box_container" unique_id=425811384] +layout_mode = 2 +text = "Address" +horizontal_alignment = 1 + [node name="address_text" type="LineEdit" parent="join_panel/v_box_container" unique_id=672176728] custom_minimum_size = Vector2(150, 30) layout_mode = 2 -text = "127.0.0.1:8080" +text = "127.0.0.1" + +[node name="port_label" type="Label" parent="join_panel/v_box_container" unique_id=1606834624] +layout_mode = 2 +text = "Port" +horizontal_alignment = 1 + +[node name="port_text" type="SpinBox" parent="join_panel/v_box_container" unique_id=418812818] +custom_minimum_size = Vector2(150, 30) +layout_mode = 2 +min_value = 1.0 +max_value = 16000.0 +value = 8080.0 [node name="join" type="Button" parent="join_panel/v_box_container" unique_id=1953386108] layout_mode = 2 diff --git a/rust/src/cli_parser.rs b/rust/src/cli_parser.rs index 2098b9e..5192e41 100644 --- a/rust/src/cli_parser.rs +++ b/rust/src/cli_parser.rs @@ -90,7 +90,7 @@ impl INode3D for CliParser { if let Some(port) = options.host { NetworkManager::singleton().bind_mut().host(port); } else if let Some(addr) = options.join { - NetworkManager::singleton().bind_mut().join(addr); + NetworkManager::singleton().bind_mut().join_directly(addr); } } } diff --git a/rust/src/game_settings.rs b/rust/src/game_settings.rs index 46d12a3..2a36287 100644 --- a/rust/src/game_settings.rs +++ b/rust/src/game_settings.rs @@ -137,7 +137,8 @@ impl VolumeSettings { #[derive(Clone, Debug, Serialize, Deserialize)] pub struct RecentServer { - pub addr: SocketAddr, + pub host: String, + pub port: u16, pub visited: u128, } @@ -240,12 +241,12 @@ impl GameSettingsManager { self.run_deferred(|s| s.signals().on_settings_changed().emit()); } - pub fn visit_server(&mut self, server: SocketAddr) { + pub fn visit_server(&mut self, host: String, port: u16) { if let Some(server) = self .settings .recent_servers .iter_mut() - .find(|s| s.addr == server) + .find(|s| s.host == host && s.port == port) { server.visited = SystemTime::now() .duration_since(UNIX_EPOCH) @@ -253,7 +254,8 @@ impl GameSettingsManager { .as_nanos(); } else { self.settings.recent_servers.push(RecentServer { - addr: server, + host, + port, visited: SystemTime::now() .duration_since(UNIX_EPOCH) .unwrap_or(Duration::from_millis(0)) diff --git a/rust/src/net/network_manager.rs b/rust/src/net/network_manager.rs index 5e6b084..1b70187 100644 --- a/rust/src/net/network_manager.rs +++ b/rust/src/net/network_manager.rs @@ -1,6 +1,6 @@ use std::{ collections::HashMap, - net::SocketAddr, + net::{SocketAddr, ToSocketAddrs}, time::{Duration, Instant}, }; @@ -56,7 +56,7 @@ impl INode for NetworkManager { cli.bind_mut() .signals() .on_join() - .connect_other(self, |s, addr| s.join(addr.parse().unwrap())); + .connect_other(self, |s, addr| s.join_directly(addr.parse().unwrap())); cli.bind_mut() .signals() .on_disconnect() @@ -229,7 +229,24 @@ impl NetworkManager { } } - pub fn join(&mut self, addr: SocketAddr) { + pub fn join(&mut self, host: String, port: u16) -> Result<(), ()> { + match format!("{}:{}", host, port).to_socket_addrs() { + Ok(mut addrs) => { + if let Some(addr) = addrs.next() { + GameSettingsManager::singleton() + .bind_mut() + .visit_server(host, port); + self.join_directly(addr); + Ok(()) + } else { + Err(()) + } + } + Err(_) => Err(()), + } + } + + pub fn join_directly(&mut self, addr: SocketAddr) { if self.peer.is_none() { let mut cli = CommandLinePanel::singleton(); cli.bind_mut() diff --git a/rust/src/net/protocol.rs b/rust/src/net/protocol.rs index 24841bd..01756ba 100644 --- a/rust/src/net/protocol.rs +++ b/rust/src/net/protocol.rs @@ -19,10 +19,6 @@ impl NetworkManager { match peer { PeerKind::Client(peer, addr) => match message { teanet::PeerMessage::NewConnection(connection) => { - GameSettingsManager::singleton() - .bind_mut() - .visit_server(addr.clone()); - cli.bind_mut().publish_message( format!("Connected to: {}", connection.address), CliColor::Info, diff --git a/rust/src/ui/main_menu.rs b/rust/src/ui/main_menu.rs index c3ec2b2..27b13f7 100644 --- a/rust/src/ui/main_menu.rs +++ b/rust/src/ui/main_menu.rs @@ -47,6 +47,8 @@ pub struct MainMenu { #[export] join_addr: Option>, #[export] + join_port: Option>, + #[export] recent_servers_button: Option>, #[export] @@ -83,7 +85,7 @@ impl INode3D for MainMenu { .recent_servers .clone(); for server in &self.recent_servers { - recent_servers_button.add_item(&server.addr.to_string()); + recent_servers_button.add_item(&format!("{}:{}", server.host, server.port)); } recent_servers_button.select(-1); @@ -203,21 +205,24 @@ impl MainMenu { #[func] pub fn join_game(&mut self) { - if let Some(addr) = self.join_addr.clone() { + if let Some(addr) = self.join_addr.clone() + && let Some(port) = self.join_port.clone() + { self.show_popup("Joining", "joining game", false); PopupQueue::singleton().bind_mut().queue(Popup { title: "Joining".to_owned(), message: "joining game".to_owned(), ok: false, }); - match addr.get_text().to_string().parse() { - Ok(addr) => { - NetworkManager::singleton().bind_mut().join(addr); - } - Err(err) => { + match NetworkManager::singleton() + .bind_mut() + .join(addr.get_text().to_string(), port.get_value() as u16) + { + Ok(_) => {} + Err(_) => { PopupQueue::singleton().bind_mut().queue(Popup { - title: "Error".to_owned(), - message: format!("Error joining: {}", err), + title: "Error".to_string(), + message: "Address is invalid".to_string(), ok: true, }); } @@ -279,8 +284,10 @@ impl MainMenu { if idx >= 0 { if let Some(server) = self.recent_servers.get(idx as usize) && let Some(addr) = &mut self.join_addr + && let Some(port) = &mut self.join_port { - addr.set_text(&server.addr.to_string()); + addr.set_text(&server.host); + port.set_value(server.port as f64); } if let Some(button) = &mut self.recent_servers_button { button.select(-1);