1
mirror of https://github.com/revanced/revanced-patcher synced 2025-09-06 16:38:50 +02:00

Compare commits

...

19 Commits

Author SHA1 Message Date
semantic-release-bot
70ca184cf9 chore(release): 1.0.0-dev.15 [skip ci]
# [1.0.0-dev.15](https://github.com/revanced/revanced-patcher/compare/v1.0.0-dev.14...v1.0.0-dev.15) (2022-05-25)

### Features

* utility functions to get metadata of patch & sigs ([72f16b7](72f16b7785))
2022-05-25 20:55:57 +00:00
Lucaskyy
72f16b7785 feat: utility functions to get metadata of patch & sigs 2022-05-25 22:54:20 +02:00
Lucaskyy
fc03639b26 chore: fix typo 2022-05-25 22:52:57 +02:00
semantic-release-bot
88a85f94e7 chore(release): 1.0.0-dev.14 [skip ci]
# [1.0.0-dev.14](https://github.com/revanced/revanced-patcher/compare/v1.0.0-dev.13...v1.0.0-dev.14) (2022-05-24)

### Bug Fixes

* reformat (trigger release) ([45a167e](45a167e785))
2022-05-24 18:12:32 +00:00
Lucaskyy
45a167e785 fix: reformat (trigger release) 2022-05-24 20:11:04 +02:00
Lucaskyy
699d8abf59 refactor: use apktool fork
also fixed some compilation issues
2022-05-24 17:43:43 +02:00
semantic-release-bot
b58c718699 chore(release): 1.0.0-dev.13 [skip ci]
# [1.0.0-dev.13](https://github.com/revanced/revanced-patcher/compare/v1.0.0-dev.12...v1.0.0-dev.13) (2022-05-24)

### Performance Improvements

* decode manifest only when not using resource patcher ([40b1fa4](40b1fa43e1))
2022-05-24 00:11:23 +00:00
oSumAtrIX
266d6810a9 refactor: use resourceData.get(path) instead of a reader/writer
Signed-off-by: oSumAtrIX <johan.melkonyan1@web.de>
2022-05-24 01:41:48 +02:00
oSumAtrIX
40b1fa43e1 perf: decode manifest only when not using resource patcher
Signed-off-by: oSumAtrIX <johan.melkonyan1@web.de>
2022-05-24 01:28:31 +02:00
oSumAtrIX
94f9594eed chore: update kotlin jvm
Signed-off-by: oSumAtrIX <johan.melkonyan1@web.de>
2022-05-24 00:16:57 +02:00
oSumAtrIX
cff58ab180 refactor: improve ExampleResourcePatch
Signed-off-by: oSumAtrIX <johan.melkonyan1@web.de>
2022-05-23 20:58:03 +02:00
oSumAtrIX
989646b0b5 chore: update dependencies
Signed-off-by: oSumAtrIX <johan.melkonyan1@web.de>
2022-05-23 20:56:17 +02:00
semantic-release-bot
5c3fbaee7a chore(release): 1.0.0-dev.12 [skip ci]
# [1.0.0-dev.12](https://github.com/revanced/revanced-patcher/compare/v1.0.0-dev.11...v1.0.0-dev.12) (2022-05-22)

### Bug Fixes

* using old instance of `Androlib` when saving ([5630e49](5630e49663))
2022-05-22 15:23:09 +00:00
oSumAtrIX
08525e9c26 Merge remote-tracking branch 'origin/dev' into dev 2022-05-22 17:21:50 +02:00
oSumAtrIX
5630e49663 fix: using old instance of Androlib when saving
Signed-off-by: oSumAtrIX <johan.melkonyan1@web.de>
2022-05-22 17:21:18 +02:00
semantic-release-bot
0543122427 chore(release): 1.0.0-dev.11 [skip ci]
# [1.0.0-dev.11](https://github.com/revanced/revanced-patcher/compare/v1.0.0-dev.10...v1.0.0-dev.11) (2022-05-22)

### Features

* `PatchLoader` ([1a99eca](1a99ecaffe))
* use annotations instead of metadata objects ([6726884](6726884be5))
2022-05-22 15:17:31 +00:00
oSumAtrIX
0873703056 Merge pull request #33 from revanced/annotations
feat: use annotations instead of metadata objects
2022-05-22 17:14:48 +02:00
oSumAtrIX
1a99ecaffe feat: PatchLoader
Signed-off-by: oSumAtrIX <johan.melkonyan1@web.de>
2022-05-22 17:12:46 +02:00
oSumAtrIX
6726884be5 feat: use annotations instead of metadata objects
Signed-off-by: oSumAtrIX <johan.melkonyan1@web.de>
2022-05-22 13:46:20 +02:00
62 changed files with 715 additions and 473 deletions

View File

@@ -1,3 +1,39 @@
# [1.0.0-dev.15](https://github.com/revanced/revanced-patcher/compare/v1.0.0-dev.14...v1.0.0-dev.15) (2022-05-25)
### Features
* utility functions to get metadata of patch & sigs ([72f16b7](https://github.com/revanced/revanced-patcher/commit/72f16b778587c28d8f8e91da502f197e7dc35d6d))
# [1.0.0-dev.14](https://github.com/revanced/revanced-patcher/compare/v1.0.0-dev.13...v1.0.0-dev.14) (2022-05-24)
### Bug Fixes
* reformat (trigger release) ([45a167e](https://github.com/revanced/revanced-patcher/commit/45a167e7856da0306f796953775c7b7543d9bec0))
# [1.0.0-dev.13](https://github.com/revanced/revanced-patcher/compare/v1.0.0-dev.12...v1.0.0-dev.13) (2022-05-24)
### Performance Improvements
* decode manifest only when not using resource patcher ([40b1fa4](https://github.com/revanced/revanced-patcher/commit/40b1fa43e1704ace29d3e349df2f4a8ea828c5c2))
# [1.0.0-dev.12](https://github.com/revanced/revanced-patcher/compare/v1.0.0-dev.11...v1.0.0-dev.12) (2022-05-22)
### Bug Fixes
* using old instance of `Androlib` when saving ([5630e49](https://github.com/revanced/revanced-patcher/commit/5630e4966310311cdfd53e2ba128255047626adc))
# [1.0.0-dev.11](https://github.com/revanced/revanced-patcher/compare/v1.0.0-dev.10...v1.0.0-dev.11) (2022-05-22)
### Features
* `PatchLoader` ([1a99eca](https://github.com/revanced/revanced-patcher/commit/1a99ecaffe5e55977655316e68b014fdeba374a1))
* use annotations instead of metadata objects ([6726884](https://github.com/revanced/revanced-patcher/commit/6726884be5af56b6856749e73fb9f4f97559854a))
# [1.0.0-dev.10](https://github.com/revanced/revanced-patcher/compare/v1.0.0-dev.9...v1.0.0-dev.10) (2022-05-07)

View File

@@ -1,5 +1,5 @@
plugins {
kotlin("jvm") version "1.6.20"
kotlin("jvm") version "1.6.21"
java
`maven-publish`
}
@@ -8,15 +8,17 @@ group = "app.revanced"
repositories {
mavenCentral()
maven {
url = uri("https://maven.pkg.github.com/revanced/multidexlib2")
credentials {
// DO NOT set these variables in the project's gradle.properties.
// Instead, you should set them in:
// Windows: %homepath%\.gradle\gradle.properties
// Linux: ~/.gradle/gradle.properties
username = project.findProperty("gpr.user") as String? ?: System.getenv("GITHUB_ACTOR") // DO NOT CHANGE!
password = project.findProperty("gpr.key") as String? ?: System.getenv("GITHUB_TOKEN") // DO NOT CHANGE!
listOf("multidexlib2", "Apktool").forEach { repo ->
maven {
url = uri("https://maven.pkg.github.com/revanced/$repo")
credentials {
// DO NOT set these variables in the project's gradle.properties.
// Instead, you should set them in:
// Windows: %homepath%\.gradle\gradle.properties
// Linux: ~/.gradle/gradle.properties
username = project.findProperty("gpr.user") as String? ?: System.getenv("GITHUB_ACTOR") // DO NOT CHANGE!
password = project.findProperty("gpr.key") as String? ?: System.getenv("GITHUB_TOKEN") // DO NOT CHANGE!
}
}
}
}
@@ -24,11 +26,13 @@ repositories {
dependencies {
implementation(kotlin("stdlib"))
api("org.apktool:apktool-lib:2.6.1")
api("xpp3:xpp3:1.1.4c")
api("org.apktool:apktool-lib:2.6.2-SNAPSHOT")
api("app.revanced:multidexlib2:2.5.2.r2")
api("org.smali:smali:2.5.2")
testImplementation(kotlin("test"))
implementation(kotlin("reflect"))
}
tasks {

View File

@@ -1,2 +1,2 @@
kotlin.code.style = official
version = 1.0.0-dev.10
version = 1.0.0-dev.15

View File

@@ -1,18 +1,25 @@
package app.revanced.patcher
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.data.PatcherData
import app.revanced.patcher.data.base.Data
import app.revanced.patcher.data.implementation.findIndexed
import app.revanced.patcher.extensions.findAnnotationRecursively
import app.revanced.patcher.extensions.nullOutputStream
import app.revanced.patcher.patch.base.Patch
import app.revanced.patcher.patch.implementation.BytecodePatch
import app.revanced.patcher.patch.implementation.ResourcePatch
import app.revanced.patcher.patch.implementation.metadata.PatchMetadata
import app.revanced.patcher.patch.implementation.misc.PatchResultSuccess
import app.revanced.patcher.signature.MethodSignature
import app.revanced.patcher.signature.resolver.SignatureResolver
import app.revanced.patcher.signature.implementation.method.MethodSignature
import app.revanced.patcher.signature.implementation.method.resolver.MethodSignatureResolver
import app.revanced.patcher.util.ListBackedSet
import brut.androlib.Androlib
import brut.androlib.meta.UsesFramework
import brut.androlib.res.AndrolibResources
import brut.androlib.res.data.ResPackage
import brut.androlib.res.decoder.AXmlResourceParser
import brut.androlib.res.decoder.ResAttrDecoder
import brut.androlib.res.decoder.XmlPullStreamDecoder
import brut.directory.ExtFile
import lanchon.multidexlib2.BasicDexFileNamer
import lanchon.multidexlib2.DexIO
@@ -40,33 +47,51 @@ class Patcher(
val packageVersion: String
val packageName: String
private val usesFramework: UsesFramework
private lateinit var usesFramework: UsesFramework
private val patcherData: PatcherData
private val opcodes: Opcodes
private var signaturesResolved = false
private val androlib = Androlib()
init {
val extFileInput = ExtFile(inputFile)
val resourceTable = androlib.getResTable(extFileInput, true)
val outDir = File(resourceCacheDirectory)
if (outDir.exists()) outDir.deleteRecursively()
outDir.mkdir()
// 1. decode resources to cache directory
androlib.decodeManifestWithResources(extFileInput, outDir, resourceTable)
androlib.decodeResourcesFull(extFileInput, outDir, resourceTable)
// load the resource table from the input file
val androlib = Androlib()
val resourceTable = androlib.getResTable(extFileInput, true)
// 2. read framework ids from the resource table
usesFramework = UsesFramework()
usesFramework.ids = resourceTable.listFramePackages().map { it.id }.sorted()
if (patchResources) {
// 1. decode resources to cache directory
androlib.decodeManifestWithResources(extFileInput, outDir, resourceTable)
androlib.decodeResourcesFull(extFileInput, outDir, resourceTable)
// 3. read package info
packageName = resourceTable.packageOriginal
// 2. read framework ids from the resource table
usesFramework = UsesFramework()
usesFramework.ids = resourceTable.listFramePackages().map { it.id }.sorted()
} else {
// create decoder for the resource table
val decoder = ResAttrDecoder()
decoder.currentPackage = ResPackage(resourceTable, 0, null)
// create xml parser with the decoder
val axmlParser = AXmlResourceParser()
axmlParser.attrDecoder = decoder
// parse package information with the decoder and parser which will set required values in the resource table
// instead of decodeManifest another more low level solution can be created to make it faster/better
XmlPullStreamDecoder(
axmlParser, AndrolibResources().resXmlSerializer
).decodeManifest(
extFileInput.directory.getFileInput("AndroidManifest.xml"), nullOutputStream
)
}
// set package information
packageVersion = resourceTable.versionInfo.versionName
packageName = resourceTable.currentResPackage.name
// read dex files
val dexFile = MultiDexIO.readDexFile(true, inputFile, NAMER, null, null)
opcodes = dexFile.opcodes
@@ -82,9 +107,7 @@ class Patcher(
* @param throwOnDuplicates If this is set to true, the patcher will throw an exception if a duplicate class has been found.
*/
fun addFiles(
files: Iterable<File>,
allowedOverwrites: Iterable<String> = emptyList(),
throwOnDuplicates: Boolean = false
files: Iterable<File>, allowedOverwrites: Iterable<String> = emptyList(), throwOnDuplicates: Boolean = false
) {
for (file in files) {
val dexFile = MultiDexIO.readDexFile(true, file, NAMER, null, null)
@@ -123,16 +146,16 @@ class Patcher(
// build modified resources
if (patchResources) {
val extDir = ExtFile(resourceCacheDirectory)
androlib.buildResources(extDir, usesFramework)
// TODO: figure out why a new instance of Androlib is necessary here
Androlib().buildResources(extDir, usesFramework)
}
// write dex modified files
val output = mutableMapOf<String, MemoryDataStore>()
MultiDexIO.writeDexFile(
true, -1, // core count
output, NAMER, newDexFile,
DexIO.DEFAULT_MAX_DEX_POOL_SIZE,
null
output, NAMER, newDexFile, DexIO.DEFAULT_MAX_DEX_POOL_SIZE, null
)
return output
}
@@ -159,11 +182,12 @@ class Patcher(
return emptyList()
}
SignatureResolver(patcherData.bytecodeData.classes.internalClasses, signatures).resolve(patcherData)
MethodSignatureResolver(patcherData.bytecodeData.classes.internalClasses, signatures).resolve(patcherData)
signaturesResolved = true
return signatures
}
/**
* Apply patches loaded into the patcher.
* @param stopOnError If true, the patches will stop on the first error.
@@ -172,9 +196,8 @@ class Patcher(
* If the [Patch] failed to apply, an Exception will always be returned to the wrapping Result object.
*/
fun applyPatches(
stopOnError: Boolean = false,
callback: (String) -> Unit = {}
): Map<PatchMetadata, Result<PatchResultSuccess>> {
stopOnError: Boolean = false, callback: (String) -> Unit = {}
): Map<String, Result<PatchResultSuccess>> {
if (!signaturesResolved) {
resolveSignatures()
}
@@ -183,7 +206,12 @@ class Patcher(
val resourcePatch = patch is ResourcePatch
if (!patchResources && resourcePatch) continue
callback(patch.metadata.shortName)
val patchNameAnnotation = patch::class.java.findAnnotationRecursively(Name::class.java)
patchNameAnnotation?.let {
callback(it.name)
}
val result: Result<PatchResultSuccess> = try {
val data = if (resourcePatch) {
patcherData.resourceData
@@ -201,7 +229,11 @@ class Patcher(
} catch (e: Exception) {
Result.failure(e)
}
this[patch.metadata] = result
patchNameAnnotation?.let {
this[patchNameAnnotation.name] = result
}
if (result.isFailure && stopOnError) break
}
}

View File

@@ -0,0 +1,28 @@
package app.revanced.patcher.annotation
import app.revanced.patcher.patch.base.Patch
import app.revanced.patcher.signature.implementation.method.MethodSignature
/**
* Annotation to constrain a [Patch] or [MethodSignature] to compatible packages.
* @param compatiblePackages A list of packages a [Patch] or [MethodSignature] is compatible with.
*/
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
annotation class Compatibility(
val compatiblePackages: Array<Package>,
)
/**
* Annotation to represent packages a patch can be compatible with.
* @param name The package identifier name.
* @param versions The versions of the package the [Patch] or [MethodSignature]is compatible with.
*/
@Target()
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
annotation class Package(
val name: String,
val versions: Array<String>
)

View File

@@ -0,0 +1,38 @@
package app.revanced.patcher.annotation
import app.revanced.patcher.patch.base.Patch
import app.revanced.patcher.signature.implementation.method.MethodSignature
/**
* Annotation to name a [Patch] or [MethodSignature].
* @param name A suggestive name for the [Patch] or [MethodSignature].
*/
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
annotation class Name(
val name: String,
)
/**
* Annotation to describe a [Patch] or [MethodSignature].
* @param description A description for the [Patch] or [MethodSignature].
*/
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
annotation class Description(
val description: String,
)
/**
* Annotation to version a [Patch] or [MethodSignature].
* @param version The version of a [Patch] or [MethodSignature].
*/
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
annotation class Version(
val version: String,
)

View File

@@ -1,12 +1,11 @@
package app.revanced.patcher.data.implementation
import app.revanced.patcher.data.base.Data
import app.revanced.patcher.methodWalker.MethodWalker
import app.revanced.patcher.patch.base.Patch
import app.revanced.patcher.patch.implementation.BytecodePatch
import app.revanced.patcher.proxy.ClassProxy
import app.revanced.patcher.signature.SignatureResolverResult
import app.revanced.patcher.signature.implementation.method.resolver.SignatureResolverResult
import app.revanced.patcher.util.ProxyBackedClassList
import app.revanced.patcher.util.method.MethodWalker
import org.jf.dexlib2.iface.ClassDef
import org.jf.dexlib2.iface.Method
@@ -28,7 +27,7 @@ class BytecodeData(
* Find a class by a given predicate
* @return A proxy for the first class that matches the predicate
*/
fun findClass(predicate: (ClassDef) -> Boolean): ClassProxy? {
fun findClass(predicate: (ClassDef) -> Boolean): app.revanced.patcher.util.proxy.ClassProxy? {
// if we already proxied the class matching the predicate...
for (patch in patches) {
if (patch !is BytecodePatch) continue
@@ -77,10 +76,10 @@ internal inline fun <T> Iterable<T>.findIndexed(predicate: (T) -> Boolean): Pair
return null
}
fun BytecodeData.proxy(classDef: ClassDef): ClassProxy {
fun BytecodeData.proxy(classDef: ClassDef): app.revanced.patcher.util.proxy.ClassProxy {
var proxy = this.classes.proxies.find { it.immutableClass.type == classDef.type }
if (proxy == null) {
proxy = ClassProxy(classDef)
proxy = app.revanced.patcher.util.proxy.ClassProxy(classDef)
this.classes.proxies.add(proxy)
}
return proxy

View File

@@ -14,8 +14,7 @@ class ResourceData(private val resourceCacheDirectory: File) : Data {
private fun resolve(path: String) = resourceCacheDirectory.resolve(path)
fun forEach(action: (File) -> Unit) = resourceCacheDirectory.walkTopDown().forEach(action)
fun reader(path: String) = resolve(path).reader()
fun writer(path: String) = resolve(path).writer()
fun get(path: String) = resolve(path)
fun replace(path: String, oldValue: String, newValue: String, oldValueIsRegex: Boolean = false) {
// TODO: buffer this somehow

View File

@@ -0,0 +1,32 @@
package app.revanced.patcher.extensions
import app.revanced.patcher.annotation.Compatibility
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.patch.base.Patch
import app.revanced.patcher.signature.implementation.method.MethodSignature
import app.revanced.patcher.signature.implementation.method.annotation.FuzzyPatternScanMethod
import app.revanced.patcher.signature.implementation.method.annotation.MatchingMethod
private inline fun <reified T : Annotation> Any.firstAnnotation() =
this::class.annotations.first { it is T } as T
private inline fun <reified T : Annotation> Any.recursiveAnnotation() =
this::class.java.findAnnotationRecursively(T::class.java)!!
object PatchExtensions {
val Patch<*>.name get() = firstAnnotation<Name>().name
val Patch<*>.version get() = firstAnnotation<Version>().version
val Patch<*>.description get() = firstAnnotation<Description>().description
val Patch<*>.compatiblePackages get() = recursiveAnnotation<Compatibility>().compatiblePackages
}
object MethodSignatureExtensions {
val MethodSignature.name get() = firstAnnotation<Name>().name
val MethodSignature.version get() = firstAnnotation<Version>().version
val MethodSignature.description get() = firstAnnotation<Description>().description
val MethodSignature.compatiblePackages get() = recursiveAnnotation<Compatibility>().compatiblePackages
val MethodSignature.matchingMethod get() = firstAnnotation<MatchingMethod>()
val MethodSignature.fuzzyThreshold get() = firstAnnotation<FuzzyPatternScanMethod>().threshold
}

View File

@@ -1,6 +1,6 @@
package app.revanced.patcher.extensions
import app.revanced.patcher.proxy.mutableTypes.MutableMethod.Companion.toMutable
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod.Companion.toMutable
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.builder.BuilderInstruction
import org.jf.dexlib2.builder.MutableMethodImplementation
@@ -9,6 +9,34 @@ import org.jf.dexlib2.iface.reference.MethodReference
import org.jf.dexlib2.immutable.ImmutableMethod
import org.jf.dexlib2.immutable.ImmutableMethodImplementation
import org.jf.dexlib2.util.MethodUtil
import java.io.OutputStream
/**
* Recursively find a given annotation on a class
* @param targetAnnotation The annotation to find
* @return The annotation
*/
fun <T : Annotation> Class<*>.findAnnotationRecursively(targetAnnotation: Class<T>) =
this.findAnnotationRecursively(targetAnnotation, mutableSetOf())
private fun <T : Annotation> Class<*>.findAnnotationRecursively(
targetAnnotation: Class<T>,
traversed: MutableSet<Annotation>
): T? {
val found = this.annotations.firstOrNull { it.annotationClass.java.name == targetAnnotation.name }
@Suppress("UNCHECKED_CAST")
if (found != null) return found as T
for (annotation in this.annotations) {
if (traversed.contains(annotation)) continue
traversed.add(annotation)
return (annotation.annotationClass.java.findAnnotationRecursively(targetAnnotation, traversed)) ?: continue
}
return null
}
infix fun AccessFlags.or(other: AccessFlags) = this.value or other.value
infix fun Int.or(other: AccessFlags) = this or other.value
@@ -78,4 +106,9 @@ internal fun parametersEqual(
)
}
}
}
}
internal val nullOutputStream: OutputStream =
object : OutputStream() {
override fun write(b: Int) {}
}

View File

@@ -0,0 +1,9 @@
package app.revanced.patcher.patch.annotations
/**
* Annotation to mark a Class as a patch.
*/
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
annotation class Patch

View File

@@ -3,20 +3,16 @@ package app.revanced.patcher.patch.base
import app.revanced.patcher.data.base.Data
import app.revanced.patcher.patch.implementation.BytecodePatch
import app.revanced.patcher.patch.implementation.ResourcePatch
import app.revanced.patcher.patch.implementation.metadata.PatchMetadata
import app.revanced.patcher.patch.implementation.misc.PatchResult
/**
* A ReVanced patch.
* Can either be a [ResourcePatch] or a [BytecodePatch]
* Can either be a [ResourcePatch] or a [BytecodePatch].
*/
abstract class Patch<out T : Data>(
open val metadata: PatchMetadata
) {
abstract class Patch<out T : Data> {
/**
* The main function of the [Patch] which the patcher will call.
*/
abstract fun execute(data: @UnsafeVariance T): PatchResult // FIXME: remove the UnsafeVariance annotation
}

View File

@@ -2,15 +2,12 @@ package app.revanced.patcher.patch.implementation
import app.revanced.patcher.data.implementation.BytecodeData
import app.revanced.patcher.patch.base.Patch
import app.revanced.patcher.patch.implementation.metadata.PatchMetadata
import app.revanced.patcher.signature.MethodSignature
import app.revanced.patcher.signature.implementation.method.MethodSignature
/**
* Bytecode patch for the Patcher.
* @param metadata [PatchMetadata] for the patch.
* @param signatures A list of [MethodSignature] this patch relies on.
*/
abstract class BytecodePatch(
override val metadata: PatchMetadata,
val signatures: Iterable<MethodSignature>
) : Patch<BytecodeData>(metadata)
) : Patch<BytecodeData>()

View File

@@ -2,12 +2,8 @@ package app.revanced.patcher.patch.implementation
import app.revanced.patcher.data.implementation.ResourceData
import app.revanced.patcher.patch.base.Patch
import app.revanced.patcher.patch.implementation.metadata.PatchMetadata
/**
* Resource patch for the Patcher.
* @param metadata [PatchMetadata] for the patch.
*/
abstract class ResourcePatch(
override val metadata: PatchMetadata
) : Patch<ResourceData>(metadata)
abstract class ResourcePatch : Patch<ResourceData>()

View File

@@ -1,29 +0,0 @@
package app.revanced.patcher.patch.implementation.metadata
import app.revanced.patcher.patch.base.Patch
/**
* Metadata about a [Patch].
* @param shortName A suggestive short name for the [Patch].
* @param name A suggestive name for the [Patch].
* @param description A description for the [Patch].
* @param compatiblePackages A list of packages this [Patch] is compatible with.
* @param version The version of the [Patch].
*/
data class PatchMetadata(
val shortName: String,
val name: String,
val description: String,
val compatiblePackages: Iterable<PackageMetadata>,
val version: String,
)
/**
* Metadata about a package.
* @param name The package name.
* @param versions Compatible versions of the package.
*/
data class PackageMetadata(
val name: String,
val versions: Iterable<String>
)

View File

@@ -1,111 +0,0 @@
package app.revanced.patcher.signature
import app.revanced.patcher.data.implementation.MethodNotFoundException
import app.revanced.patcher.patch.implementation.metadata.PackageMetadata
import org.jf.dexlib2.Opcode
/**
* Represents the [MethodSignature] for a method.
* @param metadata Metadata for this [MethodSignature].
* @param returnType The return type of the method.
* @param accessFlags The access flags of the method.
* @param methodParameters The parameters of the method.
* @param opcodes The list of opcodes of the method.
* @param strings A list of strings which a method contains.
* A `null` opcode is equals to an unknown opcode.
*/
class MethodSignature(
val metadata: MethodSignatureMetadata,
internal val returnType: String?,
internal val accessFlags: Int?,
internal val methodParameters: Iterable<String>?,
internal val opcodes: Iterable<Opcode?>?,
internal val strings: Iterable<String>? = null
) {
/**
* The result of the signature
*/
var result: SignatureResolverResult? = null
get() {
return field ?: throw MethodNotFoundException(
"Could not resolve required signature ${metadata.name}"
)
}
val resolved: Boolean
get() {
var resolved = false
try {
resolved = result != null
} catch (_: Exception) {
}
return resolved
}
}
/**
* Metadata about a [MethodSignature].
* @param name A suggestive name for the [MethodSignature].
* @param methodMetadata Metadata about the method for the [MethodSignature].
* @param patternScanMethod The pattern scanning method the pattern scanner should rely on.
* Can either be [PatternScanMethod.Fuzzy] or [PatternScanMethod.Direct].
* @param description An optional description for the [MethodSignature].
* @param compatiblePackages The list of packages the [MethodSignature] is compatible with.
* @param version The version of this signature.
*/
data class MethodSignatureMetadata(
val name: String,
val methodMetadata: MethodMetadata?,
val patternScanMethod: PatternScanMethod,
val compatiblePackages: Iterable<PackageMetadata>,
val description: String?,
val version: String
)
/**
* Metadata about the method for a [MethodSignature].
* @param definingClass The defining class name of the method.
* @param name A suggestive name for the method which the [MethodSignature] was created for.
*/
data class MethodMetadata(
val definingClass: String?,
val name: String?
)
/**
* The method, the patcher should rely on when scanning the opcode pattern of a [MethodSignature]
*/
interface PatternScanMethod {
/**
* When comparing the signature, if one or more of the opcodes do not match, skip.
*/
class Direct : PatternScanMethod
/**
* When comparing the signature, if [threshold] or more of the opcodes do not match, skip.
*/
class Fuzzy(internal val threshold: Int) : PatternScanMethod {
/**
* A list of warnings the resolver found.
*
* This list will be allocated when the signature has been found.
* Meaning, if the signature was not found,
* or the signature was not yet resolved,
* the list will be null.
*/
var warnings: List<Warning>? = null
/**
* Represents a resolver warning.
* @param correctOpcode The opcode the instruction list has.
* @param wrongOpcode The opcode the pattern list of the signature currently has.
* @param instructionIndex The index of the opcode relative to the instruction list.
* @param patternIndex The index of the opcode relative to the pattern list from the signature.
*/
data class Warning(
val correctOpcode: Opcode,
val wrongOpcode: Opcode,
val instructionIndex: Int,
val patternIndex: Int,
)
}
}

View File

@@ -1,48 +0,0 @@
package app.revanced.patcher.signature
import app.revanced.patcher.extensions.softCompareTo
import app.revanced.patcher.proxy.ClassProxy
import app.revanced.patcher.signature.resolver.SignatureResolver
import org.jf.dexlib2.iface.Method
/**
* Represents the result of a [SignatureResolver].
* @param definingClassProxy The [ClassProxy] that the matching method was found in.
* @param resolvedMethod The actual matching method.
* @param scanData Opcodes pattern scan result.
*/
data class SignatureResolverResult(
val definingClassProxy: ClassProxy,
val scanData: PatternScanResult,
private val resolvedMethod: Method,
) {
/**
* Returns the **mutable** method by the [resolvedMethod] from the [definingClassProxy].
*
* Please note, this method allocates a [ClassProxy].
* Use [immutableMethod] where possible.
*/
val method
get() = definingClassProxy.resolve().methods.first {
it.softCompareTo(resolvedMethod)
}
/**
* Returns the **immutable** method by the [resolvedMethod] from the [definingClassProxy].
*
* If you need to modify the method, use [method] instead.
*/
val immutableMethod: Method
get() = definingClassProxy.immutableClass.methods.first {
it.softCompareTo(resolvedMethod)
}
fun findParentMethod(signature: MethodSignature): SignatureResolverResult? {
return SignatureResolver.resolveFromProxy(definingClassProxy, signature)
}
}
data class PatternScanResult(
val startIndex: Int,
val endIndex: Int
)

View File

@@ -0,0 +1,9 @@
package app.revanced.patcher.signature.base
import app.revanced.patcher.signature.implementation.method.MethodSignature
/**
* A ReVanced signature.
* Can be a [MethodSignature].
*/
interface Signature

View File

@@ -0,0 +1,47 @@
package app.revanced.patcher.signature.implementation.method
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.data.implementation.MethodNotFoundException
import app.revanced.patcher.signature.base.Signature
import app.revanced.patcher.signature.implementation.method.resolver.SignatureResolverResult
import org.jf.dexlib2.Opcode
/**
* Represents the [MethodSignature] for a method.
* @param returnType The return type of the method.
* @param accessFlags The access flags of the method.
* @param methodParameters The parameters of the method.
* @param opcodes The list of opcodes of the method.
* @param strings A list of strings which a method contains.
* A `null` opcode is equals to an unknown opcode.
*/
abstract class MethodSignature(
internal val returnType: String?,
internal val accessFlags: Int?,
internal val methodParameters: Iterable<String>?,
internal val opcodes: Iterable<Opcode?>?,
internal val strings: Iterable<String>? = null
) : Signature {
/**
* The result of the signature
*/
var result: SignatureResolverResult? = null
get() {
return field ?: throw MethodNotFoundException(
"Could not resolve required signature ${
(this::class.annotations.find { it is Name }?.let {
(it as Name).name
})
}"
)
}
val resolved: Boolean
get() {
var resolved = false
try {
resolved = result != null
} catch (_: Exception) {
}
return resolved
}
}

View File

@@ -0,0 +1,32 @@
package app.revanced.patcher.signature.implementation.method.annotation
import app.revanced.patcher.signature.implementation.method.MethodSignature
/**
* Annotations for a method which matches to a [MethodSignature].
* @param definingClass The defining class name of the method.
* @param name A suggestive name for the method which the [MethodSignature] was created for.
*/
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class MatchingMethod(
val definingClass: String = "L<unspecified-class>;",
val name: String = "<unspecified-method>"
)
/**
* Annotations to scan a pattern [MethodSignature] with fuzzy algorithm.
* @param threshold if [threshold] or more of the opcodes do not match, skip.
*/
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class FuzzyPatternScanMethod(
val threshold: Int = 1
)
/**
* Annotations to scan a pattern [MethodSignature] directly.
*/
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class DirectPatternScanMethod

View File

@@ -1,13 +1,11 @@
package app.revanced.patcher.signature.resolver
package app.revanced.patcher.signature.implementation.method.resolver
import app.revanced.patcher.data.PatcherData
import app.revanced.patcher.data.implementation.proxy
import app.revanced.patcher.extensions.findAnnotationRecursively
import app.revanced.patcher.extensions.parametersEqual
import app.revanced.patcher.proxy.ClassProxy
import app.revanced.patcher.signature.MethodSignature
import app.revanced.patcher.signature.PatternScanMethod
import app.revanced.patcher.signature.PatternScanResult
import app.revanced.patcher.signature.SignatureResolverResult
import app.revanced.patcher.signature.implementation.method.MethodSignature
import app.revanced.patcher.signature.implementation.method.annotation.FuzzyPatternScanMethod
import org.jf.dexlib2.Opcode
import org.jf.dexlib2.iface.ClassDef
import org.jf.dexlib2.iface.Method
@@ -15,7 +13,7 @@ import org.jf.dexlib2.iface.instruction.Instruction
import org.jf.dexlib2.iface.instruction.formats.Instruction21c
import org.jf.dexlib2.iface.reference.StringReference
internal class SignatureResolver(
internal class MethodSignatureResolver(
private val classes: List<ClassDef>,
private val methodSignatures: Iterable<MethodSignature>
) {
@@ -39,7 +37,10 @@ internal class SignatureResolver(
// These functions do not require the constructor values, so they can be static.
companion object {
fun resolveFromProxy(classProxy: ClassProxy, signature: MethodSignature): SignatureResolverResult? {
fun resolveFromProxy(
classProxy: app.revanced.patcher.util.proxy.ClassProxy,
signature: MethodSignature
): SignatureResolverResult? {
for (method in classProxy.immutableClass.methods) {
val result = compareSignatureToMethod(signature, method) ?: continue
return SignatureResolverResult(
@@ -107,9 +108,10 @@ internal class SignatureResolver(
val count = instructions.count()
val pattern = signature.opcodes!!
val size = pattern.count()
val method = signature.metadata.patternScanMethod
val threshold = if (method is PatternScanMethod.Fuzzy)
method.threshold else 0
val threshold =
signature::class.java.findAnnotationRecursively(FuzzyPatternScanMethod::class.java)?.threshold
?: 0
for (instructionIndex in 0 until count) {
var patternIndex = 0
@@ -126,11 +128,9 @@ internal class SignatureResolver(
patternIndex-- // fix pattern offset
val result = PatternScanResult(instructionIndex, instructionIndex + patternIndex)
if (method is PatternScanMethod.Fuzzy) {
method.warnings = generateWarnings(
signature, instructions, result
)
}
result.warnings = generateWarnings(signature, instructions, result)
return result
}
}
@@ -152,7 +152,7 @@ internal class SignatureResolver(
correctOpcode != patternOpcode
) {
this.add(
PatternScanMethod.Fuzzy.Warning(
PatternScanResult.Warning(
correctOpcode, patternOpcode,
instructionIndex, patternIndex,
)

View File

@@ -0,0 +1,75 @@
package app.revanced.patcher.signature.implementation.method.resolver
import app.revanced.patcher.extensions.softCompareTo
import app.revanced.patcher.signature.implementation.method.MethodSignature
import app.revanced.patcher.util.proxy.ClassProxy
import org.jf.dexlib2.Opcode
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 scanResult Opcodes pattern scan result.
*/
data class SignatureResolverResult(
val definingClassProxy: ClassProxy,
val scanResult: PatternScanResult,
private val resolvedMethod: Method,
) {
/**
* Returns the **mutable** method by the [resolvedMethod] from the [definingClassProxy].
*
* Please note, this method allocates a [ClassProxy].
* Use [immutableMethod] where possible.
*/
val method
get() = definingClassProxy.resolve().methods.first {
it.softCompareTo(resolvedMethod)
}
/**
* Returns the **immutable** method by the [resolvedMethod] from the [definingClassProxy].
*
* If you need to modify the method, use [method] instead.
*/
@Suppress("MemberVisibilityCanBePrivate")
val immutableMethod: Method
get() = definingClassProxy.immutableClass.methods.first {
it.softCompareTo(resolvedMethod)
}
fun findParentMethod(signature: MethodSignature): SignatureResolverResult? {
return MethodSignatureResolver.resolveFromProxy(definingClassProxy, signature)
}
}
data class PatternScanResult(
val startIndex: Int,
val endIndex: Int
) {
/**
* A list of warnings the resolver found.
*
* This list will be allocated when the signature has been found.
* Meaning, if the signature was not found,
* or the signature was not yet resolved,
* the list will be null.
*/
var warnings: List<Warning>? = null
/**
* Represents a resolver warning.
* @param correctOpcode The opcode the instruction list has.
* @param wrongOpcode The opcode the pattern list of the signature currently has.
* @param instructionIndex The index of the opcode relative to the instruction list.
* @param patternIndex The index of the opcode relative to the pattern list from the signature.
*/
data class Warning(
val correctOpcode: Opcode,
val wrongOpcode: Opcode,
val instructionIndex: Int,
val patternIndex: Int,
)
}

View File

@@ -1,6 +1,6 @@
package app.revanced.patcher.util
class ListBackedSet<E>(private val list: MutableList<E>) : MutableSet<E> {
internal class ListBackedSet<E>(private val list: MutableList<E>) : MutableSet<E> {
override val size get() = list.size
override fun add(element: E) = list.add(element)
override fun addAll(elements: Collection<E>) = list.addAll(elements)

View File

@@ -1,16 +1,15 @@
package app.revanced.patcher.util
import app.revanced.patcher.proxy.ClassProxy
import org.jf.dexlib2.iface.ClassDef
class ProxyBackedClassList(internal val internalClasses: MutableList<ClassDef>) : List<ClassDef> {
internal val proxies = mutableListOf<ClassProxy>()
internal val proxies = mutableListOf<app.revanced.patcher.util.proxy.ClassProxy>()
fun add(classDef: ClassDef) {
internalClasses.add(classDef)
}
fun add(classProxy: ClassProxy) {
fun add(classProxy: app.revanced.patcher.util.proxy.ClassProxy) {
proxies.add(classProxy)
}

View File

@@ -1,9 +1,9 @@
package app.revanced.patcher.methodWalker
package app.revanced.patcher.util.method
import app.revanced.patcher.data.implementation.BytecodeData
import app.revanced.patcher.data.implementation.MethodNotFoundException
import app.revanced.patcher.extensions.softCompareTo
import app.revanced.patcher.proxy.mutableTypes.MutableMethod
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
import org.jf.dexlib2.Format
import org.jf.dexlib2.iface.Method
import org.jf.dexlib2.iface.instruction.formats.Instruction35c

View File

@@ -0,0 +1,38 @@
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.name
.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

@@ -1,6 +1,6 @@
package app.revanced.patcher.proxy
package app.revanced.patcher.util.proxy
import app.revanced.patcher.proxy.mutableTypes.MutableClass
import app.revanced.patcher.util.proxy.mutableTypes.MutableClass
import org.jf.dexlib2.iface.ClassDef
/**

View File

@@ -1,6 +1,6 @@
package app.revanced.patcher.proxy.mutableTypes
package app.revanced.patcher.util.proxy.mutableTypes
import app.revanced.patcher.proxy.mutableTypes.MutableAnnotationElement.Companion.toMutable
import app.revanced.patcher.util.proxy.mutableTypes.MutableAnnotationElement.Companion.toMutable
import org.jf.dexlib2.base.BaseAnnotation
import org.jf.dexlib2.iface.Annotation

View File

@@ -1,7 +1,7 @@
package app.revanced.patcher.proxy.mutableTypes
package app.revanced.patcher.util.proxy.mutableTypes
import app.revanced.patcher.proxy.mutableTypes.encodedValue.MutableEncodedValue
import app.revanced.patcher.proxy.mutableTypes.encodedValue.MutableEncodedValue.Companion.toMutable
import app.revanced.patcher.util.proxy.mutableTypes.encodedValue.MutableEncodedValue
import app.revanced.patcher.util.proxy.mutableTypes.encodedValue.MutableEncodedValue.Companion.toMutable
import org.jf.dexlib2.base.BaseAnnotationElement
import org.jf.dexlib2.iface.AnnotationElement
import org.jf.dexlib2.iface.value.EncodedValue

View File

@@ -1,8 +1,8 @@
package app.revanced.patcher.proxy.mutableTypes
package app.revanced.patcher.util.proxy.mutableTypes
import app.revanced.patcher.proxy.mutableTypes.MutableAnnotation.Companion.toMutable
import app.revanced.patcher.proxy.mutableTypes.MutableField.Companion.toMutable
import app.revanced.patcher.proxy.mutableTypes.MutableMethod.Companion.toMutable
import app.revanced.patcher.util.proxy.mutableTypes.MutableAnnotation.Companion.toMutable
import app.revanced.patcher.util.proxy.mutableTypes.MutableField.Companion.toMutable
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod.Companion.toMutable
import com.google.common.collect.Iterables
import org.jf.dexlib2.base.reference.BaseTypeReference
import org.jf.dexlib2.iface.ClassDef

View File

@@ -1,8 +1,8 @@
package app.revanced.patcher.proxy.mutableTypes
package app.revanced.patcher.util.proxy.mutableTypes
import app.revanced.patcher.proxy.mutableTypes.MutableAnnotation.Companion.toMutable
import app.revanced.patcher.proxy.mutableTypes.encodedValue.MutableEncodedValue
import app.revanced.patcher.proxy.mutableTypes.encodedValue.MutableEncodedValue.Companion.toMutable
import app.revanced.patcher.util.proxy.mutableTypes.MutableAnnotation.Companion.toMutable
import app.revanced.patcher.util.proxy.mutableTypes.encodedValue.MutableEncodedValue
import app.revanced.patcher.util.proxy.mutableTypes.encodedValue.MutableEncodedValue.Companion.toMutable
import org.jf.dexlib2.HiddenApiRestriction
import org.jf.dexlib2.base.reference.BaseFieldReference
import org.jf.dexlib2.iface.Field

View File

@@ -1,7 +1,7 @@
package app.revanced.patcher.proxy.mutableTypes
package app.revanced.patcher.util.proxy.mutableTypes
import app.revanced.patcher.proxy.mutableTypes.MutableAnnotation.Companion.toMutable
import app.revanced.patcher.proxy.mutableTypes.MutableMethodParameter.Companion.toMutable
import app.revanced.patcher.util.proxy.mutableTypes.MutableAnnotation.Companion.toMutable
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethodParameter.Companion.toMutable
import org.jf.dexlib2.HiddenApiRestriction
import org.jf.dexlib2.base.reference.BaseMethodReference
import org.jf.dexlib2.builder.MutableMethodImplementation

Some files were not shown because too many files have changed in this diff Show More