fix(YouTube): When getting a Drawable, the theme is not set, so spam logs are printed (unpatched YouTube bug) https://github.com/inotia00/ReVanced_Extended/issues/2658#issuecomment-2589966775

This commit is contained in:
inotia00 2025-01-15 22:21:06 +09:00
parent 400628b248
commit e67104e86e
4 changed files with 102 additions and 0 deletions

View File

@ -0,0 +1,24 @@
package app.revanced.patches.youtube.utils.fix.attributes
import app.revanced.patches.youtube.utils.resourceid.ytOutlineMoonZ
import app.revanced.util.fingerprint.legacyFingerprint
import app.revanced.util.or
import com.android.tools.smali.dexlib2.AccessFlags
import com.android.tools.smali.dexlib2.Opcode
/**
* Tested on YouTube 19.25.xx ~ YouTube 20.02.xx.
*/
internal val setSleepTimerDrawableFingerprint = legacyFingerprint(
name = "setSleepTimerDrawableFingerprint",
returnType = "V",
accessFlags = AccessFlags.PUBLIC or AccessFlags.FINAL,
parameters = listOf("Z", "Ljava/lang/String;"),
opcodes = listOf(
Opcode.INVOKE_VIRTUAL, // Context.getResources()
Opcode.MOVE_RESULT_OBJECT,
Opcode.CONST, // R.drawable.yt_outline_moon_z_vd_theme_24
Opcode.INVOKE_VIRTUAL, // Resources.getDrawable(int)
),
literals = listOf(ytOutlineMoonZ),
)

View File

@ -0,0 +1,67 @@
package app.revanced.patches.youtube.utils.fix.attributes
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
import app.revanced.patcher.extensions.InstructionExtensions.removeInstructions
import app.revanced.patcher.extensions.InstructionExtensions.replaceInstruction
import app.revanced.patcher.patch.bytecodePatch
import app.revanced.patches.youtube.utils.playservice.is_19_25_or_greater
import app.revanced.patches.youtube.utils.playservice.versionCheckPatch
import app.revanced.patches.youtube.utils.resourceid.sharedResourceIdPatch
import app.revanced.util.fingerprint.matchOrThrow
import app.revanced.util.referenceMatchesOrThrow
import com.android.tools.smali.dexlib2.iface.instruction.FiveRegisterInstruction
val themeAttributesPatch = bytecodePatch(
description = "themeAttributesPatch"
) {
dependsOn(
sharedResourceIdPatch,
versionCheckPatch,
)
execute {
if (!is_19_25_or_greater) {
return@execute
}
/**
* There is a warning in the logcat of an unpatched YouTube 19.25.39+:
*
* Drawable com.google.android.youtube:drawable/yt_outline_moon_z_vd_theme_24 has unresolved theme attributes! Consider using Resources.getDrawable(int, Theme) or Context.getDrawable(int).
* java.lang.RuntimeException
* at android.content.res.Resources.getDrawable(Resources.java:857)
*
* According to [stackoverflow](https://stackoverflow.com/questions/28932306/logcat-says-resource-has-unresolved-theme-attributes),
* Replace [Resources.getDrawable(int)] with [Context.getDrawable(int)].
*/
setSleepTimerDrawableFingerprint.matchOrThrow().let {
it.method.apply {
val getResourcesIndex = it.patternMatch!!.startIndex
val getDrawableIndex = it.patternMatch!!.endIndex
// Verify that the correct pattern has been found.
referenceMatchesOrThrow(
getResourcesIndex,
"Landroid/content/Context;->getResources()Landroid/content/res/Resources;"
)
referenceMatchesOrThrow(
getDrawableIndex,
"Landroid/content/res/Resources;->getDrawable(I)Landroid/graphics/drawable/Drawable;"
)
val contextRegister =
getInstruction<FiveRegisterInstruction>(getResourcesIndex).registerC
val identifierRegister =
getInstruction<FiveRegisterInstruction>(getDrawableIndex).registerD
replaceInstruction(
getDrawableIndex,
"invoke-virtual {v$contextRegister, v$identifierRegister}, " +
"Landroid/content/Context;->getDrawable(I)Landroid/graphics/drawable/Drawable;"
)
removeInstructions(getResourcesIndex, 2)
}
}
}
}

View File

@ -222,6 +222,8 @@ var youTubeLogo = -1L
private set private set
var ytFillBell = -1L var ytFillBell = -1L
private set private set
var ytOutlineMoonZ = -1L
private set
var ytOutlinePictureInPictureWhite = -1L var ytOutlinePictureInPictureWhite = -1L
private set private set
var ytOutlineVideoCamera = -1L var ytOutlineVideoCamera = -1L
@ -656,6 +658,10 @@ internal val sharedResourceIdPatch = resourcePatch(
DRAWABLE, DRAWABLE,
"yt_fill_bell_black_24" "yt_fill_bell_black_24"
] ]
ytOutlineMoonZ = resourceMappings[
DRAWABLE,
"yt_outline_moon_z_vd_theme_24"
]
ytOutlinePictureInPictureWhite = resourceMappings[ ytOutlinePictureInPictureWhite = resourceMappings[
DRAWABLE, DRAWABLE,
"yt_outline_picture_in_picture_white_24" "yt_outline_picture_in_picture_white_24"

View File

@ -529,6 +529,11 @@ fun Method.findInstructionIndicesReversedOrThrow(opcode: Opcode): List<Int> {
return instructions return instructions
} }
fun Method.referenceMatchesOrThrow(targetIndex: Int, reference: String) {
val targetReference = getInstruction<ReferenceInstruction>(targetIndex).reference.toString()
if (reference != targetReference) throw PatchException("References do not match. Expected: '$reference', Found: '$targetReference'")
}
/** /**
* Called for _all_ instructions with the given literal value. * Called for _all_ instructions with the given literal value.
*/ */