Files
adbAutomation/waitUntilThisAppears.ps1
2025-12-02 18:19:49 +00:00

257 lines
9.7 KiB
PowerShell

param (
[Parameter(Mandatory=$true)]
[string]$operator,
[Parameter(Mandatory=$false)]
[string]$timeout,
[Parameter(Mandatory=$false)]
[string[]]$repeatActions,
[Parameter(Mandatory=$false)]
[int]$repetitions,
[Parameter(Mandatory=$false)]
[string]$actionArgs,
[Parameter(Mandatory=$false)]
[string]$instanceIP,
[Parameter(Mandatory=$true)]
[string[]]$conditions
)
if(-not $instanceIP){$instanceIP = "10.0.211.121"}
$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 Fetch-ScreenShot(){
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
}
function Get-ScreenshotData($text) {
$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
$midX =[math]::Round($x1+$width/2)
$midY =[math]::Round($y1+$height/2)
[PSCustomObject]@{
Text = $_.text
Coords = "$midX $midY"
}
}
$Element = $Data | Where-Object {$_.Text -and $_.Text.ToLower() -eq $text}
$coordsForText=$Element.Coords
return $coordsForText
}
function Get-ScreenData() {
$maxDumpRetries =20
$dumpRetryCount = 0
for ($attempt = 1; $attempt -le 3; $attempt++) {
try {
logger("Capturando hierarquia da UI (Tentativa $attempt)...")
Start-Sleep -Milliseconds 250
$cnt = 0
logger("$dumpOutput")
while($dumpRetryCount -lt $maxDumpRetries){
if($cnt -gt 50){break}
logger("tentando fazer dump")
$dumpOutput = adb -s $instanceIP shell uiautomator dump 2> $null
logger("$dumpOutput é saida do commando dump")
if($dumpOutput -Like "*dumped*") {
logger("Comando 'uiautomator dump' deu successo. Saindo do loop")
break}
tskill adb *> $null
logger("deu kill-server, pois deu erro o dump")
adb start-server *> $null
while($dumpRetryCount -lt $maxDumpRetries){
$connected = adb connect $instanceIP 2> $null
logger("$connected")
if($connected -like "*connected*"){break}else{logger"falhou, tenando connectar de novo"}
}
logger("deu connect")
$cnt+=1
}
if ($dumpOutput -match 'dumped to: (/[\w/.-]+\.xml)') {
$dumpPath = $Matches[1].Trim()
$fileName = $dumpPath.Split('/')[2]
} else {
logger("Não foi possível encontrar o caminho do dump XML. Tentando novamente...")
Start-Sleep -Seconds 1; continue
}
if (Test-Path ".\$fileName") { Remove-Item ".\$fileName" }
$fileNameLocal = "C:\LDPlayer\LDPlayer9\screen.xml"
logger("começando baixar o arquivo $fileName")
while($true){
$fetchXML=adb -s $instanceIP pull $dumpPath $fileNameLocal 2>null
logger($fetchXML)
if($fetchXML -notLike "*1 file pulled*"){logger("error found");continue}else{logger("sucess");break}
logger("done")
}
if ((Test-Path "$fileNameLocal") -and (Get-Item "$fileNameLocal").Length -gt 0) {
$xmlContent = Get-Content -Raw -Path $fileNameLocal -Encoding UTF8
return
<#return [PSCustomObject]@{
Xml = [xml]$xmlContent
Hash = (Get-FileHash -Algorithm MD5 -InputStream ([System.IO.MemoryStream]::new([System.Text.Encoding]::UTF8.GetBytes($xmlContent)))).Hash
}#>
}
} catch { logger("Exceção na tentativa $attempt. $_");
Start-Sleep -Seconds 1
}
}
logger("Falha crítica ao capturar a UI após 3 tentativas.")
return $null
}
$timeoutSeconds = $timeout
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
if($timeout){logger("timetout is $timeoutSeconds")
if($repeatActions){logger("repeat action $repeatActions")}else{logger("no repeat action")}
}else{logger("no timeout")}
$actionCount = $repeatActions.Count
$repetitions
$numberOfRepeats=0
while($true) {
logger("waiting for condition: $allConditions to appear/disapear")
if($timeoutSeconds -ge 0){
if($stopwatch.Elapsed.TotalSeconds -ge $timeoutSeconds) {
if(($repeatActions) -and ($numberOfRepeats -gt $repetitions))
{
logger("timeout reached, repeating action $repeatActions")
start-sleep -seconds 2
if($actionArgs -gt 0){
for($i = 0; $i -lt $actionCount; $i += 2){
& "$global:LDPlayerPath\actions.ps1" -action $repeatActions[$i], $repeatActions[$i+1]
}
} else {
for($i = 0; $i -lt $actionCount; $i+= 2){
& "$global:LDPlayerPath\actions.ps1" -action $repeatActions[$i], $repeatActions[$i+1]}
}
$stopwatch.Reset()
$numberOfRepeats+=1
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
continue
} else
{
if(!$repeatActions){logger("timeout reached, breaking loop, but no conditions satisfied")}
else {logger("finished repeating $repetions times, but no conditions satisfied, so breaking loop to avoid AD INFINITUM")}
break
}
}
}
Get-screenData
$xml = (get-content -path C:\LDPlayer\LDPlayer9\screen.xml)
if (-not $conditions) { return $false }
if (-not $operator) {return $false}
$allConditions = $conditions
switch ($operator) {
"OR" {
logger("OR operator selected to find any of these: $allConditions")
foreach ($condition in $allConditions) { if ($xml | select-string $condition) {
$textInfo = (Get-Culture).TextInfo
$titleCaseString = $textInfo.ToTitleCase($condition)
$conditionMet = $titleCaseString -replace '\s', ''
logger("$conditionMet")
logger("CONDITION MET, BREAKING LOOP and stopping wait")
return $conditionMet
}
}
logger("condition NOT met YET, continuing loop")
continue
}
"AND" {
$allMet = $true
foreach ($condition in $allConditions) { if (-not ($xml | select-string $condition)) {
$allMet = $false
break
}
}
if($allMet){
return $true
}
continue
}
"NAND" {
logger("NAND operator selected to return true if any one of these are NOT present: $allConditions")
foreach ($condition in $allConditions) {
if (-not ($xml | select-string $condition)) {
logger("at least $condition is dissapeared, so returning TRUE and breaking wait loop since NAND operator was specified")
return $true
}
}
logger("All conditions are present, so continuing wait loop since NAND operator was specified")
continue
}
"NOR" {
logger("NOR operator selected to return TRUE only when all of the conditions are absent: $allConditions")
$allAbsent = $true
foreach($condition in $allConditions){
if($xml | Select-String $condition) {
$allAbsent =$false
logger("$condition is present so continuing wait loop since all have to be absent")
break
}
}
if($allAbsent){
logger("None of the conditions are present so returning TRUE and breaking wait loop (since neither this NOR that condition was present");return $true
}
}
}
Start-Sleep -Milliseconds 500
}