chore: lint code

This commit is contained in:
inotia00
2024-01-26 03:35:21 +09:00
parent e783f18df0
commit aa6d8106c5
11 changed files with 85 additions and 163 deletions

View File

@ -12,6 +12,7 @@ import app.revanced.patches.music.misc.tastebuilder.fingerprints.TasteBuilderSyn
import app.revanced.patches.music.utils.resourceid.SharedResourceIdPatch
import app.revanced.patches.music.utils.resourceid.SharedResourceIdPatch.MusicTastebuilderShelf
import app.revanced.util.exception
import app.revanced.util.getTargetIndex
import app.revanced.util.getWideLiteralInstructionIndex
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
@ -49,11 +50,7 @@ object TasteBuilderPatch : BytecodePatch(
parentResult.mutableMethod.apply {
val freeRegister = implementation!!.registerCount - parameters.size - 2
val constIndex = getWideLiteralInstructionIndex(MusicTastebuilderShelf)
val targetIndex = implementation!!.instructions.let {
constIndex + it.subList(constIndex, it.size - 1).indexOfFirst { instruction ->
instruction.opcode == Opcode.MOVE_RESULT_OBJECT
}
}
val targetIndex = getTargetIndex(constIndex, Opcode.MOVE_RESULT_OBJECT)
val targetRegister = getInstruction<OneRegisterInstruction>(targetIndex).registerA
addInstructions(

View File

@ -4,15 +4,16 @@ import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.PatchException
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
import app.revanced.patcher.util.smali.ExternalLabel
import app.revanced.patches.shared.fingerprints.litho.EmptyComponentBuilderFingerprint
import app.revanced.util.exception
import app.revanced.util.getEmptyStringInstructionIndex
import app.revanced.util.getReference
import app.revanced.util.getTargetIndex
import app.revanced.util.getTargetIndexReversed
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.builder.instruction.BuilderInstruction35c
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.instruction.TwoRegisterInstruction
import com.android.tools.smali.dexlib2.iface.reference.FieldReference
@ -34,8 +35,7 @@ object ComponentParserPatch : BytecodePatch(
internal fun generalHook(descriptor: String) {
insertMethod.apply {
addInstructionsWithLabels(
insertIndex,
"""
insertIndex, """
invoke-static {v$stringBuilderRegister, v$identifierRegister, v$objectRegister}, $descriptor(Ljava/lang/StringBuilder;Ljava/lang/String;Ljava/lang/Object;)Z
move-result v$stringBuilderRegister
if-eqz v$stringBuilderRegister, :filter
@ -48,8 +48,7 @@ object ComponentParserPatch : BytecodePatch(
internal fun pathBuilderHook(descriptor: String) {
insertMethod.apply {
addInstructionsWithLabels(
insertIndex,
"""
insertIndex, """
invoke-static {v$stringBuilderRegister}, $descriptor(Ljava/lang/StringBuilder;)Z
move-result v$stringBuilderRegister
if-eqz v$stringBuilderRegister, :filter
@ -59,28 +58,15 @@ object ComponentParserPatch : BytecodePatch(
}
}
private fun MutableMethod.getTargetIndexDownTo(
startIndex: Int,
opcode: Opcode
): Int {
for (index in startIndex downTo 0) {
if (getInstruction(index).opcode != opcode)
continue
return index
}
throw PatchException("Failed to find hook method")
}
override fun execute(context: BytecodeContext) {
/**
* Shared fingerprint
*/
EmptyComponentBuilderFingerprint.result?.let { result ->
result.mutableMethod.apply {
EmptyComponentBuilderFingerprint.result?.let {
it.mutableMethod.apply {
insertMethod = this
emptyComponentIndex = result.scanResult.patternScanResult!!.startIndex + 1
emptyComponentIndex = it.scanResult.patternScanResult!!.startIndex + 1
val builderMethodDescriptor =
getInstruction<ReferenceInstruction>(emptyComponentIndex).reference
@ -97,9 +83,7 @@ object ComponentParserPatch : BytecodePatch(
val stringBuilderIndex =
implementation!!.instructions.indexOfFirst { instruction ->
val fieldReference =
(instruction as? ReferenceInstruction)?.reference as? FieldReference
fieldReference?.let { reference -> reference.type == "Ljava/lang/StringBuilder;" } == true
instruction.getReference<FieldReference>()?.type == "Ljava/lang/StringBuilder;"
}
stringBuilderRegister =
@ -108,15 +92,11 @@ object ComponentParserPatch : BytecodePatch(
insertIndex = stringBuilderIndex + 1
val emptyStringIndex = getEmptyStringInstructionIndex()
val identifierIndex = getTargetIndexDownTo(emptyStringIndex, Opcode.IPUT_OBJECT)
val identifierIndex = getTargetIndexReversed(emptyStringIndex, Opcode.IPUT_OBJECT)
identifierRegister =
getInstruction<TwoRegisterInstruction>(identifierIndex).registerA
val objectIndex = implementation!!.instructions.let {
emptyStringIndex + it.subList(emptyStringIndex, it.size - 1).indexOfFirst { instruction ->
instruction.opcode == Opcode.INVOKE_VIRTUAL
}
}
val objectIndex = getTargetIndex(emptyStringIndex, Opcode.INVOKE_VIRTUAL)
objectRegister = getInstruction<BuilderInstruction35c>(objectIndex).registerC
}
} ?: throw EmptyComponentBuilderFingerprint.exception

View File

@ -17,6 +17,7 @@ import app.revanced.patches.youtube.utils.litho.LithoFilterPatch
import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch
import app.revanced.patches.youtube.utils.settings.SettingsPatch
import app.revanced.util.exception
import app.revanced.util.getTargetIndex
import app.revanced.util.getWideLiteralInstructionIndex
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction
@ -86,11 +87,7 @@ object CommentComponentPatch : BytecodePatch(
ShortsLiveStreamEmojiPickerOnClickListenerFingerprint.result?.let { parentResult ->
parentResult.mutableMethod.apply {
val emojiPickerEndpointIndex = getWideLiteralInstructionIndex(126326492)
val emojiPickerOnClickListenerIndex = implementation!!.instructions.let {
emojiPickerEndpointIndex + it.subList(emojiPickerEndpointIndex, it.size - 1).indexOfFirst { instruction ->
instruction.opcode == Opcode.INVOKE_DIRECT
}
}
val emojiPickerOnClickListenerIndex = getTargetIndex(emojiPickerEndpointIndex, Opcode.INVOKE_DIRECT)
val emojiPickerOnClickListenerMethod =
context.toMethodWalker(this)
.nextMethod(emojiPickerOnClickListenerIndex, true)

View File

@ -14,6 +14,7 @@ import app.revanced.patches.youtube.utils.integrations.Constants.FULLSCREEN
import app.revanced.patches.youtube.utils.settings.SettingsPatch
import app.revanced.util.exception
import app.revanced.util.getStringInstructionIndex
import app.revanced.util.getTargetIndex
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
@ -63,11 +64,7 @@ object KeepLandScapeModePatch : BytecodePatch(
BroadcastReceiverFingerprint.result?.let { result ->
result.mutableMethod.apply {
val stringIndex = getStringInstructionIndex("android.intent.action.SCREEN_ON")
val insertIndex = implementation!!.instructions.let {
stringIndex + it.subList(stringIndex, it.size - 1).indexOfFirst { instruction ->
instruction.opcode == Opcode.IF_EQZ
} + 1
}
val insertIndex = getTargetIndex(stringIndex, Opcode.IF_EQZ) + 1
addInstruction(
insertIndex,

View File

@ -18,6 +18,7 @@ import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.Image
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
@ -108,13 +109,8 @@ object NavigationButtonsPatch : BytecodePatch(
* Create Button
*/
parentResult.mutableMethod.apply {
val insertIndex = implementation!!.instructions.let {
val scanStart = getWideLiteralInstructionIndex(ImageOnlyTab)
scanStart + it.subList(scanStart, it.size - 1).indexOfFirst { instruction ->
instruction.opcode == Opcode.INVOKE_VIRTUAL
}
} + 2
val constIndex = getWideLiteralInstructionIndex(ImageOnlyTab)
val insertIndex = getTargetIndex(constIndex, Opcode.INVOKE_VIRTUAL) + 2
injectHook(CREATE_BUTTON_HOOK, insertIndex)
}

View File

@ -15,6 +15,7 @@ import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.Total
import app.revanced.patches.youtube.utils.settings.SettingsPatch
import app.revanced.util.exception
import app.revanced.util.getReference
import app.revanced.util.getTargetIndex
import app.revanced.util.getWideLiteralInstructionIndex
import app.revanced.util.indexOfFirstInstruction
import com.android.tools.smali.dexlib2.Opcode
@ -68,14 +69,10 @@ object AppendTimeStampInformationPatch : BytecodePatch(
setOf(TotalTimeFingerprint)
) {
override fun execute(context: BytecodeContext) {
TotalTimeFingerprint.result?.let { result ->
result.mutableMethod.apply {
TotalTimeFingerprint.result?.let {
it.mutableMethod.apply {
val constIndex = getWideLiteralInstructionIndex(TotalTime)
val charSequenceIndex = implementation!!.instructions.let {
constIndex + it.subList(constIndex, it.size - 1).indexOfFirst { instruction ->
instruction.opcode == Opcode.MOVE_RESULT_OBJECT
}
}
val charSequenceIndex = getTargetIndex(constIndex, Opcode.MOVE_RESULT_OBJECT)
val charSequenceRegister = getInstruction<OneRegisterInstruction>(charSequenceIndex).registerA
val textViewIndex = indexOfFirstInstruction {
getReference<MethodReference>()?.name == "getText"

View File

@ -36,6 +36,8 @@ import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.ReelR
import app.revanced.patches.youtube.utils.resourceid.SharedResourceIdPatch.RightComment
import app.revanced.patches.youtube.utils.settings.SettingsPatch
import app.revanced.util.exception
import app.revanced.util.getTargetIndex
import app.revanced.util.getTargetIndexReversed
import app.revanced.util.getWideLiteralInstructionIndex
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
@ -118,7 +120,7 @@ object ShortsComponentPatch : BytecodePatch(
val insertIndex = getWideLiteralInstructionIndex(ReelRightDislikeIcon)
val insertRegister = getInstruction<OneRegisterInstruction>(insertIndex).registerA
val jumpIndex = getTargetIndexUpTo(insertIndex, Opcode.CONST_CLASS) + 2
val jumpIndex = getTargetIndex(insertIndex, Opcode.CONST_CLASS) + 2
addInstructionsWithLabels(
insertIndex + 1, """
@ -155,7 +157,7 @@ object ShortsComponentPatch : BytecodePatch(
val insertRegister = getInstruction<OneRegisterInstruction>(insertIndex).registerA
val jumpIndex = getTargetIndexUpTo(insertIndex, Opcode.CONST_CLASS) + 2
val jumpIndex = getTargetIndex(insertIndex, Opcode.CONST_CLASS) + 2
addInstructionsWithLabels(
insertIndex + 1, """
@ -210,8 +212,8 @@ object ShortsComponentPatch : BytecodePatch(
val targetIndex = getWideLiteralInstructionIndex(ReelForcedMuteButton)
val targetRegister = getInstruction<OneRegisterInstruction>(targetIndex).registerA
val insertIndex = getTargetIndexDownTo(targetIndex, Opcode.IF_EQZ)
val jumpIndex = getTargetIndexUpTo(targetIndex, Opcode.GOTO)
val insertIndex = getTargetIndexReversed(targetIndex, Opcode.IF_EQZ)
val jumpIndex = getTargetIndex(targetIndex, Opcode.GOTO)
addInstructionsWithLabels(
insertIndex, """
@ -224,7 +226,7 @@ object ShortsComponentPatch : BytecodePatch(
} ?: ShortsPivotFingerprint.result?.let {
it.mutableMethod.apply {
val targetIndex = getWideLiteralInstructionIndex(ReelPivotButton)
val insertIndex = getTargetIndexDownTo(targetIndex, Opcode.INVOKE_STATIC) + 2
val insertIndex = getTargetIndexReversed(targetIndex, Opcode.INVOKE_STATIC) + 2
hideButtons(
insertIndex,
@ -301,30 +303,4 @@ object ShortsComponentPatch : BytecodePatch(
"""
)
}
private fun MutableMethod.getTargetIndexDownTo(
startIndex: Int,
opcode: Opcode
): Int {
for (index in startIndex downTo 0) {
if (getInstruction(index).opcode != opcode)
continue
return index
}
throw PatchException("Failed to find hook method")
}
private fun MutableMethod.getTargetIndexUpTo(
startIndex: Int,
opcode: Opcode
): Int {
for (index in startIndex until implementation!!.instructions.size) {
if (getInstruction(index).opcode != opcode)
continue
return index
}
throw PatchException("Failed to find hook method")
}
}

View File

@ -3,15 +3,19 @@ package app.revanced.patches.youtube.shorts.startupshortsreset
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWithLabels
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.extensions.InstructionExtensions.removeInstruction
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.PatchException
import app.revanced.patcher.patch.annotation.CompatiblePackage
import app.revanced.patcher.patch.annotation.Patch
import app.revanced.patcher.util.smali.ExternalLabel
import app.revanced.patches.youtube.shorts.startupshortsreset.fingerprints.UserWasInShortsFingerprint
import app.revanced.patches.youtube.utils.integrations.Constants.SHORTS
import app.revanced.patches.youtube.utils.settings.SettingsPatch
import app.revanced.util.exception
import com.android.tools.smali.dexlib2.iface.instruction.OneRegisterInstruction
import app.revanced.util.getTargetIndexReversed
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction
import com.android.tools.smali.dexlib2.iface.instruction.TwoRegisterInstruction
@Patch(
name = "Disable shorts on startup",
@ -57,19 +61,26 @@ object DisableShortsOnStartupPatch : BytecodePatch(
UserWasInShortsFingerprint.result?.let {
it.mutableMethod.apply {
val insertIndex = it.scanResult.patternScanResult!!.startIndex
val insertRegister = getInstruction<OneRegisterInstruction>(insertIndex).registerA
val startIndex = it.scanResult.patternScanResult!!.startIndex
val targetIndex = getTargetIndexReversed(startIndex, Opcode.RETURN_VOID) + 1
if (getInstruction(targetIndex).opcode != Opcode.IGET_OBJECT)
throw PatchException("Failed to find insert index")
val replaceReference = getInstruction<ReferenceInstruction>(targetIndex).reference
val replaceInstruction = getInstruction<TwoRegisterInstruction>(targetIndex)
addInstructionsWithLabels(
insertIndex,
targetIndex + 1,
"""
invoke-static { }, $SHORTS->disableStartupShortsPlayer()Z
move-result v$insertRegister
if-eqz v$insertRegister, :show_startup_shorts_player
move-result v${replaceInstruction.registerA}
if-eqz v${replaceInstruction.registerA}, :show_startup_shorts_player
return-void
""",
ExternalLabel("show_startup_shorts_player", getInstruction(insertIndex))
:show_startup_shorts_player
iget-object v${replaceInstruction.registerA}, v${replaceInstruction.registerB}, $replaceReference
"""
)
removeInstruction(targetIndex)
}
} ?: throw UserWasInShortsFingerprint.exception

View File

@ -9,6 +9,10 @@ object UserWasInShortsFingerprint : MethodFingerprint(
returnType = "V",
accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL,
parameters = listOf("Ljava/lang/Object;"),
opcodes = listOf(Opcode.CONST_WIDE_32),
opcodes = listOf(
null,
Opcode.RETURN_VOID,
Opcode.MOVE_EXCEPTION
),
strings = listOf("Failed to read user_was_in_shorts proto after successful warmup")
)

View File

@ -7,9 +7,7 @@ import app.revanced.patcher.extensions.InstructionExtensions.addInstructionsWith
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.extensions.InstructionExtensions.removeInstruction
import app.revanced.patcher.patch.BytecodePatch
import app.revanced.patcher.patch.PatchException
import app.revanced.patcher.patch.annotation.Patch
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
import app.revanced.patcher.util.smali.ExternalLabel
import app.revanced.patches.youtube.utils.integrations.Constants.UTILS_PATH
import app.revanced.patches.youtube.utils.returnyoutubedislike.shorts.fingerprints.IncognitoFingerprint
@ -17,6 +15,7 @@ import app.revanced.patches.youtube.utils.returnyoutubedislike.shorts.fingerprin
import app.revanced.patches.youtube.utils.returnyoutubedislike.shorts.fingerprints.TextComponentSpecFingerprint
import app.revanced.patches.youtube.utils.settings.SettingsPatch
import app.revanced.util.exception
import app.revanced.util.getTargetIndexReversed
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction
import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction
@ -30,13 +29,16 @@ object ReturnYouTubeDislikeShortsPatch : BytecodePatch(
TextComponentSpecFingerprint
)
) {
private const val INTEGRATIONS_RYD_CLASS_DESCRIPTOR =
"$UTILS_PATH/ReturnYouTubeDislikePatch;"
override fun execute(context: BytecodeContext) {
ShortsTextViewFingerprint.result?.let {
it.mutableMethod.apply {
val startIndex = it.scanResult.patternScanResult!!.startIndex
val isDisLikesBooleanIndex = getTargetIndexDownTo(startIndex, Opcode.IGET_BOOLEAN)
val textViewFieldIndex = getTargetIndexDownTo(startIndex, Opcode.IGET_OBJECT)
val isDisLikesBooleanIndex = getTargetIndexReversed(startIndex, Opcode.IGET_BOOLEAN)
val textViewFieldIndex = getTargetIndexReversed(startIndex, Opcode.IGET_OBJECT)
// If the field is true, the TextView is for a dislike button.
val isDisLikesBooleanReference =
@ -103,20 +105,4 @@ object ReturnYouTubeDislikeShortsPatch : BytecodePatch(
} ?: throw IncognitoFingerprint.exception
}
}
private fun MutableMethod.getTargetIndexDownTo(
startIndex: Int,
opcode: Opcode
): Int {
for (index in startIndex downTo 0) {
if (getInstruction(index).opcode != opcode)
continue
return index
}
throw PatchException("Failed to find target method")
}
private const val INTEGRATIONS_RYD_CLASS_DESCRIPTOR =
"$UTILS_PATH/ReturnYouTubeDislikePatch;"
}

View File

@ -2,7 +2,7 @@ package app.revanced.util
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.extensions.InstructionExtensions.replaceInstruction
import app.revanced.patcher.fingerprint.MethodFingerprint
import app.revanced.patcher.patch.PatchException
@ -145,40 +145,21 @@ inline fun <reified T : Reference> Instruction.getReference() =
fun Method.indexOfFirstInstruction(predicate: Instruction.() -> Boolean) =
this.implementation!!.instructions.indexOfFirst(predicate)
/**
* Get the index of the last [Instruction] that matches the predicate.
*
* @param predicate The predicate to match.
* @return The index of the first [Instruction] that matches the predicate.
*/
fun Method.indexOfLastInstruction(predicate: Instruction.() -> Boolean) =
this.implementation!!.instructions.indexOfFirst(predicate)
/**
* Return the resolved methods of [MethodFingerprint]s early.
*/
fun List<MethodFingerprint>.returnEarly(bool: Boolean = false) {
val const = if (bool) "0x1" else "0x0"
this.forEach { fingerprint ->
fingerprint.result?.let { result ->
val stringInstructions = when (result.method.returnType.first()) {
'L' -> """
const/4 v0, $const
return-object v0
"""
'V' -> "return-void"
'I', 'Z' -> """
const/4 v0, $const
return v0
"""
else -> throw Exception("This case should never happen.")
fun MutableMethod.getTargetIndex(startIndex: Int, opcode: Opcode) =
implementation!!.instructions.let {
startIndex + it.subList(startIndex, it.size - 1).indexOfFirst { instruction ->
instruction.opcode == opcode
}
}
result.mutableMethod.addInstructions(0, stringInstructions)
} ?: throw fingerprint.exception
fun MutableMethod.getTargetIndexReversed(startIndex: Int, opcode: Opcode): Int {
for (index in startIndex downTo 0) {
if (getInstruction(index).opcode != opcode)
continue
return index
}
throw PatchException("Failed to find target index")
}
fun BytecodeContext.updatePatchStatus(