Skip to content

Commit

Permalink
feat: Use an extension property to create new exception when failing …
Browse files Browse the repository at this point in the history
…to resolve a fingerprint

This commit adds the extension property to the public API
  • Loading branch information
oSumAtrIX committed Aug 24, 2023
1 parent d975eea commit 47eac14
Show file tree
Hide file tree
Showing 91 changed files with 259 additions and 247 deletions.
37 changes: 24 additions & 13 deletions src/main/kotlin/app/revanced/extensions/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,40 @@ import com.android.tools.smali.dexlib2.util.MethodUtil
import org.w3c.dom.Node

/**
* Return [PatchException] from a [MethodFingerprint].
* The [PatchException] of failing to resolve a [MethodFingerprint].
*
* @return The [PatchException] for the [MethodFingerprint].
* @return The [PatchException].
*/
internal fun MethodFingerprint.toErrorResult() = PatchException("Failed to resolve $name")
val MethodFingerprint.exception
get() = PatchException("Failed to resolve $name")

/**
* Find the [MutableMethod] from a given [Method] in a [MutableClass].
*
* @param method The [Method] to find.
* @return The [MutableMethod].
*/
internal fun MutableClass.findMutableMethodOf(method: Method) = this.methods.first {
fun MutableClass.findMutableMethodOf(method: Method) = this.methods.first {
MethodUtil.methodSignaturesMatch(it, method)
}

/**
* apply a transform to all methods of the class
* apply a transform to all methods of the class.
*
* @param transform the transformation function. original method goes in, transformed method goes out
* @param transform the transformation function. original method goes in, transformed method goes out.
*/
internal fun MutableClass.transformMethods(transform: MutableMethod.() -> MutableMethod) {
fun MutableClass.transformMethods(transform: MutableMethod.() -> MutableMethod) {
val transformedMethods = methods.map { it.transform() }
methods.clear()
methods.addAll(transformedMethods)
}

internal fun Node.doRecursively(action: (Node) -> Unit) {
fun Node.doRecursively(action: (Node) -> Unit) {
action(this)
for (i in 0 until this.childNodes.length) this.childNodes.item(i).doRecursively(action)
}

internal fun MutableMethod.injectHideViewCall(
fun MutableMethod.injectHideViewCall(
insertIndex: Int,
viewRegister: Int,
classDescriptor: String,
Expand All @@ -57,7 +58,13 @@ internal fun MutableMethod.injectHideViewCall(
"invoke-static { v$viewRegister }, $classDescriptor->$targetMethod(Landroid/view/View;)V"
)

internal fun Method.findIndexForIdResource(resourceName: String): Int {
/**
* Find the index of the first constant instruction with the id of the given resource name.
*
* @param resourceName the name of the resource to find the id for.
* @return the index of the first constant instruction with the id of the given resource name, or -1 if not found.
*/
fun Method.findIndexForIdResource(resourceName: String): Int {
fun getIdResourceId(resourceName: String) = ResourceMappingPatch.resourceMappings.single {
it.type == "id" && it.name == resourceName
}.id
Expand All @@ -66,6 +73,8 @@ internal fun Method.findIndexForIdResource(resourceName: String): Int {
}

/**
* Find the index of the first constant instruction with the given value.
*
* @return the first constant instruction with the value, or -1 if not found.
*/
fun Method.indexOfFirstConstantInstructionValue(constantValue: Long): Int {
Expand All @@ -77,17 +86,19 @@ fun Method.indexOfFirstConstantInstructionValue(constantValue: Long): Int {
}

/**
* Check if the method contains a constant with the given value.
*
* @return if the method contains a constant with the given value.
*/
fun Method.containsConstantInstructionValue(constantValue: Long): Boolean {
return indexOfFirstConstantInstructionValue(constantValue) >= 0
}

/**
* traverse the class hierarchy starting from the given root class
* Traverse the class hierarchy starting from the given root class.
*
* @param targetClass the class to start traversing the class hierarchy from
* @param callback function that is called for every class in the hierarchy
* @param targetClass the class to start traversing the class hierarchy from.
* @param callback function that is called for every class in the hierarchy.
*/
fun BytecodeContext.traverseClassHierarchy(targetClass: MutableClass, callback: MutableClass.() -> Unit) {
callback(targetClass)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app.revanced.patches.backdrops.misc.pro.patch

import app.revanced.extensions.toErrorResult
import app.revanced.extensions.exception
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.data.BytecodeContext
Expand Down Expand Up @@ -33,6 +33,6 @@ class ProUnlockPatch : BytecodePatch(
)
}

} ?: throw ProUnlockFingerprint.toErrorResult()
} ?: throw ProUnlockFingerprint.exception
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app.revanced.patches.candylinkvpn.patch

import app.revanced.extensions.toErrorResult
import app.revanced.extensions.exception
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.data.BytecodeContext
Expand All @@ -24,6 +24,6 @@ class UnlockProPatch : BytecodePatch(
const/4 v0, 0x1
return v0
"""
) ?: throw IsPremiumPurchasedFingerprint.toErrorResult()
) ?: throw IsPremiumPurchasedFingerprint.exception
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app.revanced.patches.duolingo.unlocksuper.patch

import app.revanced.extensions.toErrorResult
import app.revanced.extensions.exception
import app.revanced.patcher.annotation.Compatibility
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
Expand Down Expand Up @@ -38,7 +38,7 @@ class UnlockDuolingoSuperPatch : BytecodePatch(
?.filterIsInstance<BuilderInstruction22c>()
?.firstOrNull { it.opcode == Opcode.IGET_BOOLEAN }
?.reference
?: throw IsUserSuperMethodFingerprint.toErrorResult()
?: throw IsUserSuperMethodFingerprint.exception

// Patch the instruction that assigns isUserSuper to true.
UserSerializationMethodFingerprint
Expand All @@ -50,7 +50,7 @@ class UnlockDuolingoSuperPatch : BytecodePatch(
"const/4 v2, 0x1"
)
}
?: throw UserSerializationMethodFingerprint.toErrorResult()
?: throw UserSerializationMethodFingerprint.exception
}

private companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app.revanced.patches.finanzonline.detection.bootloader.patch

import app.revanced.extensions.toErrorResult
import app.revanced.extensions.exception
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.data.BytecodeContext
Expand All @@ -27,7 +27,7 @@ class BootloaderDetectionPatch : BytecodePatch(
const/4 v0, 0x1
return v0
"""
) ?: throw fingerprint.toErrorResult()
) ?: throw fingerprint.exception
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app.revanced.patches.finanzonline.detection.root.patch

import app.revanced.extensions.toErrorResult
import app.revanced.extensions.exception
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.data.BytecodeContext
Expand All @@ -24,6 +24,6 @@ class RootDetectionPatch : BytecodePatch(
sget-object v0, Ljava/lang/Boolean;->FALSE:Ljava/lang/Boolean;
return-object v0
"""
) ?: throw RootDetectionFingerprint.toErrorResult()
) ?: throw RootDetectionFingerprint.exception
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app.revanced.patches.googlerecorder.restrictions.patch

import app.revanced.extensions.toErrorResult
import app.revanced.extensions.exception
import app.revanced.patcher.annotation.Compatibility
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
Expand Down Expand Up @@ -34,6 +34,6 @@ class RemoveDeviceRestrictions : BytecodePatch(
// Override "isPixelDevice()" to return true.
addInstruction(featureStringIndex, "const/4 v$featureAvailableRegister, 0x1")
}
} ?: throw OnApplicationCreateFingerprint.toErrorResult()
} ?: throw OnApplicationCreateFingerprint.exception
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app.revanced.patches.inshorts.ad.patch

import app.revanced.extensions.toErrorResult
import app.revanced.extensions.exception
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
Expand All @@ -27,6 +27,6 @@ class HideAdsPatch : BytecodePatch(
"""
)
}
} ?: throw InshortsAdsFingerprint.toErrorResult()
} ?: throw InshortsAdsFingerprint.exception
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app.revanced.patches.instagram.patches.ads.timeline.patch

import app.revanced.extensions.toErrorResult
import app.revanced.extensions.exception
import app.revanced.patcher.annotation.*
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels
Expand Down Expand Up @@ -33,16 +33,16 @@ class HideTimelineAdsPatch : BytecodePatch(
override fun execute(context: BytecodeContext) {
// region Resolve required methods to check for ads.

ShowAdFingerprint.result ?: throw ShowAdFingerprint.toErrorResult()
ShowAdFingerprint.result ?: throw ShowAdFingerprint.exception

PaidPartnershipAdFingerprint.result ?: throw PaidPartnershipAdFingerprint.toErrorResult()
PaidPartnershipAdFingerprint.result ?: throw PaidPartnershipAdFingerprint.exception

MediaFingerprint.result?.let {
GenericMediaAdFingerprint.resolve(context, it.classDef)
ShoppingAdFingerprint.resolve(context, it.classDef)

return@let
} ?: throw MediaFingerprint.toErrorResult()
} ?: throw MediaFingerprint.exception

// endregion

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app.revanced.patches.lightroom.misc.login.patch

import app.revanced.extensions.toErrorResult
import app.revanced.extensions.exception
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction
Expand All @@ -18,6 +18,6 @@ class DisableMandatoryLoginPatch : BytecodePatch(listOf(IsLoggedInFingerprint))
val index = implementation!!.instructions.lastIndex - 1
// Set isLoggedIn = true.
replaceInstruction(index, "const/4 v0, 0x1")
} ?: throw IsLoggedInFingerprint.toErrorResult()
} ?: throw IsLoggedInFingerprint.exception
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app.revanced.patches.lightroom.misc.premium.patch

import app.revanced.extensions.toErrorResult
import app.revanced.extensions.exception
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.data.BytecodeContext
Expand All @@ -18,6 +18,6 @@ class UnlockPremiumPatch : BytecodePatch(listOf(HasPurchasedFingerprint)) {
override fun execute(context: BytecodeContext) {
// Set hasPremium = true.
HasPurchasedFingerprint.result?.mutableMethod?.replaceInstruction(2, "const/4 v2, 0x1")
?: throw HasPurchasedFingerprint.toErrorResult()
?: throw HasPurchasedFingerprint.exception
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app.revanced.patches.memegenerator.detection.license.patch

import app.revanced.extensions.toErrorResult
import app.revanced.extensions.exception
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.InstructionExtensions.replaceInstructions
Expand All @@ -20,6 +20,6 @@ class LicenseValidationPatch : BytecodePatch(
return p0
"""
)
} ?: throw LicenseValidationFingerprint.toErrorResult()
} ?: throw LicenseValidationFingerprint.exception
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app.revanced.patches.memegenerator.detection.signature.patch

import app.revanced.extensions.toErrorResult
import app.revanced.extensions.exception
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.InstructionExtensions.replaceInstructions
Expand All @@ -20,6 +20,6 @@ class SignatureVerificationPatch : BytecodePatch(
return p0
"""
)
} ?: throw VerifySignatureFingerprint.toErrorResult()
} ?: throw VerifySignatureFingerprint.exception
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app.revanced.patches.memegenerator.misc.pro.patch

import app.revanced.extensions.toErrorResult
import app.revanced.extensions.exception
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.data.BytecodeContext
Expand Down Expand Up @@ -34,6 +34,6 @@ class UnlockProVersionPatch : BytecodePatch(
return-object p0
"""
)
} ?: throw IsFreeVersionFingerprint.toErrorResult()
} ?: throw IsFreeVersionFingerprint.exception
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app.revanced.patches.messenger.ads.inbox.patch

import app.revanced.extensions.toErrorResult
import app.revanced.extensions.exception
import app.revanced.patcher.annotation.*
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction
Expand All @@ -18,7 +18,7 @@ class HideInboxAdsPatch : BytecodePatch(
override fun execute(context: BytecodeContext) {
LoadInboxAdsFingerprint.result?.mutableMethod?.apply {
this.replaceInstruction(0, "return-void")
} ?: throw LoadInboxAdsFingerprint.toErrorResult()
} ?: throw LoadInboxAdsFingerprint.exception
}
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app.revanced.patches.messenger.inputfield.patch

import app.revanced.extensions.toErrorResult
import app.revanced.extensions.exception
import app.revanced.patcher.annotation.*
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
Expand All @@ -27,6 +27,6 @@ class DisableSwitchingEmojiToStickerInMessageInputField : BytecodePatch(listOf(S
"const-string v$targetRegister, \"expression\""
)
}
} ?: throw SwitchMessangeInputEmojiButtonFingerprint.toErrorResult()
} ?: throw SwitchMessangeInputEmojiButtonFingerprint.exception
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app.revanced.patches.messenger.inputfield.patch

import app.revanced.extensions.toErrorResult
import app.revanced.extensions.exception
import app.revanced.patcher.annotation.Compatibility
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
Expand All @@ -18,6 +18,6 @@ import app.revanced.patches.messenger.inputfield.fingerprints.SendTypingIndicato
class DisableTypingIndicator : BytecodePatch(listOf(SendTypingIndicatorFingerprint)) {
override fun execute(context: BytecodeContext) {
SendTypingIndicatorFingerprint.result?.mutableMethod?.replaceInstruction(0, "return-void")
?: throw SendTypingIndicatorFingerprint.toErrorResult()
?: throw SendTypingIndicatorFingerprint.exception
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package app.revanced.patches.music.interaction.permanentrepeat.patch

import app.revanced.extensions.toErrorResult
import app.revanced.extensions.exception
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.data.BytecodeContext
Expand Down Expand Up @@ -31,6 +31,6 @@ class PermanentRepeatPatch : BytecodePatch(
ExternalLabel("repeat", getInstruction(repeatIndex))
)
}
} ?: throw RepeatTrackFingerprint.toErrorResult()
} ?: throw RepeatTrackFingerprint.exception
}
}
Loading

0 comments on commit 47eac14

Please sign in to comment.