11using Atlasd . Battlenet . Exceptions ;
22using Atlasd . Daemon ;
3+ using Atlasd . Localization ;
34using System ;
5+ using System . Collections . Generic ;
6+ using System . Globalization ;
47using System . IO ;
58using System . Linq ;
69using System . Text ;
@@ -24,11 +27,17 @@ public SID_NOTIFYJOIN(byte[] buffer)
2427
2528 public override bool Invoke ( MessageContext context )
2629 {
30+ if ( context == null || context . Client == null || ! context . Client . Connected ) return false ;
31+ var gameState = context . Client . GameState ;
32+
2733 Logging . WriteLine ( Logging . LogLevel . Debug , Logging . LogType . Client_Game , context . Client . RemoteEndPoint , $ "[{ Common . DirectionToString ( context . Direction ) } ] { MessageName ( Id ) } ({ 4 + Buffer . Length } bytes)") ;
2834
2935 if ( context . Direction != MessageDirection . ClientToServer )
3036 throw new GameProtocolViolationException ( context . Client , $ "{ MessageName ( Id ) } must be sent from client to server") ;
3137
38+ if ( gameState == null || gameState . ActiveAccount == null )
39+ throw new GameProtocolViolationException ( context . Client , $ "{ MessageName ( Id ) } received but client not logged into an account") ;
40+
3241 if ( Buffer . Length < 10 )
3342 throw new GameProtocolViolationException ( context . Client , $ "{ MessageName ( Id ) } buffer must be at least 10 bytes") ;
3443
@@ -40,22 +49,50 @@ public override bool Invoke(MessageContext context)
4049 var gameName = r . ReadByteString ( ) ;
4150 var gamePassword = r . ReadByteString ( ) ;
4251
43- if ( context . Client . GameState . ActiveChannel != null )
44- context . Client . GameState . ActiveChannel . RemoveUser ( context . Client . GameState ) ;
52+ if ( gameState . ActiveChannel != null )
53+ gameState . ActiveChannel . RemoveUser ( gameState ) ;
4554
4655 lock ( Battlenet . Common . ActiveGameAds )
4756 {
4857 foreach ( var gameAd in Battlenet . Common . ActiveGameAds )
4958 {
5059 if ( gameAd . Name . SequenceEqual ( gameName ) )
5160 {
52- if ( gameAd . HasClient ( context . Client . GameState ) || gameAd . AddClient ( context . Client . GameState ) )
53- context . Client . GameState . GameAd = gameAd ;
61+ if ( gameAd . HasClient ( gameState ) || gameAd . AddClient ( gameState ) )
62+ gameState . GameAd = gameAd ;
5463 break ;
5564 }
5665 }
5766 }
5867
68+ var mutualFriend = false ;
69+ var friendByteStrings = ( List < byte [ ] > ) gameState . ActiveAccount . Get ( Account . FriendsKey , new List < byte [ ] > ( ) ) ;
70+ foreach ( var friendByteString in friendByteStrings )
71+ {
72+ if ( Battlenet . Common . GetClientByOnlineName ( Encoding . UTF8 . GetString ( friendByteString ) , out var friendGameState ) && friendGameState != null && friendGameState . ActiveAccount != null )
73+ {
74+ mutualFriend = false ;
75+ var subfriendByteStrings = ( List < byte [ ] > ) friendGameState . ActiveAccount . Get ( Account . FriendsKey , new List < byte [ ] > ( ) ) ;
76+ foreach ( var subfriendByteString in subfriendByteStrings )
77+ {
78+ if ( Battlenet . Common . GetClientByOnlineName ( Encoding . UTF8 . GetString ( subfriendByteString ) , out var subfriendGameState ) && subfriendGameState != null )
79+ {
80+ mutualFriend = ( subfriendGameState == gameState ) ;
81+ if ( mutualFriend ) break ;
82+ }
83+ }
84+ if ( mutualFriend )
85+ {
86+ var buffer = Resources . YourFriendEnteredGame ;
87+ buffer = buffer . Replace ( "{friend}" , gameState . Username , true , CultureInfo . InvariantCulture ) ;
88+ buffer = buffer . Replace ( "{game}" , Product . ProductName ( gameState . Product , true ) , true , CultureInfo . InvariantCulture ) ;
89+ buffer = buffer . Replace ( "{gameAd}" , Encoding . UTF8 . GetString ( gameState . GameAd . Name ) , true , CultureInfo . InvariantCulture ) ;
90+ foreach ( var line in buffer . Split ( Battlenet . Common . NewLine ) )
91+ new ChatEvent ( ChatEvent . EventIds . EID_WHISPERFROM , gameState . ChannelFlags , gameState . Ping , Channel . RenderOnlineName ( friendGameState , gameState ) , buffer ) . WriteTo ( friendGameState . Client ) ;
92+ }
93+ }
94+ }
95+
5996 return true ;
6097 }
6198 }
0 commit comments