From e1f3da76c46dde7fa57828df93ec0f46297e1eaf Mon Sep 17 00:00:00 2001 From: inotia00 <108592928+inotia00@users.noreply.github.com> Date: Sat, 22 Jun 2024 19:52:46 +0900 Subject: [PATCH] refactor(Utils): appropriate message is displayed in the exception when the index is not found --- .../components/AccountComponentsPatch.kt | 14 +-- .../components/ActionBarComponentsPatch.kt | 16 ++-- .../patches/music/ads/general/AdsPatch.kt | 12 +-- .../components/FlyoutMenuComponentsPatch.kt | 20 ++--- .../components/LayoutComponentsPatch.kt | 10 +-- .../OldStyleLibraryShelfPatch.kt | 4 +- .../redirection/DislikeRedirectionPatch.kt | 16 ++-- .../NavigationBarComponentsPatch.kt | 10 +-- .../components/PlayerComponentsPatch.kt | 43 +++++---- .../utils/fix/header/RestoreOldHeaderPatch.kt | 4 +- .../ReturnYouTubeDislikeBytecodePatch.kt | 4 +- .../utils/settings/SettingsBytecodePatch.kt | 4 +- .../sponsorblock/SponsorBlockBytecodePatch.kt | 10 +-- .../information/VideoInformationPatch.kt | 8 +- .../video/playback/VideoPlaybackPatch.kt | 4 +- .../patches/reddit/ad/general/AdsPatch.kt | 8 +- .../RecentlyVisitedShelfPatch.kt | 18 ++-- .../utils/settings/SettingsBytecodePatch.kt | 4 +- .../patches/shared/ads/BaseAdsPatch.kt | 12 +-- .../BaseCustomPlaybackSpeedPatch.kt | 16 ++-- .../dialog/BaseViewerDiscretionDialogPatch.kt | 4 +- .../shared/drawable/DrawableColorPatch.kt | 4 +- .../patches/shared/litho/LithoFilterPatch.kt | 14 +-- .../BaseMainActivityResolvePatch.kt | 4 +- .../shared/opus/BaseOpusCodecsPatch.kt | 4 +- .../BaseSpoofAppVersionPatch.kt | 4 +- .../feed/components/FeedComponentsPatch.kt | 18 ++-- .../general/audiotracks/AudioTracksPatch.kt | 10 +-- .../components/LayoutComponentsPatch.kt | 10 +-- .../general/layoutswitch/LayoutSwitchPatch.kt | 8 +- .../NavigationBarComponentsPatch.kt | 4 +- .../general/toolbar/ToolBarComponentsPatch.kt | 20 ++--- .../OpenLinksDirectlyPatch.kt | 4 +- .../ambientmode/AmbientModeSwitchPatch.kt | 8 +- .../player/buttons/PlayerButtonsPatch.kt | 12 +-- .../player/comments/CommentsComponentPatch.kt | 6 +- .../components/PlayerComponentsPatch.kt | 14 +-- .../DescriptionComponentsPatch.kt | 8 +- .../flyoutmenu/hide/PlayerFlyoutMenuPatch.kt | 4 +- .../flyoutmenu/toggle/ChangeTogglePatch.kt | 17 ++-- .../fullscreen/FullscreenComponentsPatch.kt | 22 ++--- .../hapticfeedback/HapticFeedBackPatch.kt | 4 +- .../OverlayButtonsBytecodePatch.kt | 10 +-- .../player/seekbar/SeekbarComponentsPatch.kt | 10 +-- .../player/speedoverlay/SpeedOverlayPatch.kt | 24 ++--- .../shorts/components/ShortsComponentPatch.kt | 24 ++--- .../components/ShortsNavigationBarPatch.kt | 4 +- .../shorts/repeat/ShortsRepeatPatch.kt | 6 +- .../ResumingShortsOnStartupPatch.kt | 5 +- .../utils/castbutton/CastButtonPatch.kt | 6 +- .../utils/fix/cairo/CairoSettingsPatch.kt | 4 +- .../FullscreenButtonViewStubPatch.kt | 4 +- .../ConversionContextObfuscationPatch.kt | 4 +- .../fix/shortsplayback/ShortsPlaybackPatch.kt | 4 +- .../SuggestedVideoEndScreenPatch.kt | 8 +- .../playercontrols/PlayerControlsPatch.kt | 8 +- .../PlayerControlsVisibilityHookPatch.kt | 4 +- .../utils/playertype/PlayerTypeHookPatch.kt | 8 +- .../general/ReturnYouTubeDislikePatch.kt | 6 +- .../ReturnYouTubeDislikeRollingNumberPatch.kt | 10 +-- .../shorts/ReturnYouTubeDislikeShortsPatch.kt | 14 +-- .../sponsorblock/SponsorBlockBytecodePatch.kt | 22 ++--- .../information/VideoInformationPatch.kt | 22 ++--- .../video/playback/VideoPlaybackPatch.kt | 10 ++- .../kotlin/app/revanced/util/BytecodeUtils.kt | 88 +++++++++++++++++-- 65 files changed, 407 insertions(+), 338 deletions(-) diff --git a/src/main/kotlin/app/revanced/patches/music/account/components/AccountComponentsPatch.kt b/src/main/kotlin/app/revanced/patches/music/account/components/AccountComponentsPatch.kt index 5b21df07c..9b4288ad1 100644 --- a/src/main/kotlin/app/revanced/patches/music/account/components/AccountComponentsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/account/components/AccountComponentsPatch.kt @@ -14,8 +14,8 @@ import app.revanced.patches.music.utils.integrations.Constants.ACCOUNT_CLASS_DES import app.revanced.patches.music.utils.resourceid.SharedResourceIdPatch import app.revanced.patches.music.utils.settings.CategoryType import app.revanced.patches.music.utils.settings.SettingsPatch -import app.revanced.util.getTargetIndexWithMethodReferenceName -import app.revanced.util.getTargetIndexWithReference +import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow +import app.revanced.util.getTargetIndexWithReferenceOrThrow import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction @@ -43,8 +43,8 @@ object AccountComponentsPatch : BaseBytecodePatch( MenuEntryFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val textIndex = getTargetIndexWithMethodReferenceName("setText") - val viewIndex = getTargetIndexWithMethodReferenceName("addView") + val textIndex = getTargetIndexWithMethodReferenceNameOrThrow("setText") + val viewIndex = getTargetIndexWithMethodReferenceNameOrThrow("addView") val textRegister = getInstruction(textIndex).registerD val viewRegister = getInstruction(viewIndex).registerD @@ -64,9 +64,9 @@ object AccountComponentsPatch : BaseBytecodePatch( AccountSwitcherAccessibilityLabelFingerprint.resultOrThrow().let { result -> result.mutableMethod.apply { - val textColorIndex = getTargetIndexWithMethodReferenceName("setTextColor") + val textColorIndex = getTargetIndexWithMethodReferenceNameOrThrow("setTextColor") val setVisibilityIndex = - getTargetIndexWithMethodReferenceName(textColorIndex, "setVisibility") + getTargetIndexWithMethodReferenceNameOrThrow(textColorIndex, "setVisibility") val textViewInstruction = getInstruction(setVisibilityIndex) @@ -99,7 +99,7 @@ object AccountComponentsPatch : BaseBytecodePatch( TermsOfServiceFingerprint.resultOrThrow().let { it.mutableMethod.apply { val insertIndex = - getTargetIndexWithReference("/PrivacyTosFooter;->setVisibility(I)V") + getTargetIndexWithReferenceOrThrow("/PrivacyTosFooter;->setVisibility(I)V") val visibilityRegister = getInstruction(insertIndex).registerD diff --git a/src/main/kotlin/app/revanced/patches/music/actionbar/components/ActionBarComponentsPatch.kt b/src/main/kotlin/app/revanced/patches/music/actionbar/components/ActionBarComponentsPatch.kt index 49ad7a99c..7adeadfdc 100644 --- a/src/main/kotlin/app/revanced/patches/music/actionbar/components/ActionBarComponentsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/actionbar/components/ActionBarComponentsPatch.kt @@ -15,10 +15,10 @@ import app.revanced.patches.music.utils.resourceid.SharedResourceIdPatch.LikeDis import app.revanced.patches.music.utils.settings.CategoryType import app.revanced.patches.music.utils.settings.SettingsPatch import app.revanced.patches.music.video.information.VideoInformationPatch -import app.revanced.util.getTargetIndexWithMethodReferenceName -import app.revanced.util.getTargetIndexWithReference +import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow +import app.revanced.util.getTargetIndexWithReferenceOrThrow import app.revanced.util.getWideLiteralInstructionIndex -import app.revanced.util.indexOfFirstInstruction +import app.revanced.util.indexOfFirstInstructionOrThrow import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.Opcode @@ -48,7 +48,7 @@ object ActionBarComponentsPatch : BaseBytecodePatch( it.mutableMethod.apply { // hook download button - val addViewIndex = getTargetIndexWithMethodReferenceName("addView") + val addViewIndex = getTargetIndexWithMethodReferenceNameOrThrow("addView") val addViewRegister = getInstruction(addViewIndex).registerD @@ -58,13 +58,13 @@ object ActionBarComponentsPatch : BaseBytecodePatch( ) // hide action button label - val noLabelIndex = indexOfFirstInstruction { + val noLabelIndex = indexOfFirstInstructionOrThrow { val reference = (this as? ReferenceInstruction)?.reference.toString() opcode == Opcode.INVOKE_DIRECT && reference.endsWith("(Landroid/content/Context;)V") && !reference.contains("Lcom/google/android/libraries/youtube/common/ui/YouTubeButton;") } - 2 - val replaceIndex = indexOfFirstInstruction { + val replaceIndex = indexOfFirstInstructionOrThrow { val reference = (this as? ReferenceInstruction)?.reference.toString() opcode == Opcode.INVOKE_DIRECT && reference.endsWith("Lcom/google/android/libraries/youtube/common/ui/YouTubeButton;->(Landroid/content/Context;)V") @@ -83,10 +83,10 @@ object ActionBarComponentsPatch : BaseBytecodePatch( removeInstruction(replaceIndex) // hide action button - val hasNextIndex = getTargetIndexWithMethodReferenceName("hasNext") + val hasNextIndex = getTargetIndexWithMethodReferenceNameOrThrow("hasNext") val freeRegister = min(implementation!!.registerCount - parameters.size - 2, 15) - val spannedIndex = getTargetIndexWithReference(")Landroid/text/Spanned;") + val spannedIndex = getTargetIndexWithReferenceOrThrow(")Landroid/text/Spanned;") val spannedRegister = getInstruction(spannedIndex).registerC val spannedReference = getInstruction(spannedIndex).reference diff --git a/src/main/kotlin/app/revanced/patches/music/ads/general/AdsPatch.kt b/src/main/kotlin/app/revanced/patches/music/ads/general/AdsPatch.kt index 8a67834df..d8aed25c0 100644 --- a/src/main/kotlin/app/revanced/patches/music/ads/general/AdsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/ads/general/AdsPatch.kt @@ -25,8 +25,8 @@ import app.revanced.patches.music.utils.resourceid.SharedResourceIdPatch.Interst import app.revanced.patches.music.utils.settings.CategoryType import app.revanced.patches.music.utils.settings.SettingsPatch import app.revanced.patches.shared.litho.LithoFilterPatch -import app.revanced.util.getTargetIndex -import app.revanced.util.getTargetIndexWithReference +import app.revanced.util.getTargetIndexOrThrow +import app.revanced.util.getTargetIndexWithReferenceOrThrow import app.revanced.util.getWalkerMethod import app.revanced.util.getWideLiteralInstructionIndex import app.revanced.util.patch.BaseBytecodePatch @@ -139,15 +139,15 @@ object AdsPatch : BaseBytecodePatch( it.mutableMethod.apply { val constIndex = getWideLiteralInstructionIndex(SharedResourceIdPatch.PrivacyTosFooter) - val walkerIndex = getTargetIndex(constIndex + 2, Opcode.INVOKE_VIRTUAL) - val viewIndex = getTargetIndex(constIndex, Opcode.IGET_OBJECT) + val walkerIndex = getTargetIndexOrThrow(constIndex + 2, Opcode.INVOKE_VIRTUAL) + val viewIndex = getTargetIndexOrThrow(constIndex, Opcode.IGET_OBJECT) val viewReference = getInstruction(viewIndex).reference.toString() val walkerMethod = getWalkerMethod(context, walkerIndex) walkerMethod.apply { - val insertIndex = getTargetIndexWithReference(viewReference) - val nullCheckIndex = getTargetIndex(insertIndex - 1, Opcode.IF_NEZ) + val insertIndex = getTargetIndexWithReferenceOrThrow(viewReference) + val nullCheckIndex = getTargetIndexOrThrow(insertIndex - 1, Opcode.IF_NEZ) val nullCheckRegister = getInstruction(nullCheckIndex).registerA diff --git a/src/main/kotlin/app/revanced/patches/music/flyoutmenu/components/FlyoutMenuComponentsPatch.kt b/src/main/kotlin/app/revanced/patches/music/flyoutmenu/components/FlyoutMenuComponentsPatch.kt index eb0f090cd..9e67be635 100644 --- a/src/main/kotlin/app/revanced/patches/music/flyoutmenu/components/FlyoutMenuComponentsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/flyoutmenu/components/FlyoutMenuComponentsPatch.kt @@ -25,11 +25,11 @@ import app.revanced.patches.music.utils.settings.SettingsPatch import app.revanced.patches.music.utils.videotype.VideoTypeHookPatch import app.revanced.patches.music.video.information.VideoInformationPatch import app.revanced.patches.shared.litho.LithoFilterPatch -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.literalInstructionBooleanHook import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow @@ -98,7 +98,7 @@ object FlyoutMenuComponentsPatch : BaseBytecodePatch( val constIndex = getWideLiteralInstructionIndex(SharedResourceIdPatch.TrimSilenceSwitch) val onCheckedChangedListenerIndex = - getTargetIndex(constIndex, Opcode.INVOKE_DIRECT) + getTargetIndexOrThrow(constIndex, Opcode.INVOKE_DIRECT) val onCheckedChangedListenerReference = getInstruction(onCheckedChangedListenerIndex).reference val onCheckedChangedListenerDefiningClass = @@ -108,7 +108,7 @@ object FlyoutMenuComponentsPatch : BaseBytecodePatch( onCheckedChangedListenerClass.methods.find { method -> method.name == "onCheckedChanged" } ?.apply { - val walkerIndex = indexOfFirstInstruction { + val walkerIndex = indexOfFirstInstructionOrThrow { val reference = ((this as? ReferenceInstruction)?.reference as? MethodReference) @@ -118,7 +118,7 @@ object FlyoutMenuComponentsPatch : BaseBytecodePatch( && reference.parameterTypes[0] == "Z" } getWalkerMethod(context, walkerIndex).apply { - val insertIndex = getTargetIndex(Opcode.MOVE_RESULT) + val insertIndex = getTargetIndexOrThrow(Opcode.MOVE_RESULT) val insertRegister = getInstruction(insertIndex).registerA @@ -142,7 +142,7 @@ object FlyoutMenuComponentsPatch : BaseBytecodePatch( MenuItemFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val freeIndex = getTargetIndex(Opcode.OR_INT_LIT16) + val freeIndex = getTargetIndexOrThrow(Opcode.OR_INT_LIT16) val textViewIndex = it.scanResult.patternScanResult!!.startIndex val imageViewIndex = it.scanResult.patternScanResult!!.endIndex @@ -153,7 +153,7 @@ object FlyoutMenuComponentsPatch : BaseBytecodePatch( val imageViewRegister = getInstruction(imageViewIndex).registerA - val enumIndex = indexOfFirstInstruction { + val enumIndex = indexOfFirstInstructionOrThrow { opcode == Opcode.INVOKE_STATIC && (this as? ReferenceInstruction)?.reference.toString() .contains("(I)L") @@ -176,7 +176,7 @@ object FlyoutMenuComponentsPatch : BaseBytecodePatch( TouchOutsideFingerprint.resultOrThrow().let { it.mutableMethod.apply { val setOnClickListenerIndex = - getTargetIndexWithMethodReferenceName("setOnClickListener") + getTargetIndexWithMethodReferenceNameOrThrow("setOnClickListener") val setOnClickListenerRegister = getInstruction(setOnClickListenerIndex).registerC @@ -190,7 +190,7 @@ object FlyoutMenuComponentsPatch : BaseBytecodePatch( EndButtonsContainerFingerprint.resultOrThrow().let { it.mutableMethod.apply { val startIndex = getWideLiteralInstructionIndex(EndButtonsContainer) - val targetIndex = getTargetIndex(startIndex, Opcode.MOVE_RESULT_OBJECT) + val targetIndex = getTargetIndexOrThrow(startIndex, Opcode.MOVE_RESULT_OBJECT) val targetRegister = getInstruction(targetIndex).registerA addInstruction( diff --git a/src/main/kotlin/app/revanced/patches/music/general/components/LayoutComponentsPatch.kt b/src/main/kotlin/app/revanced/patches/music/general/components/LayoutComponentsPatch.kt index 1da58db9b..7fd43f8e4 100644 --- a/src/main/kotlin/app/revanced/patches/music/general/components/LayoutComponentsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/general/components/LayoutComponentsPatch.kt @@ -33,8 +33,8 @@ import app.revanced.patches.music.utils.settings.CategoryType import app.revanced.patches.music.utils.settings.SettingsPatch import app.revanced.patches.shared.litho.LithoFilterPatch import app.revanced.patches.shared.voicesearch.VoiceSearchUtils.patchXml -import app.revanced.util.getTargetIndex -import app.revanced.util.getTargetIndexWithMethodReferenceName +import app.revanced.util.getTargetIndexOrThrow +import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow import app.revanced.util.getWideLiteralInstructionIndex import app.revanced.util.literalInstructionBooleanHook import app.revanced.util.patch.BaseBytecodePatch @@ -186,7 +186,7 @@ object LayoutComponentsPatch : BaseBytecodePatch( if (SettingsPatch.upward0642) { TopBarMenuItemImageViewFingerprint.resultOrThrow().mutableMethod.apply { val constIndex = getWideLiteralInstructionIndex(TopBarMenuItemImageView) - val targetIndex = getTargetIndex(constIndex, Opcode.MOVE_RESULT_OBJECT) + val targetIndex = getTargetIndexOrThrow(constIndex, Opcode.MOVE_RESULT_OBJECT) val targetRegister = getInstruction(targetIndex).registerA addInstruction( @@ -237,7 +237,7 @@ object LayoutComponentsPatch : BaseBytecodePatch( parentResult.mutableMethod.apply { val constIndex = getWideLiteralInstructionIndex(SharedResourceIdPatch.MusicTasteBuilderShelf) - val targetIndex = getTargetIndex(constIndex, Opcode.MOVE_RESULT_OBJECT) + val targetIndex = getTargetIndexOrThrow(constIndex, Opcode.MOVE_RESULT_OBJECT) val targetRegister = getInstruction(targetIndex).registerA addInstruction( @@ -284,7 +284,7 @@ object LayoutComponentsPatch : BaseBytecodePatch( ) SearchBarFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val setVisibilityIndex = getTargetIndexWithMethodReferenceName("setVisibility") + val setVisibilityIndex = getTargetIndexWithMethodReferenceNameOrThrow("setVisibility") val setVisibilityInstruction = getInstruction(setVisibilityIndex) diff --git a/src/main/kotlin/app/revanced/patches/music/general/oldstylelibraryshelf/OldStyleLibraryShelfPatch.kt b/src/main/kotlin/app/revanced/patches/music/general/oldstylelibraryshelf/OldStyleLibraryShelfPatch.kt index 4beb0b2f9..de8629052 100644 --- a/src/main/kotlin/app/revanced/patches/music/general/oldstylelibraryshelf/OldStyleLibraryShelfPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/general/oldstylelibraryshelf/OldStyleLibraryShelfPatch.kt @@ -9,7 +9,7 @@ import app.revanced.patches.music.utils.integrations.Constants.GENERAL_CLASS_DES import app.revanced.patches.music.utils.settings.CategoryType import app.revanced.patches.music.utils.settings.SettingsPatch import app.revanced.util.getStringInstructionIndex -import app.revanced.util.getTargetIndexReversed +import app.revanced.util.getTargetIndexReversedOrThrow import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.Opcode @@ -28,7 +28,7 @@ object OldStyleLibraryShelfPatch : BaseBytecodePatch( BrowseIdFingerprint.resultOrThrow().let { it.mutableMethod.apply { val stringIndex = getStringInstructionIndex("FEmusic_offline") - val targetIndex = getTargetIndexReversed(stringIndex, Opcode.IGET_OBJECT) + val targetIndex = getTargetIndexReversedOrThrow(stringIndex, Opcode.IGET_OBJECT) val targetRegister = getInstruction(targetIndex).registerA addInstructions( diff --git a/src/main/kotlin/app/revanced/patches/music/general/redirection/DislikeRedirectionPatch.kt b/src/main/kotlin/app/revanced/patches/music/general/redirection/DislikeRedirectionPatch.kt index 2da3d7dd6..577898621 100644 --- a/src/main/kotlin/app/revanced/patches/music/general/redirection/DislikeRedirectionPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/general/redirection/DislikeRedirectionPatch.kt @@ -12,10 +12,10 @@ import app.revanced.patches.music.utils.integrations.Constants.GENERAL_CLASS_DES import app.revanced.patches.music.utils.settings.CategoryType import app.revanced.patches.music.utils.settings.SettingsPatch import app.revanced.util.getStringInstructionIndex -import app.revanced.util.getTargetIndexReversed -import app.revanced.util.getTargetIndexWithReference +import app.revanced.util.getTargetIndexReversedOrThrow +import app.revanced.util.getTargetIndexWithReferenceOrThrow import app.revanced.util.getWalkerMethod -import app.revanced.util.indexOfFirstInstruction +import app.revanced.util.indexOfFirstInstructionOrThrow import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.Opcode @@ -42,15 +42,15 @@ object DislikeRedirectionPatch : BaseBytecodePatch( PendingIntentReceiverFingerprint.resultOrThrow().let { it.mutableMethod.apply { val startIndex = getStringInstructionIndex("YTM Dislike") - val onClickRelayIndex = getTargetIndexReversed(startIndex, Opcode.INVOKE_VIRTUAL) + val onClickRelayIndex = getTargetIndexReversedOrThrow(startIndex, Opcode.INVOKE_VIRTUAL) val onClickRelayMethod = getWalkerMethod(context, onClickRelayIndex) onClickRelayMethod.apply { - val onClickMethodIndex = getTargetIndexReversed(Opcode.INVOKE_DIRECT) + val onClickMethodIndex = getTargetIndexReversedOrThrow(Opcode.INVOKE_DIRECT) val onClickMethod = getWalkerMethod(context, onClickMethodIndex) onClickMethod.apply { - val onClickIndex = indexOfFirstInstruction { + val onClickIndex = indexOfFirstInstructionOrThrow { val reference = ((this as? ReferenceInstruction)?.reference as? MethodReference) @@ -69,7 +69,7 @@ object DislikeRedirectionPatch : BaseBytecodePatch( DislikeButtonOnClickListenerFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val onClickIndex = getTargetIndexWithReference(onClickReference.toString()) + val onClickIndex = getTargetIndexWithReferenceOrThrow(onClickReference.toString()) injectCall(onClickIndex) } } @@ -83,7 +83,7 @@ object DislikeRedirectionPatch : BaseBytecodePatch( } private fun MutableMethod.injectCall(onClickIndex: Int) { - val targetIndex = getTargetIndexReversed(onClickIndex, Opcode.IF_EQZ) + val targetIndex = getTargetIndexReversedOrThrow(onClickIndex, Opcode.IF_EQZ) val insertRegister = getInstruction(targetIndex).registerA addInstructionsWithLabels( diff --git a/src/main/kotlin/app/revanced/patches/music/navigation/components/NavigationBarComponentsPatch.kt b/src/main/kotlin/app/revanced/patches/music/navigation/components/NavigationBarComponentsPatch.kt index 5aaf2f6aa..ae412cf8f 100644 --- a/src/main/kotlin/app/revanced/patches/music/navigation/components/NavigationBarComponentsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/navigation/components/NavigationBarComponentsPatch.kt @@ -12,8 +12,8 @@ import app.revanced.patches.music.utils.integrations.Constants.NAVIGATION_CLASS_ import app.revanced.patches.music.utils.resourceid.SharedResourceIdPatch import app.revanced.patches.music.utils.settings.CategoryType import app.revanced.patches.music.utils.settings.SettingsPatch -import app.revanced.util.getTargetIndex -import app.revanced.util.getTargetIndexWithMethodReferenceName +import app.revanced.util.getTargetIndexOrThrow +import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow import app.revanced.util.getWideLiteralInstructionIndex import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow @@ -64,7 +64,7 @@ object NavigationBarComponentsPatch : BaseBytecodePatch( TabLayoutTextFingerprint.resultOrThrow().let { it.mutableMethod.apply { val constIndex = getWideLiteralInstructionIndex(SharedResourceIdPatch.Text1) - val targetIndex = getTargetIndex(constIndex, Opcode.CHECK_CAST) + val targetIndex = getTargetIndexOrThrow(constIndex, Opcode.CHECK_CAST) val targetParameter = getInstruction(targetIndex).reference val targetRegister = getInstruction(targetIndex).registerA @@ -98,9 +98,9 @@ object NavigationBarComponentsPatch : BaseBytecodePatch( it.mutableMethod.apply { val enumIndex = it.scanResult.patternScanResult!!.startIndex + 3 val enumRegister = getInstruction(enumIndex).registerA - val insertEnumIndex = getTargetIndex(Opcode.AND_INT_LIT8) - 2 + val insertEnumIndex = getTargetIndexOrThrow(Opcode.AND_INT_LIT8) - 2 - val pivotTabIndex = getTargetIndexWithMethodReferenceName("getVisibility") + val pivotTabIndex = getTargetIndexWithMethodReferenceNameOrThrow("getVisibility") val pivotTabRegister = getInstruction(pivotTabIndex).registerC addInstruction( diff --git a/src/main/kotlin/app/revanced/patches/music/player/components/PlayerComponentsPatch.kt b/src/main/kotlin/app/revanced/patches/music/player/components/PlayerComponentsPatch.kt index 42cbcfec6..eeb09a26d 100644 --- a/src/main/kotlin/app/revanced/patches/music/player/components/PlayerComponentsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/player/components/PlayerComponentsPatch.kt @@ -51,9 +51,9 @@ import app.revanced.patches.music.utils.videotype.VideoTypeHookPatch import app.revanced.patches.shared.litho.LithoFilterPatch import app.revanced.util.getReference 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.getWalkerMethod import app.revanced.util.getWideLiteralInstructionIndex import app.revanced.util.literalInstructionBooleanHook @@ -133,8 +133,8 @@ object PlayerComponentsPatch : BaseBytecodePatch( colorMathPlayerMethodParameter = parameters val relativeIndex = it.scanResult.patternScanResult!!.endIndex + 1 - val invokeVirtualIndex = getTargetIndex(relativeIndex, Opcode.INVOKE_VIRTUAL) - val iGetIndex = getTargetIndex(relativeIndex, Opcode.IGET) + val invokeVirtualIndex = getTargetIndexOrThrow(relativeIndex, Opcode.INVOKE_VIRTUAL) + val iGetIndex = getTargetIndexOrThrow(relativeIndex, Opcode.IGET) colorMathPlayerInvokeVirtualReference = getInstruction(invokeVirtualIndex).reference @@ -142,11 +142,11 @@ object PlayerComponentsPatch : BaseBytecodePatch( getInstruction(iGetIndex).reference // black player background - val invokeDirectIndex = getTargetIndex(Opcode.INVOKE_DIRECT) + val invokeDirectIndex = getTargetIndexOrThrow(Opcode.INVOKE_DIRECT) val targetMethod = getWalkerMethod(context, invokeDirectIndex) targetMethod.apply { - val insertIndex = getTargetIndex(0, Opcode.IF_NE) + val insertIndex = getTargetIndexOrThrow(0, Opcode.IF_NE) addInstructions( insertIndex, """ @@ -161,7 +161,7 @@ object PlayerComponentsPatch : BaseBytecodePatch( parentResult.mutableMethod.apply { val colorGreyIndex = getWideLiteralInstructionIndex(ColorGrey) - val iPutIndex = getTargetIndex(colorGreyIndex, Opcode.IPUT) + val iPutIndex = getTargetIndexOrThrow(colorGreyIndex, Opcode.IPUT) colorMathPlayerIPutReference = getInstruction(iPutIndex).reference @@ -175,10 +175,7 @@ object PlayerComponentsPatch : BaseBytecodePatch( mutableMethod.apply { val freeRegister = implementation!!.registerCount - parameters.size - 3 - val invokeDirectIndex = getTargetIndexReversed( - implementation!!.instructions.size - 1, - Opcode.INVOKE_DIRECT - ) + val invokeDirectIndex = getTargetIndexReversedOrThrow(Opcode.INVOKE_DIRECT) val invokeDirectReference = getInstruction(invokeDirectIndex).reference @@ -339,9 +336,9 @@ object PlayerComponentsPatch : BaseBytecodePatch( reversed: Boolean ): Reference { val targetIndex = if (reversed) - getTargetIndexReversed(swipeToDismissWidgetIndex, opcode) + getTargetIndexReversedOrThrow(swipeToDismissWidgetIndex, opcode) else - getTargetIndex(swipeToDismissWidgetIndex, opcode) + getTargetIndexOrThrow(swipeToDismissWidgetIndex, opcode) return getInstruction(targetIndex).reference } @@ -369,7 +366,7 @@ object PlayerComponentsPatch : BaseBytecodePatch( it.mutableMethod.apply { val stringIndex = getStringInstructionIndex("INTERACTION_LOGGING_GESTURE_TYPE_SWIPE") - val sPutObjectIndex = getTargetIndex(stringIndex, Opcode.SPUT_OBJECT) + val sPutObjectIndex = getTargetIndexOrThrow(stringIndex, Opcode.SPUT_OBJECT) swipeToDismissSGetObjectReference = getInstruction(sPutObjectIndex).reference @@ -407,7 +404,7 @@ object PlayerComponentsPatch : BaseBytecodePatch( dismissBehaviorMethod.apply { val insertIndex = - getTargetIndexWithFieldReferenceType("Ljava/util/concurrent/atomic/AtomicBoolean;") + getTargetIndexWithFieldReferenceTypeOrThrow("Ljava/util/concurrent/atomic/AtomicBoolean;") val primaryRegister = getInstruction(insertIndex).registerB val secondaryRegister = primaryRegister + 1 @@ -530,7 +527,7 @@ object PlayerComponentsPatch : BaseBytecodePatch( SwitchToggleColorFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val invokeDirectIndex = getTargetIndex(Opcode.INVOKE_DIRECT) + val invokeDirectIndex = getTargetIndexOrThrow(Opcode.INVOKE_DIRECT) val walkerMethod = getWalkerMethod(context, invokeDirectIndex) walkerMethod.addInstructions( @@ -563,7 +560,7 @@ object PlayerComponentsPatch : BaseBytecodePatch( AudioVideoSwitchToggleFingerprint.resultOrThrow().mutableMethod.apply { val constIndex = getWideLiteralInstructionIndex(AudioVideoSwitchToggle) - val viewIndex = getTargetIndex(constIndex, Opcode.MOVE_RESULT_OBJECT) + val viewIndex = getTargetIndexOrThrow(constIndex, Opcode.MOVE_RESULT_OBJECT) val viewRegister = getInstruction(viewIndex).registerA addInstruction( @@ -659,7 +656,7 @@ object PlayerComponentsPatch : BaseBytecodePatch( val startIndex = it.scanResult.patternScanResult!!.startIndex val endIndex = it.scanResult.patternScanResult!!.endIndex val imageViewIndex = - getTargetIndexWithFieldReferenceType("Landroid/widget/ImageView;") + getTargetIndexWithFieldReferenceTypeOrThrow("Landroid/widget/ImageView;") val shuffleReference1 = getInstruction(startIndex).reference val shuffleReference2 = @@ -833,7 +830,7 @@ object PlayerComponentsPatch : BaseBytecodePatch( val miniPlayerPlayPauseReplayButtonRegister = getInstruction(miniPlayerPlayPauseReplayButtonIndex).registerA val findViewByIdIndex = - getTargetIndex(miniPlayerPlayPauseReplayButtonIndex, Opcode.INVOKE_VIRTUAL) + getTargetIndexOrThrow(miniPlayerPlayPauseReplayButtonIndex, Opcode.INVOKE_VIRTUAL) val parentViewRegister = getInstruction(findViewByIdIndex).registerC @@ -856,7 +853,7 @@ object PlayerComponentsPatch : BaseBytecodePatch( val constRegister = getInstruction(miniPlayerPlayPauseReplayButtonIndex).registerA val findViewByIdIndex = - getTargetIndex(miniPlayerPlayPauseReplayButtonIndex, Opcode.INVOKE_VIRTUAL) + getTargetIndexOrThrow(miniPlayerPlayPauseReplayButtonIndex, Opcode.INVOKE_VIRTUAL) val findViewByIdRegister = getInstruction(findViewByIdIndex).registerC @@ -874,7 +871,7 @@ object PlayerComponentsPatch : BaseBytecodePatch( val miniPlayerPlayPauseReplayButtonIndex = getWideLiteralInstructionIndex(MiniPlayerPlayPauseReplayButton) val invokeStaticIndex = - getTargetIndex(miniPlayerPlayPauseReplayButtonIndex, Opcode.INVOKE_STATIC) + getTargetIndexOrThrow(miniPlayerPlayPauseReplayButtonIndex, Opcode.INVOKE_STATIC) val viewArrayRegister = getInstruction(invokeStaticIndex).registerC addInstructions( @@ -892,7 +889,7 @@ object PlayerComponentsPatch : BaseBytecodePatch( fieldName: String ) { val startIndex = getStringInstructionIndex(intentString) - val onClickIndex = getTargetIndexReversed(startIndex, Opcode.INVOKE_VIRTUAL) + val onClickIndex = getTargetIndexReversedOrThrow(startIndex, Opcode.INVOKE_VIRTUAL) val onClickReference = getInstruction(onClickIndex).reference val onClickReferenceDefiningClass = (onClickReference as MethodReference).definingClass diff --git a/src/main/kotlin/app/revanced/patches/music/utils/fix/header/RestoreOldHeaderPatch.kt b/src/main/kotlin/app/revanced/patches/music/utils/fix/header/RestoreOldHeaderPatch.kt index c245f8bcf..00f504fec 100644 --- a/src/main/kotlin/app/revanced/patches/music/utils/fix/header/RestoreOldHeaderPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/utils/fix/header/RestoreOldHeaderPatch.kt @@ -7,7 +7,7 @@ import app.revanced.patcher.patch.BytecodePatch import app.revanced.patcher.patch.annotation.Patch import app.revanced.patches.music.layout.header.ChangeHeaderPatch import app.revanced.patches.music.utils.fix.header.fingerprints.HeaderSwitchConfigFingerprint -import app.revanced.util.getTargetIndex +import app.revanced.util.getTargetIndexOrThrow import app.revanced.util.getWideLiteralInstructionIndex import com.android.tools.smali.dexlib2.Opcode import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction @@ -33,7 +33,7 @@ object RestoreOldHeaderPatch : BytecodePatch( HeaderSwitchConfigFingerprint.result?.let { it.mutableMethod.apply { val targetIndex = - getTargetIndex(getWideLiteralInstructionIndex(45617851), Opcode.MOVE_RESULT) + getTargetIndexOrThrow(getWideLiteralInstructionIndex(45617851), Opcode.MOVE_RESULT) val targetRegister = getInstruction(targetIndex).registerA addInstruction( diff --git a/src/main/kotlin/app/revanced/patches/music/utils/returnyoutubedislike/ReturnYouTubeDislikeBytecodePatch.kt b/src/main/kotlin/app/revanced/patches/music/utils/returnyoutubedislike/ReturnYouTubeDislikeBytecodePatch.kt index 94f783970..7c7223d9f 100644 --- a/src/main/kotlin/app/revanced/patches/music/utils/returnyoutubedislike/ReturnYouTubeDislikeBytecodePatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/utils/returnyoutubedislike/ReturnYouTubeDislikeBytecodePatch.kt @@ -13,7 +13,7 @@ import app.revanced.patches.music.utils.returnyoutubedislike.fingerprints.LikeFi import app.revanced.patches.music.utils.returnyoutubedislike.fingerprints.RemoveLikeFingerprint import app.revanced.patches.music.utils.returnyoutubedislike.fingerprints.TextComponentFingerprint import app.revanced.patches.music.video.videoid.VideoIdPatch -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.OneRegisterInstruction @@ -53,7 +53,7 @@ object ReturnYouTubeDislikeBytecodePatch : BytecodePatch( TextComponentFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val insertIndex = indexOfFirstInstruction { + val insertIndex = indexOfFirstInstructionOrThrow { opcode == Opcode.INVOKE_STATIC && (this as ReferenceInstruction).reference.toString() .endsWith("Ljava/lang/CharSequence;") diff --git a/src/main/kotlin/app/revanced/patches/music/utils/settings/SettingsBytecodePatch.kt b/src/main/kotlin/app/revanced/patches/music/utils/settings/SettingsBytecodePatch.kt index 40663bd8b..d78940ff1 100644 --- a/src/main/kotlin/app/revanced/patches/music/utils/settings/SettingsBytecodePatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/utils/settings/SettingsBytecodePatch.kt @@ -17,7 +17,7 @@ import app.revanced.patches.music.utils.settings.fingerprints.PreferenceFingerpr import app.revanced.patches.music.utils.settings.fingerprints.SettingsHeadersFragmentFingerprint import app.revanced.patches.shared.fingerprints.SharedSettingFingerprint import app.revanced.patches.shared.integrations.Constants.INTEGRATIONS_UTILS_CLASS_DESCRIPTOR -import app.revanced.util.getTargetIndex +import app.revanced.util.getTargetIndexOrThrow import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.Opcode import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction @@ -52,7 +52,7 @@ object SettingsBytecodePatch : BytecodePatch( */ SharedSettingFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val stringIndex = getTargetIndex(Opcode.CONST_STRING) + val stringIndex = getTargetIndexOrThrow(Opcode.CONST_STRING) val stringRegister = getInstruction(stringIndex).registerA replaceInstruction( diff --git a/src/main/kotlin/app/revanced/patches/music/utils/sponsorblock/SponsorBlockBytecodePatch.kt b/src/main/kotlin/app/revanced/patches/music/utils/sponsorblock/SponsorBlockBytecodePatch.kt index e1e1160d5..56521cdae 100644 --- a/src/main/kotlin/app/revanced/patches/music/utils/sponsorblock/SponsorBlockBytecodePatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/utils/sponsorblock/SponsorBlockBytecodePatch.kt @@ -14,8 +14,8 @@ import app.revanced.patches.music.utils.sponsorblock.fingerprints.MusicPlaybackC import app.revanced.patches.music.utils.sponsorblock.fingerprints.SeekbarOnDrawFingerprint import app.revanced.patches.music.video.information.VideoInformationPatch import app.revanced.patches.music.video.videoid.VideoIdPatch -import app.revanced.util.getTargetIndexWithMethodReferenceName -import app.revanced.util.getTargetIndexWithMethodReferenceNameReversed +import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow +import app.revanced.util.getTargetIndexWithMethodReferenceNameReversedOrThrow import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction @@ -70,7 +70,7 @@ object SponsorBlockBytecodePatch : BytecodePatch( ) // Set seekbar thickness - val roundIndex = getTargetIndexWithMethodReferenceName("round") + 1 + val roundIndex = getTargetIndexWithMethodReferenceNameOrThrow("round") + 1 val roundRegister = getInstruction(roundIndex).registerA addInstruction( roundIndex + 1, @@ -79,7 +79,7 @@ object SponsorBlockBytecodePatch : BytecodePatch( ) // Draw segment - val drawCircleIndex = getTargetIndexWithMethodReferenceNameReversed("drawCircle") + val drawCircleIndex = getTargetIndexWithMethodReferenceNameReversedOrThrow("drawCircle") val drawCircleInstruction = getInstruction(drawCircleIndex) addInstruction( drawCircleIndex, @@ -114,7 +114,7 @@ object SponsorBlockBytecodePatch : BytecodePatch( ) // Draw segment - val drawCircleIndex = getTargetIndexWithMethodReferenceName("drawCircle") + val drawCircleIndex = getTargetIndexWithMethodReferenceNameOrThrow("drawCircle") val drawCircleInstruction = getInstruction(drawCircleIndex) addInstruction( drawCircleIndex, diff --git a/src/main/kotlin/app/revanced/patches/music/video/information/VideoInformationPatch.kt b/src/main/kotlin/app/revanced/patches/music/video/information/VideoInformationPatch.kt index 940ab152a..f01fc4a54 100644 --- a/src/main/kotlin/app/revanced/patches/music/video/information/VideoInformationPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/video/information/VideoInformationPatch.kt @@ -23,8 +23,8 @@ import app.revanced.patches.music.video.information.fingerprints.VideoQualityLis import app.revanced.patches.music.video.information.fingerprints.VideoQualityTextFingerprint import app.revanced.patches.music.video.videoid.VideoIdPatch import app.revanced.util.addFieldAndInstructions -import app.revanced.util.getTargetIndexWithFieldReferenceTypeReversed -import app.revanced.util.getTargetIndexWithMethodReferenceNameReversed +import app.revanced.util.getTargetIndexWithFieldReferenceTypeReversedOrThrow +import app.revanced.util.getTargetIndexWithMethodReferenceNameReversedOrThrow import app.revanced.util.getWalkerMethod import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.AccessFlags @@ -138,8 +138,8 @@ object VideoInformationPatch : BytecodePatch( ) VideoLengthFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val invalidateIndex = getTargetIndexWithMethodReferenceNameReversed("invalidate") - val rectangleIndex = getTargetIndexWithFieldReferenceTypeReversed( + val invalidateIndex = getTargetIndexWithMethodReferenceNameReversedOrThrow("invalidate") + val rectangleIndex = getTargetIndexWithFieldReferenceTypeReversedOrThrow( invalidateIndex + 1, "Landroid/graphics/Rect;" ) diff --git a/src/main/kotlin/app/revanced/patches/music/video/playback/VideoPlaybackPatch.kt b/src/main/kotlin/app/revanced/patches/music/video/playback/VideoPlaybackPatch.kt index 88a235e58..9ac5bbd5e 100644 --- a/src/main/kotlin/app/revanced/patches/music/video/playback/VideoPlaybackPatch.kt +++ b/src/main/kotlin/app/revanced/patches/music/video/playback/VideoPlaybackPatch.kt @@ -13,7 +13,7 @@ import app.revanced.patches.music.video.information.VideoInformationPatch import app.revanced.patches.music.video.playback.fingerprints.PlaybackSpeedBottomSheetFingerprint import app.revanced.patches.music.video.playback.fingerprints.UserQualityChangeFingerprint import app.revanced.patches.music.video.videoid.VideoIdPatch -import app.revanced.util.getTargetIndex +import app.revanced.util.getTargetIndexOrThrow import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.Opcode @@ -52,7 +52,7 @@ object VideoPlaybackPatch : BaseBytecodePatch( it.mutableClass.methods.find { method -> method.name == "onItemClick" } onItemClickMethod?.apply { - val targetIndex = getTargetIndex(Opcode.IGET) + val targetIndex = getTargetIndexOrThrow(Opcode.IGET) val targetRegister = getInstruction(targetIndex).registerA diff --git a/src/main/kotlin/app/revanced/patches/reddit/ad/general/AdsPatch.kt b/src/main/kotlin/app/revanced/patches/reddit/ad/general/AdsPatch.kt index be7f9945d..de989f069 100644 --- a/src/main/kotlin/app/revanced/patches/reddit/ad/general/AdsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/reddit/ad/general/AdsPatch.kt @@ -12,8 +12,8 @@ import app.revanced.patches.reddit.utils.compatibility.Constants.COMPATIBLE_PACK import app.revanced.patches.reddit.utils.integrations.Constants.PATCHES_PATH import app.revanced.patches.reddit.utils.settings.SettingsBytecodePatch.updateSettingsStatus import app.revanced.patches.reddit.utils.settings.SettingsPatch -import app.revanced.util.getTargetIndexWithFieldReferenceName -import app.revanced.util.getTargetIndexWithMethodReferenceName +import app.revanced.util.getTargetIndexWithFieldReferenceNameOrThrow +import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction @@ -42,7 +42,7 @@ object AdsPatch : BaseBytecodePatch( // region Filter promoted ads (does not work in popular or latest feed) AdPostFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val targetIndex = getTargetIndexWithFieldReferenceName("children") + val targetIndex = getTargetIndexWithFieldReferenceNameOrThrow("children") val targetRegister = getInstruction(targetIndex).registerA addInstructions( @@ -59,7 +59,7 @@ object AdsPatch : BaseBytecodePatch( // By removing the appending instruction no ad posts gets appended to the feed. NewAdPostFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val targetIndex = getTargetIndexWithMethodReferenceName("add") + val targetIndex = getTargetIndexWithMethodReferenceNameOrThrow("add") val targetInstruction = getInstruction(targetIndex) replaceInstruction( diff --git a/src/main/kotlin/app/revanced/patches/reddit/layout/recentlyvisited/RecentlyVisitedShelfPatch.kt b/src/main/kotlin/app/revanced/patches/reddit/layout/recentlyvisited/RecentlyVisitedShelfPatch.kt index 22361979c..d9338e274 100644 --- a/src/main/kotlin/app/revanced/patches/reddit/layout/recentlyvisited/RecentlyVisitedShelfPatch.kt +++ b/src/main/kotlin/app/revanced/patches/reddit/layout/recentlyvisited/RecentlyVisitedShelfPatch.kt @@ -9,10 +9,10 @@ import app.revanced.patches.reddit.utils.compatibility.Constants.COMPATIBLE_PACK import app.revanced.patches.reddit.utils.integrations.Constants.PATCHES_PATH import app.revanced.patches.reddit.utils.settings.SettingsBytecodePatch.updateSettingsStatus import app.revanced.patches.reddit.utils.settings.SettingsPatch -import app.revanced.util.getTargetIndex -import app.revanced.util.getTargetIndexReversed -import app.revanced.util.getTargetIndexWithFieldReferenceName -import app.revanced.util.getTargetIndexWithReference +import app.revanced.util.getTargetIndexOrThrow +import app.revanced.util.getTargetIndexReversedOrThrow +import app.revanced.util.getTargetIndexWithFieldReferenceNameOrThrow +import app.revanced.util.getTargetIndexWithReferenceOrThrow import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.Opcode @@ -41,19 +41,19 @@ object RecentlyVisitedShelfPatch : BaseBytecodePatch( it.mutableClass.methods.find { method -> method.name == "" } ?.apply { val recentlyVisitedFieldIndex = - getTargetIndexWithFieldReferenceName("RECENTLY_VISITED") + getTargetIndexWithFieldReferenceNameOrThrow("RECENTLY_VISITED") val recentlyVisitedObjectIndex = - getTargetIndex(recentlyVisitedFieldIndex, Opcode.IPUT_OBJECT) + getTargetIndexOrThrow(recentlyVisitedFieldIndex, Opcode.IPUT_OBJECT) recentlyVisitedReference = getInstruction(recentlyVisitedObjectIndex).reference } ?: throw PatchException("Constructor method not found!") it.mutableMethod.apply { val recentlyVisitedObjectIndex = - getTargetIndexWithReference(recentlyVisitedReference.toString()) + getTargetIndexWithReferenceOrThrow(recentlyVisitedReference.toString()) arrayOf( - getTargetIndex(recentlyVisitedObjectIndex, Opcode.INVOKE_STATIC), - getTargetIndexReversed(recentlyVisitedObjectIndex, Opcode.INVOKE_STATIC) + getTargetIndexOrThrow(recentlyVisitedObjectIndex, Opcode.INVOKE_STATIC), + getTargetIndexReversedOrThrow(recentlyVisitedObjectIndex, Opcode.INVOKE_STATIC) ).forEach { staticIndex -> val insertRegister = getInstruction(staticIndex + 1).registerA diff --git a/src/main/kotlin/app/revanced/patches/reddit/utils/settings/SettingsBytecodePatch.kt b/src/main/kotlin/app/revanced/patches/reddit/utils/settings/SettingsBytecodePatch.kt index 1b834e30a..6e8766941 100644 --- a/src/main/kotlin/app/revanced/patches/reddit/utils/settings/SettingsBytecodePatch.kt +++ b/src/main/kotlin/app/revanced/patches/reddit/utils/settings/SettingsBytecodePatch.kt @@ -15,7 +15,7 @@ import app.revanced.patches.reddit.utils.settings.fingerprints.AcknowledgementsL import app.revanced.patches.reddit.utils.settings.fingerprints.OssLicensesMenuActivityOnCreateFingerprint import app.revanced.patches.reddit.utils.settings.fingerprints.SettingsStatusLoadFingerprint import app.revanced.patches.shared.fingerprints.SharedSettingFingerprint -import app.revanced.util.getTargetIndex +import app.revanced.util.getTargetIndexOrThrow import app.revanced.util.getWideLiteralInstructionIndex import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.Opcode @@ -49,7 +49,7 @@ object SettingsBytecodePatch : BytecodePatch( */ SharedSettingFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val stringIndex = getTargetIndex(Opcode.CONST_STRING) + val stringIndex = getTargetIndexOrThrow(Opcode.CONST_STRING) val stringRegister = getInstruction(stringIndex).registerA replaceInstruction( diff --git a/src/main/kotlin/app/revanced/patches/shared/ads/BaseAdsPatch.kt b/src/main/kotlin/app/revanced/patches/shared/ads/BaseAdsPatch.kt index 22bea5d06..590371d12 100644 --- a/src/main/kotlin/app/revanced/patches/shared/ads/BaseAdsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/shared/ads/BaseAdsPatch.kt @@ -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(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(dialogIndex).registerC - val freeIndex = getTargetIndex(dialogIndex, Opcode.IF_EQZ) + val freeIndex = getTargetIndexOrThrow(dialogIndex, Opcode.IF_EQZ) val freeRegister = getInstruction(freeIndex).registerA addInstructions( diff --git a/src/main/kotlin/app/revanced/patches/shared/customspeed/BaseCustomPlaybackSpeedPatch.kt b/src/main/kotlin/app/revanced/patches/shared/customspeed/BaseCustomPlaybackSpeedPatch.kt index 1ecd2a24d..ca1e9b1de 100644 --- a/src/main/kotlin/app/revanced/patches/shared/customspeed/BaseCustomPlaybackSpeedPatch.kt +++ b/src/main/kotlin/app/revanced/patches/shared/customspeed/BaseCustomPlaybackSpeedPatch.kt @@ -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(sizeIndex).registerA addInstructions( @@ -49,7 +49,7 @@ abstract class BaseCustomPlaybackSpeedPatch( """ ) - val arrayIndex = getTargetIndexWithFieldReferenceType("[F") + val arrayIndex = getTargetIndexWithFieldReferenceTypeOrThrow("[F") val arrayRegister = getInstruction(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(limiterMinConstIndex).registerA diff --git a/src/main/kotlin/app/revanced/patches/shared/dialog/BaseViewerDiscretionDialogPatch.kt b/src/main/kotlin/app/revanced/patches/shared/dialog/BaseViewerDiscretionDialogPatch.kt index 4fbfd9fa3..93473d2ba 100644 --- a/src/main/kotlin/app/revanced/patches/shared/dialog/BaseViewerDiscretionDialogPatch.kt +++ b/src/main/kotlin/app/revanced/patches/shared/dialog/BaseViewerDiscretionDialogPatch.kt @@ -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(showDialogIndex).registerC val methodName = diff --git a/src/main/kotlin/app/revanced/patches/shared/drawable/DrawableColorPatch.kt b/src/main/kotlin/app/revanced/patches/shared/drawable/DrawableColorPatch.kt index 616d49143..cf7ee092d 100644 --- a/src/main/kotlin/app/revanced/patches/shared/drawable/DrawableColorPatch.kt +++ b/src/main/kotlin/app/revanced/patches/shared/drawable/DrawableColorPatch.kt @@ -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(insertIndex).registerD } } diff --git a/src/main/kotlin/app/revanced/patches/shared/litho/LithoFilterPatch.kt b/src/main/kotlin/app/revanced/patches/shared/litho/LithoFilterPatch.kt index ebd13581e..eac47709e 100644 --- a/src/main/kotlin/app/revanced/patches/shared/litho/LithoFilterPatch.kt +++ b/src/main/kotlin/app/revanced/patches/shared/litho/LithoFilterPatch.kt @@ -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(stringBuilderIndex).registerA val emptyStringIndex = getStringInstructionIndex("") - val identifierIndex = getTargetIndexReversed(emptyStringIndex, Opcode.IPUT_OBJECT) + val identifierIndex = getTargetIndexReversedOrThrow(emptyStringIndex, Opcode.IPUT_OBJECT) val identifierRegister = getInstruction(identifierIndex).registerA - val objectIndex = getTargetIndex(emptyStringIndex, Opcode.INVOKE_VIRTUAL) + val objectIndex = getTargetIndexOrThrow(emptyStringIndex, Opcode.INVOKE_VIRTUAL) val objectRegister = getInstruction(objectIndex).registerC val insertIndex = stringBuilderIndex + 1 diff --git a/src/main/kotlin/app/revanced/patches/shared/mainactivity/BaseMainActivityResolvePatch.kt b/src/main/kotlin/app/revanced/patches/shared/mainactivity/BaseMainActivityResolvePatch.kt index 1cca74bba..bc9d883d3 100644 --- a/src/main/kotlin/app/revanced/patches/shared/mainactivity/BaseMainActivityResolvePatch.kt +++ b/src/main/kotlin/app/revanced/patches/shared/mainactivity/BaseMainActivityResolvePatch.kt @@ -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) = diff --git a/src/main/kotlin/app/revanced/patches/shared/opus/BaseOpusCodecsPatch.kt b/src/main/kotlin/app/revanced/patches/shared/opus/BaseOpusCodecsPatch.kt index e57e69ebe..6a8984cf2 100644 --- a/src/main/kotlin/app/revanced/patches/shared/opus/BaseOpusCodecsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/shared/opus/BaseOpusCodecsPatch.kt @@ -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(targetIndex).reference } } diff --git a/src/main/kotlin/app/revanced/patches/shared/spoofappversion/BaseSpoofAppVersionPatch.kt b/src/main/kotlin/app/revanced/patches/shared/spoofappversion/BaseSpoofAppVersionPatch.kt index a72c71af9..9bcca0159 100644 --- a/src/main/kotlin/app/revanced/patches/shared/spoofappversion/BaseSpoofAppVersionPatch.kt +++ b/src/main/kotlin/app/revanced/patches/shared/spoofappversion/BaseSpoofAppVersionPatch.kt @@ -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(insertIndex).registerA addInstructions( diff --git a/src/main/kotlin/app/revanced/patches/youtube/feed/components/FeedComponentsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/feed/components/FeedComponentsPatch.kt index c0ebc8958..0e513c8f7 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/feed/components/FeedComponentsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/feed/components/FeedComponentsPatch.kt @@ -33,11 +33,11 @@ import app.revanced.patches.youtube.utils.playertype.PlayerTypeHookPatch import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.CaptionToggleContainer import app.revanced.patches.youtube.utils.settings.SettingsPatch -import app.revanced.util.getTargetIndex -import app.revanced.util.getTargetIndexReversed +import app.revanced.util.getTargetIndexOrThrow +import app.revanced.util.getTargetIndexReversedOrThrow import app.revanced.util.getTargetIndexWithMethodReferenceName import app.revanced.util.getWideLiteralInstructionIndex -import app.revanced.util.indexOfFirstInstruction +import app.revanced.util.indexOfFirstInstructionOrThrow import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.Opcode @@ -114,7 +114,7 @@ object FeedComponentsPatch : BaseBytecodePatch( CaptionsButtonFingerprint.resultOrThrow().mutableMethod.apply { val constIndex = getWideLiteralInstructionIndex(CaptionToggleContainer) - val insertIndex = getTargetIndexReversed(constIndex, Opcode.IF_EQZ) + val insertIndex = getTargetIndexReversedOrThrow(constIndex, Opcode.IF_EQZ) val insertRegister = getInstruction(insertIndex).registerA addInstructions( @@ -127,7 +127,7 @@ object FeedComponentsPatch : BaseBytecodePatch( CaptionsButtonSyntheticFingerprint.resultOrThrow().mutableMethod.apply { val constIndex = getWideLiteralInstructionIndex(CaptionToggleContainer) - val targetIndex = getTargetIndex(constIndex, Opcode.MOVE_RESULT_OBJECT) + val targetIndex = getTargetIndexOrThrow(constIndex, Opcode.MOVE_RESULT_OBJECT) val targetRegister = getInstruction(targetIndex).registerA addInstruction( @@ -190,7 +190,7 @@ object FeedComponentsPatch : BaseBytecodePatch( ElementParserFingerprint.resultOrThrow().let { it.mutableMethod.apply { val freeRegister = implementation!!.registerCount - parameters.size - 2 - val insertIndex = indexOfFirstInstruction { + val insertIndex = indexOfFirstInstructionOrThrow { val reference = ((this as? ReferenceInstruction)?.reference as? MethodReference) reference?.parameterTypes?.size == 1 @@ -198,7 +198,7 @@ object FeedComponentsPatch : BaseBytecodePatch( && reference.returnType.startsWith("L") } - val objectIndex = getTargetIndex(Opcode.MOVE_OBJECT) + val objectIndex = getTargetIndexOrThrow(Opcode.MOVE_OBJECT) val objectRegister = getInstruction(objectIndex).registerA val jumpIndex = it.scanResult.patternScanResult!!.startIndex @@ -257,7 +257,7 @@ object FeedComponentsPatch : BaseBytecodePatch( val iteratorRegister = getInstruction(iteratorIndex).registerC - val targetIndex = indexOfFirstInstruction { + val targetIndex = indexOfFirstInstructionOrThrow { val reference = ((this as? ReferenceInstruction)?.reference as? MethodReference) opcode == Opcode.INVOKE_INTERFACE @@ -265,7 +265,7 @@ object FeedComponentsPatch : BaseBytecodePatch( && reference.parameterTypes == channelTabBuilderMethod.parameterTypes } - val objectIndex = getTargetIndexReversed(targetIndex, Opcode.IGET_OBJECT) + val objectIndex = getTargetIndexReversedOrThrow(targetIndex, Opcode.IGET_OBJECT) val objectInstruction = getInstruction(objectIndex) val objectReference = getInstruction(objectIndex).reference diff --git a/src/main/kotlin/app/revanced/patches/youtube/general/audiotracks/AudioTracksPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/general/audiotracks/AudioTracksPatch.kt index 0cacad87c..ca9efa1cb 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/general/audiotracks/AudioTracksPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/general/audiotracks/AudioTracksPatch.kt @@ -7,8 +7,8 @@ import app.revanced.patches.youtube.general.audiotracks.fingerprints.StreamingMo import app.revanced.patches.youtube.utils.compatibility.Constants.COMPATIBLE_PACKAGE import app.revanced.patches.youtube.utils.integrations.Constants.GENERAL_CLASS_DESCRIPTOR import app.revanced.patches.youtube.utils.settings.SettingsPatch -import app.revanced.util.getTargetIndexWithReference -import app.revanced.util.indexOfFirstInstruction +import app.revanced.util.getTargetIndexWithReferenceOrThrow +import app.revanced.util.indexOfFirstInstructionOrThrow import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.Opcode @@ -28,16 +28,16 @@ object AudioTracksPatch : BaseBytecodePatch( StreamingModelBuilderFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val formatStreamModelIndex = indexOfFirstInstruction { + val formatStreamModelIndex = indexOfFirstInstructionOrThrow { opcode == Opcode.CHECK_CAST && (this as ReferenceInstruction).reference.toString() == "Lcom/google/android/libraries/youtube/innertube/model/media/FormatStreamModel;" } - val arrayListIndex = getTargetIndexWithReference( + val arrayListIndex = getTargetIndexWithReferenceOrThrow( formatStreamModelIndex, "Ljava/util/List;->add(Ljava/lang/Object;)Z" ) val insertIndex = - getTargetIndexWithReference(arrayListIndex, "Ljava/util/List;->isEmpty()Z") + 2 + getTargetIndexWithReferenceOrThrow(arrayListIndex, "Ljava/util/List;->isEmpty()Z") + 2 val formatStreamModelRegister = getInstruction(formatStreamModelIndex).registerA diff --git a/src/main/kotlin/app/revanced/patches/youtube/general/components/LayoutComponentsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/general/components/LayoutComponentsPatch.kt index 24bf7483c..7ed0dbf65 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/general/components/LayoutComponentsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/general/components/LayoutComponentsPatch.kt @@ -27,8 +27,8 @@ import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.AccountSwitcherAccessibility import app.revanced.patches.youtube.utils.settings.SettingsPatch import app.revanced.patches.youtube.utils.viewgroup.ViewGroupMarginLayoutParamsHookPatch -import app.revanced.util.getTargetIndex -import app.revanced.util.getTargetIndexWithFieldReferenceType +import app.revanced.util.getTargetIndexOrThrow +import app.revanced.util.getTargetIndexWithFieldReferenceTypeOrThrow import app.revanced.util.getTargetIndexWithMethodReferenceName import app.revanced.util.getWideLiteralInstructionIndex import app.revanced.util.patch.BaseBytecodePatch @@ -173,7 +173,7 @@ object LayoutComponentsPatch : BaseBytecodePatch( AccountSwitcherAccessibilityLabelFingerprint.resultOrThrow().let { it.mutableMethod.apply { val constIndex = getWideLiteralInstructionIndex(AccountSwitcherAccessibility) - val insertIndex = getTargetIndex(constIndex, Opcode.IF_EQZ) + val insertIndex = getTargetIndexOrThrow(constIndex, Opcode.IF_EQZ) val setVisibilityIndex = getTargetIndexWithMethodReferenceName(insertIndex, "setVisibility") val visibilityRegister = @@ -195,7 +195,7 @@ object LayoutComponentsPatch : BaseBytecodePatch( SettingsMenuFingerprint.resultOrThrow().let { it.mutableMethod.apply { val insertIndex = - getTargetIndexWithFieldReferenceType("Landroid/support/v7/widget/RecyclerView;") + getTargetIndexWithFieldReferenceTypeOrThrow("Landroid/support/v7/widget/RecyclerView;") val insertRegister = getInstruction(insertIndex).registerA addInstruction( @@ -229,7 +229,7 @@ object LayoutComponentsPatch : BaseBytecodePatch( TooltipContentFullscreenFingerprint.resultOrThrow().mutableMethod.apply { val literalIndex = getWideLiteralInstructionIndex(45384061) - val targetIndex = getTargetIndex(literalIndex, Opcode.MOVE_RESULT) + val targetIndex = getTargetIndexOrThrow(literalIndex, Opcode.MOVE_RESULT) val targetRegister = getInstruction(targetIndex).registerA addInstruction( diff --git a/src/main/kotlin/app/revanced/patches/youtube/general/layoutswitch/LayoutSwitchPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/general/layoutswitch/LayoutSwitchPatch.kt index 821a473e5..9b38b5f04 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/general/layoutswitch/LayoutSwitchPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/general/layoutswitch/LayoutSwitchPatch.kt @@ -10,8 +10,8 @@ import app.revanced.patches.youtube.general.layoutswitch.fingerprints.LayoutSwit import app.revanced.patches.youtube.utils.compatibility.Constants.COMPATIBLE_PACKAGE import app.revanced.patches.youtube.utils.integrations.Constants.GENERAL_CLASS_DESCRIPTOR import app.revanced.patches.youtube.utils.settings.SettingsPatch -import app.revanced.util.getTargetIndex -import app.revanced.util.getTargetIndexReversed +import app.revanced.util.getTargetIndexOrThrow +import app.revanced.util.getTargetIndexReversedOrThrow import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.Opcode @@ -34,7 +34,7 @@ object LayoutSwitchPatch : BaseBytecodePatch( GetFormFactorFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val jumpIndex = getTargetIndexReversed(Opcode.SGET_OBJECT) + val jumpIndex = getTargetIndexReversedOrThrow(Opcode.SGET_OBJECT) addInstructionsWithLabels( 0, """ @@ -56,7 +56,7 @@ object LayoutSwitchPatch : BaseBytecodePatch( LayoutSwitchFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val insertIndex = getTargetIndex(Opcode.IF_NEZ) + val insertIndex = getTargetIndexOrThrow(Opcode.IF_NEZ) val insertRegister = getInstruction(insertIndex).registerA addInstructions( diff --git a/src/main/kotlin/app/revanced/patches/youtube/general/navigation/NavigationBarComponentsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/general/navigation/NavigationBarComponentsPatch.kt index f81c3354e..cc81f0930 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/general/navigation/NavigationBarComponentsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/general/navigation/NavigationBarComponentsPatch.kt @@ -13,7 +13,7 @@ import app.revanced.patches.youtube.utils.integrations.Constants.GENERAL_CLASS_D import app.revanced.patches.youtube.utils.navigation.NavigationBarHookPatch import app.revanced.patches.youtube.utils.settings.SettingsPatch import app.revanced.util.getStringInstructionIndex -import app.revanced.util.getTargetIndexWithMethodReferenceName +import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction @@ -82,7 +82,7 @@ object NavigationBarComponentsPatch : BaseBytecodePatch( PivotBarSetTextFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val targetIndex = getTargetIndexWithMethodReferenceName("setText") + val targetIndex = getTargetIndexWithMethodReferenceNameOrThrow("setText") val targetRegister = getInstruction(targetIndex).registerC addInstruction( diff --git a/src/main/kotlin/app/revanced/patches/youtube/general/toolbar/ToolBarComponentsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/general/toolbar/ToolBarComponentsPatch.kt index 3d90b05c5..941a486ec 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/general/toolbar/ToolBarComponentsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/general/toolbar/ToolBarComponentsPatch.kt @@ -39,10 +39,10 @@ import app.revanced.patches.youtube.utils.settings.SettingsPatch.contexts import app.revanced.patches.youtube.utils.toolbar.ToolBarHookPatch import app.revanced.util.REGISTER_TEMPLATE_REPLACEMENT import app.revanced.util.doRecursively -import app.revanced.util.getTargetIndex -import app.revanced.util.getTargetIndexWithMethodReferenceName -import app.revanced.util.getTargetIndexWithReference -import app.revanced.util.getTargetIndexWithReferenceReversed +import app.revanced.util.getTargetIndexOrThrow +import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow +import app.revanced.util.getTargetIndexWithReferenceOrThrow +import app.revanced.util.getTargetIndexWithReferenceReversedOrThrow import app.revanced.util.getWalkerMethod import app.revanced.util.getWideLiteralInstructionIndex import app.revanced.util.literalInstructionHook @@ -129,7 +129,7 @@ object ToolBarComponentsPatch : BaseBytecodePatch( ) DrawerContentViewFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val insertIndex = getTargetIndexWithMethodReferenceName("addView") + val insertIndex = getTargetIndexWithMethodReferenceNameOrThrow("addView") val insertRegister = getInstruction(insertIndex).registerD addInstruction( @@ -145,7 +145,7 @@ object ToolBarComponentsPatch : BaseBytecodePatch( setActionBarRingoMutableClass.methods.first { method -> MethodUtil.isConstructor(method) }.apply { - val insertIndex = getTargetIndex(Opcode.IPUT_BOOLEAN) + val insertIndex = getTargetIndexOrThrow(Opcode.IPUT_BOOLEAN) val insertRegister = getInstruction(insertIndex).registerA addInstruction( @@ -278,12 +278,12 @@ object ToolBarComponentsPatch : BaseBytecodePatch( CreateSearchSuggestionsFingerprint.resultOrThrow().let { result -> result.mutableMethod.apply { val relativeIndex = getWideLiteralInstructionIndex(40) - val replaceIndex = getTargetIndexWithReferenceReversed( + val replaceIndex = getTargetIndexWithReferenceReversedOrThrow( relativeIndex, "Landroid/widget/ImageView;->setVisibility(I)V" ) - 1 - val jumpIndex = getTargetIndexWithReference( + val jumpIndex = getTargetIndexWithReferenceOrThrow( relativeIndex, "Landroid/net/Uri;->parse(Ljava/lang/String;)Landroid/net/Uri;" ) + 4 @@ -333,7 +333,7 @@ object ToolBarComponentsPatch : BaseBytecodePatch( it.mutableMethod.apply { val startIndex = it.scanResult.patternScanResult!!.startIndex val setVisibilityIndex = - getTargetIndexWithMethodReferenceName(startIndex, "setVisibility") + getTargetIndexWithMethodReferenceNameOrThrow(startIndex, "setVisibility") val setVisibilityInstruction = getInstruction(setVisibilityIndex) @@ -349,7 +349,7 @@ object ToolBarComponentsPatch : BaseBytecodePatch( it.mutableMethod.apply { val startIndex = getWideLiteralInstructionIndex(VoiceSearch) val setOnClickListenerIndex = - getTargetIndexWithMethodReferenceName(startIndex, "setOnClickListener") + getTargetIndexWithMethodReferenceNameOrThrow(startIndex, "setOnClickListener") val viewRegister = getInstruction(setOnClickListenerIndex).registerC diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/openlinksdirectly/OpenLinksDirectlyPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/openlinksdirectly/OpenLinksDirectlyPatch.kt index 1606118a0..0846f8970 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/misc/openlinksdirectly/OpenLinksDirectlyPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/openlinksdirectly/OpenLinksDirectlyPatch.kt @@ -8,7 +8,7 @@ import app.revanced.patches.youtube.misc.openlinksdirectly.fingerprints.OpenLink import app.revanced.patches.youtube.utils.compatibility.Constants.COMPATIBLE_PACKAGE import app.revanced.patches.youtube.utils.integrations.Constants.MISC_PATH import app.revanced.patches.youtube.utils.settings.SettingsPatch -import app.revanced.util.getTargetIndexWithMethodReferenceName +import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction @@ -32,7 +32,7 @@ object OpenLinksDirectlyPatch : BaseBytecodePatch( ).forEach { fingerprint -> fingerprint.resultOrThrow().let { it.mutableMethod.apply { - val insertIndex = getTargetIndexWithMethodReferenceName("parse") + val insertIndex = getTargetIndexWithMethodReferenceNameOrThrow("parse") val insertRegister = getInstruction(insertIndex).registerC diff --git a/src/main/kotlin/app/revanced/patches/youtube/player/ambientmode/AmbientModeSwitchPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/player/ambientmode/AmbientModeSwitchPatch.kt index d105c1658..fdf42fca0 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/player/ambientmode/AmbientModeSwitchPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/player/ambientmode/AmbientModeSwitchPatch.kt @@ -11,8 +11,8 @@ import app.revanced.patches.youtube.utils.compatibility.Constants.COMPATIBLE_PAC import app.revanced.patches.youtube.utils.integrations.Constants.PLAYER_CLASS_DESCRIPTOR import app.revanced.patches.youtube.utils.settings.SettingsPatch import app.revanced.util.getStringInstructionIndex -import app.revanced.util.getTargetIndex -import app.revanced.util.getTargetIndexReversed +import app.revanced.util.getTargetIndexOrThrow +import app.revanced.util.getTargetIndexReversedOrThrow import app.revanced.util.literalInstructionBooleanHook import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow @@ -49,9 +49,9 @@ object AmbientModeSwitchPatch : BaseBytecodePatch( getStringInstructionIndex("android.os.action.POWER_SAVE_MODE_CHANGED") val targetIndex = if (reversed) - getTargetIndexReversed(stringIndex, Opcode.INVOKE_DIRECT) + getTargetIndexReversedOrThrow(stringIndex, Opcode.INVOKE_DIRECT) else - getTargetIndex(stringIndex, Opcode.INVOKE_DIRECT) + getTargetIndexOrThrow(stringIndex, Opcode.INVOKE_DIRECT) val targetClass = (getInstruction(targetIndex).reference as MethodReference).definingClass diff --git a/src/main/kotlin/app/revanced/patches/youtube/player/buttons/PlayerButtonsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/player/buttons/PlayerButtonsPatch.kt index 6ee5b6e64..20cb639bf 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/player/buttons/PlayerButtonsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/player/buttons/PlayerButtonsPatch.kt @@ -24,7 +24,7 @@ import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.FullS import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.PlayerCollapseButton import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.TitleAnchor import app.revanced.patches.youtube.utils.settings.SettingsPatch -import app.revanced.util.getTargetIndex +import app.revanced.util.getTargetIndexOrThrow import app.revanced.util.getWideLiteralInstructionIndex import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow @@ -65,7 +65,7 @@ object PlayerButtonsPatch : BaseBytecodePatch( it.mutableMethod.apply { val constIndex = getWideLiteralInstructionIndex(AutoNavToggle) val constRegister = getInstruction(constIndex).registerA - val jumpIndex = getTargetIndex(constIndex + 2, Opcode.INVOKE_VIRTUAL) + 1 + val jumpIndex = getTargetIndexOrThrow(constIndex + 2, Opcode.INVOKE_VIRTUAL) + 1 addInstructionsWithLabels( constIndex, """ @@ -125,7 +125,7 @@ object PlayerButtonsPatch : BaseBytecodePatch( TitleAnchorFingerprint.resultOrThrow().mutableMethod.apply { val titleAnchorConstIndex = getWideLiteralInstructionIndex(TitleAnchor) - val titleAnchorIndex = getTargetIndex(titleAnchorConstIndex, Opcode.MOVE_RESULT_OBJECT) + val titleAnchorIndex = getTargetIndexOrThrow(titleAnchorConstIndex, Opcode.MOVE_RESULT_OBJECT) val titleAnchorRegister = getInstruction(titleAnchorIndex).registerA @@ -137,7 +137,7 @@ object PlayerButtonsPatch : BaseBytecodePatch( val playerCollapseButtonConstIndex = getWideLiteralInstructionIndex(PlayerCollapseButton) val playerCollapseButtonIndex = - getTargetIndex(playerCollapseButtonConstIndex, Opcode.CHECK_CAST) + getTargetIndexOrThrow(playerCollapseButtonConstIndex, Opcode.CHECK_CAST) val playerCollapseButtonRegister = getInstruction(playerCollapseButtonIndex).registerA @@ -158,7 +158,7 @@ object PlayerButtonsPatch : BaseBytecodePatch( (instruction.value as? WideLiteralInstruction)?.wideLiteral == FullScreenButton } val constIndex = buttonCalls.elementAt(buttonCalls.size - 1).index - val castIndex = getTargetIndex(constIndex, Opcode.CHECK_CAST) + val castIndex = getTargetIndexOrThrow(constIndex, Opcode.CHECK_CAST) val insertIndex = castIndex + 1 val insertRegister = getInstruction(castIndex).registerA @@ -179,7 +179,7 @@ object PlayerButtonsPatch : BaseBytecodePatch( PlayerControlsVisibilityModelFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val callIndex = getTargetIndex(Opcode.INVOKE_DIRECT_RANGE) + val callIndex = getTargetIndexOrThrow(Opcode.INVOKE_DIRECT_RANGE) val callInstruction = getInstruction(callIndex) val hasNextParameterRegister = callInstruction.startRegister + HAS_NEXT diff --git a/src/main/kotlin/app/revanced/patches/youtube/player/comments/CommentsComponentPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/player/comments/CommentsComponentPatch.kt index ef259fa2a..132063250 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/player/comments/CommentsComponentPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/player/comments/CommentsComponentPatch.kt @@ -12,7 +12,7 @@ import app.revanced.patches.youtube.utils.integrations.Constants.COMPONENTS_PATH import app.revanced.patches.youtube.utils.integrations.Constants.PLAYER_CLASS_DESCRIPTOR import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch import app.revanced.patches.youtube.utils.settings.SettingsPatch -import app.revanced.util.getTargetIndex +import app.revanced.util.getTargetIndexOrThrow import app.revanced.util.getWalkerMethod import app.revanced.util.getWideLiteralInstructionIndex import app.revanced.util.patch.BaseBytecodePatch @@ -58,12 +58,12 @@ object CommentsComponentPatch : BaseBytecodePatch( it.mutableMethod.apply { val emojiPickerEndpointIndex = getWideLiteralInstructionIndex(126326492) val emojiPickerOnClickListenerIndex = - getTargetIndex(emojiPickerEndpointIndex, Opcode.INVOKE_DIRECT) + getTargetIndexOrThrow(emojiPickerEndpointIndex, Opcode.INVOKE_DIRECT) val emojiPickerOnClickListenerMethod = getWalkerMethod(context, emojiPickerOnClickListenerIndex) emojiPickerOnClickListenerMethod.apply { - val insertIndex = getTargetIndex(Opcode.IF_EQZ) + val insertIndex = getTargetIndexOrThrow(Opcode.IF_EQZ) val insertRegister = getInstruction(insertIndex).registerA diff --git a/src/main/kotlin/app/revanced/patches/youtube/player/components/PlayerComponentsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/player/components/PlayerComponentsPatch.kt index 5b84fbca7..72e068b9b 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/player/components/PlayerComponentsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/player/components/PlayerComponentsPatch.kt @@ -38,9 +38,9 @@ import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.FadeD import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.ScrimOverlay import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.SeekUndoEduOverlayStub import app.revanced.patches.youtube.utils.settings.SettingsPatch -import app.revanced.util.getTargetIndex -import app.revanced.util.getTargetIndexReversed -import app.revanced.util.getTargetIndexWithMethodReferenceName +import app.revanced.util.getTargetIndexOrThrow +import app.revanced.util.getTargetIndexReversedOrThrow +import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow import app.revanced.util.getWideLiteralInstructionIndex import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow @@ -90,7 +90,7 @@ object PlayerComponentsPatch : BaseBytecodePatch( YouTubeControlsOverlayFingerprint.resultOrThrow().let { it.mutableMethod.apply { val constIndex = getWideLiteralInstructionIndex(ScrimOverlay) - val targetIndex = getTargetIndex(constIndex, Opcode.CHECK_CAST) + val targetIndex = getTargetIndexOrThrow(constIndex, Opcode.CHECK_CAST) val targetParameter = getInstruction(targetIndex).reference val targetRegister = getInstruction(targetIndex).registerA @@ -203,7 +203,7 @@ object PlayerComponentsPatch : BaseBytecodePatch( it.mutableMethod.apply { val constIndex = getWideLiteralInstructionIndex(FadeDurationFast) val constRegister = getInstruction(constIndex).registerA - val insertIndex = getTargetIndexReversed(constIndex, Opcode.INVOKE_VIRTUAL) + 1 + val insertIndex = getTargetIndexReversedOrThrow(constIndex, Opcode.INVOKE_VIRTUAL) + 1 val jumpIndex = implementation!!.instructions.let { instruction -> insertIndex + instruction.subList(insertIndex, instruction.size - 1) .indexOfFirst { instructions -> @@ -272,7 +272,7 @@ object PlayerComponentsPatch : BaseBytecodePatch( val insertRegister = getInstruction(insertIndex).registerA val onClickListenerIndex = - getTargetIndexWithMethodReferenceName(insertIndex, "setOnClickListener") + getTargetIndexWithMethodReferenceNameOrThrow(insertIndex, "setOnClickListener") val constComponent = getConstComponent(insertIndex, onClickListenerIndex - 1) if (constComponent.isNotEmpty()) { @@ -318,7 +318,7 @@ object PlayerComponentsPatch : BaseBytecodePatch( method.parameters == listOf("Landroid/view/View${'$'}OnClickListener;") }?.apply { val setOnClickListenerIndex = - getTargetIndexWithMethodReferenceName("setOnClickListener") + getTargetIndexWithMethodReferenceNameOrThrow("setOnClickListener") val setOnClickListenerRegister = getInstruction(setOnClickListenerIndex).registerC diff --git a/src/main/kotlin/app/revanced/patches/youtube/player/descriptions/DescriptionComponentsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/player/descriptions/DescriptionComponentsPatch.kt index cffe83adb..cb40ff1c4 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/player/descriptions/DescriptionComponentsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/player/descriptions/DescriptionComponentsPatch.kt @@ -19,7 +19,7 @@ import app.revanced.patches.youtube.utils.playertype.PlayerTypeHookPatch import app.revanced.patches.youtube.utils.recyclerview.BottomSheetRecyclerViewPatch import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch import app.revanced.patches.youtube.utils.settings.SettingsPatch -import app.revanced.util.getTargetIndexWithMethodReferenceName +import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction @@ -61,7 +61,7 @@ object DescriptionComponentsPatch : BaseBytecodePatch( it.mutableMethod.apply { val freeRegister = implementation!!.registerCount - parameters.size - 2 val imageSpanIndex = it.scanResult.patternScanResult!!.startIndex - val setTextIndex = getTargetIndexWithMethodReferenceName("setText") + val setTextIndex = getTargetIndexWithMethodReferenceNameOrThrow("setText") addInstruction(setTextIndex, "nop") addInstructionsWithLabels( @@ -94,7 +94,7 @@ object DescriptionComponentsPatch : BaseBytecodePatch( if (SettingsPatch.upward1902) { TextViewComponentFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val insertIndex = getTargetIndexWithMethodReferenceName("setTextIsSelectable") + val insertIndex = getTargetIndexWithMethodReferenceNameOrThrow("setTextIsSelectable") val insertInstruction = getInstruction(insertIndex) replaceInstruction( @@ -111,7 +111,7 @@ object DescriptionComponentsPatch : BaseBytecodePatch( ) EngagementPanelTitleFingerprint.resultOrThrow().mutableMethod.apply { val contentDescriptionIndex = - getTargetIndexWithMethodReferenceName("setContentDescription") + getTargetIndexWithMethodReferenceNameOrThrow("setContentDescription") val contentDescriptionRegister = getInstruction(contentDescriptionIndex).registerD diff --git a/src/main/kotlin/app/revanced/patches/youtube/player/flyoutmenu/hide/PlayerFlyoutMenuPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/player/flyoutmenu/hide/PlayerFlyoutMenuPatch.kt index a8f55bebe..6688bbcae 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/player/flyoutmenu/hide/PlayerFlyoutMenuPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/player/flyoutmenu/hide/PlayerFlyoutMenuPatch.kt @@ -16,7 +16,7 @@ import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.BottomSheetFooterText import app.revanced.patches.youtube.utils.settings.SettingsPatch import app.revanced.util.REGISTER_TEMPLATE_REPLACEMENT -import app.revanced.util.getTargetIndexWithMethodReferenceName +import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow import app.revanced.util.literalInstructionBooleanHook import app.revanced.util.literalInstructionViewHook import app.revanced.util.patch.BaseBytecodePatch @@ -64,7 +64,7 @@ object PlayerFlyoutMenuPatch : BaseBytecodePatch( QualityMenuViewInflateFingerprint ).forEach { fingerprint -> fingerprint.resultOrThrow().mutableMethod.apply { - val insertIndex = getTargetIndexWithMethodReferenceName("addHeaderView") + val insertIndex = getTargetIndexWithMethodReferenceNameOrThrow("addHeaderView") val insertRegister = getInstruction(insertIndex).registerD addInstructions( diff --git a/src/main/kotlin/app/revanced/patches/youtube/player/flyoutmenu/toggle/ChangeTogglePatch.kt b/src/main/kotlin/app/revanced/patches/youtube/player/flyoutmenu/toggle/ChangeTogglePatch.kt index ac5fa30bc..2810fa8b6 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/player/flyoutmenu/toggle/ChangeTogglePatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/player/flyoutmenu/toggle/ChangeTogglePatch.kt @@ -18,9 +18,10 @@ import app.revanced.patches.youtube.utils.integrations.Constants.PLAYER_CLASS_DE import app.revanced.patches.youtube.utils.settings.SettingsPatch import app.revanced.util.getReference import app.revanced.util.getStringInstructionIndex -import app.revanced.util.getTargetIndex -import app.revanced.util.getTargetIndexReversed +import app.revanced.util.getTargetIndexOrThrow +import app.revanced.util.getTargetIndexReversedOrThrow import app.revanced.util.indexOfFirstInstruction +import app.revanced.util.indexOfFirstInstructionOrThrow import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.Opcode @@ -132,7 +133,7 @@ object ChangeTogglePatch : BaseBytecodePatch( CinematicLightingFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val iGetIndex = indexOfFirstInstruction { + val iGetIndex = indexOfFirstInstructionOrThrow { opcode == Opcode.IGET && getReference()?.definingClass == definingClass } @@ -140,10 +141,10 @@ object ChangeTogglePatch : BaseBytecodePatch( val stringIndex = getStringInstructionIndex("menu_item_cinematic_lighting") - val checkCastIndex = getTargetIndexReversed(stringIndex, Opcode.CHECK_CAST) + val checkCastIndex = getTargetIndexReversedOrThrow(stringIndex, Opcode.CHECK_CAST) val iGetObjectPrimaryIndex = - getTargetIndexReversed(checkCastIndex, Opcode.IGET_OBJECT) - val iGetObjectSecondaryIndex = getTargetIndex(checkCastIndex, Opcode.IGET_OBJECT) + getTargetIndexReversedOrThrow(checkCastIndex, Opcode.IGET_OBJECT) + val iGetObjectSecondaryIndex = getTargetIndexOrThrow(checkCastIndex, Opcode.IGET_OBJECT) val checkCastReference = getInstruction(checkCastIndex).reference @@ -152,14 +153,14 @@ object ChangeTogglePatch : BaseBytecodePatch( val iGetObjectSecondaryReference = getInstruction(iGetObjectSecondaryIndex).reference - val invokeVirtualIndex = getTargetIndex(stringIndex, Opcode.INVOKE_VIRTUAL) + val invokeVirtualIndex = getTargetIndexOrThrow(stringIndex, Opcode.INVOKE_VIRTUAL) val invokeVirtualInstruction = getInstruction(invokeVirtualIndex) val freeRegisterC = invokeVirtualInstruction.registerC val freeRegisterD = invokeVirtualInstruction.registerD val freeRegisterE = invokeVirtualInstruction.registerE - val insertIndex = getTargetIndex(stringIndex, Opcode.RETURN_VOID) + val insertIndex = getTargetIndexOrThrow(stringIndex, Opcode.RETURN_VOID) addInstructionsWithLabels( insertIndex, """ diff --git a/src/main/kotlin/app/revanced/patches/youtube/player/fullscreen/FullscreenComponentsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/player/fullscreen/FullscreenComponentsPatch.kt index 3b5fbed2d..872d46fed 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/player/fullscreen/FullscreenComponentsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/player/fullscreen/FullscreenComponentsPatch.kt @@ -32,8 +32,8 @@ import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.FullS import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.QuickActionsElementContainer import app.revanced.patches.youtube.utils.settings.SettingsPatch import app.revanced.util.getStringInstructionIndex -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.patch.BaseBytecodePatch @@ -80,7 +80,7 @@ object FullscreenComponentsPatch : BaseBytecodePatch( EngagementPanelFingerprint.resultOrThrow().let { it.mutableMethod.apply { val literalIndex = getWideLiteralInstructionIndex(FullScreenEngagementPanel) - val targetIndex = getTargetIndex(literalIndex, Opcode.CHECK_CAST) + val targetIndex = getTargetIndexOrThrow(literalIndex, Opcode.CHECK_CAST) val targetRegister = getInstruction(targetIndex).registerA addInstruction( @@ -93,7 +93,7 @@ object FullscreenComponentsPatch : BaseBytecodePatch( PlayerTitleViewFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val insertIndex = getTargetIndexWithMethodReferenceName("addView") + val insertIndex = getTargetIndexWithMethodReferenceNameOrThrow("addView") val insertReference = getInstruction(insertIndex).reference.toString() if (!insertReference.startsWith("Landroid/widget/FrameLayout;")) @@ -116,7 +116,7 @@ object FullscreenComponentsPatch : BaseBytecodePatch( it.mutableMethod.apply { val constIndex = getWideLiteralInstructionIndex(AutoNavPreviewStub) val constRegister = getInstruction(constIndex).registerA - val jumpIndex = getTargetIndex(constIndex + 2, Opcode.INVOKE_VIRTUAL) + 1 + val jumpIndex = getTargetIndexOrThrow(constIndex + 2, Opcode.INVOKE_VIRTUAL) + 1 addInstructionsWithLabels( constIndex, """ @@ -158,7 +158,7 @@ object FullscreenComponentsPatch : BaseBytecodePatch( } val constIndex = containerCalls.elementAt(containerCalls.size - 1).index - val checkCastIndex = getTargetIndex(constIndex, Opcode.CHECK_CAST) + val checkCastIndex = getTargetIndexOrThrow(constIndex, Opcode.CHECK_CAST) val insertRegister = getInstruction(checkCastIndex).registerA @@ -182,8 +182,8 @@ object FullscreenComponentsPatch : BaseBytecodePatch( YouTubeControlsOverlayFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val targetIndex = getTargetIndexWithMethodReferenceName("setFocusableInTouchMode") - val walkerIndex = getTargetIndex(targetIndex, Opcode.INVOKE_STATIC) + val targetIndex = getTargetIndexWithMethodReferenceNameOrThrow("setFocusableInTouchMode") + val walkerIndex = getTargetIndexOrThrow(targetIndex, Opcode.INVOKE_STATIC) val walkerMethod = getWalkerMethod(context, walkerIndex) walkerMethod.apply { @@ -243,8 +243,8 @@ object FullscreenComponentsPatch : BaseBytecodePatch( it.mutableMethod.apply { val stringIndex = getStringInstructionIndex("Acquiring NetLatencyActionLogger failed. taskId=") - val invokeIndex = getTargetIndex(stringIndex, Opcode.INVOKE_INTERFACE) - val targetIndex = getTargetIndex(invokeIndex, Opcode.CHECK_CAST) + val invokeIndex = getTargetIndexOrThrow(stringIndex, Opcode.INVOKE_INTERFACE) + val targetIndex = getTargetIndexOrThrow(invokeIndex, Opcode.CHECK_CAST) val targetClass = context .findClass(getInstruction(targetIndex).reference.toString())!! .mutableClass @@ -310,7 +310,7 @@ object FullscreenComponentsPatch : BaseBytecodePatch( result.mutableMethod.apply { val stringIndex = getStringInstructionIndex("android.intent.action.SCREEN_ON") - val insertIndex = getTargetIndex(stringIndex, Opcode.IF_EQZ) + 1 + val insertIndex = getTargetIndexOrThrow(stringIndex, Opcode.IF_EQZ) + 1 addInstruction( insertIndex, diff --git a/src/main/kotlin/app/revanced/patches/youtube/player/hapticfeedback/HapticFeedBackPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/player/hapticfeedback/HapticFeedBackPatch.kt index cc7f977c2..c1568aef0 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/player/hapticfeedback/HapticFeedBackPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/player/hapticfeedback/HapticFeedBackPatch.kt @@ -13,7 +13,7 @@ import app.revanced.patches.youtube.player.hapticfeedback.fingerprints.ZoomHapti import app.revanced.patches.youtube.utils.compatibility.Constants.COMPATIBLE_PACKAGE import app.revanced.patches.youtube.utils.integrations.Constants.PLAYER_CLASS_DESCRIPTOR import app.revanced.patches.youtube.utils.settings.SettingsPatch -import app.revanced.util.getTargetIndex +import app.revanced.util.getTargetIndexOrThrow import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.Opcode @@ -65,7 +65,7 @@ object HapticFeedBackPatch : BaseBytecodePatch( var register = 0 if (name == "run") { - index = getTargetIndex(Opcode.SGET) + index = getTargetIndexOrThrow(Opcode.SGET) register = getInstruction(index).registerA } diff --git a/src/main/kotlin/app/revanced/patches/youtube/player/overlaybuttons/OverlayButtonsBytecodePatch.kt b/src/main/kotlin/app/revanced/patches/youtube/player/overlaybuttons/OverlayButtonsBytecodePatch.kt index 9a59becf8..e533feb96 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/player/overlaybuttons/OverlayButtonsBytecodePatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/player/overlaybuttons/OverlayButtonsBytecodePatch.kt @@ -18,8 +18,8 @@ import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch import app.revanced.patches.youtube.video.information.VideoInformationPatch import app.revanced.util.addFieldAndInstructions import app.revanced.util.getReference -import app.revanced.util.getTargetIndexWithReference -import app.revanced.util.indexOfFirstInstruction +import app.revanced.util.getTargetIndexWithReferenceOrThrow +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.OneRegisterInstruction @@ -84,7 +84,7 @@ object OverlayButtonsBytecodePatch : BytecodePatch( PlayerButtonConstructorFingerprint.resultOrThrow().mutableMethod.apply { val registerResolver = implementation!!.registerCount - parameters.size - 1 + 6 // p6 - val invokerObjectIndex = indexOfFirstInstruction { + val invokerObjectIndex = indexOfFirstInstructionOrThrow { opcode == Opcode.IPUT_OBJECT && getReference()?.definingClass == definingClass && (this as TwoRegisterInstruction).registerA == registerResolver @@ -93,7 +93,7 @@ object OverlayButtonsBytecodePatch : BytecodePatch( getInstruction(invokerObjectIndex).reference val onClickListenerReferenceIndex = - getTargetIndexWithReference("(Ljava/lang/Object;I[B)V") + getTargetIndexWithReferenceOrThrow("(Ljava/lang/Object;I[B)V") val onClickListenerReference = getInstruction(onClickListenerReferenceIndex).reference val onClickListenerClass = @@ -103,7 +103,7 @@ object OverlayButtonsBytecodePatch : BytecodePatch( onClickListenerClass.methods.find { method -> method.name == "onClick" } ?.apply { val invokeInterfaceIndex = - getTargetIndexWithReference(invokerObjectReference.toString()) + 1 + getTargetIndexWithReferenceOrThrow(invokerObjectReference.toString()) + 1 if (getInstruction(invokeInterfaceIndex).opcode != Opcode.INVOKE_INTERFACE) throw PatchException("Opcode does not match") invokeInterfaceReference = diff --git a/src/main/kotlin/app/revanced/patches/youtube/player/seekbar/SeekbarComponentsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/player/seekbar/SeekbarComponentsPatch.kt index 436d8ea8b..6038b4e32 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/player/seekbar/SeekbarComponentsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/player/seekbar/SeekbarComponentsPatch.kt @@ -29,8 +29,8 @@ import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.ReelT import app.revanced.patches.youtube.utils.settings.SettingsPatch import app.revanced.patches.youtube.utils.settings.SettingsPatch.contexts import app.revanced.patches.youtube.video.information.VideoInformationPatch -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.literalInstructionBooleanHook @@ -132,10 +132,10 @@ object SeekbarComponentsPatch : BaseBytecodePatch( TotalTimeFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val charSequenceIndex = getTargetIndexWithMethodReferenceName("getString") + 1 + val charSequenceIndex = getTargetIndexWithMethodReferenceNameOrThrow("getString") + 1 val charSequenceRegister = getInstruction(charSequenceIndex).registerA - val textViewIndex = getTargetIndexWithMethodReferenceName("getText") + val textViewIndex = getTargetIndexWithMethodReferenceNameOrThrow("getText") val textViewRegister = getInstruction(textViewIndex).registerC @@ -204,7 +204,7 @@ object SeekbarComponentsPatch : BaseBytecodePatch( PlayerButtonsVisibilityFingerprint.resultOrThrow().let { it.mutableMethod.apply { val freeRegister = implementation!!.registerCount - parameters.size - 2 - val viewIndex = getTargetIndex(Opcode.INVOKE_INTERFACE) + val viewIndex = getTargetIndexOrThrow(Opcode.INVOKE_INTERFACE) val viewRegister = getInstruction(viewIndex).registerD addInstructionsWithLabels( diff --git a/src/main/kotlin/app/revanced/patches/youtube/player/speedoverlay/SpeedOverlayPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/player/speedoverlay/SpeedOverlayPatch.kt index bb069796a..df53dc40f 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/player/speedoverlay/SpeedOverlayPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/player/speedoverlay/SpeedOverlayPatch.kt @@ -20,12 +20,12 @@ import app.revanced.patches.youtube.player.speedoverlay.fingerprints.SpeedOverla import app.revanced.patches.youtube.utils.integrations.Constants.PLAYER_CLASS_DESCRIPTOR import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch import app.revanced.util.getReference -import app.revanced.util.getTargetIndex -import app.revanced.util.getTargetIndexReversed -import app.revanced.util.getTargetIndexWithMethodReferenceName -import app.revanced.util.getTargetIndexWithMethodReferenceNameReversed +import app.revanced.util.getTargetIndexOrThrow +import app.revanced.util.getTargetIndexReversedOrThrow +import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow +import app.revanced.util.getTargetIndexWithMethodReferenceNameReversedOrThrow import app.revanced.util.getWalkerMethod -import app.revanced.util.indexOfFirstInstruction +import app.revanced.util.indexOfFirstInstructionOrThrow import app.revanced.util.literalInstructionBooleanHook import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.Opcode @@ -102,7 +102,7 @@ object SpeedOverlayPatch : BytecodePatch( // Used on YouTube 19.18.41~ NextGenWatchLayoutFingerprint.resultOrThrow().mutableMethod.apply { - val booleanValueIndex = getTargetIndexWithMethodReferenceName("booleanValue") + val booleanValueIndex = getTargetIndexWithMethodReferenceNameOrThrow("booleanValue") val insertIndex = findIGetIndex(booleanValueIndex - 10, booleanValueIndex) val insertInstruction = getInstruction(insertIndex) @@ -139,9 +139,9 @@ object SpeedOverlayPatch : BytecodePatch( } slideToSeekBooleanMethod.apply { - var insertIndex = getTargetIndex(Opcode.IGET_OBJECT) + var insertIndex = getTargetIndexOrThrow(Opcode.IGET_OBJECT) var insertRegister = getInstruction(insertIndex).registerA - var jumpIndex = getTargetIndexReversed(Opcode.INVOKE_VIRTUAL) + var jumpIndex = getTargetIndexReversedOrThrow(Opcode.INVOKE_VIRTUAL) hook(insertIndex, insertRegister, jumpIndex) @@ -151,7 +151,7 @@ object SpeedOverlayPatch : BytecodePatch( ?: throw PatchException("Could not find constructor method") constructorMethod.apply { - val syntheticIndex = getTargetIndexReversed(Opcode.NEW_INSTANCE) + val syntheticIndex = getTargetIndexReversedOrThrow(Opcode.NEW_INSTANCE) val syntheticClass = getInstruction(syntheticIndex).reference.toString() @@ -162,7 +162,7 @@ object SpeedOverlayPatch : BytecodePatch( syntheticMethod.apply { val speedOverlayValueIndex = - indexOfFirstInstruction { (this as? NarrowLiteralInstruction)?.narrowLiteral == 2.0f.toRawBits() } + indexOfFirstInstructionOrThrow { (this as? NarrowLiteralInstruction)?.narrowLiteral == 2.0f.toRawBits() } val speedOverlayValueRegister = getInstruction(speedOverlayValueIndex).registerA @@ -173,13 +173,13 @@ object SpeedOverlayPatch : BytecodePatch( """ ) - insertIndex = getTargetIndexWithMethodReferenceNameReversed( + insertIndex = getTargetIndexWithMethodReferenceNameReversedOrThrow( speedOverlayValueIndex, "removeCallbacks" ) + 1 insertRegister = getInstruction(insertIndex - 1).registerC - jumpIndex = getTargetIndex(speedOverlayValueIndex, Opcode.RETURN_VOID) + 1 + jumpIndex = getTargetIndexOrThrow(speedOverlayValueIndex, Opcode.RETURN_VOID) + 1 hook(insertIndex, insertRegister, jumpIndex) } } diff --git a/src/main/kotlin/app/revanced/patches/youtube/shorts/components/ShortsComponentPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/shorts/components/ShortsComponentPatch.kt index f2de96b0c..24994db00 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/shorts/components/ShortsComponentPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/shorts/components/ShortsComponentPatch.kt @@ -34,9 +34,9 @@ import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.ReelR import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.RightComment import app.revanced.patches.youtube.utils.settings.SettingsPatch import app.revanced.patches.youtube.video.information.VideoInformationPatch -import app.revanced.util.getTargetIndex -import app.revanced.util.getTargetIndexReversed -import app.revanced.util.getTargetIndexWithReference +import app.revanced.util.getTargetIndexOrThrow +import app.revanced.util.getTargetIndexReversedOrThrow +import app.revanced.util.getTargetIndexWithReferenceOrThrow import app.revanced.util.getWideLiteralInstructionIndex import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow @@ -96,7 +96,7 @@ object ShortsComponentPatch : BaseBytecodePatch( val constIndex = getWideLiteralInstructionIndex(ReelRightDislikeIcon) val constRegister = getInstruction(constIndex).registerA - val jumpIndex = getTargetIndex(constIndex, Opcode.CONST_CLASS) + 2 + val jumpIndex = getTargetIndexOrThrow(constIndex, Opcode.CONST_CLASS) + 2 addInstructionsWithLabels( constIndex + 1, """ @@ -117,7 +117,7 @@ object ShortsComponentPatch : BaseBytecodePatch( it.mutableMethod.apply { val insertIndex = getWideLiteralInstructionIndex(ReelRightLikeIcon) val insertRegister = getInstruction(insertIndex).registerA - val jumpIndex = getTargetIndex(insertIndex, Opcode.CONST_CLASS) + 2 + val jumpIndex = getTargetIndexOrThrow(insertIndex, Opcode.CONST_CLASS) + 2 addInstructionsWithLabels( insertIndex + 1, """ @@ -139,8 +139,8 @@ object ShortsComponentPatch : BaseBytecodePatch( val targetIndex = getWideLiteralInstructionIndex(ReelForcedMuteButton) val targetRegister = getInstruction(targetIndex).registerA - val insertIndex = getTargetIndexReversed(targetIndex, Opcode.IF_EQZ) - val jumpIndex = getTargetIndex(targetIndex, Opcode.GOTO) + val insertIndex = getTargetIndexReversedOrThrow(targetIndex, Opcode.IF_EQZ) + val jumpIndex = getTargetIndexOrThrow(targetIndex, Opcode.GOTO) addInstructionsWithLabels( insertIndex, """ @@ -157,7 +157,7 @@ object ShortsComponentPatch : BaseBytecodePatch( (instruction.value as? WideLiteralInstruction)?.wideLiteral == ReelPivotButton } val targetIndex = constCalls.elementAt(constCalls.size - 1).index - val insertIndex = getTargetIndexReversed(targetIndex, Opcode.INVOKE_STATIC) + 1 + val insertIndex = getTargetIndexReversedOrThrow(targetIndex, Opcode.INVOKE_STATIC) + 1 if (insertIndex == 0) throw PatchException("insert index not found") @@ -267,7 +267,7 @@ object ShortsComponentPatch : BaseBytecodePatch( TextComponentSpecFingerprint.resultOrThrow().let { it.mutableMethod.apply { val insertIndex = - getTargetIndexWithReference("Landroid/text/SpannableString;->valueOf(Ljava/lang/CharSequence;)Landroid/text/SpannableString;") + getTargetIndexWithReferenceOrThrow("Landroid/text/SpannableString;->valueOf(Ljava/lang/CharSequence;)Landroid/text/SpannableString;") val charSequenceRegister = getInstruction(insertIndex).registerC @@ -318,9 +318,9 @@ object ShortsComponentPatch : BaseBytecodePatch( it.mutableMethod.apply { val constIndex = getWideLiteralInstructionIndex(id) val insertIndex = if (reversed) - getTargetIndexReversed(constIndex, Opcode.CHECK_CAST) + getTargetIndexReversedOrThrow(constIndex, Opcode.CHECK_CAST) else - getTargetIndex(constIndex, Opcode.CHECK_CAST) + getTargetIndexOrThrow(constIndex, Opcode.CHECK_CAST) val insertRegister = getInstruction(insertIndex).registerA addInstruction( @@ -338,7 +338,7 @@ object ShortsComponentPatch : BaseBytecodePatch( resultOrThrow().let { it.mutableMethod.apply { val constIndex = getWideLiteralInstructionIndex(id) - val insertIndex = getTargetIndex(constIndex, Opcode.CHECK_CAST) + val insertIndex = getTargetIndexOrThrow(constIndex, Opcode.CHECK_CAST) hideButtons(insertIndex, descriptor) } diff --git a/src/main/kotlin/app/revanced/patches/youtube/shorts/components/ShortsNavigationBarPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/shorts/components/ShortsNavigationBarPatch.kt index 002685585..1bc60c84a 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/shorts/components/ShortsNavigationBarPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/shorts/components/ShortsNavigationBarPatch.kt @@ -10,7 +10,7 @@ import app.revanced.patches.youtube.shorts.components.fingerprints.RenderBottomN import app.revanced.patches.youtube.shorts.components.fingerprints.SetPivotBarFingerprint import app.revanced.patches.youtube.utils.fingerprints.InitializeButtonsFingerprint import app.revanced.patches.youtube.utils.integrations.Constants.SHORTS_CLASS_DESCRIPTOR -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.OneRegisterInstruction @@ -51,7 +51,7 @@ object ShortsNavigationBarPatch : BytecodePatch( BottomNavigationBarFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val targetIndex = getTargetIndexWithMethodReferenceName("findViewById") + 1 + val targetIndex = getTargetIndexWithMethodReferenceNameOrThrow("findViewById") + 1 val insertRegister = getInstruction(targetIndex).registerA addInstructions( diff --git a/src/main/kotlin/app/revanced/patches/youtube/shorts/repeat/ShortsRepeatPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/shorts/repeat/ShortsRepeatPatch.kt index 8941d8452..eb4d9c90a 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/shorts/repeat/ShortsRepeatPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/shorts/repeat/ShortsRepeatPatch.kt @@ -13,7 +13,7 @@ import app.revanced.patches.youtube.utils.settings.SettingsPatch import app.revanced.util.containsReferenceInstructionIndex import app.revanced.util.findMutableMethodOf import app.revanced.util.getStringInstructionIndex -import app.revanced.util.getTargetIndex +import app.revanced.util.getTargetIndexOrThrow import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.Opcode @@ -45,7 +45,7 @@ object ShortsRepeatPatch : BaseBytecodePatch( val endScreenStringIndex = getStringInstructionIndex("REEL_LOOP_BEHAVIOR_END_SCREEN") val endScreenReferenceIndex = - getTargetIndex(endScreenStringIndex, Opcode.SPUT_OBJECT) + getTargetIndexOrThrow(endScreenStringIndex, Opcode.SPUT_OBJECT) val endScreenReference = getInstruction(endScreenReferenceIndex).reference.toString() @@ -74,7 +74,7 @@ object ShortsRepeatPatch : BaseBytecodePatch( fieldName: String ) { val stringIndex = getStringInstructionIndex(enumName) - val insertIndex = getTargetIndex(stringIndex, Opcode.SPUT_OBJECT) + val insertIndex = getTargetIndexOrThrow(stringIndex, Opcode.SPUT_OBJECT) val insertRegister = getInstruction(insertIndex).registerA addInstruction( diff --git a/src/main/kotlin/app/revanced/patches/youtube/shorts/startupshortsreset/ResumingShortsOnStartupPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/shorts/startupshortsreset/ResumingShortsOnStartupPatch.kt index 67c9c4b12..d40fa3d02 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/shorts/startupshortsreset/ResumingShortsOnStartupPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/shorts/startupshortsreset/ResumingShortsOnStartupPatch.kt @@ -14,7 +14,7 @@ import app.revanced.patches.youtube.utils.integrations.Constants.SHORTS_CLASS_DE import app.revanced.patches.youtube.utils.settings.SettingsPatch import app.revanced.util.getReference import app.revanced.util.getWalkerMethod -import app.revanced.util.indexOfFirstInstruction +import app.revanced.util.indexOfFirstInstructionOrThrow import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.Opcode @@ -70,12 +70,11 @@ object ResumingShortsOnStartupPatch : BaseBytecodePatch( UserWasInShortsFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val listenableInstructionIndex = indexOfFirstInstruction { + val listenableInstructionIndex = indexOfFirstInstructionOrThrow { opcode == Opcode.INVOKE_INTERFACE && getReference()?.definingClass == "Lcom/google/common/util/concurrent/ListenableFuture;" && getReference()?.name == "isDone" } - if (listenableInstructionIndex < 0) throw PatchException("Could not find instruction index") val originalInstructionRegister = getInstruction(listenableInstructionIndex).registerC val freeRegister = diff --git a/src/main/kotlin/app/revanced/patches/youtube/utils/castbutton/CastButtonPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/utils/castbutton/CastButtonPatch.kt index aefa52e4d..6db2ab6bb 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/utils/castbutton/CastButtonPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/utils/castbutton/CastButtonPatch.kt @@ -17,7 +17,7 @@ import app.revanced.patches.youtube.utils.integrations.Constants.PATCH_STATUS_CL import app.revanced.patches.youtube.utils.integrations.Constants.PLAYER_CLASS_DESCRIPTOR import app.revanced.patches.youtube.utils.integrations.Constants.UTILS_PATH import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch -import app.revanced.util.getTargetIndexWithMethodReferenceName +import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow import app.revanced.util.resultOrThrow import app.revanced.util.updatePatchStatus import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction @@ -64,7 +64,7 @@ object CastButtonPatch : BytecodePatch( internal fun hookPlayerButton(context: BytecodeContext) { playerButtonMethod.apply { - val index = getTargetIndexWithMethodReferenceName("setVisibility") + val index = getTargetIndexWithMethodReferenceNameOrThrow("setVisibility") val instruction = getInstruction(index) val viewRegister = instruction.registerC val visibilityRegister = instruction.registerD @@ -84,7 +84,7 @@ object CastButtonPatch : BytecodePatch( internal fun hookToolBarButton(context: BytecodeContext) { toolbarMenuItemInitializeMethod.apply { - val index = getTargetIndexWithMethodReferenceName("setShowAsAction") + 1 + val index = getTargetIndexWithMethodReferenceNameOrThrow("setShowAsAction") + 1 addInstruction( index, diff --git a/src/main/kotlin/app/revanced/patches/youtube/utils/fix/cairo/CairoSettingsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/utils/fix/cairo/CairoSettingsPatch.kt index 8ab5824d1..47bae5fe4 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/utils/fix/cairo/CairoSettingsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/utils/fix/cairo/CairoSettingsPatch.kt @@ -7,7 +7,7 @@ import app.revanced.patcher.patch.BytecodePatch import app.revanced.patcher.patch.annotation.Patch import app.revanced.patches.youtube.misc.backgroundplayback.BackgroundPlaybackPatch import app.revanced.patches.youtube.utils.fix.cairo.fingerprints.CarioFragmentConfigFingerprint -import app.revanced.util.getTargetIndex +import app.revanced.util.getTargetIndexOrThrow import app.revanced.util.getWideLiteralInstructionIndex import com.android.tools.smali.dexlib2.Opcode import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction @@ -33,7 +33,7 @@ object CairoSettingsPatch : BytecodePatch( CarioFragmentConfigFingerprint.result?.let { it.mutableMethod.apply { val targetIndex = - getTargetIndex(getWideLiteralInstructionIndex(45532100), Opcode.MOVE_RESULT) + getTargetIndexOrThrow(getWideLiteralInstructionIndex(45532100), Opcode.MOVE_RESULT) val targetRegister = getInstruction(targetIndex).registerA addInstruction( diff --git a/src/main/kotlin/app/revanced/patches/youtube/utils/fix/fullscreen/FullscreenButtonViewStubPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/utils/fix/fullscreen/FullscreenButtonViewStubPatch.kt index 2ccc59013..fba7caf5f 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/utils/fix/fullscreen/FullscreenButtonViewStubPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/utils/fix/fullscreen/FullscreenButtonViewStubPatch.kt @@ -7,7 +7,7 @@ import app.revanced.patcher.patch.BytecodePatch import app.revanced.patcher.patch.annotation.Patch import app.revanced.patches.youtube.utils.fix.fullscreen.fingerprints.FullscreenButtonPositionFingerprint import app.revanced.patches.youtube.utils.fix.fullscreen.fingerprints.FullscreenButtonViewStubFingerprint -import app.revanced.util.getTargetIndex +import app.revanced.util.getTargetIndexOrThrow import app.revanced.util.getWideLiteralInstructionIndex import com.android.tools.smali.dexlib2.Opcode import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction @@ -33,7 +33,7 @@ object FullscreenButtonViewStubPatch : BytecodePatch( ).forEach { (fingerprint, literalValue) -> fingerprint.result?.let { it.mutableMethod.apply { - val targetIndex = getTargetIndex( + val targetIndex = getTargetIndexOrThrow( getWideLiteralInstructionIndex(literalValue.toLong()), Opcode.MOVE_RESULT ) diff --git a/src/main/kotlin/app/revanced/patches/youtube/utils/fix/litho/ConversionContextObfuscationPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/utils/fix/litho/ConversionContextObfuscationPatch.kt index 2541f2c72..46f00812d 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/utils/fix/litho/ConversionContextObfuscationPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/utils/fix/litho/ConversionContextObfuscationPatch.kt @@ -6,7 +6,7 @@ import app.revanced.patcher.extensions.InstructionExtensions.getInstruction import app.revanced.patcher.patch.BytecodePatch import app.revanced.patcher.patch.annotation.Patch import app.revanced.patches.youtube.utils.fix.litho.fingerprints.ObfuscationConfigFingerprint -import app.revanced.util.getTargetIndex +import app.revanced.util.getTargetIndexOrThrow import app.revanced.util.getWideLiteralInstructionIndex import com.android.tools.smali.dexlib2.Opcode import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction @@ -27,7 +27,7 @@ object ConversionContextObfuscationPatch : BytecodePatch( ObfuscationConfigFingerprint.result?.let { it.mutableMethod.apply { val targetIndex = - getTargetIndex(getWideLiteralInstructionIndex(45631264), Opcode.MOVE_RESULT) + getTargetIndexOrThrow(getWideLiteralInstructionIndex(45631264), Opcode.MOVE_RESULT) val targetRegister = getInstruction(targetIndex).registerA addInstruction( diff --git a/src/main/kotlin/app/revanced/patches/youtube/utils/fix/shortsplayback/ShortsPlaybackPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/utils/fix/shortsplayback/ShortsPlaybackPatch.kt index 8a2d452fb..1f9cfd69a 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/utils/fix/shortsplayback/ShortsPlaybackPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/utils/fix/shortsplayback/ShortsPlaybackPatch.kt @@ -6,7 +6,7 @@ import app.revanced.patcher.extensions.InstructionExtensions.getInstruction import app.revanced.patcher.patch.BytecodePatch import app.revanced.patcher.patch.annotation.Patch import app.revanced.patches.youtube.utils.fix.shortsplayback.fingerprints.ShortsPlaybackFingerprint -import app.revanced.util.getTargetIndex +import app.revanced.util.getTargetIndexOrThrow import app.revanced.util.getWideLiteralInstructionIndex import com.android.tools.smali.dexlib2.Opcode import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction @@ -28,7 +28,7 @@ object ShortsPlaybackPatch : BytecodePatch( ShortsPlaybackFingerprint.result?.let { it.mutableMethod.apply { val targetIndex = - getTargetIndex(getWideLiteralInstructionIndex(45387052), Opcode.MOVE_RESULT) + getTargetIndexOrThrow(getWideLiteralInstructionIndex(45387052), Opcode.MOVE_RESULT) val targetRegister = getInstruction(targetIndex).registerA addInstruction( diff --git a/src/main/kotlin/app/revanced/patches/youtube/utils/fix/suggestedvideoendscreen/SuggestedVideoEndScreenPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/utils/fix/suggestedvideoendscreen/SuggestedVideoEndScreenPatch.kt index f51dc0f29..702185779 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/utils/fix/suggestedvideoendscreen/SuggestedVideoEndScreenPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/utils/fix/suggestedvideoendscreen/SuggestedVideoEndScreenPatch.kt @@ -8,8 +8,8 @@ import app.revanced.patcher.patch.annotation.Patch import app.revanced.patcher.util.smali.ExternalLabel import app.revanced.patches.youtube.utils.fix.suggestedvideoendscreen.fingerprints.RemoveOnLayoutChangeListenerFingerprint import app.revanced.patches.youtube.utils.integrations.Constants.PLAYER_CLASS_DESCRIPTOR -import app.revanced.util.getTargetIndex -import app.revanced.util.getTargetIndexReversed +import app.revanced.util.getTargetIndexOrThrow +import app.revanced.util.getTargetIndexReversedOrThrow import app.revanced.util.getWalkerMethod import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.Opcode @@ -36,9 +36,9 @@ object SuggestedVideoEndScreenPatch : BytecodePatch( it.getWalkerMethod(context, it.scanResult.patternScanResult!!.endIndex) walkerIndex.apply { - val invokeInterfaceIndex = getTargetIndex(Opcode.INVOKE_INTERFACE) + val invokeInterfaceIndex = getTargetIndexOrThrow(Opcode.INVOKE_INTERFACE) val iGetObjectIndex = - getTargetIndexReversed(invokeInterfaceIndex, Opcode.IGET_OBJECT) + getTargetIndexReversedOrThrow(invokeInterfaceIndex, Opcode.IGET_OBJECT) val invokeInterfaceReference = getInstruction(invokeInterfaceIndex).reference diff --git a/src/main/kotlin/app/revanced/patches/youtube/utils/playercontrols/PlayerControlsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/utils/playercontrols/PlayerControlsPatch.kt index 512a67180..c7c9dcc31 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/utils/playercontrols/PlayerControlsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/utils/playercontrols/PlayerControlsPatch.kt @@ -16,8 +16,8 @@ import app.revanced.patches.youtube.utils.playercontrols.fingerprints.MotionEven import app.revanced.patches.youtube.utils.playercontrols.fingerprints.PlayerControlsVisibilityEntityModelFingerprint import app.revanced.patches.youtube.utils.playercontrols.fingerprints.PlayerControlsVisibilityFingerprint import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch -import app.revanced.util.getTargetIndex -import app.revanced.util.getTargetIndexWithMethodReferenceName +import app.revanced.util.getTargetIndexOrThrow +import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.Opcode import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction @@ -56,7 +56,7 @@ object PlayerControlsPatch : BytecodePatch( ) PlayerButtonsVisibilityFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val viewIndex = getTargetIndex(Opcode.INVOKE_INTERFACE) + val viewIndex = getTargetIndexOrThrow(Opcode.INVOKE_INTERFACE) val viewRegister = getInstruction(viewIndex).registerD addInstruction( @@ -88,7 +88,7 @@ object PlayerControlsPatch : BytecodePatch( YouTubeControlsOverlayFingerprint.resultOrThrow().mutableClass ) MotionEventFingerprint.resultOrThrow().mutableMethod.apply { - val insertIndex = getTargetIndexWithMethodReferenceName("setTranslationY") + 1 + val insertIndex = getTargetIndexWithMethodReferenceNameOrThrow("setTranslationY") + 1 addInstruction( insertIndex, diff --git a/src/main/kotlin/app/revanced/patches/youtube/utils/playercontrols/PlayerControlsVisibilityHookPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/utils/playercontrols/PlayerControlsVisibilityHookPatch.kt index 0935296eb..b991ec65c 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/utils/playercontrols/PlayerControlsVisibilityHookPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/utils/playercontrols/PlayerControlsVisibilityHookPatch.kt @@ -9,7 +9,7 @@ import app.revanced.patcher.patch.annotation.Patch import app.revanced.patches.youtube.utils.integrations.Constants.UTILS_PATH import app.revanced.patches.youtube.utils.playercontrols.fingerprints.PlayerControlsVisibilityEntityModelFingerprint import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch -import app.revanced.util.getTargetIndex +import app.revanced.util.getTargetIndexOrThrow import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.Opcode import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction @@ -31,7 +31,7 @@ object PlayerControlsVisibilityHookPatch : BytecodePatch( val staticReference = getInstruction(startIndex + 1).reference it.mutableClass.methods.find { method -> method.name == "" }?.apply { - val targetIndex = getTargetIndex(Opcode.IPUT_OBJECT) + val targetIndex = getTargetIndexOrThrow(Opcode.IPUT_OBJECT) val targetRegister = getInstruction(targetIndex).registerA diff --git a/src/main/kotlin/app/revanced/patches/youtube/utils/playertype/PlayerTypeHookPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/utils/playertype/PlayerTypeHookPatch.kt index f46a579e2..33024923f 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/utils/playertype/PlayerTypeHookPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/utils/playertype/PlayerTypeHookPatch.kt @@ -17,8 +17,8 @@ import app.revanced.patches.youtube.utils.playertype.fingerprint.VideoStateFinge import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch import app.revanced.util.addFieldAndInstructions import app.revanced.util.getStringInstructionIndex -import app.revanced.util.getTargetIndex -import app.revanced.util.getTargetIndexWithMethodReferenceName +import app.revanced.util.getTargetIndexOrThrow +import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.Opcode import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction @@ -90,7 +90,7 @@ object PlayerTypeHookPatch : BytecodePatch( targetClass.methods.find { method -> method.name == "" } ?.apply { - val browseIdFieldIndex = getTargetIndex(Opcode.IPUT_OBJECT) + val browseIdFieldIndex = getTargetIndexOrThrow(Opcode.IPUT_OBJECT) val browseIdFieldName = (getInstruction(browseIdFieldIndex).reference as FieldReference).name @@ -128,7 +128,7 @@ object PlayerTypeHookPatch : BytecodePatch( // Insert before the first ViewGroup method call after inflating, // so this works regardless which layout is used. ActionBarSearchResultsFingerprint.resultOrThrow().mutableMethod.apply { - val instructionIndex = getTargetIndexWithMethodReferenceName("setLayoutDirection") + val instructionIndex = getTargetIndexWithMethodReferenceNameOrThrow("setLayoutDirection") val viewRegister = getInstruction(instructionIndex).registerC addInstruction( diff --git a/src/main/kotlin/app/revanced/patches/youtube/utils/returnyoutubedislike/general/ReturnYouTubeDislikePatch.kt b/src/main/kotlin/app/revanced/patches/youtube/utils/returnyoutubedislike/general/ReturnYouTubeDislikePatch.kt index fc3a3d4c2..b381e3c19 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/utils/returnyoutubedislike/general/ReturnYouTubeDislikePatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/utils/returnyoutubedislike/general/ReturnYouTubeDislikePatch.kt @@ -18,7 +18,7 @@ import app.revanced.patches.youtube.utils.returnyoutubedislike.shorts.ReturnYouT import app.revanced.patches.youtube.utils.settings.SettingsPatch import app.revanced.patches.youtube.video.information.VideoInformationPatch import app.revanced.patches.youtube.video.videoid.VideoIdPatch -import app.revanced.util.getTargetIndexWithFieldReferenceType +import app.revanced.util.getTargetIndexWithFieldReferenceTypeOrThrow import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction @@ -71,12 +71,12 @@ object ReturnYouTubeDislikePatch : BaseBytecodePatch( TextComponentContextFingerprint.resultOrThrow().let { it.mutableMethod.apply { val conversionContextFieldIndex = - getTargetIndexWithFieldReferenceType("Ljava/util/Map;") - 1 + getTargetIndexWithFieldReferenceTypeOrThrow("Ljava/util/Map;") - 1 val conversionContextFieldReference = getInstruction(conversionContextFieldIndex).reference val charSequenceIndex = - getTargetIndexWithFieldReferenceType("Ljava/util/BitSet;") - 1 + getTargetIndexWithFieldReferenceTypeOrThrow("Ljava/util/BitSet;") - 1 val charSequenceRegister = getInstruction(charSequenceIndex).registerA val freeRegister = diff --git a/src/main/kotlin/app/revanced/patches/youtube/utils/returnyoutubedislike/rollingnumber/ReturnYouTubeDislikeRollingNumberPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/utils/returnyoutubedislike/rollingnumber/ReturnYouTubeDislikeRollingNumberPatch.kt index d143ca8fa..29490b004 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/utils/returnyoutubedislike/rollingnumber/ReturnYouTubeDislikeRollingNumberPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/utils/returnyoutubedislike/rollingnumber/ReturnYouTubeDislikeRollingNumberPatch.kt @@ -17,8 +17,8 @@ import app.revanced.patches.youtube.utils.returnyoutubedislike.rollingnumber.fin import app.revanced.patches.youtube.utils.returnyoutubedislike.rollingnumber.fingerprints.RollingNumberMeasureTextParentFingerprint import app.revanced.patches.youtube.utils.returnyoutubedislike.rollingnumber.fingerprints.RollingNumberSetterFingerprint import app.revanced.patches.youtube.utils.settings.SettingsPatch -import app.revanced.util.getTargetIndex -import app.revanced.util.getTargetIndexWithMethodReferenceName +import app.revanced.util.getTargetIndexOrThrow +import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.Opcode import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction @@ -60,7 +60,7 @@ object ReturnYouTubeDislikeRollingNumberPatch : BytecodePatch( rollingNumberClass.methods.find { method -> method.name == "" } ?.apply { - val rollingNumberFieldIndex = getTargetIndex(Opcode.IPUT_OBJECT) + val rollingNumberFieldIndex = getTargetIndexOrThrow(Opcode.IPUT_OBJECT) charSequenceFieldReference = getInstruction(rollingNumberFieldIndex).reference } ?: throw PatchException("RollingNumberClass not found!") @@ -103,7 +103,7 @@ object ReturnYouTubeDislikeRollingNumberPatch : BytecodePatch( """ ) - val ifGeIndex = getTargetIndex(Opcode.IF_GE) + val ifGeIndex = getTargetIndexOrThrow(Opcode.IF_GE) val ifGeInstruction = getInstruction(ifGeIndex) removeInstruction(ifGeIndex) @@ -153,7 +153,7 @@ object ReturnYouTubeDislikeRollingNumberPatch : BytecodePatch( realTimeUpdateTextViewMethod ).forEach { insertMethod -> insertMethod.apply { - val setTextIndex = getTargetIndexWithMethodReferenceName("setText") + val setTextIndex = getTargetIndexWithMethodReferenceNameOrThrow("setText") val textViewRegister = getInstruction(setTextIndex).registerC val textSpanRegister = diff --git a/src/main/kotlin/app/revanced/patches/youtube/utils/returnyoutubedislike/shorts/ReturnYouTubeDislikeShortsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/utils/returnyoutubedislike/shorts/ReturnYouTubeDislikeShortsPatch.kt index 22399ebdd..f67c0568a 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/utils/returnyoutubedislike/shorts/ReturnYouTubeDislikeShortsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/utils/returnyoutubedislike/shorts/ReturnYouTubeDislikeShortsPatch.kt @@ -12,9 +12,9 @@ import app.revanced.patches.youtube.utils.fingerprints.TextComponentSpecFingerpr import app.revanced.patches.youtube.utils.integrations.Constants.UTILS_PATH import app.revanced.patches.youtube.utils.returnyoutubedislike.shorts.fingerprints.ShortsTextViewFingerprint import app.revanced.patches.youtube.utils.settings.SettingsPatch -import app.revanced.util.getTargetIndex -import app.revanced.util.getTargetIndexReversed -import app.revanced.util.getTargetIndexWithReference +import app.revanced.util.getTargetIndexOrThrow +import app.revanced.util.getTargetIndexReversedOrThrow +import app.revanced.util.getTargetIndexWithReferenceOrThrow import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.Opcode import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction @@ -36,8 +36,8 @@ object ReturnYouTubeDislikeShortsPatch : BytecodePatch( it.mutableMethod.apply { val startIndex = it.scanResult.patternScanResult!!.startIndex - val isDisLikesBooleanIndex = getTargetIndexReversed(startIndex, Opcode.IGET_BOOLEAN) - val textViewFieldIndex = getTargetIndexReversed(startIndex, Opcode.IGET_OBJECT) + val isDisLikesBooleanIndex = getTargetIndexReversedOrThrow(startIndex, Opcode.IGET_BOOLEAN) + val textViewFieldIndex = getTargetIndexReversedOrThrow(startIndex, Opcode.IGET_OBJECT) // If the field is true, the TextView is for a dislike button. val isDisLikesBooleanReference = @@ -49,7 +49,7 @@ object ReturnYouTubeDislikeShortsPatch : BytecodePatch( // Check if the hooked TextView object is that of the dislike button. // If RYD is disabled, or the TextView object is not that of the dislike button, the execution flow is not interrupted. // Otherwise, the TextView object is modified, and the execution flow is interrupted to prevent it from being changed afterward. - val insertIndex = getTargetIndex(Opcode.CHECK_CAST) + 1 + val insertIndex = getTargetIndexOrThrow(Opcode.CHECK_CAST) + 1 addInstructionsWithLabels( insertIndex, """ @@ -72,7 +72,7 @@ object ReturnYouTubeDislikeShortsPatch : BytecodePatch( TextComponentSpecFingerprint.resultOrThrow().let { it.mutableMethod.apply { val insertIndex = - getTargetIndexWithReference("Landroid/text/SpannableString;->valueOf(Ljava/lang/CharSequence;)Landroid/text/SpannableString;") + getTargetIndexWithReferenceOrThrow("Landroid/text/SpannableString;->valueOf(Ljava/lang/CharSequence;)Landroid/text/SpannableString;") val charSequenceRegister = getInstruction(insertIndex).registerC diff --git a/src/main/kotlin/app/revanced/patches/youtube/utils/sponsorblock/SponsorBlockBytecodePatch.kt b/src/main/kotlin/app/revanced/patches/youtube/utils/sponsorblock/SponsorBlockBytecodePatch.kt index 07364b179..5fc565df1 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/utils/sponsorblock/SponsorBlockBytecodePatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/utils/sponsorblock/SponsorBlockBytecodePatch.kt @@ -19,10 +19,10 @@ import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.Inset import app.revanced.patches.youtube.utils.sponsorblock.fingerprints.RectangleFieldInvalidatorFingerprint import app.revanced.patches.youtube.utils.sponsorblock.fingerprints.SegmentPlaybackControllerFingerprint import app.revanced.patches.youtube.video.information.VideoInformationPatch -import app.revanced.util.getTargetIndex -import app.revanced.util.getTargetIndexWithFieldReferenceTypeReversed -import app.revanced.util.getTargetIndexWithMethodReferenceName -import app.revanced.util.getTargetIndexWithMethodReferenceNameReversed +import app.revanced.util.getTargetIndexOrThrow +import app.revanced.util.getTargetIndexWithFieldReferenceTypeReversedOrThrow +import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow +import app.revanced.util.getTargetIndexWithMethodReferenceNameReversedOrThrow import app.revanced.util.getWideLiteralInstructionIndex import app.revanced.util.resultOrThrow import app.revanced.util.updatePatchStatus @@ -80,7 +80,7 @@ object SponsorBlockBytecodePatch : BytecodePatch( SeekbarOnDrawFingerprint.resultOrThrow().mutableMethod.apply { // Get left and right of seekbar rectangle - val moveObjectIndex = getTargetIndex(Opcode.MOVE_OBJECT_FROM16) + val moveObjectIndex = getTargetIndexOrThrow(Opcode.MOVE_OBJECT_FROM16) addInstruction( moveObjectIndex + 1, @@ -89,7 +89,7 @@ object SponsorBlockBytecodePatch : BytecodePatch( ) // Set seekbar thickness - val roundIndex = getTargetIndexWithMethodReferenceName("round") + 1 + val roundIndex = getTargetIndexWithMethodReferenceNameOrThrow("round") + 1 val roundRegister = getInstruction(roundIndex).registerA addInstruction( @@ -99,7 +99,7 @@ object SponsorBlockBytecodePatch : BytecodePatch( ) // Draw segment - val drawCircleIndex = getTargetIndexWithMethodReferenceNameReversed("drawCircle") + val drawCircleIndex = getTargetIndexWithMethodReferenceNameReversedOrThrow("drawCircle") val drawCircleInstruction = getInstruction(drawCircleIndex) addInstruction( drawCircleIndex, @@ -116,7 +116,7 @@ object SponsorBlockBytecodePatch : BytecodePatch( // Append timestamp TotalTimeFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val targetIndex = getTargetIndexWithMethodReferenceName("getString") + 1 + val targetIndex = getTargetIndexWithMethodReferenceNameOrThrow("getString") + 1 val targetRegister = getInstruction(targetIndex).registerA addInstructions( @@ -132,7 +132,7 @@ object SponsorBlockBytecodePatch : BytecodePatch( YouTubeControlsOverlayFingerprint.resultOrThrow().let { it.mutableMethod.apply { val targetIndex = getWideLiteralInstructionIndex(InsetOverlayViewLayout) - val checkCastIndex = getTargetIndex(targetIndex, Opcode.CHECK_CAST) + val checkCastIndex = getTargetIndexOrThrow(targetIndex, Opcode.CHECK_CAST) val targetRegister = getInstruction(checkCastIndex).registerA @@ -146,8 +146,8 @@ object SponsorBlockBytecodePatch : BytecodePatch( // Replace strings RectangleFieldInvalidatorFingerprint.resultOrThrow().let { result -> result.mutableMethod.apply { - val invalidateIndex = getTargetIndexWithMethodReferenceNameReversed("invalidate") - val rectangleIndex = getTargetIndexWithFieldReferenceTypeReversed( + val invalidateIndex = getTargetIndexWithMethodReferenceNameReversedOrThrow("invalidate") + val rectangleIndex = getTargetIndexWithFieldReferenceTypeReversedOrThrow( invalidateIndex + 1, "Landroid/graphics/Rect;" ) diff --git a/src/main/kotlin/app/revanced/patches/youtube/video/information/VideoInformationPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/video/information/VideoInformationPatch.kt index 77bde957e..9109d7eab 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/video/information/VideoInformationPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/video/information/VideoInformationPatch.kt @@ -34,10 +34,10 @@ import app.revanced.patches.youtube.video.playerresponse.PlayerResponseMethodHoo import app.revanced.patches.youtube.video.videoid.VideoIdPatch import app.revanced.util.addFieldAndInstructions import app.revanced.util.getReference -import app.revanced.util.getTargetIndex -import app.revanced.util.getTargetIndexReversed +import app.revanced.util.getTargetIndexOrThrow +import app.revanced.util.getTargetIndexReversedOrThrow import app.revanced.util.getWalkerMethod -import app.revanced.util.indexOfFirstInstruction +import app.revanced.util.indexOfFirstInstructionOrThrow import app.revanced.util.resultOrThrow import com.android.tools.smali.dexlib2.AccessFlags import com.android.tools.smali.dexlib2.Opcode @@ -192,11 +192,10 @@ object VideoInformationPatch : BytecodePatch( PlaybackInitializationFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val targetIndex = indexOfFirstInstruction { + val targetIndex = indexOfFirstInstructionOrThrow { opcode == Opcode.INVOKE_DIRECT && getReference()?.returnType == PLAYER_RESPONSE_MODEL_CLASS_DESCRIPTOR } + 1 - if (targetIndex == 0) throw PatchException("Could not find instruction index.") val targetRegister = getInstruction(targetIndex).registerA addInstruction( @@ -213,11 +212,10 @@ object VideoInformationPatch : BytecodePatch( VideoIdFingerprintBackgroundPlay.resultOrThrow().let { it.mutableMethod.apply { - val targetIndex = indexOfFirstInstruction { + val targetIndex = indexOfFirstInstructionOrThrow { opcode == Opcode.INVOKE_INTERFACE && getReference()?.definingClass == PLAYER_RESPONSE_MODEL_CLASS_DESCRIPTOR } - if (targetIndex < 0) throw PatchException("Could not find instruction index.") val targetRegister = getInstruction(targetIndex).registerC addInstruction( @@ -232,11 +230,10 @@ object VideoInformationPatch : BytecodePatch( VideoIdFingerprintShorts.resultOrThrow().let { it.mutableMethod.apply { - val targetIndex = indexOfFirstInstruction { + val targetIndex = indexOfFirstInstructionOrThrow { opcode == Opcode.INVOKE_INTERFACE && getReference()?.definingClass == PLAYER_RESPONSE_MODEL_CLASS_DESCRIPTOR } - if (targetIndex < 0) throw PatchException("Could not find instruction index.") val targetRegister = getInstruction(targetIndex).registerC addInstruction( @@ -281,10 +278,10 @@ object VideoInformationPatch : BytecodePatch( OnPlaybackSpeedItemClickFingerprint.resultOrThrow().let { it.mutableMethod.apply { speedSelectionInsertMethod = this - val speedSelectionValueInstructionIndex = getTargetIndex(Opcode.IGET) + val speedSelectionValueInstructionIndex = getTargetIndexOrThrow(Opcode.IGET) val setPlaybackSpeedContainerClassFieldIndex = - getTargetIndexReversed(speedSelectionValueInstructionIndex, Opcode.IGET_OBJECT) + getTargetIndexReversedOrThrow(speedSelectionValueInstructionIndex, Opcode.IGET_OBJECT) val setPlaybackSpeedContainerClassFieldReference = getInstruction(setPlaybackSpeedContainerClassFieldIndex).reference.toString() @@ -455,12 +452,11 @@ object VideoInformationPatch : BytecodePatch( private fun MethodFingerprint.getMethodName(returnType: String): String { resultOrThrow().mutableMethod.apply { - val targetIndex = indexOfFirstInstruction { + val targetIndex = indexOfFirstInstructionOrThrow { opcode == Opcode.INVOKE_INTERFACE && getReference()?.definingClass == PLAYER_RESPONSE_MODEL_CLASS_DESCRIPTOR && getReference()?.returnType == returnType } - if (targetIndex < 0) throw PatchException("Could not find instruction index.") val targetReference = getInstruction(targetIndex).reference return "invoke-interface {v${REGISTER_PLAYER_RESPONSE_MODEL}}, $targetReference" diff --git a/src/main/kotlin/app/revanced/patches/youtube/video/playback/VideoPlaybackPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/video/playback/VideoPlaybackPatch.kt index 5aed85022..62b50b1af 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/video/playback/VideoPlaybackPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/video/playback/VideoPlaybackPatch.kt @@ -34,7 +34,9 @@ import app.revanced.patches.youtube.video.videoid.VideoIdPatch import app.revanced.util.getReference import app.revanced.util.getStringInstructionIndex import app.revanced.util.getTargetIndex +import app.revanced.util.getTargetIndexOrThrow import app.revanced.util.indexOfFirstInstruction +import app.revanced.util.indexOfFirstInstructionOrThrow import app.revanced.util.patch.BaseBytecodePatch import app.revanced.util.resultOrThrow import app.revanced.util.updatePatchStatus @@ -133,7 +135,7 @@ object VideoPlaybackPatch : BaseBytecodePatch( speedSelectionInsertMethod ).forEach { it.apply { - val speedSelectionValueInstructionIndex = getTargetIndex(Opcode.IGET) + val speedSelectionValueInstructionIndex = getTargetIndexOrThrow(Opcode.IGET) val speedSelectionValueRegister = getInstruction(speedSelectionValueInstructionIndex).registerA @@ -206,7 +208,7 @@ object VideoPlaybackPatch : BaseBytecodePatch( QualityMenuViewInflateFingerprint.resultOrThrow().let { it.mutableMethod.apply { - val insertIndex = getTargetIndex(Opcode.CHECK_CAST) + val insertIndex = getTargetIndexOrThrow(Opcode.CHECK_CAST) val insertRegister = getInstruction(insertIndex).registerA addInstruction( @@ -219,10 +221,10 @@ object VideoPlaybackPatch : BaseBytecodePatch( it.mutableClass.methods.find { method -> method.name == "onItemClick" } onItemClickMethod?.apply { - val insertIndex = getTargetIndex(Opcode.IGET_OBJECT) + val insertIndex = getTargetIndexOrThrow(Opcode.IGET_OBJECT) val insertRegister = getInstruction(insertIndex).registerA - val jumpIndex = indexOfFirstInstruction { + val jumpIndex = indexOfFirstInstructionOrThrow { opcode == Opcode.IGET_OBJECT && this.getReference()?.type == videoQualityClass } diff --git a/src/main/kotlin/app/revanced/util/BytecodeUtils.kt b/src/main/kotlin/app/revanced/util/BytecodeUtils.kt index 4631c4193..9498c17f0 100644 --- a/src/main/kotlin/app/revanced/util/BytecodeUtils.kt +++ b/src/main/kotlin/app/revanced/util/BytecodeUtils.kt @@ -340,10 +340,81 @@ inline fun Instruction.getReference() = fun Method.indexOfFirstInstruction(predicate: Instruction.() -> Boolean) = this.implementation!!.instructions.indexOfFirst(predicate) -fun MutableMethod.getTargetIndex(opcode: Opcode) = getTargetIndex(0, opcode) +fun MutableMethod.getTargetIndexOrThrow(opcode: Opcode) = + getTargetIndexOrThrow(0, opcode) -fun MutableMethod.getTargetIndexReversed(opcode: Opcode) = - getTargetIndexReversed(implementation!!.instructions.size - 1, opcode) +fun MutableMethod.getTargetIndexOrThrow(startIndex: Int, opcode: Opcode) = + checkIndex(getTargetIndex(startIndex, opcode), startIndex, opcode) + +fun MutableMethod.getTargetIndexReversedOrThrow(opcode: Opcode) = + getTargetIndexReversedOrThrow(implementation!!.instructions.lastIndex, opcode) + +fun MutableMethod.getTargetIndexReversedOrThrow(startIndex: Int, opcode: Opcode) = + checkIndex(getTargetIndexReversed(startIndex, opcode), startIndex, opcode) + +fun Method.getTargetIndexWithFieldReferenceNameOrThrow(filedName: String) = + checkIndex(getTargetIndexWithFieldReferenceName(filedName), 0, filedName) + +fun MutableMethod.getTargetIndexWithFieldReferenceNameOrThrow(startIndex: Int, filedName: String) = + checkIndex(getTargetIndexWithFieldReferenceName(startIndex, filedName), startIndex, filedName) + +fun MutableMethod.getTargetIndexWithFieldReferenceNameReversedOrThrow(returnType: String) = + getTargetIndexWithFieldReferenceNameReversedOrThrow(implementation!!.instructions.lastIndex, returnType) + +fun MutableMethod.getTargetIndexWithFieldReferenceNameReversedOrThrow(startIndex: Int, returnType: String) = + checkIndex(getTargetIndexWithFieldReferenceNameReversed(startIndex, returnType), startIndex, returnType) + +fun Method.getTargetIndexWithFieldReferenceTypeOrThrow(returnType: String) = + checkIndex(getTargetIndexWithFieldReferenceType(returnType), 0, returnType) + +fun MutableMethod.getTargetIndexWithFieldReferenceTypeOrThrow(startIndex: Int, returnType: String) = + checkIndex(getTargetIndexWithFieldReferenceType(startIndex, returnType), startIndex, returnType) + +fun MutableMethod.getTargetIndexWithFieldReferenceTypeReversedOrThrow(returnType: String) = + getTargetIndexWithFieldReferenceTypeReversedOrThrow(implementation!!.instructions.lastIndex, returnType) + +fun MutableMethod.getTargetIndexWithFieldReferenceTypeReversedOrThrow(startIndex: Int, returnType: String) = + checkIndex(getTargetIndexWithFieldReferenceTypeReversed(startIndex, returnType), startIndex, returnType) + +fun Method.getTargetIndexWithMethodReferenceNameOrThrow(methodName: String) = + checkIndex(getTargetIndexWithMethodReferenceName(methodName), 0, methodName) + +fun MutableMethod.getTargetIndexWithMethodReferenceNameOrThrow(startIndex: Int, methodName: String) = + checkIndex(getTargetIndexWithMethodReferenceName(startIndex, methodName), startIndex, methodName) + +fun MutableMethod.getTargetIndexWithMethodReferenceNameReversedOrThrow(methodName: String) = + getTargetIndexWithMethodReferenceNameReversedOrThrow(implementation!!.instructions.lastIndex, methodName) + +fun MutableMethod.getTargetIndexWithMethodReferenceNameReversedOrThrow(startIndex: Int, methodName: String) = + checkIndex(getTargetIndexWithMethodReferenceNameReversed(startIndex, methodName), startIndex, methodName) + +fun Method.getTargetIndexWithReferenceOrThrow(reference: String) = + checkIndex(getTargetIndexWithReference(reference), 0, reference) + +fun MutableMethod.getTargetIndexWithReferenceOrThrow(startIndex: Int, reference: String) = + checkIndex(getTargetIndexWithReference(startIndex, reference), startIndex, reference) + +fun MutableMethod.getTargetIndexWithReferenceReversedOrThrow(reference: String) = + getTargetIndexWithReferenceReversedOrThrow(implementation!!.instructions.lastIndex, reference) + +fun MutableMethod.getTargetIndexWithReferenceReversedOrThrow(startIndex: Int, reference: String) = + checkIndex(getTargetIndexWithReferenceReversed(startIndex, reference), startIndex, reference) + +fun checkIndex(index: Int, startIndex: Int, opcode: Opcode): Int { + if (index < 0) { + throw PatchException("Target index not found. startIndex: $startIndex, opcode: $opcode") + } + return index +} + +fun checkIndex(index: Int, startIndex: Int, name: String): Int { + if (index < 0) { + throw PatchException("Target index not found. startIndex: $startIndex, name: $name") + } + return index +} + +fun MutableMethod.getTargetIndex(opcode: Opcode) = getTargetIndex(0, opcode) fun MutableMethod.getTargetIndex(startIndex: Int, opcode: Opcode) = implementation!!.instructions.let { @@ -352,6 +423,9 @@ fun MutableMethod.getTargetIndex(startIndex: Int, opcode: Opcode) = } } +fun MutableMethod.getTargetIndexReversed(opcode: Opcode) = + getTargetIndexReversed(implementation!!.instructions.size - 1, opcode) + fun MutableMethod.getTargetIndexReversed(startIndex: Int, opcode: Opcode): Int { for (index in startIndex downTo 0) { if (getInstruction(index).opcode != opcode) @@ -368,16 +442,16 @@ fun Method.getTargetIndexWithFieldReferenceName(filedName: String) = implementat } } ?: -1 -fun MutableMethod.getTargetIndexWithFieldReferenceNameReversed(returnType: String) = - getTargetIndexWithFieldReferenceTypeReversed(implementation!!.instructions.size - 1, returnType) - fun MutableMethod.getTargetIndexWithFieldReferenceName(startIndex: Int, filedName: String) = implementation!!.instructions.let { - startIndex + it.subList(startIndex, it.size - 1).indexOfFirst { instruction -> + startIndex + it.subList(startIndex, it.lastIndex).indexOfFirst { instruction -> instruction.getReference()?.name == filedName } } +fun MutableMethod.getTargetIndexWithFieldReferenceNameReversed(returnType: String) = + getTargetIndexWithFieldReferenceTypeReversed(implementation!!.instructions.lastIndex, returnType) + fun MutableMethod.getTargetIndexWithFieldReferenceNameReversed( startIndex: Int, filedName: String