From 524dd2432b966fd970b28a65bd3c0486a6d986a7 Mon Sep 17 00:00:00 2001 From: Nimish Coelho Date: Tue, 2 Dec 2025 18:17:18 +0000 Subject: [PATCH] uploaded automation scripts --- actions.ps1 | 251 ++++++++++++++++++++++++++++++++++++++++ checkADB.ps1 | 45 +++++++ checkAdbByName.ps1 | 64 ++++++++++ closeLDPlayerByName.ps1 | 61 ++++++++++ closeLdplayer.ps1 | 38 ++++++ 5 files changed, 459 insertions(+) create mode 100644 actions.ps1 create mode 100644 checkADB.ps1 create mode 100644 checkAdbByName.ps1 create mode 100644 closeLDPlayerByName.ps1 create mode 100644 closeLdplayer.ps1 diff --git a/actions.ps1 b/actions.ps1 new file mode 100644 index 0000000..1813128 --- /dev/null +++ b/actions.ps1 @@ -0,0 +1,251 @@ + +param ( + [Parameter(Mandatory=$true)] + [string[]]$action, + + [Parameter(Mandatory=$false)] + [switch]$waitForChange, + + [Parameter(Mandatory=$false)] + [int]$numberOfPresses, + + [Parameter(Mandatory=$false)] + [string]$instanceIP, + + [Parameter(Mandatory=$false)] + [switch]$fakeTap +) +if(-not $instanceIP){$instanceIP = "10.0.211.121"} + +$DEBUG = 0 +$global:LDPlayerPath ="C:\Ldplayer\LDplayer9" + +function logger($logMessage) { + [int]$number = (get-content -path "$global:LDPlayerPath\index_count.txt") + if($number -gt 21){ $number=0} + $instanceList = Get-Content -Path "$global:LDPlayerPath\instanceList.txt" + $instanceNumber = $instanceList[$number] + $instanceData = (cat C:\Ldplayer\LDplayer9\vms\config\leidian${instanceNumber}.config) + $convertedData = $instanceData | ConvertFrom-Json + $name = $convertedData."statusSettings.playerName" + $count = $number+1 + + $scriptName = Split-Path -Leaf $PSCommandPath + $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff" + $timestampedLogMessage = $timestamp + "-$name, ($count/22)-$scriptName-PID: ${PID}-:" + $logMessage + $logFilePath = "$global:LDPlayerPath\LDAutoLog.txt" + $fs = New-Object System.IO.FileStream($logFilePath, [System.IO.FileMode]::Append, [System.IO.FileAccess]::Write, [System.IO.FileShare]::Read) + $sw = New-Object System.IO.StreamWriter($fs) + try { + $sw.WriteLine($timestampedLogMessage) + } finally { + $sw.Close() + $fs.Close() + } + } + +function Get-ScreenshotData($text) { + adb connect $instanceIP *>$null + $status = & adb devices + if ($status -eq "${instanceIP}:5555 offline") { + $instanceIP | Out-File -FilePath "C:\Ldplayer\Ldplayer9\restartADB.txt" + adb connect $instanceIP *>null + } +adb -s $instanceIP exec-out screencap -p > $global:LDPlayerPath\screenshot.png *>$null + + & "C:\Program Files\Tesseract-OCR\tesseract.exe" "$global:LDPlayerPath\screenshot.png" "$global:LDPlayerPath\tesseract" -l eng tsv *>$null + +$tsvFile = "$global:LDPlayerPath\tesseract.tsv" + +$wordCoords = Get-Content $tsvFile | ConvertFrom-Csv -Delimiter "`t" +$wordCoords | Where-Object ($_.level -eq 5) | Select-Object text, left, top, width, height + + +$Data = $wordCoords | ForEach-Object { + $x1 =[int]$_._left + $y1 =[int]$_.top + $width = [int]$_.width + $height = [int]$_.height + $z1 = $width/2 + $z2 = $height/2 + $midX =[math]::Round($x1+$z1) + $midY =[math]::Round($y1+$z2) + [PSCustomObject]@{ + Text = $_.text + Coords = "$midX $midY" + } +} +$Element = $Data | Where-Object {$_.Text -and $_.Text.ToLower() -eq $text} + +$coordsForText=$Element.Coords +if($coordsForText[1]){ return $coordsForTest[0]} +return $coordsForText +} + +function Find-Element($xml, $keyword) { + if (-not $xml -is [System.Xml.XmlDocument] -or -not $keyword) { return $null } + $sanitizedKeyword = $keyword.Replace("'", "''").ToLower() + if($fakeTap){ + Write-Host "faketap" + $query = "//node[contains(translate(@text, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '$sanitizedKeyword') or contains(translate(@content-desc, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '$sanitizedKeyword')]" + + } else { + $query = "//node[@class='android.widget.Button' and (contains(translate(@text, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '$sanitizedKeyword') or contains(translate(@content-desc, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '$sanitizedKeyword'))]" + } + $element = $xml.SelectSingleNode($query) + return $xml.SelectSingleNode($query) +} + +function Wait-For-ScreenChange($initialHash, [ref]$newXmlData) { + $timeoutSeconds = 10 + logger("Aguardando alteracao na tela (timeout de $timeoutSeconds segundos)...") + $stopwatch = [System.Diagnostics.Stopwatch]::StartNew() + while ($stopwatch.Elapsed.TotalSeconds -lt $timeoutSeconds) { + $currentScreenData = Get-ScreenData -adbTarget $adbTarget + if ($currentScreenData -and $currentScreenData.Hash -ne $initialHash) { + $stopwatch.Stop(); logger("Tela alterada! Prosseguindo..."); $newXmlData.Value = $currentScreenData; return $true + } + Start-Sleep -Seconds 1 + } + $stopwatch.Stop(); logger("!!!ERROR: TIMEOUT: A tela nao mudou apos $timeoutSeconds segundos."); return $false +} + + +function Invoke-TapAction($adbTarget, $xml, $keyword) { + $element = Find-Element -xml $xml -keyword $keyword + if (-not $element) { logger("!!WARNING: Elemento '$keyword' para tocar nao foi encontrado."); return $false } + $bounds = $element.bounds + if ($bounds -notmatch '\[(\d+),(\d+)\]\[(\d+),(\d+)\]') { logger("!!WARNING: Formato de 'bounds' invalido: $bounds"); return $false } + $xMid = ([int]$Matches[1] + [int]$Matches[3]) / 2 + $yMid = ([int]$Matches[2] + [int]$Matches[4]) / 2 + logger("Acao: Tocando em '$element'...") + adb -s $instanceIP shell input tap "$xMid $yMid" + return $true +} + +function Invoke-TypeAction($adbTarget, $textToType) { + $formattedText = $textToType.Replace(' ', '%s') + logger("Acao: Digitando o texto '$textToType'...") + adb -s $instanceIP shell input text "'$formattedText'" 2>&1 + return $true +} + + +if($waitForChange) { + $xmlContent = (Get-Content -Path $global:LDPlayerPath\screen.xml) + $initialHash = (Get-FileHash -Algorithm MD5 -InputStream ([System.IO.MemoryStream]::new([System.Text.Encoding]::UTF8.GetBytes($xmlContent)))).Hash + } + + +$actionType = $action.split('\s')[0] +$cnt=0 + while($true){ + if($cnt -gt 50){return $false} + $connected = adb connect $instanceIP 2>$null + logger("$connected") + $isOnline = adb devices 2> $null + $pattern = [regex]::Escape($instanceIP) + ':\d+\s+device' + if($isOnline -match $pattern){ + break + }else{ + logger("falhou, tenando connectar de novo") + & tskill adb + Start-Sleep -Milliseconds 1000 + $cnt+=1 + } + } + +switch ($actionType) { + +"tap" { + & $global:LDPlayerPath\test.ps1 -instanceIP $instanceIP + $actionType = $action.split('\s')[0] + $keyword = $action.split('\s')[1] + $xmlFile = get-content -path "$global:LDPlayerPath\screen.xml" + $xml = [xml]$xmlFile + logger("$actionType") + + $element = Find-Element -xml $xml -keyword $keyword + if (-not $element) { logger("!!WARNING: Elemento '$keyword' para tocar nao foi encontrado."); return $false} + $bounds = $element.bounds + if ($bounds -notmatch '\[(\d+),(\d+)\]\[(\d+),(\d+)\]') { logger("!!WARNING: Formato de 'bounds' invalido: $bounds"); return } + $xMid = ([int]$Matches[1] + [int]$Matches[3]) / 2 + $yMid = ([int]$Matches[2] + [int]$Matches[4]) / 2 + logger("Acao: Tocando em '$keyword'...") + adb -s $instanceIP shell input tap "$xMid $yMid" 2>&1 +} + +"tap_image" { + $actionType = $action.split('\s')[0] + $keyword = $action.split('\s')[1] + $keyword + $coords = Get-ScreenshotData($keyword) + if (-not $coords) { logger("!!WARNING: Elemento '$keyword' para tocar nao foi encontrado."); return} + if($coords[1]){$newCoords=$coords[1]}else{$newCoords=$coords} + logger("Acao: Tocando em '$keyword' $newCoords...") + $tapCommand = adb -s $instanceIP shell input tap $newCoords 2>&1 + $tapCommand + $coords + } else {logger("not clickable and since not mentioned, not forcing")} + + +"type" { + $textToType = $action[1] + logger("$actionType") + $formattedText = $textToType + logger("Acao: Digitando o texto '$textToType'...") + $letters = $formattedText.ToCharArray() + foreach($char in $letters) { + $charInterval = get-random -minimum .1 -maximum 50 + logger("$char") + adb -s $instanceIP shell input text "'$char'" *>$null + + start-sleep -Milliseconds $charInterval + } + +} + +"key" { + + $keyEventName = $action[1] + if (-not $numberOfPresses) {$numberOfPresses = 1} + logger("$actionType") + logger("$keyEventName") + $KeyEventMapJson = get-content -path "$global:LDPlayerPath\adb_key_events.json" -Raw + + + $keyEventMap = $KeyEventMapJson | ConvertFrom-Json + if ($keyEventMap.key_events.($keyEventName)) { + + $keyCode = $keyEventMap.key_events.$keyEventName + + for ($i = $numberOfPresses; $i -gt 0; $i--){ + logger("Acao: Enviando evento de tecla '$keyEventName' $i/$numberOfPresses (codigo $keyCode)...") + adb -s $instanceIP shell input keyevent $keyCode *>$null + } + } else { logger("!!WARNING: Evento de tecla '$keyEventName' desconhecido."); } +} + +"open" { + + $appName = $action[1] + logger("$actionType") + logger("$appName") + $csvData = Import-Csv -Path "$global:LDPlayerPath\appReference.csv" + $appReference = @{} + $csvData | ForEach-Object { + $appReference[$_.Key] = $_.Value + } + + + + if ($appReference[$appName]) { + + $appCode = $appReference[$appName] + logger("Acao: Abrindo app '$appName' (codigo $appCode)...") + $adbOUT = & adb -s $instanceIP shell monkey -p $appCode 1 2>&1 + logger("$adbOUT") + if($adbOUT -like "*No activities found to run*") {return $false} + } else { logger("!!WARNING: App '$appCode' desconhecido.")} +} +} \ No newline at end of file diff --git a/checkADB.ps1 b/checkADB.ps1 new file mode 100644 index 0000000..c0b5bd4 --- /dev/null +++ b/checkADB.ps1 @@ -0,0 +1,45 @@ +$global:LDPlayerPath ="C:\Ldplayer\LDplayer9" +$instanceIP = "10.0.211.121" +function logger($logMessage) { + [int]$number = (get-content -path "$global:LDPlayerPath\index_count.txt") + if($number -gt 21){ $number=1} + $instanceList = Get-Content -Path "$global:LDPlayerPath\instanceList.txt" + $instanceNumber = $instanceList[$number-1] + $instanceData = (cat C:\Ldplayer\LDplayer9\vms\config\leidian${instanceNumber}.config) + $convertedData = $instanceData | ConvertFrom-Json + $name = $convertedData."statusSettings.playerName" + $count = $number + + $scriptName = Split-Path -Leaf $PSCommandPath + $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff" + $timestampedLogMessage = $timestamp + "-$name, ($count/22)-$scriptName-PID: ${PID}-:" + $logMessage + $logFilePath = "$global:LDPlayerPath\LDAutoLog.txt" + $fs = New-Object System.IO.FileStream($logFilePath, [System.IO.FileMode]::Append, [System.IO.FileAccess]::Write, [System.IO.FileShare]::Read) + $sw = New-Object System.IO.StreamWriter($fs) + try { + $sw.WriteLine($timestampedLogMessage) + } finally { + $sw.Close() + $fs.Close() + } + } + + [int]$cnt=0 +while($true){ + if($cnt%10 -eq 0) { logger("10 iterations passed starting ld instance again...");& $global:LDPlayerPath\closeLdplayer.ps1 ;& $global:LDPlayerPath\start_next_ldplayer.ps1} + if($cnt -lt 2){logger("Waiting for adb device to appear")} + $adbResponse = (adb connect $instanceIP) + $adbResponse + if($adbResponse -like "*connected to 10.0.211.121:5555*"){ + logger("adb connected successfuly to 10.0.211.121 at iteration $cnt") + $isOnline = adb devices 2> $null + $pattern = [regex]::Escape($instanceIP) + ':\d+\s+device' + if($isOnline -match $pattern){ + break + + } else { return $true} + } + $instanceIP | Out-File -FilePath "C:\Ldplayer\Ldplayer9\restartADB.txt" + $cnt+=1 + start-sleep -milliseconds 1000 +} \ No newline at end of file diff --git a/checkAdbByName.ps1 b/checkAdbByName.ps1 new file mode 100644 index 0000000..172cdd3 --- /dev/null +++ b/checkAdbByName.ps1 @@ -0,0 +1,64 @@ +param ( + [string]$instanceName +) + +$global:LDPlayerPath ="C:\Ldplayer\LDplayer9" +$global:instanceNameglobal=$instanceName +function logger($logMessage) { + [int]$number = (get-content -path "$global:LDPlayerPath\index_count.txt") + if($number -gt 21){ $number=0} + $instanceList = Get-Content -Path "$global:LDPlayerPath\instanceList.txt" + #$instanceNumber = $instanceList[$number] + #$instanceData = (cat C:\Ldplayer\LDplayer9\vms\config\leidian${instanceNumber}.config) + #$convertedData = $instanceData | ConvertFrom-Json + foreach ( $inst in $instanceList) { + $instanceData = (cat C:\Ldplayer\LDplayer9\vms\config\leidian${inst}.config) + $convertedData = $instanceData | ConvertFrom-Json + $name = $convertedData."statusSettings.playerName" + if ($global:instanceNameglobal -eq $name){ + $count = $inst + break + } + } + #if(-not $instanceName){$name = $convertedData."statusSettings.playerName"} + #$count = $number+1 + + $scriptName = Split-Path -Leaf $PSCommandPath + $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff" + $timestampedLogMessage = $timestamp + "${global:instanceNameglobal} -instanceID: ${count} -${scriptName} -PID: ${PID}-:" + $logMessage + #$timestampedLogMessage + $logFilePath = "$global:LDPlayerPath\LDAutoLog.txt" + $fs = New-Object System.IO.FileStream($logFilePath, [System.IO.FileMode]::Append, [System.IO.FileAccess]::Write, [System.IO.FileShare]::Read) + $sw = New-Object System.IO.StreamWriter($fs) + try { + $sw.WriteLine($timestampedLogMessage) + } finally { + $sw.Close() + $fs.Close() + } + } + + [int]$cnt=0 +while($true){ + if($cnt -gt 100) {break} + #if(($cnt%15 -eq 0) -and ($cnt -ne 0)) { logger("$cnt iterations passed starting ld instance again..."); rm "C:\Ldplayer\Ldplayer9\runningName.txt"; & $global:LDPlayerPath\startLDPlayerByName.ps1 -instanceName $instanceName} + + if($cnt -lt 2){logger("Waiting for adb device to appear")} + + $adbResponse = (adb connect 10.0.211.125) 2>null + $adbResponse + $instanceIP = "10.0.211.125" + if($adbResponse -like "*connected to 10.0.211.125:5555*"){ + logger("adb connected successfuly to 10.0.211.125 at iteration $cnt") + $isOnline = adb devices 2> $null + $pattern = [regex]::Escape($instanceIP) + ':\d+\s+device' + if($isOnline -match $pattern){ + break + + } else { return $true} + } + $instanceIP | Out-File -FilePath "C:\Ldplayer\Ldplayer9\restartADB.txt" + $cnt+=1 + start-sleep -milliseconds 1000 +} + diff --git a/closeLDPlayerByName.ps1 b/closeLDPlayerByName.ps1 new file mode 100644 index 0000000..6b97593 --- /dev/null +++ b/closeLDPlayerByName.ps1 @@ -0,0 +1,61 @@ +#run ldplayer instance +param ( + [string]$instanceName +) + +$global:LDPlayerPath ="C:\Ldplayer\LDplayer9" +$global:instanceNameglobal=$instanceName +function logger($logMessage) { + [int]$number = (get-content -path "$global:LDPlayerPath\index_count.txt") + if($number -gt 21){ $number=0} + $instanceList = Get-Content -Path "$global:LDPlayerPath\instanceList.txt" + #$instanceNumber = $instanceList[$number] + #$instanceData = (cat C:\Ldplayer\LDplayer9\vms\config\leidian${instanceNumber}.config) + #$convertedData = $instanceData | ConvertFrom-Json + foreach ( $inst in $instanceList) { + $instanceData = (cat C:\Ldplayer\LDplayer9\vms\config\leidian${inst}.config) + $convertedData = $instanceData | ConvertFrom-Json + $name = $convertedData."statusSettings.playerName" + if ($global:instanceNameglobal -eq $name){ + $count = $inst + break + } + } + #if(-not $instanceName){$name = $convertedData."statusSettings.playerName"} + #$count = $number+1 + + $scriptName = Split-Path -Leaf $PSCommandPath + $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff" + $timestampedLogMessage = $timestamp + " ${global:instanceNameglobal} -instanceID: ${count} -${scriptName} -PID: ${PID}-:" + $logMessage + #$timestampedLogMessage + $logFilePath = "$global:LDPlayerPath\LDAutoLog.txt" + $fs = New-Object System.IO.FileStream($logFilePath, [System.IO.FileMode]::Append, [System.IO.FileAccess]::Write, [System.IO.FileShare]::Read) + $sw = New-Object System.IO.StreamWriter($fs) + try { + $sw.WriteLine($timestampedLogMessage) + } finally { + $sw.Close() + $fs.Close() + } + } + + +$instanceList = Get-Content -Path "$global:LDPlayerPath\instanceList.txt" +foreach ( $inst in $instanceList) { + $filename = "C:\Ldplayer\LDplayer9\vms\config\leidian${inst}.config" + $instanceData = (cat C:\Ldplayer\LDplayer9\vms\config\leidian${inst}.config) + $convertedData = $instanceData | ConvertFrom-Json + $name = $convertedData."statusSettings.playerName" + } + +logger("Closing instance $instanceName") +$launchCMD = "ldconsole.exe quit --name $instanceName" +$launchCMD | Out-File -FilePath "C:\Ldplayer\Ldplayer9\closeName.txt" + + + +<# +$number=$number + 1 +if($number -eq 23){ $number=0} +$number | Out-File -FilePath "c:\ldplayer\ldplayer9\index_count.txt" +#> \ No newline at end of file diff --git a/closeLdplayer.ps1 b/closeLdplayer.ps1 new file mode 100644 index 0000000..efd5ae7 --- /dev/null +++ b/closeLdplayer.ps1 @@ -0,0 +1,38 @@ +#close LDPlayer + +$global:LDPlayerPath ="C:\Ldplayer\LDplayer9" + +function logger($logMessage) { + [int]$number = (get-content -path "$global:LDPlayerPath\index_count.txt") + if($number -gt 21){ $number=0} + $instanceList = Get-Content -Path "$global:LDPlayerPath\instanceList.txt" + $instanceNumber = $instanceList[$number-1] + $instanceData = (cat C:\Ldplayer\LDplayer9\vms\config\leidian${instanceNumber}.config) + $convertedData = $instanceData | ConvertFrom-Json + $name = $convertedData."statusSettings.playerName" + $count = $number+1 + + $scriptName = Split-Path -Leaf $PSCommandPath + $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff" + $timestampedLogMessage = $timestamp + "-$name, ($count/22)-$scriptName-PID: ${PID}-:" + $logMessage + $logFilePath = "$global:LDPlayerPath\LDAutoLog.txt" + $fs = New-Object System.IO.FileStream($logFilePath, [System.IO.FileMode]::Append, [System.IO.FileAccess]::Write, [System.IO.FileShare]::Read) + $sw = New-Object System.IO.StreamWriter($fs) + try { + $sw.WriteLine($timestampedLogMessage) + } finally { + $sw.Close() + $fs.Close() + } + } + +[int]$number = (get-content -path "c:\ldplayer\ldplayer9\index_count.txt") +$instanceList = Get-Content -Path "$global:LDPlayerPath\instanceList.txt" +$instanceNumber = $instanceList[$number-1] +$instanceData = (cat C:\Ldplayer\LDplayer9\vms\config\leidian${instanceNumber}.config) +$convertedData = $instanceData | ConvertFrom-Json +$name = $convertedData."statusSettings.playerName" +$count = $number+1 +logger("Closing instance $instanceNumber, $name, ($count/22)") +$launchCMD = "ldconsole.exe quit --index $instanceNumber" +$launchCMD | Out-File -FilePath "C:\Ldplayer\Ldplayer9\closeNow.txt" \ No newline at end of file