Detection rewrite for issue #153 - Pass 1

This commit is contained in:
MikeMaximus
2018-09-03 12:36:25 -06:00
parent 3e141cebb5
commit 63da6eba19
8 changed files with 110 additions and 203 deletions
+99 -114
View File
@@ -10,8 +10,6 @@ Public Class mgrProcessDetection
Private oGame As clsGame
Private oDuplicateGames As New ArrayList
Private bDuplicates As Boolean
Private bVerified As Boolean = False
Private sFullCommand As String = String.Empty
Property FoundProcess As Process
Get
@@ -73,91 +71,32 @@ Public Class mgrProcessDetection
End Set
End Property
Property FullCommand As String
Get
Return sFullCommand
End Get
Set(value As String)
sFullCommand = value
End Set
End Property
Private Function HandleDuplicates(hshScanList As Hashtable) As Boolean
Dim sProcess As String
Dim sParameter As String = String.Empty
Dim bParameter As Boolean = False
Dim oInitialDupes As New ArrayList
bDuplicates = True
oDuplicateGames.Clear()
For Each o As clsGame In hshScanList.Values
sProcess = o.ProcessName.Split(":")(0)
If o.Duplicate And o.IsRegEx Then
If Regex.IsMatch(prsFoundProcess.ProcessName, sProcess) Then
oInitialDupes.Add(o.ShallowCopy)
End If
ElseIf o.Duplicate And Not o.IsRegEx Then
If sProcess = prsFoundProcess.ProcessName Then
oInitialDupes.Add(o.ShallowCopy)
End If
End If
Next
For Each o As clsGame In oInitialDupes
If (o.Parameter <> String.Empty And FullCommand.Contains(o.Parameter)) Then
sParameter = o.Parameter
bParameter = True
Exit For
End If
Next
If bParameter Then
For Each o As clsGame In oInitialDupes
If (o.Parameter = sParameter) Then
oDuplicateGames.Add(o.ShallowCopy)
End If
Next
Else
For Each o As clsGame In oInitialDupes
If (o.Parameter = String.Empty) Then
oDuplicateGames.Add(o.ShallowCopy)
End If
Next
End If
If oDuplicateGames.Count = 1 Then
oGame = DirectCast(oDuplicateGames(0), clsGame).ShallowCopy
Return True
End If
Return False
End Function
'This function will only work correctly on Windows
Private Sub GetWindowsCommand(ByVal prs As Process)
FullCommand = String.Empty
Private Function GetWindowsCommand(ByVal prs As Process) As String
Dim sFullCommand As String = String.Empty
Try
Using searcher As New ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + prs.Id.ToString)
For Each o As ManagementObject In searcher.Get()
FullCommand &= o("CommandLine") & " "
sFullCommand &= o("CommandLine") & " "
Next
End Using
Catch ex As Exception
Catch
'Do Nothing
End Try
End Sub
Return sFullCommand
End Function
'This function will only work correctly on Unix
Private Sub GetUnixCommand(ByVal prs As Process)
FullCommand = String.Empty
Private Function GetUnixCommand(ByVal prs As Process) As String
Dim sFullCommand As String = String.Empty
Try
FullCommand = File.ReadAllText("/proc/" & prs.Id.ToString() & "/cmdline").Replace(vbNullChar, " ")
sFullCommand = File.ReadAllText("/proc/" & prs.Id.ToString() & "/cmdline").Replace(vbNullChar, " ")
Catch ex As Exception
'Do Nothing
End Try
End Sub
Return sFullCommand
End Function
'This function will only work correctly on Unix
Private Function GetUnixProcessArguments(ByVal prs As Process) As String()
@@ -208,14 +147,89 @@ Public Class mgrProcessDetection
Return False
End Function
Private Function GetProcessPath(ByVal bWineProcess As Boolean) As String
Try
If Not bWineProcess Then
Return Path.GetDirectoryName(FoundProcess.MainModule.FileName)
Else
Return GetUnixSymLinkDirectory(FoundProcess)
End If
Catch
Return String.Empty
End Try
End Function
Private Sub FilterDetected(ByVal oDetectedGames As ArrayList, ByVal bWineProcess As Boolean)
Dim bMatch As Boolean = False
Dim sProcessPath As String
Dim sFullCommand As String
Dim oNotDetectedWithParameters As New ArrayList
Dim oDetectedWithParameters As New ArrayList
Dim oDetectedWithProcessPath As New ArrayList
'Get parameters of the found process
If mgrCommon.IsUnix Then
sFullCommand = GetUnixCommand(FoundProcess)
Else
sFullCommand = GetWindowsCommand(FoundProcess)
End If
'Look for any games using parameters and any matches
For Each oDetectedGame As clsGame In oDetectedGames
If oDetectedGame.Parameter <> String.Empty Then
If sFullCommand.Contains(oDetectedGame.Parameter) Then
oDetectedWithParameters.Add(oDetectedGame)
Else
oNotDetectedWithParameters.Add(oDetectedGame)
End If
End If
Next
'If we detected at least one parameter match, replace full detected list with the detected with parameter list
If oDetectedWithParameters.Count > 0 Then
oDetectedGames = oDetectedWithParameters
Else
'If there is no parameter match, remove any games using parameters from the detected list
For Each oGameNotDetected As clsGame In oNotDetectedWithParameters
oDetectedGames.Remove(oGameNotDetected)
Next
End If
'If there's only one match after parameter detection, set it as current game and we're done.
If oDetectedGames.Count = 1 Then
GameInfo = oDetectedGames(0)
Duplicate = False
Else
'Get Process Path
sProcessPath = GetProcessPath(bWineProcess)
'Check if we have any exact matches based on process path
For Each oDetectedGame As clsGame In oDetectedGames
If oDetectedGame.ProcessPath = sProcessPath Then
oDetectedWithProcessPath.Add(oDetectedGame)
End If
Next
'If there's only one match after process detection, set it as current game and we're done
If oDetectedWithProcessPath.Count = 1 Then
GameInfo = oDetectedWithProcessPath(0)
Duplicate = False
Else
'We've done all we can, the user must selected which game they were playing when the process ends
Duplicate = True
oDuplicateGames = oDetectedGames
End If
End If
End Sub
Public Function SearchRunningProcesses(ByVal hshScanList As Hashtable, ByRef bNeedsPath As Boolean, ByRef bWineProcess As Boolean, ByRef iErrorCode As Integer, ByVal bDebugMode As Boolean) As Boolean
Dim prsList() As Process = Process.GetProcesses
Dim sProcessCheck As String = String.Empty
Dim sProcessList As String = String.Empty
Dim bPass As Boolean
Dim oDetectedGames As New ArrayList
For Each prsCurrent As Process In prsList
bPass = False
'This needs to be wrapped due to issues with Mono.
Try
@@ -245,39 +259,20 @@ Public Class mgrProcessDetection
'Do Nothing
End Try
'Detection Pass 1
For Each oCurrentGame As clsGame In hshScanList.Values
If IsMatch(oCurrentGame, sProcessCheck) Then
prsFoundProcess = prsCurrent
oGame = oCurrentGame.ShallowCopy
bPass = True
If mgrCommon.IsUnix Then
GetUnixCommand(prsCurrent)
Else
GetWindowsCommand(prsCurrent)
End If
If oGame.Duplicate = True Then
If HandleDuplicates(hshScanList) Then
bDuplicates = False
oDuplicateGames.Clear()
End If
Else
bDuplicates = False
oDuplicateGames.Clear()
End If
If Duplicate And DuplicateList.Count = 0 Then bPass = False
If oGame.Parameter <> String.Empty And Not Duplicate And Not FullCommand.Contains(oGame.Parameter) Then bPass = False
oDetectedGames.Add(oGame.ShallowCopy)
End If
Next
'Detection Pass 2
If bPass Then
'Determine the process path if we need it
If Not oGame.AbsolutePath Or oGame.Duplicate Then
If oDetectedGames.Count > 0 Then
FilterDetected(oDetectedGames, bWineProcess)
End If
If oDetectedGames.Count > 0 Then
If Not oGame.AbsolutePath And Not oGame.MonitorOnly Then
Try
If Not bWineProcess Then
oGame.ProcessPath = Path.GetDirectoryName(prsCurrent.MainModule.FileName)
@@ -296,24 +291,14 @@ Public Class mgrProcessDetection
iErrorCode = 299
Else
If bDebugMode Then mgrCommon.ShowMessage(exWin32.NativeErrorCode & " " & exWin32.Message & vbCrLf & vbCrLf & exWin32.StackTrace, MsgBoxStyle.Critical)
'A different failure occured, drop out and continue to scan.
bPass = False
Return False
End If
Catch exAll As Exception
If bDebugMode Then mgrCommon.ShowMessage(exAll.Message & vbCrLf & vbCrLf & exAll.StackTrace, MsgBoxStyle.Critical)
'A different failure occured, drop out and continue to scan.
bPass = False
Return False
End Try
End If
'This will force two cycles for detection to try and prevent issues with UAC prompt
If Not bVerified Then
bVerified = True
Return False
Else
bVerified = False
Return True
End If
Return True
End If
Next