Skip to content

Commit

Permalink
fix(youtube): improve patching speed (#2514)
Browse files Browse the repository at this point in the history
  • Loading branch information
LisoUseInAIKyrios committed Jun 28, 2023
1 parent e9fb849 commit 77bb761
Show file tree
Hide file tree
Showing 71 changed files with 367 additions and 381 deletions.
27 changes: 19 additions & 8 deletions src/main/kotlin/app/revanced/extensions/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,28 @@ internal fun MutableMethod.injectHideViewCall(
"invoke-static { v$viewRegister }, $classDescriptor->$targetMethod(Landroid/view/View;)V"
)

internal fun MutableMethod.findIndexForIdResource(resourceName: String): Int {
internal fun Method.findIndexForIdResource(resourceName: String): Int {
fun getIdResourceId(resourceName: String) = ResourceMappingPatch.resourceMappings.single {
it.type == "id" && it.name == resourceName
}.id

val resourceId = getIdResourceId(resourceName)
return implementation!!.instructions.indexOfFirst {
if (it.opcode != Opcode.CONST) return@indexOfFirst false
return indexOfFirstConstantInstructionValue(getIdResourceId(resourceName))
}

val literal = (it as WideLiteralInstruction).wideLiteral
/**
* @return the first constant instruction with the value, or -1 if not found.
*/
fun Method.indexOfFirstConstantInstructionValue(constantValue: Long): Int {
return implementation?.let {
it.instructions.indexOfFirst { instruction ->
instruction.opcode == Opcode.CONST && (instruction as WideLiteralInstruction).wideLiteral == constantValue
}
} ?: -1
}

return@indexOfFirst resourceId == literal
}
}
/**
* @return if the method contains a constant with the given value.
*/
fun Method.containsConstantInstructionValue(constantValue: Long): Boolean {
return indexOfFirstConstantInstructionValue(constantValue) >= 0
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package app.revanced.patches.music.misc.androidauto.fingerprints

import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patches.music.misc.androidauto.patch.BypassCertificateChecksPatch


object CheckCertificateFingerprint : MethodFingerprint(
"Z",
strings = listOf("No match") // Unique in combination with boolean return type
returnType = "Z",
parameters = listOf("Ljava/lang/String;"),
strings = listOf("X509", "Failed to get public key.", "Failed to get certificate.")
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package app.revanced.patches.shared.fingerprints


import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags

object WatchWhileActivityFingerprint : MethodFingerprint(
accessFlags = AccessFlags.PUBLIC or AccessFlags.CONSTRUCTOR,
parameters = listOf(),
customFingerprint = { methodDef, _ ->
methodDef.definingClass.endsWith("WatchWhileActivity;") && methodDef.name == "<init>"
methodDef.definingClass.endsWith("WatchWhileActivity;")
}
)
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package app.revanced.patches.shared.misc.fix.verticalscroll.fingerprints


import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode

object CanScrollVerticallyFingerprint : MethodFingerprint(
"Z",
accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL,
returnType = "Z",
parameters = emptyList(),
opcodes = listOf(
Opcode.MOVE_RESULT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class SettingsPatch : BytecodePatch(
} - 6


// fixme: instead use Method.indexOfFirstConstantInstructionValue()
val copyrightPolicyIndex = instructions.indexOfFirst {
(it as? WideLiteralInstruction)?.wideLiteral == copyrightPolicyLabelId
} + 2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package app.revanced.patches.youtube.ad.getpremium.bytecode.fingerprints

import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode

object GetPremiumViewFingerprint : MethodFingerprint(
accessFlags = AccessFlags.PROTECTED or AccessFlags.FINAL,
returnType = "V",
parameters = listOf("I", "I"),
opcodes = listOf(
Opcode.ADD_INT_2ADDR,
Opcode.ADD_INT_2ADDR,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
package app.revanced.patches.youtube.interaction.seekbar.fingerprints

import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patches.youtube.interaction.seekbar.patch.EnableSeekbarTappingResourcePatch
import app.revanced.util.patch.LiteralValueFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode
import org.jf.dexlib2.iface.instruction.WideLiteralInstruction


object AccessibilityPlayerProgressTimeFingerprint : MethodFingerprint(
object AccessibilityPlayerProgressTimeFingerprint : LiteralValueFingerprint(
returnType = "L",
accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL,
customFingerprint = { methodDef, _ ->
methodDef.implementation?.instructions?.any { instruction ->
if (instruction.opcode != Opcode.CONST) return@any false

val wideLiteral = (instruction as WideLiteralInstruction).wideLiteral

EnableSeekbarTappingResourcePatch.accessibilityPlayerProgressTime == wideLiteral
} ?: false
}
literal = EnableSeekbarTappingResourcePatch.accessibilityPlayerProgressTime
)
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package app.revanced.patches.youtube.interaction.swipecontrols.fingerprints


import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags

object SwipeControlsHostActivityFingerprint : MethodFingerprint(
accessFlags = AccessFlags.PUBLIC or AccessFlags.CONSTRUCTOR,
parameters = listOf(),
customFingerprint = { methodDef, _ ->
methodDef.definingClass == "Lapp/revanced/integrations/swipecontrols/SwipeControlsHostActivity;" && methodDef.name == "<init>"
methodDef.definingClass == "Lapp/revanced/integrations/swipecontrols/SwipeControlsHostActivity;"
}
)
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package app.revanced.patches.youtube.layout.autocaptions.fingerprints

import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.annotation.FuzzyPatternScanMethod
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode


@FuzzyPatternScanMethod(3)
object StartVideoInformerFingerprint : MethodFingerprint(
"V", AccessFlags.PUBLIC or AccessFlags.FINAL, listOf("L", "L", "L", "L"), listOf(
Opcode.INVOKE_STATIC,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode

object SubtitleButtonControllerFingerprint : MethodFingerprint(
"V", AccessFlags.PUBLIC or AccessFlags.FINAL, listOf("L"), listOf(
returnType = "V",
accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL,
parameters = listOf("Lcom/google/android/libraries/youtube/player/subtitles/model/SubtitleTrack;"),
opcodes = listOf(
Opcode.IGET_OBJECT,
Opcode.IF_NEZ,
Opcode.RETURN_VOID,
Expand All @@ -16,5 +19,8 @@ object SubtitleButtonControllerFingerprint : MethodFingerprint(
Opcode.CONST,
Opcode.INVOKE_VIRTUAL,
Opcode.IGET_OBJECT,
)
),
customFingerprint = { methodDef, _ ->
methodDef.definingClass.endsWith("SubtitleButtonController;")
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ object SubtitleTrackFingerprint : MethodFingerprint(
Opcode.MOVE_RESULT,
Opcode.RETURN,
),
strings = listOf("DISABLE_CAPTIONS_OPTION")
strings = listOf("DISABLE_CAPTIONS_OPTION"),
customFingerprint = { methodDef, _ ->
methodDef.definingClass.endsWith("SubtitleTrack;")
}
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package app.revanced.patches.youtube.layout.buttons.autoplay.patch

import app.revanced.extensions.findIndexForIdResource
import app.revanced.extensions.toErrorResult
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
Expand All @@ -23,7 +24,6 @@ import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch
import org.jf.dexlib2.iface.instruction.Instruction
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
Expand All @@ -48,15 +48,8 @@ class HideAutoplayButtonPatch : BytecodePatch(
LayoutConstructorFingerprint.result?.mutableMethod?.apply {
val layoutGenMethodInstructions = implementation!!.instructions

// resolve the offsets such as ...
val autoNavPreviewStubId = ResourceMappingPatch.resourceMappings.single {
it.name == "autonav_preview_stub"
}.id

// where to insert the branch instructions and ...
val insertIndex = layoutGenMethodInstructions.indexOfFirst {
(it as? WideLiteralInstruction)?.wideLiteral == autoNavPreviewStubId
}
// resolve the offsets of where to insert the branch instructions and ...
val insertIndex = findIndexForIdResource("autonav_preview_stub")

// where to branch away
val branchIndex =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package app.revanced.patches.youtube.layout.buttons.navigation.fingerprints

import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patcher.extensions.or
import app.revanced.patches.youtube.layout.buttons.navigation.patch.ResolvePivotBarFingerprintsPatch
import org.jf.dexlib2.Opcode
import org.jf.dexlib2.iface.instruction.WideLiteralInstruction
import app.revanced.util.patch.LiteralValueFingerprint
import org.jf.dexlib2.AccessFlags

object InitializeButtonsFingerprint : MethodFingerprint(
customFingerprint = { methodDef, _ ->
methodDef.implementation?.instructions?.any {
it.opcode == Opcode.CONST && (it as WideLiteralInstruction).wideLiteral ==
ResolvePivotBarFingerprintsPatch.imageOnlyTabResourceId
} == true
}
object InitializeButtonsFingerprint : LiteralValueFingerprint(
accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL,
returnType = "V",
parameters = listOf(),
literal = ResolvePivotBarFingerprintsPatch.imageOnlyTabResourceId
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import org.jf.dexlib2.Opcode

object PlayerControlsVisibilityModelFingerprint : MethodFingerprint(
opcodes = listOf(Opcode.INVOKE_DIRECT_RANGE),
strings = listOf("hasNext", "hasPrevious", "Missing required properties:")
strings = listOf("Missing required properties:", "hasNext", "hasPrevious")
)
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package app.revanced.patches.youtube.layout.hide.albumcards.bytecode.fingerprints

import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patcher.extensions.or
import app.revanced.patches.youtube.layout.hide.albumcards.resource.patch.AlbumCardsResourcePatch
import app.revanced.util.patch.LiteralValueFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode
import org.jf.dexlib2.iface.instruction.WideLiteralInstruction

object AlbumCardsFingerprint : MethodFingerprint(
object AlbumCardsFingerprint : LiteralValueFingerprint(
accessFlags = AccessFlags.PUBLIC or AccessFlags.CONSTRUCTOR,
opcodes = listOf(
Opcode.MOVE_RESULT_OBJECT,
Opcode.CONST,
Expand All @@ -14,10 +16,5 @@ object AlbumCardsFingerprint : MethodFingerprint(
Opcode.MOVE_RESULT_OBJECT,
Opcode.CHECK_CAST,
),
customFingerprint = { methodDef, _ ->
methodDef.implementation?.instructions?.any { instruction ->
instruction.opcode.ordinal == Opcode.CONST.ordinal &&
(instruction as? WideLiteralInstruction)?.wideLiteral == AlbumCardsResourcePatch.albumCardId
} == true
}
literal = AlbumCardsResourcePatch.albumCardId
)
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package app.revanced.patches.youtube.layout.hide.breakingnews.bytecode.fingerprints

import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patcher.extensions.or
import app.revanced.patches.youtube.layout.hide.breakingnews.resource.patch.BreakingNewsResourcePatch
import app.revanced.util.patch.LiteralValueFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode
import org.jf.dexlib2.iface.instruction.WideLiteralInstruction

object BreakingNewsFingerprint : MethodFingerprint(
object BreakingNewsFingerprint : LiteralValueFingerprint(
accessFlags = AccessFlags.PUBLIC or AccessFlags.CONSTRUCTOR,
opcodes = listOf(
Opcode.CONST,
Opcode.CONST_4,
Expand All @@ -14,10 +16,5 @@ object BreakingNewsFingerprint : MethodFingerprint(
Opcode.CHECK_CAST,
Opcode.IPUT_OBJECT,
),
customFingerprint = { methodDef, _ ->
methodDef.implementation?.instructions?.any { instruction ->
instruction.opcode.ordinal == Opcode.CONST.ordinal &&
(instruction as? WideLiteralInstruction)?.wideLiteral == BreakingNewsResourcePatch.horizontalCardListId
} == true
}
literal = BreakingNewsResourcePatch.horizontalCardListId
)
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package app.revanced.patches.youtube.layout.hide.crowdfundingbox.bytecode.fingerprints

import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patcher.extensions.or
import app.revanced.patches.youtube.layout.hide.crowdfundingbox.resource.patch.CrowdfundingBoxResourcePatch
import app.revanced.util.patch.LiteralValueFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode
import org.jf.dexlib2.iface.instruction.WideLiteralInstruction

object CrowdfundingBoxFingerprint : MethodFingerprint(
object CrowdfundingBoxFingerprint : LiteralValueFingerprint(
accessFlags = AccessFlags.PUBLIC or AccessFlags.CONSTRUCTOR,
opcodes = listOf(
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT_OBJECT,
Opcode.IPUT_OBJECT,
),
customFingerprint = { methodDef, _ ->
methodDef.implementation?.instructions?.any { instruction ->
instruction.opcode.ordinal == Opcode.CONST.ordinal &&
(instruction as? WideLiteralInstruction)?.wideLiteral == CrowdfundingBoxResourcePatch.crowdfundingBoxId
} == true
}
literal = CrowdfundingBoxResourcePatch.crowdfundingBoxId
)
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package app.revanced.patches.youtube.layout.hide.endscreencards.bytecode.fingerprints

import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patches.youtube.layout.hide.endscreencards.resource.patch.HideEndscreenCardsResourcePatch
import app.revanced.util.patch.LiteralValueFingerprint
import org.jf.dexlib2.Opcode
import org.jf.dexlib2.iface.instruction.WideLiteralInstruction

object LayoutCircleFingerprint : MethodFingerprint(
object LayoutCircleFingerprint : LiteralValueFingerprint(
returnType = "Landroid/view/View;",
opcodes = listOf(
Opcode.CONST,
Opcode.CONST_4,
Opcode.INVOKE_VIRTUAL,
Opcode.MOVE_RESULT_OBJECT,
Opcode.CHECK_CAST,
),
customFingerprint = { methodDef, _ ->
methodDef.implementation?.instructions?.any { instruction ->
instruction.opcode.ordinal == Opcode.CONST.ordinal &&
(instruction as? WideLiteralInstruction)?.wideLiteral == HideEndscreenCardsResourcePatch.layoutCircle
} == true
}
literal = HideEndscreenCardsResourcePatch.layoutCircle
)
Loading

0 comments on commit 77bb761

Please sign in to comment.