Skip to content

Commit ae084be

Browse files
refactor: Log if the flag is enabled
1 parent 731e04e commit ae084be

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

patches/src/main/kotlin/app/revanced/patches/youtube/layout/returnyoutubedislike/ReturnYouTubeDislikePatch.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ val returnYouTubeDislikePatch = bytecodePatch(
180180
// Turn off a/b flag that enables new code for creating litho spans.
181181
// If enabled then the litho text span hook is never called.
182182
// Target code is very obfuscated and exactly what the code does is not clear.
183-
textComponentFeatureFlagFingerprint.method.returnEarly(false)
183+
// Return late so debug patch logs if the flag is enabled.
184+
textComponentFeatureFlagFingerprint.method.returnLate(false)
184185
}
185186

186187
// Player response video id is needed to search for the video ids in Shorts litho components.

patches/src/main/kotlin/app/revanced/util/BytecodeUtils.kt

+32-7
Original file line numberDiff line numberDiff line change
@@ -607,29 +607,54 @@ fun BytecodePatchContext.forEachLiteralValueInstruction(
607607
}
608608

609609
/**
610-
* Return the method early.
610+
* Overrides the first instruction of a method with a constant return value.
611+
* None of the method code will ever execute.
611612
*/
612-
fun MutableMethod.returnEarly(bool: Boolean = false) {
613+
fun MutableMethod.returnEarly(overrideValue: Boolean = false) = overrideReturnValue(overrideValue, false)
614+
615+
/**
616+
* Overrides all return statements with a constant value.
617+
* All method code is executed the same as unpatched.
618+
*
619+
* @see returnEarly
620+
*/
621+
internal fun MutableMethod.returnLate(overrideValue: Boolean = false) = overrideReturnValue(overrideValue, true)
622+
623+
private fun MutableMethod.overrideReturnValue(bool: Boolean, returnLate: Boolean) {
613624
val const = if (bool) "0x1" else "0x0"
614625

615-
val stringInstructions = when (returnType.first()) {
616-
'L' ->
626+
val instructions = when (returnType.first()) {
627+
'L' -> {
617628
"""
618629
const/4 v0, $const
619630
return-object v0
620631
"""
632+
}
621633

622-
'V' -> "return-void"
623-
'I', 'Z' ->
634+
'V' -> {
635+
if (returnLate) throw IllegalArgumentException("Cannot return late for method of void type")
636+
"return-void"
637+
}
638+
639+
'I', 'Z' -> {
624640
"""
625641
const/4 v0, $const
626642
return v0
627643
"""
644+
}
628645

629646
else -> throw Exception("Return type is not supported: $this")
630647
}
631648

632-
addInstructions(0, stringInstructions)
649+
if (returnLate) {
650+
findInstructionIndicesReversed {
651+
opcode == RETURN || opcode == RETURN_OBJECT
652+
}.forEach { index ->
653+
addInstructionsAtControlFlowLabel(index, instructions)
654+
}
655+
} else {
656+
addInstructions(0, instructions)
657+
}
633658
}
634659

635660
/**

0 commit comments

Comments
 (0)