Skip to content

Commit

Permalink
fix(Hide Navigation Button): Rewrite code
Browse files Browse the repository at this point in the history
  • Loading branch information
YT-Advanced committed Apr 2, 2024
1 parent 26dc7d8 commit 0ceddcc
Show file tree
Hide file tree
Showing 17 changed files with 259 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import app.revanced.patches.youtube.general.suggestions.fingerprints.BreakingNew
import app.revanced.patches.youtube.utils.browseid.BrowseIdHookPatch
import app.revanced.patches.youtube.utils.integrations.Constants.COMPONENTS_PATH
import app.revanced.patches.youtube.utils.litho.LithoFilterPatch
import app.revanced.patches.youtube.utils.navbarindex.NavBarIndexHookPatch
import app.revanced.patches.youtube.utils.navigation.NavigationBarHookPatch
import app.revanced.patches.youtube.utils.settings.SettingsPatch
import app.revanced.util.exception
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
Expand All @@ -21,7 +21,7 @@ import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
dependencies = [
BrowseIdHookPatch::class,
LithoFilterPatch::class,
NavBarIndexHookPatch::class,
NavigationBarHookPatch::class,
SettingsPatch::class
],
compatiblePackages = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
package app.revanced.patches.youtube.navigation.navigationbuttons

import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.InstructionExtensions.addInstruction
import app.revanced.patcher.extensions.InstructionExtensions.addInstructions
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.annotation.CompatiblePackage
import app.revanced.patcher.patch.annotation.Patch
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
import app.revanced.patches.youtube.utils.navigation.NavigationBarHookPatch
import app.revanced.patches.youtube.navigation.navigationbuttons.fingerprints.AutoMotiveFingerprint
import app.revanced.patches.youtube.navigation.navigationbuttons.fingerprints.PivotBarEnumFingerprint
import app.revanced.patches.youtube.navigation.navigationbuttons.fingerprints.PivotBarShortsButtonViewFingerprint
import app.revanced.patches.youtube.utils.fingerprints.PivotBarCreateButtonViewFingerprint
import app.revanced.patches.youtube.utils.integrations.Constants.NAVIGATION
import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch
import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.ImageOnlyTab
import app.revanced.patches.youtube.utils.settings.SettingsPatch
import app.revanced.util.exception
import app.revanced.util.getStringInstructionIndex
import app.revanced.util.getTargetIndex
import app.revanced.util.getWideLiteralInstructionIndex
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.Opcode.MOVE_RESULT_OBJECT
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction

@Patch(
name = "Hide navigation buttons",
description = "Adds options to hide and change navigation buttons (such as the Shorts button).",
dependencies = [
SettingsPatch::class,
SharedResourceIdPatch::class
NavigationBarHookPatch::class,
SettingsPatch::class
],
compatiblePackages = [
CompatiblePackage(
Expand Down Expand Up @@ -70,57 +60,10 @@ import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
)
@Suppress("unused")
object NavigationButtonsPatch : BytecodePatch(
setOf(
AutoMotiveFingerprint,
PivotBarCreateButtonViewFingerprint
)
setOf(AutoMotiveFingerprint)
) {
override fun execute(context: BytecodeContext) {

PivotBarCreateButtonViewFingerprint.result?.let { parentResult ->

/**
* Home, Shorts, Subscriptions Button
*/
with(
arrayOf(
PivotBarEnumFingerprint,
PivotBarShortsButtonViewFingerprint
).onEach {
it.resolve(
context,
parentResult.mutableMethod,
parentResult.mutableClass
)
}.map {
it.result?.scanResult?.patternScanResult ?: throw it.exception
}
) {
val enumScanResult = this[0]
val buttonViewResult = this[1]

val enumHookInsertIndex = enumScanResult.startIndex + 2
val buttonHookInsertIndex = buttonViewResult.endIndex - 1

mapOf(
BUTTON_HOOK to buttonHookInsertIndex,
ENUM_HOOK to enumHookInsertIndex
).forEach { (hook, insertIndex) ->
parentResult.mutableMethod.injectHook(hook, insertIndex)
}
}

/**
* Create Button
*/
parentResult.mutableMethod.apply {
val constIndex = getWideLiteralInstructionIndex(ImageOnlyTab)
val insertIndex = getTargetIndex(constIndex, Opcode.INVOKE_VIRTUAL) + 2
injectHook(CREATE_BUTTON_HOOK, insertIndex)
}

} ?: throw PivotBarCreateButtonViewFingerprint.exception

/**
* Switch create button with notifications button
*/
Expand Down Expand Up @@ -151,40 +94,4 @@ object NavigationButtonsPatch : BytecodePatch(
SettingsPatch.updatePatchStatus("Hide navigation buttons")

}

private const val REGISTER_TEMPLATE_REPLACEMENT: String = "REGISTER_INDEX"

private const val ENUM_HOOK =
"sput-object v$REGISTER_TEMPLATE_REPLACEMENT, $NAVIGATION" +
"->" +
"lastPivotTab:Ljava/lang/Enum;"

private const val BUTTON_HOOK =
"invoke-static { v$REGISTER_TEMPLATE_REPLACEMENT }, $NAVIGATION" +
"->" +
"hideNavigationButton(Landroid/view/View;)V"

private const val CREATE_BUTTON_HOOK =
"invoke-static { v$REGISTER_TEMPLATE_REPLACEMENT }, $NAVIGATION" +
"->" +
"hideCreateButton(Landroid/view/View;)V"

/**
* Injects an instruction into insertIndex of the hook.
* @param hook The hook to insert.
* @param insertIndex The index to insert the instruction at.
* [MOVE_RESULT_OBJECT] has to be the previous instruction before [insertIndex].
*/
private fun MutableMethod.injectHook(hook: String, insertIndex: Int) {
val injectTarget = this

// Register to pass to the hook
val registerIndex = insertIndex - 1 // MOVE_RESULT_OBJECT is always the previous instruction
val register = injectTarget.getInstruction<OneRegisterInstruction>(registerIndex).registerA

injectTarget.addInstruction(
insertIndex,
hook.replace("REGISTER_INDEX", register.toString()),
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package app.revanced.patches.youtube.navigation.navigationbuttons.fingerprints

import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.MethodFingerprint
import com.android.tools.smali.dexlib2.AccessFlags

internal object PivotBarConstructorFingerprint : MethodFingerprint(
accessFlags = AccessFlags.PUBLIC or AccessFlags.CONSTRUCTOR,
strings = listOf("com.google.android.apps.youtube.app.endpoint.flags")
)

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 0ceddcc

Please sign in to comment.