From 0204eee79e2cda68c45c07fbd60efaa3b5e6a3d5 Mon Sep 17 00:00:00 2001 From: Lucaskyy Date: Wed, 13 Apr 2022 19:42:50 +0200 Subject: [PATCH] refactor: migrate signature schema changes to Patcher also updated Extensions, for good measure. --- .../revanced/patcher/extensions/Extensions.kt | 7 ++- .../patcher/signature/MethodSignature.kt | 49 ++++++++++++++++++- .../app/revanced/patcher/PatcherTest.kt | 16 ++++-- 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/app/revanced/patcher/extensions/Extensions.kt b/src/main/kotlin/app/revanced/patcher/extensions/Extensions.kt index 5e077751..d50b2acb 100644 --- a/src/main/kotlin/app/revanced/patcher/extensions/Extensions.kt +++ b/src/main/kotlin/app/revanced/patcher/extensions/Extensions.kt @@ -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) { for (i in instructions.lastIndex downTo 0) { diff --git a/src/main/kotlin/app/revanced/patcher/signature/MethodSignature.kt b/src/main/kotlin/app/revanced/patcher/signature/MethodSignature.kt index 4c560d6b..57042852 100644 --- a/src/main/kotlin/app/revanced/patcher/signature/MethodSignature.kt +++ b/src/main/kotlin/app/revanced/patcher/signature/MethodSignature.kt @@ -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?, val opcodes: Iterable? -) \ No newline at end of file +) + +/** + * 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 +} \ No newline at end of file diff --git a/src/test/kotlin/app/revanced/patcher/PatcherTest.kt b/src/test/kotlin/app/revanced/patcher/PatcherTest.kt index 2285293b..a4d613dd 100644 --- a/src/test/kotlin/app/revanced/patcher/PatcherTest.kt +++ b/src/test/kotlin/app/revanced/patcher/PatcherTest.kt @@ -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 @@ -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,