Skip to content

Commit

Permalink
refactor: migrate from Signature to Fingerprint
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Not backwards compatible, since a lot of classes where renamed.
  • Loading branch information
oSumAtrIX committed Jun 26, 2022
1 parent c828fa2 commit efa8ea1
Show file tree
Hide file tree
Showing 33 changed files with 428 additions and 418 deletions.
5 changes: 4 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ repositories {
}

dependencies {
api("app.revanced:multidexlib2:2.5.2.r2")
implementation("app.revanced:multidexlib2:2.5.2.r2")

implementation("xpp3:xpp3:1.1.4c")
implementation("org.smali:smali:2.5.2")
Expand All @@ -45,6 +45,7 @@ java {

publishing {
repositories {
if (System.getenv("GITHUB_ACTOR") != null)
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/revanced/revanced-patcher")
Expand All @@ -53,6 +54,8 @@ publishing {
password = System.getenv("GITHUB_TOKEN")
}
}
else
mavenLocal()
}
publications {
register<MavenPublication>("gpr") {
Expand Down
26 changes: 12 additions & 14 deletions src/main/kotlin/app/revanced/patcher/Patcher.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package app.revanced.patcher

import app.revanced.patcher.data.Data
import app.revanced.patcher.data.PackageMetadata
import app.revanced.patcher.data.PatcherData
import app.revanced.patcher.data.base.Data
import app.revanced.patcher.data.implementation.findIndexed
import app.revanced.patcher.data.impl.findIndexed
import app.revanced.patcher.extensions.PatchExtensions.dependencies
import app.revanced.patcher.extensions.PatchExtensions.patchName
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.misc.PatchResult
import app.revanced.patcher.patch.implementation.misc.PatchResultError
import app.revanced.patcher.patch.implementation.misc.PatchResultSuccess
import app.revanced.patcher.signature.implementation.method.resolver.MethodSignatureResolver
import app.revanced.patcher.fingerprint.method.utils.MethodFingerprintUtils.resolve
import app.revanced.patcher.patch.Patch
import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultError
import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.patch.impl.BytecodePatch
import app.revanced.patcher.patch.impl.ResourcePatch
import app.revanced.patcher.util.ListBackedSet
import brut.androlib.Androlib
import brut.androlib.meta.UsesFramework
Expand Down Expand Up @@ -281,10 +280,9 @@ class Patcher(private val options: PatcherOptions) {
val data = if (isResourcePatch) {
data.resourceData
} else {
MethodSignatureResolver(
data.bytecodeData.classes.internalClasses, (patchInstance as BytecodePatch).signatures
).resolve(data)
data.bytecodeData
val bytecodeData = data.bytecodeData
(patchInstance as BytecodePatch).fingerprints.resolve(bytecodeData, bytecodeData.classes.internalClasses)
bytecodeData
}

logger.trace("Executing patch $patchName of type: ${if (isResourcePatch) "resource" else "bytecode"}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package app.revanced.patcher.data
package app.revanced.patcher

import app.revanced.patcher.data.base.Data
import app.revanced.patcher.data.implementation.BytecodeData
import app.revanced.patcher.data.implementation.ResourceData
import app.revanced.patcher.patch.base.Patch
import app.revanced.patcher.data.Data
import app.revanced.patcher.data.PackageMetadata
import app.revanced.patcher.data.impl.BytecodeData
import app.revanced.patcher.data.impl.ResourceData
import app.revanced.patcher.patch.Patch
import org.jf.dexlib2.iface.ClassDef
import java.io.File

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/app/revanced/patcher/PatcherOptions.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package app.revanced.patcher

import app.revanced.patcher.logging.impl.NopLogger
import app.revanced.patcher.logging.Logger
import app.revanced.patcher.logging.impl.NopLogger
import java.io.File

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package app.revanced.patcher.annotation

import app.revanced.patcher.patch.base.Patch
import app.revanced.patcher.signature.implementation.method.MethodSignature
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patcher.patch.Patch

/**
* Annotation to constrain a [Patch] or [MethodSignature] to compatible packages.
* @param compatiblePackages A list of packages a [Patch] or [MethodSignature] is compatible with.
* Annotation to constrain a [Patch] or [MethodFingerprint] to compatible packages.
* @param compatiblePackages A list of packages a [Patch] or [MethodFingerprint] is compatible with.
*/
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
Expand All @@ -17,7 +17,7 @@ annotation class Compatibility(
/**
* 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.
* @param versions The versions of the package the [Patch] or [MethodFingerprint]is compatible with.
*/
@Target()
@Retention(AnnotationRetention.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package app.revanced.patcher.annotation

import app.revanced.patcher.patch.base.Patch
import app.revanced.patcher.signature.implementation.method.MethodSignature
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patcher.patch.Patch

/**
* Annotation to name a [Patch] or [MethodSignature].
* @param name A suggestive name for the [Patch] or [MethodSignature].
* Annotation to name a [Patch] or [MethodFingerprint].
* @param name A suggestive name for the [Patch] or [MethodFingerprint].
*/
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
Expand All @@ -15,8 +15,8 @@ annotation class Name(
)

/**
* Annotation to describe a [Patch] or [MethodSignature].
* @param description A description for the [Patch] or [MethodSignature].
* Annotation to describe a [Patch] or [MethodFingerprint].
* @param description A description for the [Patch] or [MethodFingerprint].
*/
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
Expand All @@ -27,8 +27,8 @@ annotation class Description(


/**
* Annotation to version a [Patch] or [MethodSignature].
* @param version The version of a [Patch] or [MethodSignature].
* Annotation to version a [Patch] or [MethodFingerprint].
* @param version The version of a [Patch] or [MethodFingerprint].
*/
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
Expand Down
9 changes: 9 additions & 0 deletions src/main/kotlin/app/revanced/patcher/data/Data.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package app.revanced.patcher.data

import app.revanced.patcher.data.impl.BytecodeData
import app.revanced.patcher.data.impl.ResourceData

/**
* Constraint interface for [BytecodeData] and [ResourceData]
*/
interface Data
9 changes: 0 additions & 9 deletions src/main/kotlin/app/revanced/patcher/data/base/Data.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app.revanced.patcher.data.implementation
package app.revanced.patcher.data.impl

import app.revanced.patcher.data.base.Data
import app.revanced.patcher.data.Data
import app.revanced.patcher.util.ProxyBackedClassList
import app.revanced.patcher.util.method.MethodWalker
import org.jf.dexlib2.iface.ClassDef
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package app.revanced.patcher.data.implementation
package app.revanced.patcher.data.impl

import app.revanced.patcher.data.base.Data
import app.revanced.patcher.data.Data
import org.w3c.dom.Document
import java.io.Closeable
import java.io.File
import javax.xml.XMLConstants
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.transform.TransformerFactory
import javax.xml.transform.dom.DOMSource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ 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.data.base.Data
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
import app.revanced.patcher.data.Data
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patcher.patch.Patch
import kotlin.reflect.KClass

/**
Expand Down Expand Up @@ -47,13 +45,13 @@ object PatchExtensions {
val Class<out Patch<Data>>.compatiblePackages get() = recursiveAnnotation(Compatibility::class)?.compatiblePackages
}

object MethodSignatureExtensions {
val MethodSignature.name: String
object MethodFingerprintExtensions {
val MethodFingerprint.name: String
get() = javaClass.recursiveAnnotation(Name::class)?.name ?: this.javaClass.simpleName
val MethodSignature.version get() = javaClass.recursiveAnnotation(Version::class)?.version ?: "0.0.1"
val MethodSignature.description get() = javaClass.recursiveAnnotation(Description::class)?.description
val MethodSignature.compatiblePackages get() = javaClass.recursiveAnnotation(Compatibility::class)?.compatiblePackages
val MethodSignature.matchingMethod get() = javaClass.recursiveAnnotation(MatchingMethod::class)
val MethodSignature.fuzzyPatternScanMethod get() = javaClass.recursiveAnnotation(FuzzyPatternScanMethod::class)
val MethodSignature.fuzzyThreshold get() = fuzzyPatternScanMethod?.threshold ?: 0
val MethodFingerprint.version get() = javaClass.recursiveAnnotation(Version::class)?.version ?: "0.0.1"
val MethodFingerprint.description get() = javaClass.recursiveAnnotation(Description::class)?.description
val MethodFingerprint.compatiblePackages get() = javaClass.recursiveAnnotation(Compatibility::class)?.compatiblePackages
val MethodFingerprint.matchingMethod get() = javaClass.recursiveAnnotation(app.revanced.patcher.fingerprint.method.annotation.MatchingMethod::class)
val MethodFingerprint.fuzzyPatternScanMethod get() = javaClass.recursiveAnnotation(app.revanced.patcher.fingerprint.method.annotation.FuzzyPatternScanMethod::class)
val MethodFingerprint.fuzzyScanThreshold get() = fuzzyPatternScanMethod?.threshold ?: 0
}
24 changes: 24 additions & 0 deletions src/main/kotlin/app/revanced/patcher/extensions/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ internal fun Method.clone(
)
}

/**
* Add a smali instruction to the method.
* @param instruction The smali instruction to add.
*/
fun MutableMethod.addInstruction(instruction: String) =
this.implementation!!.addInstruction(instruction.toInstruction(this))

/**
* Add a smali instruction to the method.
* @param index The index to insert the instruction at.
Expand Down Expand Up @@ -152,3 +159,20 @@ internal val nullOutputStream: OutputStream =
object : OutputStream() {
override fun write(b: Int) {}
}

/**
* Should be used to parse a list of parameters represented by their first letter,
* or in the case of arrays prefixed with an unspecified amount of '[' character.
*/
internal fun String.parseParameters(): List<String> {
val parameters = mutableListOf<String>()
var parameter = ""
for (char in this.toCharArray()) {
parameter += char
if (char == '[') continue

parameters.add(parameter)
parameter = ""
}
return parameters
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package app.revanced.patcher.fingerprint

import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint

/**
* A ReVanced fingerprint.
* Can be a [MethodFingerprint].
*/
interface Fingerprint
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package app.revanced.patcher.signature.implementation.method.annotation
package app.revanced.patcher.fingerprint.method.annotation

import app.revanced.patcher.signature.implementation.method.MethodSignature
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint

/**
* Annotations for a method which matches to a [MethodSignature].
* Annotations for a method which matches to a [MethodFingerprint].
* @param definingClass The defining class name of the method.
* @param name A suggestive name for the method which the [MethodSignature] was created for.
* @param name A suggestive name for the method which the [MethodFingerprint] was created for.
*/
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
Expand All @@ -15,7 +15,7 @@ annotation class MatchingMethod(
)

/**
* Annotations to scan a pattern [MethodSignature] with fuzzy algorithm.
* Annotations to scan a pattern [MethodFingerprint] with fuzzy algorithm.
* @param threshold if [threshold] or more of the opcodes do not match, skip.
*/
@Target(AnnotationTarget.CLASS)
Expand All @@ -25,7 +25,7 @@ annotation class FuzzyPatternScanMethod(
)

/**
* Annotations to scan a pattern [MethodSignature] directly.
* Annotations to scan a pattern [MethodFingerprint] directly.
*/
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
Expand Down
Loading

0 comments on commit efa8ea1

Please sign in to comment.