refactor(BytecodeUtils): remove duplicate functions

This commit is contained in:
inotia00
2024-09-17 18:40:11 +09:00
parent 5e4b56cfbe
commit afd19dead4
135 changed files with 1349 additions and 1453 deletions

View File

@ -14,8 +14,8 @@ import app.revanced.patches.shared.ads.fingerprints.VideoAdsFingerprint
import app.revanced.patches.shared.integrations.Constants.PATCHES_PATH
import app.revanced.util.getReference
import app.revanced.util.getWalkerMethod
import app.revanced.util.getWideLiteralInstructionIndex
import app.revanced.util.indexOfFirstInstructionOrThrow
import app.revanced.util.indexOfFirstWideLiteralInstructionValueOrThrow
import app.revanced.util.resultOrThrow
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
@ -75,7 +75,7 @@ abstract class BaseAdsPatch(
internal fun MethodFingerprintResult.hookNonLithoFullscreenAds(literal: Long) {
mutableMethod.apply {
val targetIndex = getWideLiteralInstructionIndex(literal) + 2
val targetIndex = indexOfFirstWideLiteralInstructionValueOrThrow(literal) + 2
val targetRegister = getInstruction<OneRegisterInstruction>(targetIndex).registerA
addInstruction(

View File

@ -8,14 +8,14 @@ import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patches.shared.customspeed.fingerprints.SpeedArrayGeneratorFingerprint
import app.revanced.patches.shared.customspeed.fingerprints.SpeedLimiterFallBackFingerprint
import app.revanced.patches.shared.customspeed.fingerprints.SpeedLimiterFingerprint
import app.revanced.util.getTargetIndexOrThrow
import app.revanced.util.getTargetIndexWithFieldReferenceTypeOrThrow
import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow
import app.revanced.util.getReference
import app.revanced.util.indexOfFirstInstructionOrThrow
import app.revanced.util.resultOrThrow
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.NarrowLiteralInstruction
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
import com.android.tools.smali.dexlib2.iface.reference.FieldReference
import com.android.tools.smali.dexlib2.iface.reference.MethodReference
abstract class BaseCustomPlaybackSpeedPatch(
private val descriptor: String,
@ -39,7 +39,9 @@ abstract class BaseCustomPlaybackSpeedPatch(
"""
)
val sizeIndex = getTargetIndexWithMethodReferenceNameOrThrow("size") + 1
val sizeIndex = indexOfFirstInstructionOrThrow {
getReference<MethodReference>()?.name == "size"
} + 1
val sizeRegister = getInstruction<OneRegisterInstruction>(sizeIndex).registerA
addInstructions(
@ -49,7 +51,9 @@ abstract class BaseCustomPlaybackSpeedPatch(
"""
)
val arrayIndex = getTargetIndexWithFieldReferenceTypeOrThrow("[F")
val arrayIndex = indexOfFirstInstructionOrThrow {
getReference<FieldReference>()?.type == "[F"
}
val arrayRegister = getInstruction<OneRegisterInstruction>(arrayIndex).registerA
addInstructions(
@ -73,7 +77,7 @@ abstract class BaseCustomPlaybackSpeedPatch(
val limiterMinConstIndex =
indexOfFirstInstructionOrThrow { (this as? NarrowLiteralInstruction)?.narrowLiteral == 0.25f.toRawBits() }
val limiterMaxConstIndex =
getTargetIndexOrThrow(limiterMinConstIndex + 1, Opcode.CONST_HIGH16)
indexOfFirstInstructionOrThrow(limiterMinConstIndex + 1, Opcode.CONST_HIGH16)
val limiterMinConstDestination =
getInstruction<OneRegisterInstruction>(limiterMinConstIndex).registerA

View File

@ -7,10 +7,12 @@ import app.revanced.patcher.fingerprint.MethodFingerprint
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
import app.revanced.patches.shared.dialog.fingerprints.CreateDialogFingerprint
import app.revanced.util.getTargetIndexWithMethodReferenceNameOrThrow
import app.revanced.util.getReference
import app.revanced.util.getWalkerMethod
import app.revanced.util.indexOfFirstInstructionOrThrow
import app.revanced.util.resultOrThrow
import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction
import com.android.tools.smali.dexlib2.iface.reference.MethodReference
abstract class BaseViewerDiscretionDialogPatch(
private val classDescriptor: String,
@ -22,7 +24,9 @@ abstract class BaseViewerDiscretionDialogPatch(
}
) {
private fun MutableMethod.invoke(isAgeVerified: Boolean) {
val showDialogIndex = getTargetIndexWithMethodReferenceNameOrThrow("show")
val showDialogIndex = indexOfFirstInstructionOrThrow {
getReference<MethodReference>()?.name == "show"
}
val dialogRegister = getInstruction<FiveRegisterInstruction>(showDialogIndex).registerC
val methodName =

View File

@ -6,9 +6,11 @@ import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
import app.revanced.patches.shared.drawable.fingerprints.DrawableFingerprint
import app.revanced.util.getTargetIndexWithMethodReferenceNameReversedOrThrow
import app.revanced.util.getReference
import app.revanced.util.indexOfFirstInstructionReversedOrThrow
import app.revanced.util.resultOrThrow
import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction
import com.android.tools.smali.dexlib2.iface.reference.MethodReference
object DrawableColorPatch : BytecodePatch(
setOf(DrawableFingerprint)
@ -17,26 +19,26 @@ object DrawableColorPatch : BytecodePatch(
DrawableFingerprint.resultOrThrow().mutableMethod.apply {
insertMethod = this
insertIndex = getTargetIndexWithMethodReferenceNameReversedOrThrow("setColor")
insertIndex = indexOfFirstInstructionReversedOrThrow {
getReference<MethodReference>()?.name == "setColor"
}
insertRegister = getInstruction<FiveRegisterInstruction>(insertIndex).registerD
}
}
private var offset = 0
private lateinit var insertMethod: MutableMethod
private var insertIndex: Int = 0
private var insertRegister: Int = 0
private lateinit var insertMethod: MutableMethod
private var offset = 0
fun injectCall(
methodDescriptor: String
) {
insertMethod.addInstructions(
insertIndex + offset, """
invoke-static {v$insertRegister}, $methodDescriptor
move-result v$insertRegister
"""
invoke-static {v$insertRegister}, $methodDescriptor
move-result v$insertRegister
"""
)
offset += 2
}

View File

@ -18,14 +18,12 @@ import app.revanced.patches.shared.gms.fingerprints.CastContextFetchFingerprint
import app.revanced.patches.shared.gms.fingerprints.CastDynamiteModuleFingerprint
import app.revanced.patches.shared.gms.fingerprints.CastDynamiteModuleV2Fingerprint
import app.revanced.patches.shared.gms.fingerprints.CertificateFingerprint
import app.revanced.patches.shared.gms.fingerprints.CertificateFingerprint.GET_PACKAGE_NAME_METHOD_REFERENCE
import app.revanced.patches.shared.gms.fingerprints.GmsCoreSupportFingerprint
import app.revanced.patches.shared.gms.fingerprints.GooglePlayUtilityFingerprint
import app.revanced.patches.shared.gms.fingerprints.PrimeMethodFingerprint
import app.revanced.patches.shared.gms.fingerprints.ServiceCheckFingerprint
import app.revanced.patches.shared.integrations.Constants.PATCHES_PATH
import app.revanced.util.getReference
import app.revanced.util.getTargetIndexWithReference
import app.revanced.util.resultOrThrow
import app.revanced.util.returnEarly
import com.android.tools.smali.dexlib2.Opcode
@ -178,7 +176,7 @@ abstract class BaseGmsCoreSupportPatch(
CertificateFingerprint.result?.mutableClass?.methods?.forEach { mutableMethod ->
mutableMethod.apply {
val getPackageNameIndex =
getTargetIndexWithReference(GET_PACKAGE_NAME_METHOD_REFERENCE)
CertificateFingerprint.indexOfGetPackageNameInstruction(this)
if (getPackageNameIndex > -1) {
val targetRegister =

View File

@ -1,20 +1,28 @@
package app.revanced.patches.shared.gms.fingerprints
import app.revanced.patcher.extensions.or
import app.revanced.patches.shared.gms.fingerprints.CertificateFingerprint.GET_PACKAGE_NAME_METHOD_REFERENCE
import app.revanced.util.fingerprint.ReferenceFingerprint
import app.revanced.patcher.fingerprint.MethodFingerprint
import app.revanced.patches.shared.gms.fingerprints.CertificateFingerprint.indexOfGetPackageNameInstruction
import app.revanced.util.getReference
import app.revanced.util.indexOfFirstInstruction
import com.android.tools.smali.dexlib2.AccessFlags
import com.android.tools.smali.dexlib2.iface.Method
import com.android.tools.smali.dexlib2.iface.reference.MethodReference
/**
* Method which the package name is used to check the app signature.
*/
internal object CertificateFingerprint : ReferenceFingerprint(
internal object CertificateFingerprint : MethodFingerprint(
returnType = "Ljava/lang/String;",
accessFlags = AccessFlags.PROTECTED or AccessFlags.FINAL,
parameters = emptyList(),
strings = listOf("X.509", "user", "S"),
reference = { GET_PACKAGE_NAME_METHOD_REFERENCE }
customFingerprint = { methodDef, _ ->
indexOfGetPackageNameInstruction(methodDef) >= 0
}
) {
const val GET_PACKAGE_NAME_METHOD_REFERENCE =
"Landroid/content/Context;->getPackageName()Ljava/lang/String;"
fun indexOfGetPackageNameInstruction(methodDef: Method) =
methodDef.indexOfFirstInstruction {
getReference<MethodReference>()?.toString() == "Landroid/content/Context;->getPackageName()Ljava/lang/String;"
}
}

View File

@ -16,9 +16,9 @@ import app.revanced.patches.shared.litho.fingerprints.ByteBufferFingerprint
import app.revanced.patches.shared.litho.fingerprints.EmptyComponentsFingerprint
import app.revanced.patches.shared.litho.fingerprints.PathBuilderFingerprint
import app.revanced.util.getReference
import app.revanced.util.getStringInstructionIndex
import app.revanced.util.indexOfFirstInstructionOrThrow
import app.revanced.util.indexOfFirstInstructionReversedOrThrow
import app.revanced.util.indexOfFirstStringInstructionOrThrow
import app.revanced.util.resultOrThrow
import com.android.tools.smali.dexlib2.AccessFlags
import com.android.tools.smali.dexlib2.Opcode
@ -127,7 +127,7 @@ object LithoFilterPatch : BytecodePatch(
val stringBuilderRegister =
getInstruction<TwoRegisterInstruction>(stringBuilderIndex).registerA
val emptyStringIndex = getStringInstructionIndex("")
val emptyStringIndex = indexOfFirstStringInstructionOrThrow("")
val identifierRegister = getInstruction<TwoRegisterInstruction>(
indexOfFirstInstructionReversedOrThrow(emptyStringIndex) {
opcode == Opcode.IPUT_OBJECT

View File

@ -7,7 +7,7 @@ import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.PatchException
import app.revanced.patcher.util.proxy.mutableTypes.MutableClass
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
import app.revanced.util.getTargetIndexOrThrow
import app.revanced.util.indexOfFirstInstructionOrThrow
import app.revanced.util.resultOrThrow
import com.android.tools.smali.dexlib2.Opcode
import kotlin.properties.Delegates
@ -38,7 +38,8 @@ abstract class BaseMainActivityResolvePatch(
// set onBackPressed method
onBackPressedMethod = getMethod("onBackPressed")
onBackPressedMethodIndex = onBackPressedMethod.getTargetIndexOrThrow(Opcode.RETURN_VOID)
onBackPressedMethodIndex =
onBackPressedMethod.indexOfFirstInstructionOrThrow(Opcode.RETURN_VOID)
// set onConfigurationChanged method
onConfigurationChangedMethod = getMethod("onConfigurationChanged")

View File

@ -7,11 +7,13 @@ import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.util.smali.ExternalLabel
import app.revanced.patches.shared.opus.fingerprints.CodecReferenceFingerprint
import app.revanced.patches.shared.opus.fingerprints.CodecSelectorFingerprint
import app.revanced.util.getTargetIndexWithReferenceOrThrow
import app.revanced.util.getReference
import app.revanced.util.indexOfFirstInstructionOrThrow
import app.revanced.util.resultOrThrow
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction
import com.android.tools.smali.dexlib2.iface.reference.Reference
import com.android.tools.smali.dexlib2.iface.reference.MethodReference
/**
* This patch is generally not required for the latest versions of YouTube and YouTube Music.
@ -25,15 +27,14 @@ abstract class BaseOpusCodecsPatch(
CodecSelectorFingerprint
)
) {
private lateinit var opusCodecReference: Reference
override fun execute(context: BytecodeContext) {
CodecReferenceFingerprint.resultOrThrow().let {
it.mutableMethod.apply {
val targetIndex = getTargetIndexWithReferenceOrThrow("Ljava/util/Set;")
opusCodecReference = getInstruction<ReferenceInstruction>(targetIndex).reference
val opusCodecReference = with(CodecReferenceFingerprint.resultOrThrow().mutableMethod) {
val codecIndex = indexOfFirstInstructionOrThrow {
opcode == Opcode.INVOKE_STATIC &&
getReference<MethodReference>()?.returnType == "Ljava/util/Set;"
}
getInstruction<ReferenceInstruction>(codecIndex).reference
}
CodecSelectorFingerprint.resultOrThrow().let {

View File

@ -8,9 +8,11 @@ import app.revanced.patcher.patch.annotation.Patch
import app.revanced.patches.shared.integrations.Constants.PATCHES_PATH
import app.revanced.patches.shared.settingmenu.fingerprints.SettingsMenuFingerprint
import app.revanced.patches.shared.viewgroup.ViewGroupMarginLayoutParamsHookPatch
import app.revanced.util.getTargetIndexWithFieldReferenceTypeOrThrow
import app.revanced.util.getReference
import app.revanced.util.indexOfFirstInstructionOrThrow
import app.revanced.util.resultOrThrow
import com.android.tools.smali.dexlib2.iface.instruction.TwoRegisterInstruction
import com.android.tools.smali.dexlib2.iface.reference.FieldReference
@Patch(
description = "Hide the settings menu for YouTube or YouTube Music.",
@ -25,8 +27,9 @@ object SettingsMenuPatch : BytecodePatch(
override fun execute(context: BytecodeContext) {
SettingsMenuFingerprint.resultOrThrow().mutableMethod.apply {
val insertIndex =
getTargetIndexWithFieldReferenceTypeOrThrow("Landroid/support/v7/widget/RecyclerView;")
val insertIndex = indexOfFirstInstructionOrThrow {
getReference<FieldReference>()?.type == "Landroid/support/v7/widget/RecyclerView;"
}
val insertRegister = getInstruction<TwoRegisterInstruction>(insertIndex).registerA
addInstruction(

View File

@ -6,7 +6,7 @@ import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patches.shared.fingerprints.CreatePlayerRequestBodyWithModelFingerprint
import app.revanced.patches.shared.fingerprints.CreatePlayerRequestBodyWithModelFingerprint.indexOfReleaseInstruction
import app.revanced.util.getTargetIndexReversedOrThrow
import app.revanced.util.indexOfFirstInstructionReversedOrThrow
import app.revanced.util.resultOrThrow
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.TwoRegisterInstruction
@ -20,7 +20,8 @@ abstract class BaseSpoofAppVersionPatch(
CreatePlayerRequestBodyWithModelFingerprint.resultOrThrow().mutableMethod.apply {
val versionIndex = indexOfReleaseInstruction(this) + 1
val insertIndex = getTargetIndexReversedOrThrow(versionIndex, Opcode.IPUT_OBJECT)
val insertIndex =
indexOfFirstInstructionReversedOrThrow(versionIndex, Opcode.IPUT_OBJECT)
val insertRegister = getInstruction<TwoRegisterInstruction>(insertIndex).registerA
addInstructions(