From 301d842d626018f3b439843ef791e2c177415669 Mon Sep 17 00:00:00 2001 From: Claudemirovsky <63046606+Claudemirovsky@users.noreply.github.com> Date: Wed, 14 Feb 2024 08:00:47 -0300 Subject: [PATCH] feat(inspector): Inspect extensions in parallel --- inspector/src/main/kotlin/inspector/Main.kt | 17 ++++++++++++----- .../kotlin/inspector/util/AnimeExtension.kt | 2 +- .../main/kotlin/inspector/util/PackageTools.kt | 5 ++--- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/inspector/src/main/kotlin/inspector/Main.kt b/inspector/src/main/kotlin/inspector/Main.kt index 55be0e5..03cd558 100644 --- a/inspector/src/main/kotlin/inspector/Main.kt +++ b/inspector/src/main/kotlin/inspector/Main.kt @@ -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 @@ -47,11 +50,15 @@ suspend fun main(args: Array) { 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)) } diff --git a/inspector/src/main/kotlin/inspector/util/AnimeExtension.kt b/inspector/src/main/kotlin/inspector/util/AnimeExtension.kt index 55a27b2..7504759 100644 --- a/inspector/src/main/kotlin/inspector/util/AnimeExtension.kt +++ b/inspector/src/main/kotlin/inspector/util/AnimeExtension.kt @@ -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() is AnimeSourceFactory -> instance.createSources().filterIsInstance() else -> throw RuntimeException("Unknown source class type! ${instance.javaClass}") diff --git a/inspector/src/main/kotlin/inspector/util/PackageTools.kt b/inspector/src/main/kotlin/inspector/util/PackageTools.kt index 198932e..afa38a4 100644 --- a/inspector/src/main/kotlin/inspector/util/PackageTools.kt +++ b/inspector/src/main/kotlin/inspector/util/PackageTools.kt @@ -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 @@ -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("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() }