diff --git a/modules/assets/close.png b/modules/assets/close.png index 395604339..c9833d1ea 100644 Binary files a/modules/assets/close.png and b/modules/assets/close.png differ diff --git a/modules/core/src/main/kotlin/com/waicool20/wai2k/script/ScriptComponent.kt b/modules/core/src/main/kotlin/com/waicool20/wai2k/script/ScriptComponent.kt index aff044315..8007f7d97 100644 --- a/modules/core/src/main/kotlin/com/waicool20/wai2k/script/ScriptComponent.kt +++ b/modules/core/src/main/kotlin/com/waicool20/wai2k/script/ScriptComponent.kt @@ -48,23 +48,12 @@ interface ScriptComponent { */ suspend fun waitForLog( str: String, + period: Long = -1, timeout: Long = Long.MAX_VALUE, - fn: suspend () -> Unit = {} + fnMaxIter: Int = Int.MAX_VALUE, + fn: (suspend () -> Unit)? = null ): Boolean { - val job = scriptRunner.sessionScope.launch { - delay(250) - while (coroutineContext.isActive) fn() - } - try { - withTimeout(timeout) { - scriptRunner.logcatListener!!.lines.first { it.contains(str) } - } - return true - } catch (e: TimeoutCancellationException) { - return false - } finally { - job.cancel() - } + return waitForLog(Regex(".*$str.*"), period, timeout, fnMaxIter, fn) } /** @@ -73,22 +62,31 @@ interface ScriptComponent { */ suspend fun waitForLog( regex: Regex, + period: Long = -1, timeout: Long = Long.MAX_VALUE, - fn: suspend () -> Unit = {} + fnMaxIter: Int = Int.MAX_VALUE, + fn: (suspend () -> Unit)? = null ): Boolean { - val job = scriptRunner.sessionScope.launch { - delay(250) - while (coroutineContext.isActive) fn() + val log = scriptRunner.sessionScope.async { + try { + withTimeout(timeout) { + scriptRunner.logcatListener!!.lines.first { regex.matchEntire(it) != null } + } + true + } catch (e: TimeoutCancellationException) { + false + } } - try { - withTimeout(timeout) { - scriptRunner.logcatListener!!.lines.first { regex.matchEntire(it) != null } + val job = scriptRunner.sessionScope.launch { + if (fn == null) return@launch + var i = 0 + while (coroutineContext.isActive && i++ < fnMaxIter) { + fn() + if (period > 0) delay(period) } - return true - } catch (e: TimeoutCancellationException) { - return false - } finally { - job.cancel() } + val b = log.await() + job.cancel() + return b } } diff --git a/modules/core/src/main/kotlin/com/waicool20/wai2k/script/modules/InitModule.kt b/modules/core/src/main/kotlin/com/waicool20/wai2k/script/modules/InitModule.kt index 04af2f06d..884c73bf7 100644 --- a/modules/core/src/main/kotlin/com/waicool20/wai2k/script/modules/InitModule.kt +++ b/modules/core/src/main/kotlin/com/waicool20/wai2k/script/modules/InitModule.kt @@ -21,7 +21,6 @@ package com.waicool20.wai2k.script.modules import com.waicool20.cvauto.core.AnyRegion import com.waicool20.cvauto.core.template.FT -import com.waicool20.cvauto.core.util.isSimilar import com.waicool20.wai2k.game.LogisticsSupport import com.waicool20.wai2k.game.LogisticsSupport.Assignment import com.waicool20.wai2k.game.location.LocationId @@ -34,7 +33,6 @@ import kotlinx.coroutines.async import kotlinx.coroutines.delay import kotlinx.coroutines.isActive import kotlinx.coroutines.launch -import java.awt.Color import java.awt.image.BufferedImage import java.awt.image.RasterFormatException import java.time.Duration @@ -207,13 +205,7 @@ class InitModule(navigator: Navigator) : ScriptModule(navigator) { private suspend fun terminateExistingBattle() { logger.info("Detected ongoing battle, terminating it first") - while (coroutineContext.isActive) { - val capture = region.freeze() - // Check if in transition to map - if (capture.pickColor(50, 1050).isSimilar(Color(16, 16, 16)) && - capture.pickColor(680, 580).isSimilar(Color(222, 223, 74)) - ) break - // Region that contains the word `RESUME` if there's an ongoing battle + waitForLog("地图位置初始化", fnMaxIter = 3) { region.subRegion(1700, 780, 170, 75).click() navigator.checkLogistics() }