feat: PatchLoader

Signed-off-by: oSumAtrIX <johan.melkonyan1@web.de>
This commit is contained in:
oSumAtrIX 2022-05-22 17:12:46 +02:00
parent d20f7fd6e1
commit ec9fd15f9b
No known key found for this signature in database
GPG key ID: A9B3094ACDB604B4
4 changed files with 39 additions and 5 deletions

View file

@ -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>"
)
/**

View file

@ -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,
) {
/**

View file

@ -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)
}
}
}

View file

@ -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.")