refactor(return-youtube-dislike): matches official ReVanced's code

This commit is contained in:
inotia00 2023-04-20 07:48:08 +09:00
parent b1a4b1c91e
commit ad1ef6c5d1
5 changed files with 48 additions and 92 deletions

View File

@ -4,7 +4,7 @@ import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags import org.jf.dexlib2.AccessFlags
object TextComponentSpecParentFingerprint : MethodFingerprint( object TextComponentConstructorFingerprint : MethodFingerprint(
returnType = "V", returnType = "V",
access = AccessFlags.PRIVATE or AccessFlags.CONSTRUCTOR, access = AccessFlags.PRIVATE or AccessFlags.CONSTRUCTOR,
strings = listOf("TextComponent") strings = listOf("TextComponent")

View File

@ -5,13 +5,14 @@ import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode import org.jf.dexlib2.Opcode
object TextComponentSpecObjectFingerprint : MethodFingerprint( object TextComponentContextFingerprint : MethodFingerprint(
returnType = "V", returnType = "L",
access = AccessFlags.PROTECTED or AccessFlags.FINAL, access = AccessFlags.PROTECTED or AccessFlags.FINAL,
parameters = listOf("L"), parameters = listOf("L"),
opcodes = listOf( opcodes = listOf(
Opcode.INVOKE_STATIC_RANGE, Opcode.IGET_OBJECT, // conversion context
Opcode.MOVE_RESULT_OBJECT, Opcode.IGET_OBJECT,
Opcode.INVOKE_DIRECT Opcode.IGET_OBJECT,
Opcode.IGET_BOOLEAN
) )
) )

View File

@ -1,14 +0,0 @@
package app.revanced.patches.youtube.misc.returnyoutubedislike.fingerprints
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.Opcode
object TextComponentSpecExtensionFingerprint : MethodFingerprint(
returnType = "L",
opcodes = listOf(
Opcode.INVOKE_STATIC,
Opcode.MOVE_RESULT_OBJECT,
Opcode.CONST_4
),
strings = listOf("TextComponentSpec: No converter for extension: %s")
)

View File

@ -1,18 +1,13 @@
package app.revanced.patches.youtube.misc.returnyoutubedislike.fingerprints package app.revanced.patches.youtube.misc.returnyoutubedislike.fingerprints
import app.revanced.patcher.extensions.or
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import org.jf.dexlib2.AccessFlags
import org.jf.dexlib2.Opcode import org.jf.dexlib2.Opcode
object TextComponentSpecFingerprint : MethodFingerprint( object TextComponentSpecFingerprint : MethodFingerprint(
returnType = "V", returnType = "L",
access = AccessFlags.PROTECTED or AccessFlags.FINAL,
parameters = listOf("L", "L"),
opcodes = listOf( opcodes = listOf(
Opcode.CHECK_CAST, Opcode.INVOKE_STATIC,
Opcode.CHECK_CAST, Opcode.RETURN_OBJECT, // last instruction of the method
Opcode.IGET_OBJECT, ),
Opcode.IPUT_OBJECT strings = listOf("TextComponentSpec: No converter for extension: %s")
)
) )

View File

@ -6,8 +6,10 @@ import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version import app.revanced.patcher.annotation.Version
import app.revanced.patcher.data.BytecodeContext import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.data.toMethodWalker import app.revanced.patcher.data.toMethodWalker
import app.revanced.patcher.extensions.addInstruction
import app.revanced.patcher.extensions.addInstructions import app.revanced.patcher.extensions.addInstructions
import app.revanced.patcher.extensions.instruction import app.revanced.patcher.extensions.instruction
import app.revanced.patcher.extensions.replaceInstructions
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint.Companion.resolve import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint.Companion.resolve
import app.revanced.patcher.patch.BytecodePatch import app.revanced.patcher.patch.BytecodePatch
@ -22,13 +24,11 @@ import app.revanced.patches.youtube.misc.settings.resource.patch.SettingsPatch
import app.revanced.patches.youtube.misc.videoid.mainstream.patch.MainstreamVideoIdPatch import app.revanced.patches.youtube.misc.videoid.mainstream.patch.MainstreamVideoIdPatch
import app.revanced.util.integrations.Constants.UTILS_PATH import app.revanced.util.integrations.Constants.UTILS_PATH
import org.jf.dexlib2.Opcode import org.jf.dexlib2.Opcode
import org.jf.dexlib2.builder.instruction.BuilderInstruction3rc import org.jf.dexlib2.iface.instruction.FiveRegisterInstruction
import org.jf.dexlib2.iface.instruction.OneRegisterInstruction import org.jf.dexlib2.iface.instruction.OneRegisterInstruction
import org.jf.dexlib2.iface.instruction.ReferenceInstruction import org.jf.dexlib2.iface.instruction.ReferenceInstruction
import org.jf.dexlib2.iface.instruction.TwoRegisterInstruction import org.jf.dexlib2.iface.instruction.TwoRegisterInstruction
import org.jf.dexlib2.iface.instruction.formats.Instruction35c
import org.jf.dexlib2.iface.reference.FieldReference import org.jf.dexlib2.iface.reference.FieldReference
import org.jf.dexlib2.iface.reference.MethodReference
@Patch @Patch
@Name("return-youtube-dislike") @Name("return-youtube-dislike")
@ -47,8 +47,8 @@ class ReturnYouTubeDislikePatch : BytecodePatch(
LikeFingerprint, LikeFingerprint,
RemoveLikeFingerprint, RemoveLikeFingerprint,
ShortsTextComponentParentFingerprint, ShortsTextComponentParentFingerprint,
TextComponentSpecExtensionFingerprint, TextComponentConstructorFingerprint,
TextComponentSpecParentFingerprint TextComponentSpecFingerprint
) )
) { ) {
override fun execute(context: BytecodeContext): PatchResult { override fun execute(context: BytecodeContext): PatchResult {
@ -69,81 +69,55 @@ class ReturnYouTubeDislikePatch : BytecodePatch(
} }
TextComponentSpecParentFingerprint.result?.let { parentResult -> TextComponentConstructorFingerprint.result?.let { parentResult ->
TextComponentContextFingerprint.also { it.resolve(context, parentResult.classDef) }.result?.let {
TextComponentSpecObjectFingerprint.also { it.resolve(context, parentResult.classDef) }.result?.let {
with (it.mutableMethod) { with (it.mutableMethod) {
val startIndex = it.scanResult.patternScanResult!!.startIndex val conversionContextIndex = it.scanResult.patternScanResult!!.startIndex
val endIndex = it.scanResult.patternScanResult!!.endIndex val conversionContextRegister =
(instruction(conversionContextIndex) as TwoRegisterInstruction).registerA
val dummyRegister = (instruction(startIndex) as BuilderInstruction3rc).registerCount +
(instruction(startIndex) as BuilderInstruction3rc).startRegister - 1
val targetRegister = (instruction(endIndex) as Instruction35c).registerC
val instructions = implementation!!.instructions val instructions = implementation!!.instructions
val targetString = val targetName = "a"
((instructions.elementAt(startIndex) as ReferenceInstruction).reference as MethodReference).parameterTypes.first().toString() val targetType = "Ljava/util/concurrent/atomic/AtomicReference;"
for ((index, instruction) in instructions.withIndex()) { for ((index, instruction) in instructions.withIndex()) {
if (instruction.opcode != Opcode.IGET_OBJECT) continue if (instruction.opcode != Opcode.IGET_OBJECT) continue
val indexReference = (instruction as ReferenceInstruction).reference as FieldReference val indexReference = (instruction as ReferenceInstruction).reference as FieldReference
if (indexReference.name != targetName || indexReference.type != targetType) continue
if (indexReference.type != targetString) continue atomicReferenceRegister = (instruction(index) as TwoRegisterInstruction).registerA
objectReference = indexReference insertIndex = index + 1
objectIndex = index
break break
} }
if (objectIndex == 0) return TextComponentSpecObjectFingerprint.toErrorResult() if (insertIndex == 0) return TextComponentContextFingerprint.toErrorResult()
val conversionContextParam = 0 addInstruction(
insertIndex,
addInstructions( "invoke-static {v$conversionContextRegister, v$atomicReferenceRegister}, $INTEGRATIONS_RYD_CLASS_DESCRIPTOR->onComponentCreated(Ljava/lang/Object;Ljava/util/concurrent/atomic/AtomicReference;)V"
endIndex + 1, """
move-object/from16 v$dummyRegister, p$conversionContextParam
iget-object v$dummyRegister, v$dummyRegister, ${definingClass}->${objectReference.name}:${objectReference.type}
invoke-static {v$dummyRegister, v$targetRegister}, $INTEGRATIONS_RYD_CLASS_DESCRIPTOR->onComponentCreated(Ljava/lang/Object;Ljava/util/concurrent/atomic/AtomicReference;)V
"""
) )
} }
} ?: return TextComponentSpecObjectFingerprint.toErrorResult() } ?: return TextComponentContextFingerprint.toErrorResult()
} ?: return TextComponentConstructorFingerprint.toErrorResult()
TextComponentSpecFingerprint.also { it.resolve(context, parentResult.classDef) }.result?.let { TextComponentSpecFingerprint.result?.let {
with(it.mutableMethod) { with(it.mutableMethod) {
val insertIndex = it.scanResult.patternScanResult!!.endIndex val startIndex = it.scanResult.patternScanResult!!.startIndex
val targetRegister = (instruction(insertIndex) as TwoRegisterInstruction).registerA val endIndex = it.scanResult.patternScanResult!!.endIndex
val charSequenceRegister = (instruction(endIndex) as OneRegisterInstruction).registerA
val contextTempRegister = (instruction(startIndex) as FiveRegisterInstruction).registerD
replaceInstructions(endIndex, "move-object/from16 v$contextTempRegister, p0")
addInstructions( addInstructions(
insertIndex, """ endIndex + 1, """
iget-object p0, p0, ${definingClass}->${objectReference.name}:${objectReference.type} invoke-static {v$contextTempRegister, v$charSequenceRegister}, $INTEGRATIONS_RYD_CLASS_DESCRIPTOR->onComponentCreated(Ljava/lang/Object;Ljava/lang/CharSequence;)Ljava/lang/CharSequence;
invoke-static {p0, v$targetRegister}, $INTEGRATIONS_RYD_CLASS_DESCRIPTOR->onComponentCreated(Ljava/lang/Object;Ljava/util/concurrent/atomic/AtomicReference;)V move-result-object v$charSequenceRegister
return-object v$charSequenceRegister
""" """
) )
} }
} ?: return TextComponentSpecFingerprint.toErrorResult() } ?: return TextComponentSpecFingerprint.toErrorResult()
} ?: return TextComponentSpecParentFingerprint.toErrorResult()
TextComponentSpecExtensionFingerprint.result?.let {
with (it.mutableMethod) {
val targetIndex = it.scanResult.patternScanResult!!.startIndex + 1
val targetRegister =
(instruction(targetIndex) as OneRegisterInstruction).registerA
val dummyRegister =
(instruction(targetIndex + 1) as OneRegisterInstruction).registerA
addInstructions(
targetIndex + 1, """
move-object/from16 v$dummyRegister, p0
invoke-static {v$dummyRegister, v$targetRegister}, $INTEGRATIONS_RYD_CLASS_DESCRIPTOR->overrideLikeDislikeSpan(Ljava/lang/Object;Landroid/text/SpannableString;)Landroid/text/SpannableString;
move-result-object v$targetRegister
"""
)
}
} ?: return TextComponentSpecExtensionFingerprint.toErrorResult()
ShortsTextComponentParentFingerprint.result?.let { ShortsTextComponentParentFingerprint.result?.let {
with (context with (context
@ -183,8 +157,8 @@ class ReturnYouTubeDislikePatch : BytecodePatch(
const val INTEGRATIONS_RYD_CLASS_DESCRIPTOR = const val INTEGRATIONS_RYD_CLASS_DESCRIPTOR =
"$UTILS_PATH/ReturnYouTubeDislikePatch;" "$UTILS_PATH/ReturnYouTubeDislikePatch;"
private lateinit var objectReference: FieldReference var atomicReferenceRegister: Int = 0
var objectIndex: Int = 0 var insertIndex: Int = 0
} }
private fun MethodFingerprint.toPatch(voteKind: Vote) = VotePatch(this, voteKind) private fun MethodFingerprint.toPatch(voteKind: Vote) = VotePatch(this, voteKind)