Skip to content

Commit

Permalink
fix: migrate to latest patcher api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed May 31, 2022
1 parent 735dbc9 commit ace70e4
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 79 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ repositories {

dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.6.21")
implementation("app.revanced:revanced-patcher:1.0.0-dev.16")
implementation("app.revanced:revanced-patcher:1.0.0-dev.17")

implementation("info.picocli:picocli:4.6.3")

Expand Down
23 changes: 10 additions & 13 deletions src/main/kotlin/app/revanced/cli/MainCommand.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package app.revanced.cli

import app.revanced.patcher.PatcherOptions
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.extensions.findAnnotationRecursively
import app.revanced.patcher.util.patch.implementation.JarPatchBundle
import app.revanced.utils.adb.Adb
import app.revanced.utils.patcher.addPatchesFiltered
Expand Down Expand Up @@ -54,21 +54,18 @@ internal object MainCommand : Runnable {

override fun run() {
if (listOnly) {
for (patchBundlePath in patchBundles)
for (it in JarPatchBundle(patchBundlePath).loadPatches())
println(
"[available] ${
it.findAnnotationRecursively(
Name::class.java
)?.name ?: it::class.java.name
}"
)
for (patchBundlePath in patchBundles) for (it in JarPatchBundle(patchBundlePath).loadPatches()) {

// TODO: adjust extension methods to be able to do this
val name = (it.annotations.find { it is Name } as? Name)?.name ?: it.simpleName
println(
"[available] $name"
)
}
return
}

val patcher = app.revanced.patcher.Patcher(
inputFile, cacheDirectory, patchResources
)
val patcher = app.revanced.patcher.Patcher(PatcherOptions(inputFile, cacheDirectory, patchResources))

if (signatureCheck) {
patcher.addPatchesFiltered()
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/app/revanced/cli/Patcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ internal class Patcher {
}

if (MainCommand.patchResources) {
for (file in File(MainCommand.cacheDirectory).resolve("build/").listFiles(FileFilter { it.isDirectory })?.first()?.listFiles()!!) {
for (file in File(MainCommand.cacheDirectory).resolve("build/").listFiles(FileFilter { it.isDirectory })
?.first()?.listFiles()!!) {
if (!file.isDirectory) {
zipFileSystem.replaceFile(file.name, file.readBytes())
continue
Expand Down
40 changes: 13 additions & 27 deletions src/main/kotlin/app/revanced/utils/patcher/Patcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,23 @@ package app.revanced.utils.patcher

import app.revanced.cli.MainCommand
import app.revanced.patcher.Patcher
import app.revanced.patcher.annotation.Compatibility
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.data.base.Data
import app.revanced.patcher.extensions.findAnnotationRecursively
import app.revanced.patcher.extensions.PatchExtensions.compatiblePackages
import app.revanced.patcher.extensions.PatchExtensions.patchName
import app.revanced.patcher.patch.base.Patch
import app.revanced.patcher.util.patch.implementation.JarPatchBundle

fun Patcher.addPatchesFiltered(
packageCompatibilityFilter: Boolean = true,
packageVersionCompatibilityFilter: Boolean = true,
includeFilter: Boolean = false
) {
val packageName = this.packageName
val packageVersion = this.packageVersion

MainCommand.patchBundles.forEach { bundle ->
val includedPatches = mutableListOf<Patch<Data>>()
JarPatchBundle(bundle).loadPatches().forEach patch@{ p ->
val patch = p.getDeclaredConstructor().newInstance()

val compatibilityAnnotation = patch.javaClass.findAnnotationRecursively(Compatibility::class.java)

val patchName = patch.javaClass.findAnnotationRecursively(Name::class.java)?.name ?: patch.javaClass.name
val includedPatches = mutableListOf<Class<out Patch<Data>>>()
JarPatchBundle(bundle).loadPatches().forEach patch@{ patch ->
val compatiblePackages = patch.compatiblePackages
val patchName = patch.patchName

val prefix = "[skipped] $patchName"

Expand All @@ -33,24 +27,16 @@ fun Patcher.addPatchesFiltered(
return@patch
}

if (packageVersionCompatibilityFilter || packageCompatibilityFilter) {

if (compatibilityAnnotation == null) {
println("$prefix: Missing compatibility annotation.")
if (compatiblePackages == null) println("$prefix: Missing compatibility annotation. Continuing.")
else compatiblePackages.forEach { compatiblePackage ->
if (compatiblePackage.name != packageName) {
println("$prefix: Package name not matching ${compatiblePackage.name}.")
return@patch
}


compatibilityAnnotation.compatiblePackages.forEach { compatiblePackage ->
if (packageCompatibilityFilter && compatiblePackage.name != packageName) {
println("$prefix: Package name not matching ${compatiblePackage.name}.")
return@patch
}

if (packageVersionCompatibilityFilter && !compatiblePackage.versions.any { it == packageVersion }) {
println("$prefix: Unsupported version.")
return@patch
}
if (!compatiblePackage.versions.any { it == packageVersion }) {
println("$prefix: Unsupported version.")
return@patch
}
}

Expand Down
71 changes: 34 additions & 37 deletions src/main/kotlin/app/revanced/utils/signature/Signature.kt
Original file line number Diff line number Diff line change
@@ -1,55 +1,52 @@
package app.revanced.utils.signature

import app.revanced.patcher.Patcher
import app.revanced.patcher.extensions.findAnnotationRecursively
import app.revanced.patcher.signature.implementation.method.annotation.FuzzyPatternScanMethod
import app.revanced.patcher.signature.implementation.method.annotation.MatchingMethod
import org.jf.dexlib2.iface.Method

object Signature {

fun checkSignatures(patcher: Patcher) {
TODO()
/**
val failed = mutableListOf<String>()
for (signature in patcher.resolveSignatures()) {
val signatureClass = signature::class.java
val signatureName =
signatureClass.findAnnotationRecursively(app.revanced.patcher.annotation.Name::class.java)?.name
?: signatureClass.name
if (!signature.resolved) {
failed.add(signatureName)
continue
}

val method = signature.result!!.method
val matchingMethod =
signatureClass.findAnnotationRecursively(MatchingMethod::class.java) ?: MatchingMethod()

println(
"""
[Signature] $signatureName
[Method] ${matchingMethod.definingClass}->${matchingMethod.name}
[Match] ${method.definingClass}->${method.toStr()}
""".trimIndent()
)

signatureClass.findAnnotationRecursively(FuzzyPatternScanMethod::class.java)?.let {
val warnings = signature.result!!.scanResult.warnings!!
println(
"""
[Warnings: ${warnings.count()}]
${warnings.joinToString(separator = "\n") { warning -> "${warning.instructionIndex} / ${warning.patternIndex}: ${warning.wrongOpcode} (expected: ${warning.correctOpcode})" }}
""".trimIndent()
)
}
val signatureClass = signature::class.java
val signatureName = signature.name ?: signatureClass.simpleName
if (!signature.resolved) {
failed.add(signatureName)
continue
}
val method = signature.result!!.method
val matchingMethod = signature.matchingMethod ?: MatchingMethod()
println(
"""
[Signature] $signatureName
[Method] ${matchingMethod.definingClass}->${matchingMethod.name}
[Match] ${method.definingClass}->${method.toStr()}
""".trimIndent()
)
signature.fuzzyThreshold.let {
val warnings = signature.result!!.scanResult.warnings!!
println(
"""
[Warnings: ${warnings.count()}]
${warnings.joinToString(separator = "\n") { warning -> "${warning.instructionIndex} / ${warning.patternIndex}: ${warning.wrongOpcode} (expected: ${warning.correctOpcode})" }}
""".trimIndent()
)
}
}
println(
"""
${"=".repeat(50)}
[Failed signatures: ${failed.size}]
${failed.joinToString(separator = "\n") { it }}
"""
${"=".repeat(50)}
[Failed signatures: ${failed.size}]
${failed.joinToString(separator = "\n") { it }}
""".trimIndent()
)
*/
}

private fun Method.toStr(): String {
Expand Down

0 comments on commit ace70e4

Please sign in to comment.