@@ -269,43 +269,65 @@ public void ShowHCMInternalErrorDialog()
269269 {
270270 try
271271 {
272- string [ ] MCCProcessNames = { "MCC-Win64-Shipping" , "MCC-Win64-Winstore-Shipping" , "MCCWinstore-Win64-Shipping" } ;
273272
274- foreach ( string procName in MCCProcessNames )
273+ bool isCorrectProcessName ( string actualProcessName )
275274 {
276- foreach ( Process process in Process . GetProcesses ( ) )
275+ string [ ] MCCProcessNames = { "MCC-Win64-Shipping" , "MCC-Win64-Winstore-Shipping" , "MCCWinstore-Win64-Shipping" } ;
276+ foreach ( string desiredProcessName in MCCProcessNames )
277277 {
278- if ( String . Equals ( process . ProcessName , procName ,
279- StringComparison . OrdinalIgnoreCase ) && ! process . HasExited )
280- {
281- // We don't want to inject while MCC is booting up since LoadLibrary is occupied
282- Log . Verbose ( "Found MCC" ) ;
283- TimeSpan MCCProcessAge = DateTime . Now - process . StartTime ;
284- Log . Verbose ( "MCC age: " + MCCProcessAge ) ;
285- if ( MCCProcessAge < TimeSpan . FromSeconds ( 3 ) )
286- {
287- // We don't want to attach on young MCC because of weird issues with LoadLibrary if it's called from multiple threads (within the same process) at once
288- // TODO: Make this less dumb than just picking 3 seconds since some peoples computers are slower/faster than that.
289- Log . Verbose ( "MCC process too young: " + MCCProcessAge ) ;
290- continue ;
291- }
292- else
293- {
294- Log . Verbose ( "Found mature MCC process, PID: " + process . Id + ", Name: " + process . ProcessName + ", Age: " + MCCProcessAge ) ;
295- return process ;
296- }
278+ if ( String . Equals ( actualProcessName , desiredProcessName , StringComparison . OrdinalIgnoreCase ) )
279+ return true ;
280+ }
281+ return false ;
282+ }
297283
298284
299- }
285+ //Process? mostRecentMCCProcess = Process.GetProcesses()
286+ // .Where(process => isCorrectProcessName(process.ProcessName) && process.HasExited == false)
287+ // .OrderByDescending(process => process.StartTime) // we don't want old zombie processes
288+ // .DefaultIfEmpty(null)
289+ // .FirstOrDefault();
290+
291+ // Could do this in one big linq expression but we want to be able to debug/log for access violations at each step of the way
292+ Log . Debug ( "Getting all processes on computer" ) ;
293+ var allProcesses = Process . GetProcesses ( ) ;
294+
295+ Log . Debug ( "Filtering to MCC process names" ) ;
296+ var mccProcesses = allProcesses . Where ( process => isCorrectProcessName ( process . ProcessName ) ) ;
297+
298+ Log . Debug ( "Filtering exited processes" ) ;
299+ mccProcesses = mccProcesses . Where ( process => process . HasExited == false ) ;
300+
301+ Log . Debug ( "Sorting mcc processes by age" ) ;
302+ mccProcesses = mccProcesses . OrderByDescending ( process => process . StartTime ) ;
303+ Log . Verbose ( "Total mcc process count: " + mccProcesses . Count ( ) ) ;
304+
305+ Log . Debug ( "Grabbing most recent mcc process or null" ) ;
306+ Process ? mostRecentMCCProcess = mccProcesses . DefaultIfEmpty ( null ) . First ( ) ;
307+
308+
309+ if ( mostRecentMCCProcess != null )
310+ {
311+ // We don't want to inject while MCC is booting up since LoadLibrary is occupied
312+ Log . Verbose ( "Found MCC" ) ;
313+ TimeSpan MCCProcessAge = DateTime . Now - mostRecentMCCProcess . StartTime ;
314+ Log . Verbose ( "MCC age: " + MCCProcessAge ) ;
315+ if ( MCCProcessAge < TimeSpan . FromSeconds ( 3 ) )
316+ {
317+ // We don't want to attach on young MCC because of weird issues with LoadLibrary if it's called from multiple threads (within the same process) at once
318+ // TODO: Make this less dumb than just picking 3 seconds since some peoples computers are slower/faster than that.
319+ Log . Verbose ( "MCC process too young: " + MCCProcessAge ) ;
320+ return null ;
300321 }
301322 }
323+
324+ return mostRecentMCCProcess ;
302325 }
303326 catch ( Exception ex )
304327 {
305- Log . Error ( ex . Message ) ;
328+ Log . Error ( "Error getting MCC process: " + ex . Message + " \n Stacktrace: \n " + ex . StackTrace ) ;
306329 return null ;
307330 }
308- return null ;
309331 }
310332
311333 // Just check process list for EasyAntiCheat
@@ -326,6 +348,8 @@ private bool AnticheatIsEnabled()
326348 private DateTime _lastMCCExit = DateTime . MinValue ;
327349 private bool MCCExitedTooRecently ( )
328350 {
351+ if ( _lastMCCExit == DateTime . MinValue ) return false ;
352+
329353 Log . Verbose ( "MCC last exit time was " + ( DateTime . Now - _lastMCCExit ) + " seconds ago" ) ;
330354 return ( DateTime . Now - _lastMCCExit ) < TimeSpan . FromSeconds ( 6 ) ;
331355 }
0 commit comments