Skip to content

Commit

Permalink
Fix MangaDex and Other Sources (#715)
Browse files Browse the repository at this point in the history
  • Loading branch information
Syer10 authored Oct 16, 2023
1 parent 682c364 commit 2cf9a40
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

/**
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,6 @@ class OpAnd(var op: Op<Boolean>? = null) {
value: String?,
column: Column<String?>,
) = andWhere(value) { column like it }

}

fun <T : Comparable<T>> andFilterWithCompare(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ object BytecodeEditor {
*/
private fun String?.replaceDirectly() =
when (this) {
null -> this
null -> null
in classesToReplace -> "$REPLACEMENT_PATH/$this"
else -> this
}
Expand All @@ -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
*/
Expand Down Expand Up @@ -226,7 +252,7 @@ object BytecodeEditor {
super.visitMethodInsn(
opcode,
owner.replaceDirectly(),
name,
name.replaceMethodIndirectly(owner),
desc.replaceIndirectly(),
itf,
)
Expand Down

0 comments on commit 2cf9a40

Please sign in to comment.