feat(youtube): add hide-filter-bar patch

This commit is contained in:
oSumAtrIX 2023-05-13 01:03:48 +02:00
parent 4dbce43cf7
commit 6cc5f61e07
No known key found for this signature in database
GPG key ID: A9B3094ACDB604B4
7 changed files with 250 additions and 0 deletions

View file

@ -0,0 +1,8 @@
package app.revanced.patches.youtube.layout.hide.filterbar.annotations
import app.revanced.patcher.annotation.Compatibility
import app.revanced.patcher.annotation.Package
@Compatibility([Package("com.google.android.youtube", arrayOf("18.16.37"))])
@Target(AnnotationTarget.CLASS)
internal annotation class HideFilterBar

View file

@ -0,0 +1,14 @@
package app.revanced.patches.youtube.layout.hide.filterbar.fingerprints
import app.revanced.patches.youtube.layout.hide.filterbar.patch.HideFilterBarResourcePatch.Companion.filterBarHeightId
import org.jf.dexlib2.Opcode
object FilterBarHeightFingerprint : LiteralOpcodesFingerprint(
opcodes = listOf(
Opcode.CONST,
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT,
Opcode.IPUT
),
filterBarHeightId
)

View file

@ -0,0 +1,19 @@
package app.revanced.patches.youtube.layout.hide.filterbar.fingerprints
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.Opcode
import org.jf.dexlib2.iface.instruction.WideLiteralInstruction
abstract class LiteralOpcodesFingerprint(opcodes: List<Opcode>, literal: Long) : MethodFingerprint(
opcodes = opcodes,
customFingerprint = {
it.implementation?.instructions?.any { instruction ->
if (instruction.opcode != Opcode.CONST) return@any false
val wideLiteral = (instruction as WideLiteralInstruction).wideLiteral
literal == wideLiteral
} ?: false
}
)

View file

@ -0,0 +1,13 @@
package app.revanced.patches.youtube.layout.hide.filterbar.fingerprints
import app.revanced.patches.youtube.layout.hide.filterbar.patch.HideFilterBarResourcePatch.Companion.relatedChipCloudMarginId
import org.jf.dexlib2.Opcode
object RelatedChipCloudFingerprint : LiteralOpcodesFingerprint(
opcodes = listOf(
Opcode.CONST,
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT_OBJECT
),
relatedChipCloudMarginId
)

View file

@ -0,0 +1,15 @@
package app.revanced.patches.youtube.layout.hide.filterbar.fingerprints
import app.revanced.patches.youtube.layout.hide.filterbar.patch.HideFilterBarResourcePatch.Companion.barContainerHeightId
import org.jf.dexlib2.Opcode
object SearchResultsChipBarFingerprint : LiteralOpcodesFingerprint(
opcodes = listOf(
Opcode.CONST,
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT,
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT_OBJECT
),
barContainerHeightId
)

View file

@ -0,0 +1,87 @@
package app.revanced.patches.youtube.layout.hide.filterbar.patch
import app.revanced.extensions.toErrorResult
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.addInstructions
import app.revanced.patcher.extensions.instruction
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.patch.annotations.DependsOn
import app.revanced.patcher.patch.annotations.Patch
import app.revanced.patches.youtube.layout.hide.filterbar.annotations.HideFilterBar
import app.revanced.patches.youtube.layout.hide.filterbar.fingerprints.FilterBarHeightFingerprint
import app.revanced.patches.youtube.layout.hide.filterbar.fingerprints.RelatedChipCloudFingerprint
import app.revanced.patches.youtube.layout.hide.filterbar.fingerprints.SearchResultsChipBarFingerprint
import org.jf.dexlib2.iface.instruction.OneRegisterInstruction
import org.jf.dexlib2.iface.instruction.TwoRegisterInstruction
@Patch
@Name("hide-filter-bar")
@Description("Hides the filter bar in video feeds.")
@DependsOn([HideFilterBarResourcePatch::class])
@HideFilterBar
@Version("0.0.1")
class HideFilterBarPatch : BytecodePatch(
listOf(
RelatedChipCloudFingerprint,
SearchResultsChipBarFingerprint,
FilterBarHeightFingerprint
)
) {
override fun execute(context: BytecodeContext): PatchResult {
FilterBarHeightFingerprint.patch<TwoRegisterInstruction> { register ->
"""
invoke-static { v$register }, $INTEGRATIONS_CLASS_DESCRIPTOR->hideInFeed(I)I
move-result v$register
"""
}
RelatedChipCloudFingerprint.patch<OneRegisterInstruction>(1) { register ->
"invoke-static { v$register }, " +
"$INTEGRATIONS_CLASS_DESCRIPTOR->hideInRelatedVideos(Landroid/view/View;)V"
}
SearchResultsChipBarFingerprint.patch<OneRegisterInstruction>(-1, -2) { register ->
"""
invoke-static { v$register }, $INTEGRATIONS_CLASS_DESCRIPTOR->hideInSearch(I)I
move-result v$register
"""
}
return PatchResultSuccess()
}
private companion object {
const val INTEGRATIONS_CLASS_DESCRIPTOR =
"Lapp/revanced/integrations/patches/HideFilterBarPatch;"
/**
* Patch a [MethodFingerprint] with a given [instructions].
*
* @param RegisterInstruction The type of instruction to get the register from.
* @param insertIndexOffset The offset to add to the end index of the [MethodFingerprint].
* @param hookRegisterOffset The offset to add to the register of the hook.
* @param instructions The instructions to add with the register as a parameter.
*/
private fun <RegisterInstruction: OneRegisterInstruction> MethodFingerprint.patch(
insertIndexOffset: Int = 0,
hookRegisterOffset: Int = 0,
instructions: (Int) -> String
) =
result?.let {
it.mutableMethod.apply {
val endIndex = it.scanResult.patternScanResult!!.endIndex
val insertIndex = endIndex + insertIndexOffset
val register = instruction<RegisterInstruction>(endIndex + hookRegisterOffset).registerA
addInstructions(insertIndex, instructions(register))
}
} ?: throw toErrorResult()
}
}

View file

@ -0,0 +1,94 @@
package app.revanced.patches.youtube.layout.hide.filterbar.patch
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.data.ResourceContext
import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultSuccess
import app.revanced.patcher.patch.ResourcePatch
import app.revanced.patcher.patch.annotations.DependsOn
import app.revanced.patches.shared.mapping.misc.patch.ResourceMappingPatch
import app.revanced.patches.shared.settings.preference.impl.PreferenceScreen
import app.revanced.patches.shared.settings.preference.impl.StringResource
import app.revanced.patches.shared.settings.preference.impl.SwitchPreference
import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch
@DependsOn([SettingsPatch::class, ResourceMappingPatch::class])
@Version("0.0.1")
class HideFilterBarResourcePatch : ResourcePatch {
override fun execute(context: ResourceContext): PatchResult {
SettingsPatch.PreferenceScreen.LAYOUT.addPreferences(
PreferenceScreen(
"revanced_hide_filter_bar_preference",
StringResource(
"revanced_hide_filter_bar_preference_title",
"Hide filter bar"
),
listOf(
SwitchPreference(
"revanced_hide_filter_bar_feed_in_feed",
StringResource(
"revanced_hide_filter_bar_feed_in_feed_title",
"Hide in feed"
),
false,
StringResource(
"revanced_hide_filter_bar_feed_in_feed_summary_on",
"Hidden in feed"
),
StringResource(
"revanced_hide_filter_bar_feed_in_feed_summary_off",
"Shown in feed"
)
),
SwitchPreference(
"revanced_hide_filter_bar_feed_in_search",
StringResource(
"revanced_hide_filter_bar_feed_in_search_title",
"Hide in search"
),
false,
StringResource(
"revanced_hide_filter_bar_feed_in_search_summary_on",
"Hidden in search"
),
StringResource(
"revanced_hide_filter_bar_feed_in_search_summary_off",
"Shown in search"
)
),
SwitchPreference(
"revanced_hide_filter_bar_feed_in_related_videos",
StringResource(
"revanced_hide_filter_bar_feed_in_related_videos_title",
"Hide in related videos"
),
false,
StringResource(
"revanced_hide_filter_bar_feed_in_related_videos_summary_on",
"Hidden in related videos"
),
StringResource(
"revanced_hide_filter_bar_feed_in_related_videos_summary_off",
"Shown in related videos"
)
),
),
)
)
relatedChipCloudMarginId = "related_chip_cloud_reduced_margins".layoutResourceId("layout")
filterBarHeightId = "filter_bar_height".layoutResourceId()
barContainerHeightId = "bar_container_height".layoutResourceId()
return PatchResultSuccess()
}
internal companion object {
var filterBarHeightId = -1L
var relatedChipCloudMarginId = -1L
var barContainerHeightId = -1L
private fun String.layoutResourceId(type: String = "dimen") =
ResourceMappingPatch.resourceMappings.single { it.type == type && it.name == this }.id
}
}