1+ using System . Collections . Generic ;
2+ using System . Linq ;
3+ using CS2M . API . Commands ;
4+ using CS2M . API . Networking ;
5+ using CS2M . Commands . Data . Internal ;
6+ using CS2M . Mods ;
7+ using CS2M . Networking ;
8+ using CS2M . Util ;
9+ using LiteNetLib ;
10+
11+ namespace CS2M . Commands . Handler . Internal
12+ {
13+ public class PreconditionsCheckHandler : CommandHandler < PreconditionsCheckCommand >
14+ {
15+ public PreconditionsCheckHandler ( )
16+ {
17+ TransactionCmd = false ;
18+ }
19+
20+ protected override void Handle ( PreconditionsCheckCommand command )
21+ {
22+ }
23+
24+ public void HandleOnServer ( PreconditionsCheckCommand command , NetPeer peer )
25+ {
26+ Log . Debug ( $ "Received Preconditions Check [PeerId: { peer . Id } ]") ;
27+
28+ PreconditionsUtil . Result result = PreconditionsUtil . CheckPreconditions ( command ) ;
29+
30+ // Check the client username to see if anyone on the server already have a username
31+ if ( NetworkInterface . Instance . PlayerListConnected . Any ( p => p . Username . Equals ( command . Username ) ) )
32+ {
33+ Log . Debug ( $ "[Preconditions Check] Username '{ command . Username } ' is already connected.") ;
34+ result . Errors |= PreconditionsUtil . Errors . USERNAME_NOT_AVAILABLE ;
35+ }
36+
37+ // Check the password to see if it matches (only if the server has provided a password).
38+ if ( ! string . IsNullOrEmpty ( NetworkInterface . Instance . LocalPlayer . GetConnectionPassword ( ) ) )
39+ {
40+ if ( ! NetworkInterface . Instance . LocalPlayer . GetConnectionPassword ( ) . Equals ( command . Password ) )
41+ {
42+ Log . Debug ( $ "[Preconditions Check] Password '{ command . Password } ' is not correct.") ;
43+ result . Errors |= PreconditionsUtil . Errors . PASSWORD_INCORRECT ;
44+ }
45+ }
46+
47+ if ( result . Errors == PreconditionsUtil . Errors . NONE )
48+ {
49+ NetworkInterface . Instance . LocalPlayer . SendToClient ( peer , new PreconditionsSuccessCommand ( ) ) ;
50+
51+ // Add the new player as a connected player
52+ NetworkInterface . Instance . PlayerConnected ( new RemotePlayer ( peer , command . Username , PlayerType . CLIENT ) ) ;
53+ }
54+ else
55+ {
56+ NetworkInterface . Instance . LocalPlayer . SendToClient ( peer , new PreconditionsErrorCommand ( )
57+ {
58+ Errors = result . Errors ,
59+ ModVersion = VersionUtil . GetModVersion ( ) ,
60+ GameVersion = VersionUtil . GetGameVersion ( ) ,
61+ Mods = ModSupport . Instance . RequiredModsForSync ,
62+ DlcIds = new List < int > ( ) , //TODO: Update with correct DLC List
63+ } ) ;
64+ }
65+ }
66+ }
67+ }
0 commit comments