From 99800ac558deec5035ba94254ea6595e569d4c42 Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Sun, 18 Sep 2022 07:33:42 +0200 Subject: [PATCH] fix(hide-create-button): increase patching compatibility across versions Signed-off-by: oSumAtrIX --- .../patch/CreateButtonRemoverPatch.kt | 79 ------------------- .../FullscreenViewAdderFingerprint.kt | 2 +- .../FullscreenViewAdderParentFingerprint.kt | 2 +- .../annotations/CreateButtonCompatibility.kt | 2 +- .../PivotBarCreateButtonViewFingerprint.kt | 24 ++++++ .../patch/CreateButtonRemoverPatch.kt | 72 +++++++++++++++++ 6 files changed, 99 insertions(+), 82 deletions(-) delete mode 100644 src/main/kotlin/app/revanced/patches/youtube/layout/createbutton/patch/CreateButtonRemoverPatch.kt rename src/main/kotlin/app/revanced/patches/youtube/layout/{ => pivotbar}/createbutton/annotations/CreateButtonCompatibility.kt (86%) create mode 100644 src/main/kotlin/app/revanced/patches/youtube/layout/pivotbar/createbutton/fingerprints/PivotBarCreateButtonViewFingerprint.kt create mode 100644 src/main/kotlin/app/revanced/patches/youtube/layout/pivotbar/createbutton/patch/CreateButtonRemoverPatch.kt diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/createbutton/patch/CreateButtonRemoverPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/createbutton/patch/CreateButtonRemoverPatch.kt deleted file mode 100644 index 68d17912f..000000000 --- a/src/main/kotlin/app/revanced/patches/youtube/layout/createbutton/patch/CreateButtonRemoverPatch.kt +++ /dev/null @@ -1,79 +0,0 @@ -package app.revanced.patches.youtube.layout.createbutton.patch - -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.patch.PatchResult -import app.revanced.patcher.patch.PatchResultError -import app.revanced.patcher.patch.PatchResultSuccess -import app.revanced.patcher.patch.annotations.DependsOn -import app.revanced.patcher.patch.annotations.Patch -import app.revanced.patcher.patch.impl.BytecodePatch -import app.revanced.patches.youtube.layout.createbutton.annotations.CreateButtonCompatibility -import app.revanced.patches.youtube.layout.createbutton.fingerprints.CreateButtonFingerprint -import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch -import app.revanced.patches.youtube.misc.mapping.patch.ResourceIdMappingProviderResourcePatch -import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch -import app.revanced.patches.youtube.misc.settings.framework.components.impl.StringResource -import app.revanced.patches.youtube.misc.settings.framework.components.impl.SwitchPreference -import org.jf.dexlib2.Opcode -import org.jf.dexlib2.iface.instruction.OneRegisterInstruction -import org.jf.dexlib2.iface.instruction.ReferenceInstruction -import org.jf.dexlib2.iface.instruction.WideLiteralInstruction -import org.jf.dexlib2.iface.reference.MethodReference - -@Patch -@DependsOn([IntegrationsPatch::class, ResourceIdMappingProviderResourcePatch::class, SettingsPatch::class]) -@Name("disable-create-button") -@Description("Hides the create button in the navigation bar.") -@CreateButtonCompatibility -@Version("0.0.1") -class CreateButtonRemoverPatch : BytecodePatch( - listOf( - CreateButtonFingerprint - ) -) { - override fun execute(data: BytecodeData): PatchResult { - SettingsPatch.PreferenceScreen.LAYOUT.addPreferences( - SwitchPreference( - "revanced_create_button_enabled", - StringResource("revanced_create_button_enabled_title", "Show create button"), - false, - StringResource("revanced_create_button_summary_on", "Create button is shown"), - StringResource("revanced_create_button_summary_off", "Create button is hidden") - ) - ) - - val result = CreateButtonFingerprint.result!! - - // Get the required register which holds the view object we need to pass to the method hideCreateButton - val implementation = result.mutableMethod.implementation!! - - val imageOnlyLayout = - ResourceIdMappingProviderResourcePatch.resourceMappings.single { it.type == "layout" && it.name == "image_only_tab" } - - val imageOnlyLayoutConstIndex = - implementation.instructions.indexOfFirst { (it as? WideLiteralInstruction)?.wideLiteral == imageOnlyLayout.id } - - val (instructionIndex, instruction) = implementation.instructions.drop(imageOnlyLayoutConstIndex).withIndex() - .first { - (((it.value as? ReferenceInstruction)?.reference) as? MethodReference)?.definingClass?.contains("PivotBar") - ?: false - } - - if (instruction.opcode != Opcode.INVOKE_VIRTUAL) return PatchResultError("Could not find the correct instruction") - - val moveResultIndex = imageOnlyLayoutConstIndex + instructionIndex + 1 - val moveResultInstruction = implementation.instructions[moveResultIndex] as OneRegisterInstruction - - // Hide the button view via proxy by passing it to the hideCreateButton method - result.mutableMethod.addInstruction( - moveResultIndex + 1, - "invoke-static { v${moveResultInstruction.registerA} }, Lapp/revanced/integrations/patches/HideCreateButtonPatch;->hideCreateButton(Landroid/view/View;)V" - ) - - return PatchResultSuccess() - } -} diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/fullscreenpanels/fingerprints/FullscreenViewAdderFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/fullscreenpanels/fingerprints/FullscreenViewAdderFingerprint.kt index 0e714c1f1..91937674b 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/layout/fullscreenpanels/fingerprints/FullscreenViewAdderFingerprint.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/fullscreenpanels/fingerprints/FullscreenViewAdderFingerprint.kt @@ -5,7 +5,7 @@ import app.revanced.patcher.annotation.Version import app.revanced.patcher.fingerprint.method.annotation.DirectPatternScanMethod import app.revanced.patcher.fingerprint.method.annotation.MatchingMethod import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint -import app.revanced.patches.youtube.layout.shorts.button.annotations.ShortsButtonCompatibility +import app.revanced.patches.youtube.layout.pivotbar.shortsbutton.annotations.ShortsButtonCompatibility import org.jf.dexlib2.Opcode @Name("fullscreen-view-adder-fingerprint") diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/fullscreenpanels/fingerprints/FullscreenViewAdderParentFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/fullscreenpanels/fingerprints/FullscreenViewAdderParentFingerprint.kt index fc3126d5d..eaee7d91c 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/layout/fullscreenpanels/fingerprints/FullscreenViewAdderParentFingerprint.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/fullscreenpanels/fingerprints/FullscreenViewAdderParentFingerprint.kt @@ -5,7 +5,7 @@ import app.revanced.patcher.annotation.Version import app.revanced.patcher.fingerprint.method.annotation.DirectPatternScanMethod import app.revanced.patcher.fingerprint.method.annotation.MatchingMethod import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint -import app.revanced.patches.youtube.layout.shorts.button.annotations.ShortsButtonCompatibility +import app.revanced.patches.youtube.layout.pivotbar.shortsbutton.annotations.ShortsButtonCompatibility import org.jf.dexlib2.Opcode @Name("fullscreen-view-adder-parent-fingerprint") diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/createbutton/annotations/CreateButtonCompatibility.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/pivotbar/createbutton/annotations/CreateButtonCompatibility.kt similarity index 86% rename from src/main/kotlin/app/revanced/patches/youtube/layout/createbutton/annotations/CreateButtonCompatibility.kt rename to src/main/kotlin/app/revanced/patches/youtube/layout/pivotbar/createbutton/annotations/CreateButtonCompatibility.kt index 503417369..79e0d9a89 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/layout/createbutton/annotations/CreateButtonCompatibility.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/pivotbar/createbutton/annotations/CreateButtonCompatibility.kt @@ -1,4 +1,4 @@ -package app.revanced.patches.youtube.layout.createbutton.annotations +package app.revanced.patches.youtube.layout.pivotbar.createbutton.annotations import app.revanced.patcher.annotation.Compatibility import app.revanced.patcher.annotation.Package diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/pivotbar/createbutton/fingerprints/PivotBarCreateButtonViewFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/pivotbar/createbutton/fingerprints/PivotBarCreateButtonViewFingerprint.kt new file mode 100644 index 000000000..9279e5af9 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/pivotbar/createbutton/fingerprints/PivotBarCreateButtonViewFingerprint.kt @@ -0,0 +1,24 @@ +package app.revanced.patches.youtube.layout.pivotbar.createbutton.fingerprints + +import app.revanced.patcher.annotation.Name +import app.revanced.patcher.annotation.Version +import app.revanced.patcher.fingerprint.method.annotation.MatchingMethod +import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint +import app.revanced.patches.youtube.layout.pivotbar.createbutton.annotations.CreateButtonCompatibility +import org.jf.dexlib2.Opcode + +@Name("pivot-bar-create-button-view-fingerprint") +@MatchingMethod( + "Lknw", "z" +) +@CreateButtonCompatibility +@Version("0.0.1") +object PivotBarCreateButtonViewFingerprint : MethodFingerprint( + opcodes = listOf( + Opcode.INVOKE_DIRECT_RANGE, // unique instruction anchor + Opcode.CONST_4, + Opcode.INVOKE_VIRTUAL, + Opcode.MOVE_RESULT_OBJECT, + Opcode.INVOKE_STATIC + ) +) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/youtube/layout/pivotbar/createbutton/patch/CreateButtonRemoverPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/layout/pivotbar/createbutton/patch/CreateButtonRemoverPatch.kt new file mode 100644 index 000000000..b37f5c514 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/layout/pivotbar/createbutton/patch/CreateButtonRemoverPatch.kt @@ -0,0 +1,72 @@ +package app.revanced.patches.youtube.layout.pivotbar.createbutton.patch + +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.MethodFingerprintExtensions.name +import app.revanced.patcher.fingerprint.method.utils.MethodFingerprintUtils.resolve +import app.revanced.patcher.patch.PatchResult +import app.revanced.patcher.patch.PatchResultError +import app.revanced.patcher.patch.PatchResultSuccess +import app.revanced.patcher.patch.annotations.DependsOn +import app.revanced.patcher.patch.annotations.Patch +import app.revanced.patcher.patch.impl.BytecodePatch +import app.revanced.patches.youtube.layout.pivotbar.InjectionUtils.REGISTER_TEMPLATE_REPLACEMENT +import app.revanced.patches.youtube.layout.pivotbar.InjectionUtils.injectHook +import app.revanced.patches.youtube.layout.pivotbar.createbutton.annotations.CreateButtonCompatibility +import app.revanced.patches.youtube.layout.pivotbar.createbutton.fingerprints.PivotBarCreateButtonViewFingerprint +import app.revanced.patches.youtube.layout.pivotbar.fingerprints.PivotBarFingerprint +import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch +import app.revanced.patches.youtube.misc.mapping.patch.ResourceIdMappingProviderResourcePatch +import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch +import app.revanced.patches.youtube.misc.settings.framework.components.impl.StringResource +import app.revanced.patches.youtube.misc.settings.framework.components.impl.SwitchPreference + +@Patch +@DependsOn([IntegrationsPatch::class, ResourceIdMappingProviderResourcePatch::class, SettingsPatch::class]) +@Name("hide-create-button") +@Description("Hides the create button in the navigation bar.") +@CreateButtonCompatibility +@Version("0.0.1") +class CreateButtonRemoverPatch : BytecodePatch( + listOf( + PivotBarFingerprint + ) +) { + override fun execute(data: BytecodeData): PatchResult { + SettingsPatch.PreferenceScreen.LAYOUT.addPreferences( + SwitchPreference( + "revanced_create_button_enabled", + StringResource("revanced_create_button_enabled_title", "Show create button"), + false, + StringResource("revanced_create_button_summary_on", "Create button is shown"), + StringResource("revanced_create_button_summary_off", "Create button is hidden") + ) + ) + + /* + * Resolve fingerprints + */ + + val pivotBarResult = PivotBarFingerprint.result ?: return PatchResultError("PivotBarFingerprint failed") + + if (!PivotBarCreateButtonViewFingerprint.resolve(data, pivotBarResult.method, pivotBarResult.classDef)) + return PatchResultError("${PivotBarCreateButtonViewFingerprint.name} failed") + + val createButtonResult = PivotBarCreateButtonViewFingerprint.result!! + val insertIndex = createButtonResult.patternScanResult!!.endIndex + + /* + * Inject hooks + */ + + val integrationsClass = "Lapp/revanced/integrations/patches/HideCreateButtonPatch;" + val hook = + "invoke-static { v$REGISTER_TEMPLATE_REPLACEMENT }, $integrationsClass->hideCreateButton(Landroid/view/View;)V" + + createButtonResult.mutableMethod.injectHook(hook, insertIndex) + + return PatchResultSuccess() + } +}