From 2cf9a407e80863fc12a4202490b19142264b7c2f Mon Sep 17 00:00:00 2001 From: Mitchell Syer Date: Sun, 15 Oct 2023 21:50:30 -0400 Subject: [PATCH] Fix MangaDex and Other Sources (#715) --- gradle/libs.versions.toml | 2 +- .../tachiyomi/source/online/HttpSource.kt | 6 ++- .../graphql/queries/filter/Filter.kt | 1 - .../manga/impl/util/BytecodeEditor.kt | 46 +++++++++++++++---- 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 8085822d..ce8bc113 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -137,7 +137,7 @@ twelvemonkeys-imageio-jpeg = { module = "com.twelvemonkeys.imageio:imageio-jpeg" twelvemonkeys-imageio-webp = { module = "com.twelvemonkeys.imageio:imageio-webp", version.ref = "twelvemonkeys" } # Testing -mockk = "io.mockk:mockk:1.13.8" +mockk = "io.mockk:mockk:1.13.7" # cron scheduler cron4j = "it.sauronsoftware.cron4j:cron4j:2.2.5" diff --git a/server/src/main/kotlin/eu/kanade/tachiyomi/source/online/HttpSource.kt b/server/src/main/kotlin/eu/kanade/tachiyomi/source/online/HttpSource.kt index 394577e5..55252757 100644 --- a/server/src/main/kotlin/eu/kanade/tachiyomi/source/online/HttpSource.kt +++ b/server/src/main/kotlin/eu/kanade/tachiyomi/source/online/HttpSource.kt @@ -68,7 +68,7 @@ abstract class HttpSource : CatalogueSource { get() = network.client private fun generateId(): Long { - return generateId(name, lang, versionId) + return generateId("${name.lowercase()}/$lang/$versionId") } /** @@ -94,6 +94,10 @@ abstract class HttpSource : CatalogueSource { versionId: Int, ): Long { val key = "${name.lowercase()}/$lang/$versionId" + return generateId(key) + } + + private fun generateId(key: String): Long { val bytes = MessageDigest.getInstance("MD5").digest(key.toByteArray()) return (0..7).map { bytes[it].toLong() and 0xff shl 8 * (7 - it) }.reduce(Long::or) and Long.MAX_VALUE } diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/filter/Filter.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/filter/Filter.kt index a30d2aa1..2deac646 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/filter/Filter.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/filter/Filter.kt @@ -416,7 +416,6 @@ class OpAnd(var op: Op? = null) { value: String?, column: Column, ) = andWhere(value) { column like it } - } fun > andFilterWithCompare( diff --git a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/util/BytecodeEditor.kt b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/util/BytecodeEditor.kt index 2ccabce0..c5648b98 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/util/BytecodeEditor.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/util/BytecodeEditor.kt @@ -99,7 +99,7 @@ object BytecodeEditor { */ private fun String?.replaceDirectly() = when (this) { - null -> this + null -> null in classesToReplace -> "$REPLACEMENT_PATH/$this" else -> this } @@ -108,24 +108,50 @@ object BytecodeEditor { * Replace references to the class, used in places that have * other text around the class references * - * @return [String] with class references replaced, - * or null if [String] was null + * @return [String] with class references replaced, or null if [String] was null */ private fun String?.replaceIndirectly(): String? { - var classReference = this - if (classReference != null) { - classesToReplace.forEach { - classReference = classReference?.replace(it, "$REPLACEMENT_PATH/$it") - } + if (this == null) return null + var classReference: String = this + classesToReplace.forEach { + classReference = classReference.replace(it, "$REPLACEMENT_PATH/$it") } return classReference } + /** + * List of methods that will be fixed. Remove once https://github.com/ThexXTURBOXx/dex2jar/issues/27 + * is fixed. + */ + private val methodsToFix = + mapOf( + ("kotlin/time/Duration" to "getInWholeMilliseconds_impl") to "getInWholeMilliseconds-impl", + ) + + /** + * Replace references to the method, used in places that have + * other text around the class references + * + * @param clazz Class the method is in + * + * @return [String] with method reference replaced, or null if [String] or [clazz] was null + */ + private fun String?.replaceMethodIndirectly(clazz: String?): String? { + if (clazz == null || this == null) return this + var method: String = this + methodsToFix.forEach { + if (clazz == it.key.first) { + method = method.replace(it.key.second, it.value) + } + } + return method + } + /** * Replace all references to certain classes inside the class file * with ones that behave more like Androids * - * @param classfileBuffer Class bytecode to load into ASM for ease of modification + * @param pair Class bytecode to load into ASM for ease of modification * * @return [ByteArray] with modified bytecode */ @@ -226,7 +252,7 @@ object BytecodeEditor { super.visitMethodInsn( opcode, owner.replaceDirectly(), - name, + name.replaceMethodIndirectly(owner), desc.replaceIndirectly(), itf, )