From 7e1d82f1161237632c3a57f109db6b8903006b33 Mon Sep 17 00:00:00 2001 From: TheJeterLP Date: Mon, 18 Jul 2022 22:59:21 +0200 Subject: [PATCH] fix: make all patches toggleable with settings (#202) --- .../bytecode/patch/GeneralBytecodeAdsPatch.kt | 2 + .../resource/patch/GeneralResourceAdsPatch.kt | 0 .../patch/HideInfocardSuggestionsPatch.kt | 23 +++------ .../patch/HideAutoplayButton.kt | 48 ++++++++++++++----- .../patch/FullscreenPanelsRemoverPatch.kt | 38 +++++++-------- .../patch/MinimizedPlaybackPatch.kt | 14 +++++- .../integrations/patch/IntegrationsPatch.kt | 9 ++-- 7 files changed, 79 insertions(+), 55 deletions(-) delete mode 100644 src/main/kotlin/app/revanced/patches/youtube/ad/general/resource/patch/GeneralResourceAdsPatch.kt diff --git a/src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/patch/GeneralBytecodeAdsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/patch/GeneralBytecodeAdsPatch.kt index 10d158c9..dd387c1b 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/patch/GeneralBytecodeAdsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/patch/GeneralBytecodeAdsPatch.kt @@ -127,6 +127,7 @@ class GeneralBytecodeAdsPatch : BytecodePatch() { if (mutableMethod == null) mutableMethod = mutableClass!!.findMutableMethodOf(method) + //ToDo: Add Settings toggle for whatever this is mutableMethod!!.implementation!!.removeInstruction(removeIndex) } @@ -186,6 +187,7 @@ class GeneralBytecodeAdsPatch : BytecodePatch() { // return the method val insertIndex = 1 // after super constructor + //ToDo: Add setting here mutableMethod!!.implementation!!.addInstruction( insertIndex, BuilderInstruction10x(Opcode.RETURN_VOID) ) diff --git a/src/main/kotlin/app/revanced/patches/youtube/ad/general/resource/patch/GeneralResourceAdsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/ad/general/resource/patch/GeneralResourceAdsPatch.kt deleted file mode 100644 index e69de29b..00000000 diff --git a/src/main/kotlin/app/revanced/patches/youtube/ad/infocardsuggestions/patch/HideInfocardSuggestionsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/ad/infocardsuggestions/patch/HideInfocardSuggestionsPatch.kt index e11af224..0bef593f 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/ad/infocardsuggestions/patch/HideInfocardSuggestionsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/ad/infocardsuggestions/patch/HideInfocardSuggestionsPatch.kt @@ -4,21 +4,19 @@ import app.revanced.patcher.annotation.Description import app.revanced.patcher.annotation.Name import app.revanced.patcher.annotation.Version import app.revanced.patcher.data.impl.BytecodeData -import app.revanced.patcher.extensions.addInstructions -import app.revanced.patcher.extensions.removeInstruction +import app.revanced.patcher.extensions.replaceInstruction import app.revanced.patcher.fingerprint.method.utils.MethodFingerprintUtils.resolve -import app.revanced.patcher.patch.annotations.Dependencies -import app.revanced.patcher.patch.annotations.Patch -import app.revanced.patcher.patch.impl.BytecodePatch import app.revanced.patcher.patch.PatchResult import app.revanced.patcher.patch.PatchResultError import app.revanced.patcher.patch.PatchResultSuccess +import app.revanced.patcher.patch.annotations.Dependencies +import app.revanced.patcher.patch.annotations.Patch +import app.revanced.patcher.patch.impl.BytecodePatch import app.revanced.patches.youtube.ad.infocardsuggestions.annotations.HideInfocardSuggestionsCompatibility import app.revanced.patches.youtube.ad.infocardsuggestions.fingerprints.HideInfocardSuggestionsFingerprint import app.revanced.patches.youtube.ad.infocardsuggestions.fingerprints.HideInfocardSuggestionsParentFingerprint import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch import org.jf.dexlib2.builder.instruction.BuilderInstruction35c -import org.jf.dexlib2.iface.instruction.FiveRegisterInstruction @Patch @Dependencies([IntegrationsPatch::class]) @@ -45,17 +43,10 @@ class HideInfocardSuggestionsPatch : BytecodePatch( ?: return PatchResultError("Implementation not found.") val index = implementation.instructions.indexOfFirst { ((it as? BuilderInstruction35c)?.reference.toString() == "Landroid/view/View;->setVisibility(I)V") } - val register = "v" + (implementation.instructions.get(index) as FiveRegisterInstruction).registerD - method.removeInstruction(index) - - method.addInstructions( - index, """ - invoke-static {}, Lapp/revanced/integrations/patches/HideInfoCardSuggestionsPatch;->hideInfoCardSuggestions()I - move-result $register - invoke-virtual {p1, $register}, Landroid/view/View;->setVisibility(I)V - """ - ) + method.replaceInstruction(index, """ + invoke-static {p1}, Lapp/revanced/integrations/patches/HideInfoCardSuggestionsPatch;->hideInfoCardSuggestions(Landroid/view/View;)V + """) return PatchResultSuccess() } diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/autoplaybutton/patch/HideAutoplayButton.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/autoplaybutton/patch/HideAutoplayButton.kt index f5de3ccf..38b5c369 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/layout/autoplaybutton/patch/HideAutoplayButton.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/autoplaybutton/patch/HideAutoplayButton.kt @@ -4,21 +4,25 @@ import app.revanced.patcher.annotation.Description import app.revanced.patcher.annotation.Name import app.revanced.patcher.annotation.Version import app.revanced.patcher.data.impl.BytecodeData -import app.revanced.patcher.extensions.removeInstructions +import app.revanced.patcher.extensions.addInstructions +import app.revanced.patcher.extensions.removeInstruction import app.revanced.patcher.extensions.replaceInstructions import app.revanced.patcher.patch.PatchResult import app.revanced.patcher.patch.PatchResultSuccess import app.revanced.patcher.patch.annotations.Dependencies import app.revanced.patcher.patch.annotations.Patch import app.revanced.patcher.patch.impl.BytecodePatch +import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod import app.revanced.patches.youtube.layout.autoplaybutton.annotations.AutoplayButtonCompatibility import app.revanced.patches.youtube.layout.autoplaybutton.fingerprints.AutonavInformerFingerprint import app.revanced.patches.youtube.layout.autoplaybutton.fingerprints.LayoutConstructorFingerprint +import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch import app.revanced.patches.youtube.misc.mapping.patch.ResourceIdMappingProviderResourcePatch import org.jf.dexlib2.iface.instruction.WideLiteralInstruction +import org.jf.dexlib2.iface.instruction.formats.Instruction35c @Patch -@Dependencies([ResourceIdMappingProviderResourcePatch::class]) +@Dependencies([ResourceIdMappingProviderResourcePatch::class, IntegrationsPatch::class]) @Name("hide-autoplay-button") @Description("Hides the autoplay button in the video player.") @AutoplayButtonCompatibility @@ -36,26 +40,46 @@ class HideAutoplayButton : BytecodePatch( val autonavPreviewStub = ResourceIdMappingProviderResourcePatch.resourceMappings.single { it.type == "id" && it.name == "autonav_preview_stub" } - val autonavToggleConstIndex = - layoutGenMethod.implementation!!.instructions.indexOfFirst { (it as? WideLiteralInstruction)?.wideLiteral == autonavToggle.id } - val autonavPreviewStubConstIndex = - layoutGenMethod.implementation!!.instructions.indexOfFirst { (it as? WideLiteralInstruction)?.wideLiteral == autonavPreviewStub.id } + val instructions = layoutGenMethod.implementation!!.instructions - //remove adding autoplay button to the layout - layoutGenMethod.removeInstructions(autonavToggleConstIndex, 5) - layoutGenMethod.removeInstructions(autonavPreviewStubConstIndex, 5) + val autonavToggleConstIndex = + instructions.indexOfFirst { (it as? WideLiteralInstruction)?.wideLiteral == autonavToggle.id } + 4 + val autonavPreviewStubConstIndex = + instructions.indexOfFirst { (it as? WideLiteralInstruction)?.wideLiteral == autonavPreviewStub.id } + 4 + + injectIfBranch(layoutGenMethod, autonavToggleConstIndex) + injectIfBranch(layoutGenMethod, autonavPreviewStubConstIndex) val autonavInformerMethod = AutonavInformerFingerprint.result!!.mutableMethod //force disable autoplay since it's hard to do without the button autonavInformerMethod.replaceInstructions( - 0, - """ - const/4 v0, 0x0 + 0, """ + invoke-static {}, Lapp/revanced/integrations/patches/HideAutoplayButtonPatch;->isButtonHidden()Z + move-result v0 return v0 """ ) return PatchResultSuccess() } + + private fun injectIfBranch(method: MutableMethod, index: Int) { + val instructions = method.implementation!!.instructions + val insn = (instructions.get(index) as? Instruction35c)!! + val methodToCall = insn.reference.toString() + + //remove the invoke-virtual because we want to put it in an if-statement + method.removeInstruction(index) + method.addInstructions( + index, """ + invoke-static {}, Lapp/revanced/integrations/patches/HideAutoplayButtonPatch;->isButtonShown()Z + move-result v11 + if-eqz v11, :hidebutton + invoke-virtual {v${insn.registerC}, v${insn.registerD}, v${insn.registerE}}, $methodToCall + :hidebutton + nop + """ + ) + } } diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/fullscreenpanels/patch/FullscreenPanelsRemoverPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/fullscreenpanels/patch/FullscreenPanelsRemoverPatch.kt index 3fa06cb9..5a1e4603 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/layout/fullscreenpanels/patch/FullscreenPanelsRemoverPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/fullscreenpanels/patch/FullscreenPanelsRemoverPatch.kt @@ -4,47 +4,47 @@ import app.revanced.patcher.annotation.Description import app.revanced.patcher.annotation.Name import app.revanced.patcher.annotation.Version import app.revanced.patcher.data.impl.BytecodeData -import app.revanced.patcher.extensions.addInstruction -import app.revanced.patcher.extensions.removeInstruction +import app.revanced.patcher.extensions.addInstructions import app.revanced.patcher.patch.PatchResult +import app.revanced.patcher.patch.PatchResultError import app.revanced.patcher.patch.PatchResultSuccess +import app.revanced.patcher.patch.annotations.Dependencies import app.revanced.patcher.patch.annotations.Patch import app.revanced.patcher.patch.impl.BytecodePatch import app.revanced.patches.youtube.layout.fullscreenpanels.annotations.FullscreenPanelsCompatibility import app.revanced.patches.youtube.layout.fullscreenpanels.fingerprints.FullscreenViewAdderFingerprint -import org.jf.dexlib2.Opcode -import org.jf.dexlib2.iface.instruction.FiveRegisterInstruction +import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch import org.jf.dexlib2.iface.instruction.ReferenceInstruction import org.jf.dexlib2.iface.reference.MethodReference @Patch @Name("disable-fullscreen-panels") +@Dependencies([IntegrationsPatch::class]) @Description("Disables video description and comments panel in fullscreen view.") @FullscreenPanelsCompatibility @Version("0.0.1") -class FullscreenPanelsRemovalPatch : BytecodePatch( +class FullscreenPanelsRemoverPatch : BytecodePatch( listOf( FullscreenViewAdderFingerprint ) ) { override fun execute(data: BytecodeData): PatchResult { - val method = FullscreenViewAdderFingerprint.result?.mutableMethod!! - val implementation = method.implementation!! + val result = FullscreenViewAdderFingerprint.result + ?: return PatchResultError("Fingerprint not resolved!") - val (visibilityCallIndex, visibilityCall) = - implementation.instructions.withIndex() - .first { ((it.value as? ReferenceInstruction)?.reference as? MethodReference)?.name == ("setVisibility") } + val method = result.mutableMethod + val implementation = method.implementation + ?: return PatchResultError("Implementation not found!") - val gotoIndex = - implementation.instructions.subList(0, visibilityCallIndex).indexOfLast { it.opcode == Opcode.GOTO } + val visibilityCallIndex = implementation.instructions.withIndex() + .first { ((it.value as? ReferenceInstruction)?.reference as? MethodReference)?.name == ("setVisibility") }.index - //force the if - method.removeInstruction(gotoIndex) - - val visibilityIntRegister = (visibilityCall as FiveRegisterInstruction).registerD - - //set the visibility to GONE - method.addInstruction(visibilityCallIndex - 1, "const/16 v$visibilityIntRegister, 0x8") + method.addInstructions( + visibilityCallIndex - 1, """ + invoke-static { }, Lapp/revanced/integrations/patches/FullscreenPanelsRemoverPatch;->getFullsceenPanelsVisibility()I + move-result p1 + """ + ) return PatchResultSuccess() } diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/minimizedplayback/patch/MinimizedPlaybackPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/minimizedplayback/patch/MinimizedPlaybackPatch.kt index b7ce3ca8..6818a959 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/layout/minimizedplayback/patch/MinimizedPlaybackPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/minimizedplayback/patch/MinimizedPlaybackPatch.kt @@ -8,6 +8,7 @@ import app.revanced.patcher.data.impl.toMethodWalker import app.revanced.patcher.extensions.addInstructions import app.revanced.patcher.patch.PatchResult import app.revanced.patcher.patch.PatchResultSuccess +import app.revanced.patcher.patch.annotations.Dependencies import app.revanced.patcher.patch.annotations.Patch import app.revanced.patcher.patch.impl.BytecodePatch import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod @@ -15,6 +16,7 @@ import app.revanced.patches.youtube.layout.minimizedplayback.annotations.Minimiz import app.revanced.patches.youtube.layout.minimizedplayback.fingerprints.MinimizedPlaybackKidsFingerprint import app.revanced.patches.youtube.layout.minimizedplayback.fingerprints.MinimizedPlaybackManagerFingerprint import app.revanced.patches.youtube.layout.minimizedplayback.fingerprints.MinimizedPlaybackSettingsFingerprint +import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch import org.jf.dexlib2.iface.instruction.ReferenceInstruction import org.jf.dexlib2.iface.reference.MethodReference @@ -22,6 +24,7 @@ import org.jf.dexlib2.iface.reference.MethodReference @Patch @Name("minimized-playback") @Description("Enables minimized and background playback.") +@Dependencies([IntegrationsPatch::class]) @MinimizedPlaybackCompatibility @Version("0.0.1") class MinimizedPlaybackPatch : BytecodePatch( @@ -34,7 +37,8 @@ class MinimizedPlaybackPatch : BytecodePatch( // we return the method at the beginning instead MinimizedPlaybackManagerFingerprint.result!!.mutableMethod.addInstructions( 0, """ - const/4 v0, 0x1 + invoke-static {}, Lapp/revanced/integrations/patches/MinimizedPlaybackPatch;->isMinimizedPlaybackEnabled()Z + move-result v0 return v0 """ ) @@ -49,14 +53,20 @@ class MinimizedPlaybackPatch : BytecodePatch( settingsBooleanMethod.addInstructions( 0, """ - const/4 v0, 0x1 + invoke-static {}, Lapp/revanced/integrations/patches/MinimizedPlaybackPatch;->isMinimizedPlaybackEnabled()Z + move-result v0 return v0 """ ) MinimizedPlaybackKidsFingerprint.result!!.mutableMethod.addInstructions( 0, """ + invoke-static {}, Lapp/revanced/integrations/patches/MinimizedPlaybackPatch;->isMinimizedPlaybackEnabled()Z + move-result v0 + if-eqz v0, :enable return-void + :enable + nop """ ) diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/integrations/patch/IntegrationsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/integrations/patch/IntegrationsPatch.kt index f9faf2b9..afad662e 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/misc/integrations/patch/IntegrationsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/integrations/patch/IntegrationsPatch.kt @@ -4,7 +4,7 @@ import app.revanced.patcher.annotation.Description import app.revanced.patcher.annotation.Name import app.revanced.patcher.annotation.Version import app.revanced.patcher.data.impl.BytecodeData -import app.revanced.patcher.extensions.addInstructions +import app.revanced.patcher.extensions.addInstruction import app.revanced.patcher.extensions.or import app.revanced.patcher.patch.PatchResult import app.revanced.patcher.patch.PatchResultError @@ -37,11 +37,8 @@ class IntegrationsPatch : BytecodePatch( val implementation = method.implementation!! val count = implementation.registerCount - 1 - method.addInstructions( - 0, """ - invoke-static {v$count}, Lapp/revanced/integrations/sponsorblock/StringRef;->setContext(Landroid/content/Context;)V - sput-object v$count, Lapp/revanced/integrations/utils/ReVancedUtils;->context:Landroid/content/Context; - """ + method.addInstruction( + 0, "sput-object v$count, Lapp/revanced/integrations/utils/ReVancedUtils;->context:Landroid/content/Context;" ) val classDef = result.mutableClass