Skip to content

Commit

Permalink
feat: PatchLoader
Browse files Browse the repository at this point in the history
Signed-off-by: oSumAtrIX <johan.melkonyan1@web.de>
  • Loading branch information
oSumAtrIX committed Jun 5, 2022
1 parent d20f7fd commit ec9fd15
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<empty>",
val name: String = "<method>"
val definingClass: String = "L<unspecified-class>",
val name: String = "<unspecified-method>"
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
/**
Expand Down
34 changes: 34 additions & 0 deletions src/main/kotlin/app/revanced/patcher/util/patch/PatchLoader.kt
Original file line number Diff line number Diff line change
@@ -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<Patch<*>>

// TODO: include declared classes from patch

this.add(patch)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down

0 comments on commit ec9fd15

Please sign in to comment.