1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
23using System . IO ;
34using System . Net ;
45using System . Net . Sockets ;
@@ -25,6 +26,7 @@ public class MainMenuMultiplayerPanel : MonoBehaviour
2526 public string SERVER_LIST_PATH = Path . Combine ( "." , "servers" ) ;
2627 private string serverHostInput ;
2728 private string serverNameInput ;
29+ private string serverPortInput ;
2830
2931 private bool shouldFocus ;
3032 private bool showingAddServer ;
@@ -41,7 +43,7 @@ public void Awake()
4143
4244 if ( ! File . Exists ( SERVER_LIST_PATH ) )
4345 {
44- AddServer ( "local server" , "127.0.0.1" ) ;
46+ AddServer ( "local server" , "127.0.0.1" , "11000" ) ;
4547 }
4648
4749 CreateButton ( "Add server IP" , ShowAddServerWindow ) ;
@@ -60,7 +62,7 @@ public void CreateButton(string text, UnityAction clickEvent)
6062 multiplayerButtonInst . transform . SetParent ( savedGameAreaContent , false ) ;
6163 }
6264
63- public void CreateServerButton ( string text , string joinIp )
65+ public void CreateServerButton ( string text , string joinIp , string joinPort )
6466 {
6567 GameObject multiplayerButtonInst = Instantiate ( multiplayerButton , savedGameAreaContent , false ) ;
6668 multiplayerButtonInst . name = ( savedGameAreaContent . childCount - 1 ) . ToString ( ) ;
@@ -73,7 +75,7 @@ public void CreateServerButton(string text, string joinIp)
7375 multiplayerButtonButton . onClick . AddListener ( ( ) =>
7476 {
7577 txt . GetComponent < Text > ( ) . color = prevTextColor ; // Visual fix for black text after click (hover state still active)
76- OpenJoinServerMenu ( joinIp ) ;
78+ OpenJoinServerMenu ( joinIp , joinPort ) ;
7779 } ) ;
7880
7981 GameObject delete = Instantiate ( SavedGamesRef . GetComponent < MainMenuLoadPanel > ( ) . saveInstance . GetComponent < MainMenuLoadButton > ( ) . deleteButton ) ;
@@ -87,11 +89,11 @@ public void CreateServerButton(string text, string joinIp)
8789 delete . transform . SetParent ( multiplayerButtonInst . transform , false ) ;
8890 }
8991
90- public void AddServer ( string name , string ip )
92+ public void AddServer ( string name , string ip , string port )
9193 {
9294 using ( StreamWriter sw = new StreamWriter ( SERVER_LIST_PATH , true ) )
9395 {
94- sw . WriteLine ( $ "{ name } |{ ip } ") ;
96+ sw . WriteLine ( $ "{ name } |{ ip } | { port } ") ;
9597 }
9698 }
9799
@@ -102,12 +104,12 @@ public void RemoveServer(int index)
102104 File . WriteAllLines ( SERVER_LIST_PATH , serverLines . ToArray ( ) ) ;
103105 }
104106
105- public void OpenJoinServerMenu ( string serverIp )
107+ public void OpenJoinServerMenu ( string serverIp , string serverPort )
106108 {
107- IPEndPoint endpoint = ResolveIp ( serverIp ) ?? ResolveHostName ( serverIp ) ;
109+ IPEndPoint endpoint = ResolveIPEndPoint ( serverIp , serverPort ) ;
108110 if ( endpoint == null )
109111 {
110- Log . InGame ( $ "Unable to resolve remote address: { serverIp } ") ;
112+ Log . InGame ( $ "Unable to resolve remote address: { serverIp } : { serverPort } ") ;
111113 return ;
112114 }
113115
@@ -128,6 +130,7 @@ public void ShowAddServerWindow()
128130 {
129131 serverNameInput = "local" ;
130132 serverHostInput = "127.0.0.1" ;
133+ serverPortInput = "11000" ;
131134 showingAddServer = true ;
132135 shouldFocus = true ;
133136 }
@@ -158,41 +161,56 @@ private void LoadSavedServers()
158161 string [ ] lineData = line . Split ( '|' ) ;
159162 string serverName = lineData [ 0 ] ;
160163 string serverIp = lineData [ 1 ] ;
161- CreateServerButton ( $ "Connect to <b>{ serverName } </b>\n { serverIp } ", serverIp ) ;
164+ string serverPort ;
165+ if ( lineData . Length == 3 )
166+ {
167+ serverPort = lineData [ 2 ] ;
168+ }
169+ else
170+ {
171+ Match match = Regex . Match ( serverIp , @"^(.*?)(?::(\d{3,5}))?$" ) ;
172+ serverIp = match . Groups [ 1 ] . Value ;
173+ serverPort = match . Groups [ 2 ] . Success ? match . Groups [ 2 ] . Value : "11000" ;
174+ }
175+ CreateServerButton ( $ "Connect to <b>{ serverName } </b>\n { serverIp } :{ serverPort } ", serverIp , serverPort ) ;
162176 }
163177 }
164178 }
165179
166- private IPEndPoint ResolveIp ( string serverIp )
180+ private IPEndPoint ResolveIPEndPoint ( string serverIp , string serverPort )
167181 {
168- Match match = Regex . Match ( serverIp , @"^((?:(?:[\da-f]+:??){8}|[\d\.]+|[^:\s]+)):?(\d+)?$" ) ; // Pattern test url: https://regex101.com/r/WY4FWp/10
169- if ( ! match . Success )
182+ UriHostNameType hostType = Uri . CheckHostName ( serverIp ) ;
183+ IPAddress address ;
184+ switch ( hostType )
170185 {
171- return null ;
186+ case UriHostNameType . IPv4 :
187+ case UriHostNameType . IPv6 :
188+ IPAddress . TryParse ( serverIp , out address ) ;
189+ break ;
190+ case UriHostNameType . Dns :
191+ address = ResolveHostName ( serverIp , serverPort ) ;
192+ break ;
193+ default :
194+ return null ;
172195 }
173196
174- IPAddress ip = IPAddress . Parse ( match . Groups [ 1 ] . Value ) ;
175- int port = match . Groups [ 2 ] . Success ? int . Parse ( match . Groups [ 2 ] . Value ) : 11000 ;
176- return new IPEndPoint ( ip , port ) ;
177- }
178-
179- private IPEndPoint ResolveHostName ( string hostname )
180- {
181- Match match = Regex . Match ( hostname , @"^\s*([a-zA-Z\.]*)\:?(\d{2,5})?\s*$" ) ;
182- if ( ! match . Success )
197+ if ( address == null )
183198 {
184- Log . ErrorSensitive ( "Hostname {hostname} has an invalid format" , hostname ) ;
185199 return null ;
186200 }
201+ return new IPEndPoint ( address , int . Parse ( serverPort ) ) ;
202+ }
187203
204+ private IPAddress ResolveHostName ( string hostname , string serverPort )
205+ {
188206 try
189207 {
190- IPHostEntry hostEntry = Dns . GetHostEntry ( match . Groups [ 1 ] . Value ) ;
191- return new IPEndPoint ( hostEntry . AddressList [ 0 ] , match . Groups [ 2 ] . Success ? int . Parse ( match . Groups [ 2 ] . Value ) : 11000 ) ;
208+ IPHostEntry hostEntry = Dns . GetHostEntry ( hostname ) ;
209+ return hostEntry . AddressList [ 0 ] ;
192210 }
193211 catch ( SocketException ex )
194212 {
195- Log . ErrorSensitive ( ex , "Unable to resolve the address {hostname}" , hostname ) ;
213+ Log . ErrorSensitive ( ex , "Unable to resolve the address {hostname}:{serverPort} " , hostname , serverPort ) ;
196214 return null ;
197215 }
198216 }
@@ -201,8 +219,9 @@ private void OnAddServerButtonClicked()
201219 {
202220 serverNameInput = serverNameInput . Trim ( ) ;
203221 serverHostInput = serverHostInput . Trim ( ) ;
204- AddServer ( serverNameInput , serverHostInput ) ;
205- CreateServerButton ( $ "Connect to <b>{ serverNameInput } </b>\n { serverHostInput } ", serverHostInput ) ;
222+ serverPortInput = serverPortInput . Trim ( ) ;
223+ AddServer ( serverNameInput , serverHostInput , serverPortInput ) ;
224+ CreateServerButton ( $ "Connect to <b>{ serverNameInput } </b>\n { serverHostInput } :{ serverPortInput } ", serverHostInput , serverPortInput ) ;
206225 HideAddServerWindow ( ) ;
207226 }
208227
@@ -270,6 +289,13 @@ private void DoAddServerWindow(int windowId)
270289 serverHostInput = GUILayout . TextField ( serverHostInput , 120 ) ;
271290 }
272291
292+ using ( new GUILayout . HorizontalScope ( ) )
293+ {
294+ GUILayout . Label ( "Port:" ) ;
295+ GUI . SetNextControlName ( "serverPortField" ) ;
296+ serverPortInput = GUILayout . TextField ( serverPortInput ) ;
297+ }
298+
273299 if ( GUILayout . Button ( "Add server" ) )
274300 {
275301 OnAddServerButtonClicked ( ) ;
0 commit comments