From ac9dc347e358e4a6906624d3da01bb5904a65ec0 Mon Sep 17 00:00:00 2001 From: tom5079 Date: Fri, 22 Apr 2022 16:39:13 +0900 Subject: [PATCH] Pupil-127 Use gg.js directly --- app/build.gradle | 5 +- app/src/main/java/xyz/quaver/pupil/Pupil.kt | 35 -------- .../java/xyz/quaver/pupil/hitomi/common.kt | 86 ++++++++++++++++--- 3 files changed, 75 insertions(+), 51 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 3fb4d63b..d571f371 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -81,6 +81,7 @@ dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.1" implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2" + implementation "org.jetbrains.kotlinx:kotlinx-datetime:0.3.2" implementation "androidx.appcompat:appcompat:1.4.1" implementation "androidx.activity:activity-ktx:1.4.0" @@ -128,10 +129,6 @@ dependencies { implementation "org.jsoup:jsoup:1.14.3" - implementation ("app.cash.zipline:zipline:1.0.0-SNAPSHOT") { - exclude group: "com.squareup.okio", module: "okio" - } - implementation "xyz.quaver:documentfilex:0.7.2" implementation "xyz.quaver:floatingsearchview:1.1.7" diff --git a/app/src/main/java/xyz/quaver/pupil/Pupil.kt b/app/src/main/java/xyz/quaver/pupil/Pupil.kt index cef1a965..bd2ace8b 100644 --- a/app/src/main/java/xyz/quaver/pupil/Pupil.kt +++ b/app/src/main/java/xyz/quaver/pupil/Pupil.kt @@ -30,7 +30,6 @@ import android.util.Log import androidx.appcompat.app.AppCompatDelegate import androidx.core.content.ContextCompat import androidx.preference.PreferenceManager -import app.cash.zipline.QuickJs import com.facebook.imagepipeline.backends.okhttp3.OkHttpImagePipelineConfigFactory import com.github.piasy.biv.BigImageViewer import com.github.piasy.biv.loader.fresco.FrescoImageLoader @@ -77,46 +76,12 @@ val client: OkHttpClient clientHolder = it } -private var version = "" -var runtimeReady = false - private set -lateinit var runtime: QuickJs - class Pupil : Application() { companion object { lateinit var instance: Pupil private set } - init { - CoroutineScope(Dispatchers.IO).launch { - withContext(Dispatchers.Main) { - runtime = QuickJs.create() - } - while (true) { - kotlin.runCatching { - val newVersion = URL("https://tom5079.github.io/PupilSources/hitomi.html.ver").readText() - - if (version != newVersion) { - runtimeReady = false - evaluationContext.cancelChildren() - kotlin.runCatching { - URL("https://tom5079.github.io/PupilSources/assets/js/gg.js").readText() - }.getOrNull()?.also { gg -> - withContext(Dispatchers.Main) { - runtime.evaluate(gg) - version = newVersion - runtimeReady = true - } - } - } - } - - delay(10000) - } - } - } - override fun onCreate() { instance = this diff --git a/app/src/main/java/xyz/quaver/pupil/hitomi/common.kt b/app/src/main/java/xyz/quaver/pupil/hitomi/common.kt index 6753fd4d..6473e125 100644 --- a/app/src/main/java/xyz/quaver/pupil/hitomi/common.kt +++ b/app/src/main/java/xyz/quaver/pupil/hitomi/common.kt @@ -17,16 +17,24 @@ package xyz.quaver.pupil.hitomi import kotlinx.coroutines.* +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock +import kotlinx.datetime.Clock.System.now +import kotlinx.datetime.Instant import kotlinx.serialization.Serializable import kotlinx.serialization.decodeFromString import kotlinx.serialization.json.Json +import okhttp3.Call +import okhttp3.Callback import okhttp3.Request +import okhttp3.Response import xyz.quaver.pupil.client -import xyz.quaver.pupil.runtime -import xyz.quaver.pupil.runtimeReady import java.io.IOException import java.net.URL import java.util.concurrent.Executors +import kotlin.coroutines.resumeWithException +import kotlin.time.Duration.Companion.minutes +import kotlin.time.ExperimentalTime const val protocol = "https:" @@ -131,19 +139,73 @@ const val nozomiextension = ".nozomi" val evaluationContext = Dispatchers.Main + Job() object gg { + private var lastRetrieval: Instant? = null - suspend fun m(g: Int): Int = withContext(evaluationContext) { - while (!runtimeReady) delay(1000) - runtime.evaluate("gg.m($g)").toString().toInt() - } - suspend fun b(): String = withContext(evaluationContext) { - while (!runtimeReady) delay(1000) - runtime.evaluate("gg.b").toString() + private val mutex = Mutex() + + private var mDefault = 0 + private val mMap = mutableMapOf() + + private var b = "" + + @OptIn(ExperimentalTime::class, ExperimentalCoroutinesApi::class) + private suspend fun refresh() = withContext(Dispatchers.IO) { + mutex.withLock { + if (lastRetrieval == null || (lastRetrieval!! + 1.minutes) < now()) { + val ggjs: String = suspendCancellableCoroutine { continuation -> + val call = client.newCall(Request.Builder().url("https://ltn.hitomi.la/gg.js").build()) + + call.enqueue(object: Callback { + override fun onFailure(call: Call, e: IOException) { + if (continuation.isCancelled) return + continuation.resumeWithException(e) + } + + override fun onResponse(call: Call, response: Response) { + if (!call.isCanceled) { + response.body()?.use { + continuation.resume(it.string()) { + call.cancel() + } + } + } + } + }) + + continuation.invokeOnCancellation { + call.cancel() + } + } + + mDefault = Regex("var o = (\\d)").find(ggjs)!!.groupValues[1].toInt() + val o = Regex("o = (\\d); break;").find(ggjs)!!.groupValues[1].toInt() + + mMap.clear() + Regex("case (\\d+):").findAll(ggjs).forEach { + val case = it.groupValues[1].toInt() + mMap[case] = o + } + + b = Regex("b: '(.+)'").find(ggjs)!!.groupValues[1] + + lastRetrieval = now() + } + } } - suspend fun s(h: String): String = withContext(evaluationContext) { - while (!runtimeReady) delay(1000) - runtime.evaluate("gg.s('$h')").toString() + suspend fun m(g: Int): Int { + refresh() + + return mMap[g] ?: mDefault + } + + suspend fun b(): String { + refresh() + return b + } + fun s(h: String): String { + val m = Regex("(..)(.)$").find(h) + return m!!.groupValues.let { it[2]+it[1] }.toInt(16).toString(10) } }