pull/2431/head
kunfei 2 years ago
parent 99b676aa12
commit 3b52addea3
  1. 1
      app/src/main/java/io/legado/app/ui/book/import/BaseImportBookActivity.kt
  2. 64
      app/src/main/java/io/legado/app/utils/RegexExtensions.kt

@ -25,7 +25,6 @@ abstract class BaseImportBookActivity<VB : ViewBinding, VM : ViewModel> : VMBase
* 设置书籍保存位置 * 设置书籍保存位置
*/ */
protected suspend fun setBookStorage() = suspendCoroutine { block -> protected suspend fun setBookStorage() = suspendCoroutine { block ->
AppConfig.defaultBookTreeUri = ""
localBookTreeSelectListener = { localBookTreeSelectListener = {
block.resume(it) block.resume(it)
} }

@ -1,59 +1,35 @@
package io.legado.app.utils package io.legado.app.utils
import androidx.core.os.postDelayed
import com.script.SimpleBindings import com.script.SimpleBindings
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
import io.legado.app.exception.RegexTimeoutException import io.legado.app.exception.RegexTimeoutException
import io.legado.app.help.CrashHandler
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
private val handler by lazy { buildMainHandler() }
/** /**
* 带有超时检测的正则替换 * 带有超时检测的正则替换
* 超时重启apk,线程不能强制结束,只能重启apk
*/ */
suspend fun CharSequence.replace(regex: Regex, replacement: String, timeout: Long): String { fun CharSequence.replace(regex: Regex, replacement: String, timeout: Long): String {
val charSequence = this val charSequence = this
return suspendCancellableCoroutine { block -> val startTime = System.currentTimeMillis()
val thread = Thread { val isJs = replacement.startsWith("@js:")
try { val replacement1 = if (isJs) replacement.substring(4) else replacement
if (replacement.startsWith("@js:")) { val pattern = regex.toPattern()
val js = replacement.substring(4) val matcher = pattern.matcher(charSequence)
val pattern = regex.toPattern() val stringBuffer = StringBuffer()
val matcher = pattern.matcher(charSequence) while (matcher.find()) {
val stringBuffer = StringBuffer() if (System.currentTimeMillis() - startTime > timeout) {
while (matcher.find()) { val timeoutMsg = "替换超时,将禁用替换规则"
val bindings = SimpleBindings() throw RegexTimeoutException(timeoutMsg)
bindings["result"] = matcher.group()
val jsResult = AppConst.SCRIPT_ENGINE.eval(js, bindings).toString()
matcher.appendReplacement(stringBuffer, jsResult)
}
matcher.appendTail(stringBuffer)
block.resume(stringBuffer.toString())
} else {
val result = regex.replace(charSequence, replacement)
block.resume(result)
}
} catch (e: Exception) {
block.resumeWithException(e)
}
} }
thread.start() if (isJs) {
handler.postDelayed(timeout) { val bindings = SimpleBindings()
if (thread.isAlive) { bindings["result"] = matcher.group()
runCatching { val jsResult = AppConst.SCRIPT_ENGINE.eval(replacement1, bindings).toString()
@Suppress("DEPRECATION") matcher.appendReplacement(stringBuffer, jsResult)
thread.stop() } else {
} matcher.appendReplacement(stringBuffer, replacement1)
val timeoutMsg = "替换超时,将禁用替换规则"
val exception = RegexTimeoutException(timeoutMsg)
block.cancel(exception)
CrashHandler.saveCrashInfo2File(exception)
}
} }
} }
matcher.appendTail(stringBuffer)
return stringBuffer.toString()
} }

Loading…
Cancel
Save