Refine rust jni parseLimit

This commit is contained in:
tarsin committed 2023-06-15 12:22:30 +08:00
1 parent b7d2e4e5da
commit 1e47170c3b
4 files changed
+30 -20

No files matched your search

+1
View File
@@ -15,6 +15,7 @@
-keep class com.hippo.ehviewer.client.parser.Torrent { *; }
-keep class com.hippo.ehviewer.client.parser.TorrentResult { *; }
-keep class com.hippo.ehviewer.client.parser.Limits { *; }
-keepattributes LineNumberTable,SourceFile
-renamesourcefileattribute SourceFile
@@ -36,6 +36,7 @@ import com.hippo.ehviewer.client.parser.GalleryPageApiParser
import com.hippo.ehviewer.client.parser.GalleryPageParser
import com.hippo.ehviewer.client.parser.GalleryTokenApiParser
import com.hippo.ehviewer.client.parser.HomeParser
import com.hippo.ehviewer.client.parser.Limits
import com.hippo.ehviewer.client.parser.ProfileParser
import com.hippo.ehviewer.client.parser.RateGalleryParser
import com.hippo.ehviewer.client.parser.SignInParser
@@ -301,7 +302,7 @@ object EhEngine {
return null
}
suspend fun resetImageLimits(): HomeParser.Limits? {
suspend fun resetImageLimits(): Limits? {
return ehRequest(EhUrl.URL_HOME) {
formBody {
add("act", "limits")
@@ -8,11 +8,7 @@ object HomeParser {
private val PATTERN_FUNDS = Regex("Available: ([\\d,]+) Credits.*Available: ([\\d,]+) kGP", RegexOption.DOT_MATCHES_ALL)
private const val RESET_SUCCEED = "Image limit was successfully reset."
fun parse(body: String): Limits {
val value = parseLimit(body)
if (value.isEmpty()) throw ParseException("Parse image limits error", body)
return Limits(value[0], value[1], value[2])
}
fun parse(body: String) = parseLimit(body)
fun parseResetLimits(body: String): Limits? {
return if (body.contains(RESET_SUCCEED)) {
@@ -31,12 +27,12 @@ object HomeParser {
throw ParseException("Parse funds error", body)
}
@Parcelize
data class Limits(val current: Int = 0, val maximum: Int = 0, val resetCost: Int = 0) : Parcelable
@Parcelize
data class Funds(val fundsGP: Int, val fundsC: Int) : Parcelable
class Result(val limits: Limits, val funds: Funds)
}
private external fun parseLimit(body: String): IntArray
@Parcelize
data class Limits(val current: Int, val maximum: Int, val resetCost: Int) : Parcelable
private external fun parseLimit(body: String): Limits
+22 -10
View File
@@ -48,7 +48,16 @@ pub struct TorrentResult {
list: Vec<Torrent>,
}
fn parse_jni_string<F, R>(env: &mut JNIEnv, str: &JString, mut f: F) -> Option<R>
#[derive(Default, IntoJava)]
#[allow(non_snake_case)]
#[jnix(package = "com.hippo.ehviewer.client.parser")]
pub struct Limits {
current: i32,
maximum: i32,
resetCost: i32,
}
fn parse_jni_string<F, R>(env: &JNIEnv, str: &JString, mut f: F) -> Option<R>
where
F: FnMut(&VDom, &Parser, &JNIEnv) -> Option<R>,
{
@@ -62,22 +71,25 @@ where
#[catch_panic(default = "std::ptr::null_mut()")]
#[allow(non_snake_case)]
#[jni_fn("com.hippo.ehviewer.client.parser.HomeParserKt")]
pub fn parseLimit(mut env: JNIEnv, _class: JClass, input: JString) -> jintArray {
pub fn parseLimit(mut env: JNIEnv, _class: JClass, input: JString) -> jobject {
let mut env = JnixEnv::from(env);
let vec = parse_jni_string(&mut env, &input, |dom, parser, _env| {
let iter = dom.query_selector("strong")?;
let vec: Vec<i32> = iter
.filter_map(|e| Some(e.get(parser)?.inner_text(parser).parse::<i32>().ok()?))
.collect();
if vec.len() == 3 {
Some(vec)
Some(Limits {
current: vec[0],
maximum: vec[1],
resetCost: vec[2],
})
} else {
None
}
})
.unwrap_or(vec![]);
env.new_int_array(3)
.unwrap()
.also(|it| env.set_int_array_region(*it, 0, &vec).unwrap())
.unwrap();
vec.into_java(&env).forget().into_inner()
}
#[no_mangle]
@@ -124,8 +136,8 @@ pub fn parseFav(mut env: JNIEnv, _class: JClass, input: JString, str: jobjectArr
#[catch_panic(default = "std::ptr::null_mut()")]
#[allow(non_snake_case)]
#[jni_fn("com.hippo.ehviewer.client.parser.TorrentParserKt")]
pub fn parseTorrent(mut env: JNIEnv, _class: JClass, input: JString) -> jobject {
let env2 = JnixEnv::from(env);
pub fn parseTorrent(env: JNIEnv, _class: JClass, input: JString) -> jobject {
let mut env = JnixEnv::from(env);
parse_jni_string(&mut env, &input, |dom, parser, _env| {
Some(TorrentResult {
list: dom.query_selector("table")?.filter_map(|e| {
@@ -136,7 +148,7 @@ pub fn parseTorrent(mut env: JNIEnv, _class: JClass, input: JString) -> jobject
Some(Torrent { posted: grp[1].to_string(), size: grp[2].to_string(), seeds: grp[3].parse().ok()?, peers: grp[4].parse().ok()?, downloads: grp[5].parse().ok()?, url: grp[7].to_string(), name: name.to_string() })
}).collect()
})
}).unwrap().into_java(&env2).forget().into_inner()
}).unwrap().into_java(&env).forget().into_inner()
}
#[no_mangle]