@@ -106,49 +106,10 @@ public ServerInfo GetServerInfo()
106106 try
107107 {
108108 sInfo . Ping = new Ping ( ) . Send ( _ipEndpoint . Address ) . RoundtripTime ;
109- var request = BuildRequest ( RequestHeaders . A2S_INFO , Encoding . UTF8 . GetBytes ( requestPayload ) ) ;
110- _client . Send ( request , request . Length ) ;
111- byte [ ] response = _client . Receive ( ref _ipEndpoint ) ;
109+ byte [ ] response = SendRequest ( RequestHeaders . A2S_INFO , Encoding . UTF8 . GetBytes ( requestPayload ) ) ;
112110 if ( response . Length > 0 )
113111 {
114- IEnumerable < byte > lastSource = ExtractData ( sInfo , response , nameof ( sInfo . EDF ) ) ;
115-
116- // Handle EDF's. This part looks hideous but i will get back to this after i get done with everything. Right now this works.
117- if ( ( sInfo . EDF & 0x80 ) > 0 )
118- {
119- ( object result , int size ) = ExtractMarshalType ( lastSource , sInfo . Port . GetType ( ) ) ;
120- sInfo . Port = ( short ) result ;
121- lastSource = lastSource . Skip ( size ) ;
122- }
123- if ( ( sInfo . EDF & 0x10 ) > 0 )
124- {
125- ( object result , int size ) = ExtractMarshalType ( lastSource , sInfo . SteamID . GetType ( ) ) ;
126- sInfo . SteamID = ( long ) result ;
127- lastSource = lastSource . Skip ( size ) ;
128- }
129- if ( ( sInfo . EDF & 0x40 ) > 0 )
130- {
131- ( object result , int size ) = ExtractMarshalType ( lastSource , sInfo . SourceTVPort . GetType ( ) ) ;
132- sInfo . SourceTVPort = ( short ) result ;
133- lastSource = lastSource . Skip ( size ) ;
134-
135- IEnumerable < byte > takenBytes = lastSource . TakeWhile ( x => x != 0 ) ;
136- sInfo . SourceTVServerName = Encoding . UTF8 . GetString ( takenBytes . ToArray ( ) ) ;
137- lastSource = lastSource . Skip ( takenBytes . Count ( ) + 1 ) ;
138-
139- }
140- if ( ( sInfo . EDF & 0x20 ) > 0 )
141- {
142- IEnumerable < byte > takenBytes = lastSource . TakeWhile ( x => x != 0 ) ;
143- sInfo . Keywords = Encoding . UTF8 . GetString ( takenBytes . ToArray ( ) ) ;
144- lastSource = lastSource . Skip ( takenBytes . Count ( ) + 1 ) ;
145- }
146- if ( ( sInfo . EDF & 0x01 ) > 0 )
147- {
148- ( object result , int size ) = ExtractMarshalType ( lastSource , sInfo . GameID . GetType ( ) ) ;
149- sInfo . GameID = ( long ) result ;
150- lastSource = lastSource . Skip ( size ) ;
151- }
112+ ExtractData ( sInfo , response , nameof ( sInfo . EDF ) ) ;
152113 }
153114 }
154115 catch ( Exception )
@@ -160,6 +121,13 @@ public ServerInfo GetServerInfo()
160121 return sInfo ;
161122 }
162123
124+ private byte [ ] SendRequest ( byte requestHeader , byte [ ] payload = null )
125+ {
126+ var request = BuildRequest ( requestHeader , payload ) ;
127+ _client . Send ( request , request . Length ) ;
128+ return _client . Receive ( ref _ipEndpoint ) ;
129+ }
130+
163131 public void RenewChallenge ( )
164132 {
165133 throw new NotImplementedException ( ) ;
@@ -171,7 +139,7 @@ private byte[] BuildRequest(byte headerCode, byte[] extraParams = null)
171139 return extraParams != null ? request . Concat ( extraParams ) . ToArray ( ) : request ;
172140 }
173141
174- private IEnumerable < byte > ExtractData < TObject > ( TObject objectRef , byte [ ] dataSource , string stopAt = "" )
142+ private void ExtractData < TObject > ( TObject objectRef , byte [ ] dataSource , string edfPropName = "" )
175143 where TObject : class
176144 {
177145 IEnumerable < byte > takenBytes = new List < byte > ( ) ;
@@ -182,6 +150,14 @@ private IEnumerable<byte> ExtractData<TObject>(TObject objectRef, byte[] dataSou
182150
183151 foreach ( PropertyInfo property in propsOfObject )
184152 {
153+ CustomAttributeData edfInfo = property . CustomAttributes . FirstOrDefault ( x => x . AttributeType == typeof ( EDFAttribute ) ) ;
154+ if ( edfInfo != null )
155+ {
156+ byte edfValue = ( byte ) typeof ( TObject ) . GetProperty ( edfPropName ) . GetValue ( objectRef ) ;
157+ byte edfPropertyConditionValue = ( byte ) edfInfo . ConstructorArguments [ 0 ] . Value ;
158+ if ( ( edfValue & edfPropertyConditionValue ) <= 0 ) { continue ; }
159+ }
160+
185161 if ( property . PropertyType == typeof ( string ) )
186162 {
187163 takenBytes = strippedSource . TakeWhile ( x => x != 0 ) ;
@@ -195,14 +171,7 @@ private IEnumerable<byte> ExtractData<TObject>(TObject objectRef, byte[] dataSou
195171 property . SetValue ( objectRef , property . PropertyType . IsEnum ? Enum . Parse ( property . PropertyType , result . ToString ( ) ) : result ) ;
196172 strippedSource = strippedSource . Skip ( size ) ;
197173 }
198-
199- if ( property . Name == stopAt )
200- {
201- break ;
202- }
203174 }
204-
205- return strippedSource ;
206175 }
207176
208177 private ( object , int ) ExtractMarshalType ( IEnumerable < byte > source , Type type )
0 commit comments