Skip to content

Commit

Permalink
feat(inspector): Inspect extensions in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
Claudemirovsky committed Feb 14, 2024
1 parent 480f571 commit 301d842
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
17 changes: 12 additions & 5 deletions inspector/src/main/kotlin/inspector/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import eu.kanade.tachiyomi.App
import eu.kanade.tachiyomi.animesource.online.AnimeHttpSource
import inspector.util.AnimeExtension
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
Expand Down Expand Up @@ -47,11 +50,15 @@ suspend fun main(args: Array<String>) {

logger.info { "Found ${extensions.size} extensions" }

val extensionsInfo = extensions.associate {
logger.debug { "Installing $it" }
val (pkgName, sources) = AnimeExtension.installApk(tmpDir) { it.toFile() }
pkgName to sources.map(::SourceJson)
}
val extensionsInfo = coroutineScope {
extensions.map {
async {
logger.debug { "Installing $it" }
val (pkgName, sources) = AnimeExtension.installApk(tmpDir) { it.toFile() }
pkgName to sources.map(::SourceJson)
}
}.awaitAll()
}.toMap()

File(outputPath).writeText(Json.encodeToString(extensionsInfo))
}
Expand Down
2 changes: 1 addition & 1 deletion inspector/src/main/kotlin/inspector/util/AnimeExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ object AnimeExtension {
dex2jar(apkFile, jarFile)

// collect sources from the extension
return packageInfo.packageName to when (val instance = loadExtensionSources(jarFile.absolutePath, className)) {
return packageInfo.packageName to when (val instance = loadExtensionSources(jarFile, className)) {
is AnimeSource -> listOf(instance).filterIsInstance<AnimeHttpSource>()
is AnimeSourceFactory -> instance.createSources().filterIsInstance<AnimeHttpSource>()
else -> throw RuntimeException("Unknown source class type! ${instance.javaClass}")
Expand Down
5 changes: 2 additions & 3 deletions inspector/src/main/kotlin/inspector/util/PackageTools.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import org.w3c.dom.Node
import xyz.nulldev.androidcompat.pm.InstalledPackage.Companion.toList
import xyz.nulldev.androidcompat.pm.toPackageInfo
import java.io.File
import java.net.URL
import java.net.URLClassLoader
import java.nio.file.Files
import javax.xml.parsers.DocumentBuilderFactory
Expand Down Expand Up @@ -107,8 +106,8 @@ object PackageTools {
* loads the extension main class called $className from the jar located at $jarPath
* It may return an instance of AnimeHttpSource or AnimeSourceFactory depending on the extension.
*/
fun loadExtensionSources(jarPath: String, className: String): Any {
val classLoader = URLClassLoader(arrayOf<URL>(URL("file:$jarPath")))
fun loadExtensionSources(jarFile: File, className: String): Any {
val classLoader = URLClassLoader(arrayOf(jarFile.toURI().toURL()))
val classToLoad = Class.forName(className, false, classLoader)
return classToLoad.getDeclaredConstructor().newInstance()
}
Expand Down

0 comments on commit 301d842

Please sign in to comment.