Skip to content

Commit

Permalink
refactor: migrate signature schema changes to Patcher
Browse files Browse the repository at this point in the history
also updated Extensions, for good measure.
  • Loading branch information
Sculas committed Apr 13, 2022
1 parent 4022b8b commit 0204eee
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.builder.BuilderInstruction
import org.jf.dexlib2.builder.MutableMethodImplementation

infix fun AccessFlags.or(other: AccessFlags) = this.value or other.value
class AccessFlagExtensions {
companion object {
infix fun AccessFlags.or(other: AccessFlags) = this.value or other.value
infix fun Int.or(other: AccessFlags) = this or other.value
}
}

fun MutableMethodImplementation.addInstructions(index: Int, instructions: List<BuilderInstruction>) {
for (i in instructions.lastIndex downTo 0) {
Expand Down
49 changes: 47 additions & 2 deletions src/main/kotlin/app/revanced/patcher/signature/MethodSignature.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,61 @@ import org.jf.dexlib2.Opcode
/**
* Represents a method signature.
* @param name A suggestive name for the method which the signature was created for.
* @param metadata Metadata about this signature.
* @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 A list of opcodes of the method.
* @param accessFlags The access flags of the method.
*/
@Suppress("ArrayInDataClass")
data class MethodSignature(
val name: String,
val metadata: SignatureMetadata,
val returnType: String?,
val accessFlags: Int?,
val methodParameters: Iterable<String>?,
val opcodes: Iterable<Opcode>?
)
)

/**
* Metadata about the signature.
* @param method Metadata about the method for this signature.
* @param patcher Metadata for the Patcher, this contains things like how the Patcher should interpret this signature.
*/
data class SignatureMetadata(
val method: MethodMetadata,
val patcher: PatcherMetadata
)

/**
* Metadata about the method for this signature.
* @param definingClass The defining class name of the original method.
* @param methodName The name of the original method.
* @param comment A comment about this method and the data above.
* For example, the version this signature was originally made for.
*/
data class MethodMetadata(
val definingClass: String?,
val methodName: String?,
val comment: String
)

/**
* Metadata for the Patcher, this contains things like how the Patcher should interpret this signature.
* @param method The method the Patcher should use to resolve the signature.
*/
data class PatcherMetadata(
val method: PatcherMethod
)

interface PatcherMethod {
/**
* When comparing the signature, if one or more of the opcodes do not match, skip.
*/
class Direct : PatcherMethod

/**
* When comparing the signature, if [threshold] or more of the opcodes do not match, skip.
*/
class Fuzzy(val threshold: Int) : PatcherMethod
}
16 changes: 13 additions & 3 deletions src/test/kotlin/app/revanced/patcher/PatcherTest.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package app.revanced.patcher

import app.revanced.patcher.cache.Cache
import app.revanced.patcher.extensions.AccessFlagExtensions.Companion.or
import app.revanced.patcher.extensions.addInstructions
import app.revanced.patcher.extensions.or
import app.revanced.patcher.patch.Patch
import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.proxy.mutableTypes.MutableField.Companion.toMutable
import app.revanced.patcher.proxy.mutableTypes.MutableMethod.Companion.toMutable
import app.revanced.patcher.signature.MethodSignature
import app.revanced.patcher.signature.*
import app.revanced.patcher.smali.asInstruction
import app.revanced.patcher.smali.asInstructions
import com.google.common.collect.ImmutableList
Expand All @@ -31,8 +31,18 @@ internal class PatcherTest {
val testSignatures = listOf(
MethodSignature(
"main-method",
SignatureMetadata(
method = MethodMetadata(
definingClass = "TestClass",
methodName = "main",
comment = "Main method of TestClass. Version 1.0.0"
),
patcher = PatcherMetadata(
method = PatcherMethod.Fuzzy(2)
)
),
"V",
AccessFlags.PUBLIC or AccessFlags.STATIC,
AccessFlags.PUBLIC or AccessFlags.STATIC or AccessFlags.STATIC,
listOf("[L"),
listOf(
Opcode.CONST_STRING,
Expand Down

0 comments on commit 0204eee

Please sign in to comment.