@@ -35,7 +35,62 @@ static void Main(string[] args)
3535
3636 static void Run ( string [ ] args )
3737 {
38- var arguments = new Arguments ( args ) ;
38+ var normalizedArgs = new List < string > ( ) ;
39+ for ( var i = 0 ; i < args . Length ; i ++ )
40+ {
41+ var arg = args [ i ] ;
42+ if ( string . IsNullOrWhiteSpace ( arg ) )
43+ continue ;
44+
45+ if ( arg == "--port" || arg == "-p" )
46+ {
47+ if ( i + 1 < args . Length )
48+ normalizedArgs . Add ( $ "Server.ListenPort={ args [ ++ i ] } ") ;
49+ continue ;
50+ }
51+
52+ if ( arg . StartsWith ( "--port=" , StringComparison . Ordinal ) )
53+ {
54+ normalizedArgs . Add ( $ "Server.ListenPort={ arg [ 7 ..] } ") ;
55+ continue ;
56+ }
57+
58+ if ( arg == "--listen" || arg == "--listen-address" || arg == "--bind" )
59+ {
60+ if ( i + 1 < args . Length )
61+ normalizedArgs . Add ( $ "Server.ListenAddress={ args [ ++ i ] } ") ;
62+ continue ;
63+ }
64+
65+ if ( arg . StartsWith ( "--listen=" , StringComparison . Ordinal ) )
66+ {
67+ normalizedArgs . Add ( $ "Server.ListenAddress={ arg [ 9 ..] } ") ;
68+ continue ;
69+ }
70+
71+ if ( arg == "--lan" || arg == "--lanplay" )
72+ {
73+ normalizedArgs . Add ( "Server.LANPlay=True" ) ;
74+ continue ;
75+ }
76+
77+ if ( arg == "--mod" || arg == "-m" )
78+ {
79+ if ( i + 1 < args . Length )
80+ normalizedArgs . Add ( $ "Game.Mod={ args [ ++ i ] } ") ;
81+ continue ;
82+ }
83+
84+ if ( arg . StartsWith ( "--mod=" , StringComparison . Ordinal ) )
85+ {
86+ normalizedArgs . Add ( $ "Game.Mod={ arg [ 6 ..] } ") ;
87+ continue ;
88+ }
89+
90+ normalizedArgs . Add ( arg ) ;
91+ }
92+
93+ var arguments = new Arguments ( normalizedArgs . ToArray ( ) ) ;
3994
4095 var engineDirArg = arguments . GetValue ( "Engine.EngineDir" , null ) ;
4196 if ( ! string . IsNullOrEmpty ( engineDirArg ) )
@@ -79,10 +134,21 @@ static void Run(string[] args)
79134 if ( modID == null )
80135 throw new InvalidOperationException ( "Game.Mod argument missing or mod could not be found." ) ;
81136
137+ if ( arguments . Contains ( "Server.Port" ) )
138+ arguments . ReplaceValue ( "Server.ListenPort" , arguments . GetValue ( "Server.Port" , null ) ) ;
139+
140+ if ( arguments . Contains ( "Server.LANPlay" ) && ! arguments . Contains ( "Server.AdvertiseOnline" ) )
141+ {
142+ var lanPlay = arguments . GetValue ( "Server.LANPlay" , null ) ;
143+ if ( bool . TryParse ( lanPlay , out var lanEnabled ) )
144+ arguments . ReplaceValue ( "Server.AdvertiseOnline" , ( ! lanEnabled ) . ToString ( ) ) ;
145+ }
146+
82147 // HACK: The engine code assumes that Game.Settings is set.
83148 // This isn't nearly as bad as ModData, but is still not very nice.
84149 Game . InitializeSettings ( arguments ) ;
85150 var settings = Game . Settings . Server ;
151+ var listenAddressArg = arguments . GetValue ( "Server.ListenAddress" , null ) ;
86152
87153 Nat . Initialize ( ) ;
88154
@@ -104,7 +170,44 @@ static void Run(string[] args)
104170 // HACK: Related to the above one, initialize the translations so we can load maps with their (translated) lobby options.
105171 TranslationProvider . Initialize ( modData , modData . DefaultFileSystem ) ;
106172
107- var endpoints = new List < IPEndPoint > { new ( IPAddress . IPv6Any , settings . ListenPort ) , new ( IPAddress . Any , settings . ListenPort ) } ;
173+ var endpoints = new List < IPEndPoint > ( ) ;
174+ if ( ! string . IsNullOrWhiteSpace ( listenAddressArg ) )
175+ {
176+ var addresses = new List < IPAddress > ( ) ;
177+ var parts = listenAddressArg . Split ( ',' , StringSplitOptions . RemoveEmptyEntries ) ;
178+ foreach ( var part in parts )
179+ {
180+ var token = part . Trim ( ) ;
181+ if ( token . Length == 0 )
182+ continue ;
183+
184+ if ( token == "0.0.0.0" )
185+ addresses . Add ( IPAddress . Any ) ;
186+ else if ( token == "::" || token == "[::]" )
187+ addresses . Add ( IPAddress . IPv6Any ) ;
188+ else if ( IPAddress . TryParse ( token , out var ip ) )
189+ addresses . Add ( ip ) ;
190+ else
191+ {
192+ try
193+ {
194+ addresses . AddRange ( Dns . GetHostAddresses ( token ) ) ;
195+ }
196+ catch { }
197+ }
198+ }
199+
200+ if ( addresses . Count == 0 )
201+ throw new InvalidOperationException ( "Server.ListenAddress argument is invalid." ) ;
202+
203+ foreach ( var address in addresses )
204+ endpoints . Add ( new IPEndPoint ( address , settings . ListenPort ) ) ;
205+ }
206+ else
207+ {
208+ endpoints . Add ( new IPEndPoint ( IPAddress . IPv6Any , settings . ListenPort ) ) ;
209+ endpoints . Add ( new IPEndPoint ( IPAddress . Any , settings . ListenPort ) ) ;
210+ }
108211 var server = new Server ( endpoints , settings , modData , ServerType . Dedicated ) ;
109212
110213 GC . Collect ( ) ;
0 commit comments