From ec9fd15f9b9b9968be7fb5cb384eb8ee2a0c9ba3 Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Sun, 22 May 2022 17:12:46 +0200 Subject: [PATCH] feat: `PatchLoader` Signed-off-by: oSumAtrIX --- .../annotation/MethodSignatureMetadata.kt | 4 +-- .../resolver/MethodSignatureResolverResult.kt | 4 +-- .../patcher/util/patch/PatchLoader.kt | 34 +++++++++++++++++++ .../bytecode/patch/ExampleBytecodePatch.kt | 2 +- 4 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/app/revanced/patcher/util/patch/PatchLoader.kt diff --git a/src/main/kotlin/app/revanced/patcher/signature/implementation/method/annotation/MethodSignatureMetadata.kt b/src/main/kotlin/app/revanced/patcher/signature/implementation/method/annotation/MethodSignatureMetadata.kt index 5529b206..adcd370a 100644 --- a/src/main/kotlin/app/revanced/patcher/signature/implementation/method/annotation/MethodSignatureMetadata.kt +++ b/src/main/kotlin/app/revanced/patcher/signature/implementation/method/annotation/MethodSignatureMetadata.kt @@ -10,8 +10,8 @@ import app.revanced.patcher.signature.implementation.method.MethodSignature @Target(AnnotationTarget.CLASS) @Retention(AnnotationRetention.RUNTIME) annotation class MatchingMethod( - val definingClass: String = "L", - val name: String = "" + val definingClass: String = "L", + val name: String = "" ) /** diff --git a/src/main/kotlin/app/revanced/patcher/signature/implementation/method/resolver/MethodSignatureResolverResult.kt b/src/main/kotlin/app/revanced/patcher/signature/implementation/method/resolver/MethodSignatureResolverResult.kt index 3002930c..0465417f 100644 --- a/src/main/kotlin/app/revanced/patcher/signature/implementation/method/resolver/MethodSignatureResolverResult.kt +++ b/src/main/kotlin/app/revanced/patcher/signature/implementation/method/resolver/MethodSignatureResolverResult.kt @@ -10,11 +10,11 @@ import org.jf.dexlib2.iface.Method * Represents the result of a [MethodSignatureResolver]. * @param definingClassProxy The [ClassProxy] that the matching method was found in. * @param resolvedMethod The actual matching method. - * @param scanData Opcodes pattern scan result. + * @param scanResult Opcodes pattern scan result. */ data class SignatureResolverResult( val definingClassProxy: ClassProxy, - val scanData: PatternScanResult, + val scanResult: PatternScanResult, private val resolvedMethod: Method, ) { /** diff --git a/src/main/kotlin/app/revanced/patcher/util/patch/PatchLoader.kt b/src/main/kotlin/app/revanced/patcher/util/patch/PatchLoader.kt new file mode 100644 index 00000000..b11e4f03 --- /dev/null +++ b/src/main/kotlin/app/revanced/patcher/util/patch/PatchLoader.kt @@ -0,0 +1,34 @@ +package app.revanced.patcher.util.patch + +import app.revanced.patcher.patch.base.Patch +import java.io.File +import java.net.URLClassLoader +import java.util.jar.JarFile + +object PatchLoader { + /** + * This method loads patches from a given jar file containing [Patch]es + * @return the loaded patches represented as a list of [Patch] classes + */ + fun loadFromFile(patchesJar: File) = buildList { + val jarFile = JarFile(patchesJar) + val classLoader = URLClassLoader(arrayOf(patchesJar.toURI().toURL())) + + val entries = jarFile.entries() + while (entries.hasMoreElements()) { + val entry = entries.nextElement() + if (!entry.name.endsWith(".class") || entry.name.contains("$")) continue + + val clazz = classLoader.loadClass(entry.realName.replace('/', '.').replace(".class", "")) + + if (!clazz.isAnnotationPresent(app.revanced.patcher.patch.annotations.Patch::class.java)) continue + + @Suppress("UNCHECKED_CAST") + val patch = clazz as Class> + + // TODO: include declared classes from patch + + this.add(patch) + } + } +} \ No newline at end of file diff --git a/src/test/kotlin/app/revanced/patcher/usage/bytecode/patch/ExampleBytecodePatch.kt b/src/test/kotlin/app/revanced/patcher/usage/bytecode/patch/ExampleBytecodePatch.kt index 1a286951..2d58b9bb 100644 --- a/src/test/kotlin/app/revanced/patcher/usage/bytecode/patch/ExampleBytecodePatch.kt +++ b/src/test/kotlin/app/revanced/patcher/usage/bytecode/patch/ExampleBytecodePatch.kt @@ -55,7 +55,7 @@ class ExampleBytecodePatch : BytecodePatch( // Let's modify it, so it prints "Hello, ReVanced! Editing bytecode." // Get the start index of our opcode pattern. // This will be the index of the instruction with the opcode CONST_STRING. - val startIndex = result.scanData.startIndex + val startIndex = result.scanResult.startIndex implementation.replaceStringAt(startIndex, "Hello, ReVanced! Editing bytecode.")