refactor(Utils): appropriate message is displayed in the exception when the index is not found

This commit is contained in:
inotia00
2024-06-22 19:52:46 +09:00
parent 27c40de516
commit e1f3da76c4
65 changed files with 407 additions and 338 deletions

View File

@ -12,11 +12,11 @@ import app.revanced.patcher.util.smali.ExternalLabel
import app.revanced.patches.shared.ads.fingerprints.MusicAdsFingerprint
import app.revanced.patches.shared.ads.fingerprints.VideoAdsFingerprint
import app.revanced.patches.shared.integrations.Constants.PATCHES_PATH
import app.revanced.util.getTargetIndex
import app.revanced.util.getTargetIndexWithMethodReferenceName
import app.revanced.util.getTargetIndexOrThrow
import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow
import app.revanced.util.getWalkerMethod
import app.revanced.util.getWideLiteralInstructionIndex
import app.revanced.util.indexOfFirstInstruction
import app.revanced.util.indexOfFirstInstructionOrThrow
import app.revanced.util.resultOrThrow
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction
@ -37,7 +37,7 @@ abstract class BaseAdsPatch(
override fun execute(context: BytecodeContext) {
MusicAdsFingerprint.resultOrThrow().let {
it.mutableMethod.apply {
val targetIndex = indexOfFirstInstruction {
val targetIndex = indexOfFirstInstructionOrThrow {
val reference = ((this as? ReferenceInstruction)?.reference as? MethodReference)
opcode == Opcode.INVOKE_VIRTUAL
@ -112,7 +112,7 @@ abstract class BaseAdsPatch(
// Find the instruction whose name is "show" in [MethodReference] and click the 'AlertDialog.BUTTON_POSITIVE' button.
// In this case, an instruction for 'getButton' must be added to smali, not in integrations
// (This custom dialog cannot be cast to [AlertDialog] or [Dialog])
val dialogIndex = getTargetIndexWithMethodReferenceName("show")
val dialogIndex = getTargetIndexWithMethodReferenceNameOrThrow("show")
val dialogReference = getInstruction<ReferenceInstruction>(dialogIndex).reference
val dialogDefiningClass = (dialogReference as MethodReference).definingClass
val getButtonMethod = context.findClass(dialogDefiningClass)!!
@ -123,7 +123,7 @@ abstract class BaseAdsPatch(
val getButtonCall =
dialogDefiningClass + "->" + getButtonMethod.name + "(I)Landroid/widget/Button;"
val dialogRegister = getInstruction<FiveRegisterInstruction>(dialogIndex).registerC
val freeIndex = getTargetIndex(dialogIndex, Opcode.IF_EQZ)
val freeIndex = getTargetIndexOrThrow(dialogIndex, Opcode.IF_EQZ)
val freeRegister = getInstruction<OneRegisterInstruction>(freeIndex).registerA
addInstructions(

View File

@ -8,10 +8,10 @@ import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patches.shared.customspeed.fingerprints.SpeedArrayGeneratorFingerprint
import app.revanced.patches.shared.customspeed.fingerprints.SpeedLimiterFallBackFingerprint
import app.revanced.patches.shared.customspeed.fingerprints.SpeedLimiterFingerprint
import app.revanced.util.getTargetIndex
import app.revanced.util.getTargetIndexWithFieldReferenceType
import app.revanced.util.getTargetIndexWithMethodReferenceName
import app.revanced.util.indexOfFirstInstruction
import app.revanced.util.getTargetIndexOrThrow
import app.revanced.util.getTargetIndexWithFieldReferenceTypeOrThrow
import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow
import app.revanced.util.indexOfFirstInstructionOrThrow
import app.revanced.util.resultOrThrow
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.NarrowLiteralInstruction
@ -39,7 +39,7 @@ abstract class BaseCustomPlaybackSpeedPatch(
"""
)
val sizeIndex = getTargetIndexWithMethodReferenceName("size") + 1
val sizeIndex = getTargetIndexWithMethodReferenceNameOrThrow("size") + 1
val sizeRegister = getInstruction<OneRegisterInstruction>(sizeIndex).registerA
addInstructions(
@ -49,7 +49,7 @@ abstract class BaseCustomPlaybackSpeedPatch(
"""
)
val arrayIndex = getTargetIndexWithFieldReferenceType("[F")
val arrayIndex = getTargetIndexWithFieldReferenceTypeOrThrow("[F")
val arrayRegister = getInstruction<OneRegisterInstruction>(arrayIndex).registerA
addInstructions(
@ -71,9 +71,9 @@ abstract class BaseCustomPlaybackSpeedPatch(
).forEach {
it.mutableMethod.apply {
val limiterMinConstIndex =
indexOfFirstInstruction { (this as? NarrowLiteralInstruction)?.narrowLiteral == 0.25f.toRawBits() }
indexOfFirstInstructionOrThrow { (this as? NarrowLiteralInstruction)?.narrowLiteral == 0.25f.toRawBits() }
val limiterMaxConstIndex =
getTargetIndex(limiterMinConstIndex + 1, Opcode.CONST_HIGH16)
getTargetIndexOrThrow(limiterMinConstIndex + 1, Opcode.CONST_HIGH16)
val limiterMinConstDestination =
getInstruction<OneRegisterInstruction>(limiterMinConstIndex).registerA

View File

@ -7,7 +7,7 @@ import app.revanced.patcher.fingerprint.MethodFingerprint
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
import app.revanced.patches.shared.dialog.fingerprints.CreateDialogFingerprint
import app.revanced.util.getTargetIndexWithMethodReferenceName
import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow
import app.revanced.util.getWalkerMethod
import app.revanced.util.resultOrThrow
import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction
@ -22,7 +22,7 @@ abstract class BaseViewerDiscretionDialogPatch(
}
) {
private fun MutableMethod.invoke(isAgeVerified: Boolean) {
val showDialogIndex = getTargetIndexWithMethodReferenceName("show")
val showDialogIndex = getTargetIndexWithMethodReferenceNameOrThrow("show")
val dialogRegister = getInstruction<FiveRegisterInstruction>(showDialogIndex).registerC
val methodName =

View File

@ -6,7 +6,7 @@ import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
import app.revanced.patches.shared.drawable.fingerprints.DrawableFingerprint
import app.revanced.util.getTargetIndexWithMethodReferenceNameReversed
import app.revanced.util.getTargetIndexWithMethodReferenceNameReversedOrThrow
import app.revanced.util.resultOrThrow
import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction
@ -17,7 +17,7 @@ object DrawableColorPatch : BytecodePatch(
DrawableFingerprint.resultOrThrow().mutableMethod.apply {
insertMethod = this
insertIndex = getTargetIndexWithMethodReferenceNameReversed("setColor")
insertIndex = getTargetIndexWithMethodReferenceNameReversedOrThrow("setColor")
insertRegister = getInstruction<FiveRegisterInstruction>(insertIndex).registerD
}
}

View File

@ -15,9 +15,9 @@ import app.revanced.patches.shared.litho.fingerprints.LithoFilterPatchConstructo
import app.revanced.patches.shared.litho.fingerprints.PathBuilderFingerprint
import app.revanced.patches.shared.litho.fingerprints.SetByteBufferFingerprint
import app.revanced.util.getStringInstructionIndex
import app.revanced.util.getTargetIndex
import app.revanced.util.getTargetIndexReversed
import app.revanced.util.getTargetIndexWithFieldReferenceType
import app.revanced.util.getTargetIndexOrThrow
import app.revanced.util.getTargetIndexReversedOrThrow
import app.revanced.util.getTargetIndexWithFieldReferenceTypeOrThrow
import app.revanced.util.resultOrThrow
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.builder.instruction.BuilderInstruction35c
@ -56,7 +56,7 @@ object LithoFilterPatch : BytecodePatch(
SetByteBufferFingerprint.resultOrThrow().let {
it.mutableMethod.apply {
val insertIndex = getTargetIndex(Opcode.IF_EQZ) + 1
val insertIndex = getTargetIndexOrThrow(Opcode.IF_EQZ) + 1
addInstruction(
insertIndex,
@ -136,17 +136,17 @@ object LithoFilterPatch : BytecodePatch(
}
val stringBuilderIndex =
getTargetIndexWithFieldReferenceType("Ljava/lang/StringBuilder;")
getTargetIndexWithFieldReferenceTypeOrThrow("Ljava/lang/StringBuilder;")
val stringBuilderRegister =
getInstruction<TwoRegisterInstruction>(stringBuilderIndex).registerA
val emptyStringIndex = getStringInstructionIndex("")
val identifierIndex = getTargetIndexReversed(emptyStringIndex, Opcode.IPUT_OBJECT)
val identifierIndex = getTargetIndexReversedOrThrow(emptyStringIndex, Opcode.IPUT_OBJECT)
val identifierRegister =
getInstruction<TwoRegisterInstruction>(identifierIndex).registerA
val objectIndex = getTargetIndex(emptyStringIndex, Opcode.INVOKE_VIRTUAL)
val objectIndex = getTargetIndexOrThrow(emptyStringIndex, Opcode.INVOKE_VIRTUAL)
val objectRegister = getInstruction<BuilderInstruction35c>(objectIndex).registerC
val insertIndex = stringBuilderIndex + 1

View File

@ -7,7 +7,7 @@ import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.PatchException
import app.revanced.patcher.util.proxy.mutableTypes.MutableClass
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
import app.revanced.util.getTargetIndex
import app.revanced.util.getTargetIndexOrThrow
import app.revanced.util.resultOrThrow
import com.android.tools.smali.dexlib2.Opcode
import kotlin.properties.Delegates
@ -37,7 +37,7 @@ abstract class BaseMainActivityResolvePatch(
// set onBackPressed method
onBackPressedMethod = getMethod("onBackPressed")
onBackPressedMethodIndex = onBackPressedMethod.getTargetIndex(Opcode.RETURN_VOID)
onBackPressedMethodIndex = onBackPressedMethod.getTargetIndexOrThrow(Opcode.RETURN_VOID)
}
fun injectConstructorMethodCall(classDescriptor: String, methodDescriptor: String) =

View File

@ -7,7 +7,7 @@ import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.util.smali.ExternalLabel
import app.revanced.patches.shared.opus.fingerprints.CodecReferenceFingerprint
import app.revanced.patches.shared.opus.fingerprints.CodecSelectorFingerprint
import app.revanced.util.getTargetIndexWithReference
import app.revanced.util.getTargetIndexWithReferenceOrThrow
import app.revanced.util.resultOrThrow
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction
@ -31,7 +31,7 @@ abstract class BaseOpusCodecsPatch(
CodecReferenceFingerprint.resultOrThrow().let {
it.mutableMethod.apply {
val targetIndex = getTargetIndexWithReference("Ljava/util/Set;")
val targetIndex = getTargetIndexWithReferenceOrThrow("Ljava/util/Set;")
opusCodecReference = getInstruction<ReferenceInstruction>(targetIndex).reference
}
}

View File

@ -6,7 +6,7 @@ import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patches.shared.fingerprints.CreatePlayerRequestBodyWithModelFingerprint
import app.revanced.patches.shared.fingerprints.CreatePlayerRequestBodyWithModelFingerprint.indexOfReleaseInstruction
import app.revanced.util.getTargetIndexReversed
import app.revanced.util.getTargetIndexReversedOrThrow
import app.revanced.util.resultOrThrow
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.TwoRegisterInstruction
@ -20,7 +20,7 @@ abstract class BaseSpoofAppVersionPatch(
CreatePlayerRequestBodyWithModelFingerprint.resultOrThrow().mutableMethod.apply {
val versionIndex = indexOfReleaseInstruction(this) + 1
val insertIndex = getTargetIndexReversed(versionIndex, Opcode.IPUT_OBJECT)
val insertIndex = getTargetIndexReversedOrThrow(versionIndex, Opcode.IPUT_OBJECT)
val insertRegister = getInstruction<TwoRegisterInstruction>(insertIndex).registerA
addInstructions(