From 050bfea8f274eaa8477c28886950ed1a5f467c18 Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Sun, 28 Apr 2024 17:16:52 +0800 Subject: [PATCH] Parse favorite categories from user config page --- .../com/hippo/ehviewer/client/EhEngine.kt | 7 +++---- .../client/parser/UserConfigParser.kt | 16 ++++++++++++++++ app/src/main/rust/src/parser/config.rs | 19 +++++++++++++++++++ app/src/main/rust/src/parser/mod.rs | 1 + 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 app/src/main/kotlin/com/hippo/ehviewer/client/parser/UserConfigParser.kt create mode 100644 app/src/main/rust/src/parser/config.rs diff --git a/app/src/main/kotlin/com/hippo/ehviewer/client/EhEngine.kt b/app/src/main/kotlin/com/hippo/ehviewer/client/EhEngine.kt index abefaf9c9..fe92cc552 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/client/EhEngine.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/client/EhEngine.kt @@ -46,6 +46,7 @@ import com.hippo.ehviewer.client.parser.ProfileParser import com.hippo.ehviewer.client.parser.RateGalleryResult import com.hippo.ehviewer.client.parser.TorrentParser import com.hippo.ehviewer.client.parser.TorrentResult +import com.hippo.ehviewer.client.parser.UserConfigParser import com.hippo.ehviewer.client.parser.VoteCommentResult import com.hippo.ehviewer.client.parser.VoteTagParser import com.hippo.ehviewer.dailycheck.showEventNotification @@ -80,8 +81,6 @@ private const val MAX_REQUEST_SIZE = 25 private const val MAX_SEQUENTIAL_REQUESTS = 5 private const val REQUEST_INTERVAL = 5000L -private const val U_CONFIG_TEXT = "Selected Profile" - fun Either.saveParseError(e: Throwable) { val dir = AppConfig.externalParseErrorDir ?: return val file = File(dir, ReadableTime.getFilenamableTime(System.currentTimeMillis()) + ".txt") @@ -197,12 +196,12 @@ object EhEngine { suspend fun getUConfig(url: String = EhUrl.uConfigUrl) { runSuspendCatching { - ehRequest(url).fetchUsingAsText { check(U_CONFIG_TEXT in this) { "Unable to load config from $url!" } } + ehRequest(url).fetchUsingAsByteBuffer(UserConfigParser::parse) }.onFailure { throwable -> // It may get redirected when accessing ex for the first time if (url == EhUrl.URL_UCONFIG_EX) { logcat(throwable) - ehRequest(url).fetchUsingAsText { check(U_CONFIG_TEXT in this) { "Unable to load config from $url!" } } + ehRequest(url).fetchUsingAsByteBuffer(UserConfigParser::parse) } else { throw throwable } diff --git a/app/src/main/kotlin/com/hippo/ehviewer/client/parser/UserConfigParser.kt b/app/src/main/kotlin/com/hippo/ehviewer/client/parser/UserConfigParser.kt new file mode 100644 index 000000000..ca1914661 --- /dev/null +++ b/app/src/main/kotlin/com/hippo/ehviewer/client/parser/UserConfigParser.kt @@ -0,0 +1,16 @@ +package com.hippo.ehviewer.client.parser + +import com.hippo.ehviewer.Settings +import com.hippo.ehviewer.client.exception.ParseException +import java.nio.ByteBuffer + +object UserConfigParser { + private external fun parseFavCat(body: ByteBuffer, size: Int = body.limit()): Int + + fun parse(body: ByteBuffer) = runCatching { + val favCat = unmarshalParsingAs>(body, ::parseFavCat) + Settings.favCat = favCat + }.onFailure { + throw ParseException("Can't parse user config", it) + } +} diff --git a/app/src/main/rust/src/parser/config.rs b/app/src/main/rust/src/parser/config.rs new file mode 100644 index 000000000..787a273cb --- /dev/null +++ b/app/src/main/rust/src/parser/config.rs @@ -0,0 +1,19 @@ +use jni::objects::{JByteBuffer, JClass}; +use jni::sys::jint; +use jni::JNIEnv; +use jni_fn::jni_fn; +use quick_xml::escape::unescape; +use {parse_marshal_inplace, regex}; + +#[no_mangle] +#[allow(non_snake_case)] +#[jni_fn("com.hippo.ehviewer.client.parser.UserConfigParser")] +pub fn parseFavCat(mut env: JNIEnv, _class: JClass, buffer: JByteBuffer, limit: jint) -> jint { + parse_marshal_inplace(&mut env, buffer, limit, |_, body| { + let cat = regex!(">(); + Ok(cat) + }) +} diff --git a/app/src/main/rust/src/parser/mod.rs b/app/src/main/rust/src/parser/mod.rs index 4a0a330a5..b1188dc1d 100644 --- a/app/src/main/rust/src/parser/mod.rs +++ b/app/src/main/rust/src/parser/mod.rs @@ -1,3 +1,4 @@ +mod config; mod fav; mod home; mod list;