From c534fc49f65958a7da075d2b02ff22556115e7cb Mon Sep 17 00:00:00 2001 From: inotia00 Date: Tue, 11 Apr 2023 06:03:04 +0900 Subject: [PATCH] fix(settings): dark theme not applied in ReVanced settings https://github.com/inotia00/ReVanced_Extended/issues/690 --- .../app/revanced/extensions/Extensions.kt | 12 ---- .../bytecode/patch/SettingsBytecodePatch.kt | 61 +++---------------- 2 files changed, 8 insertions(+), 65 deletions(-) diff --git a/src/main/kotlin/app/revanced/extensions/Extensions.kt b/src/main/kotlin/app/revanced/extensions/Extensions.kt index dcc7999a4..36f9c4e07 100644 --- a/src/main/kotlin/app/revanced/extensions/Extensions.kt +++ b/src/main/kotlin/app/revanced/extensions/Extensions.kt @@ -9,7 +9,6 @@ import app.revanced.patcher.util.proxy.mutableTypes.MutableClass import app.revanced.patcher.util.proxy.mutableTypes.MutableField import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod import app.revanced.patcher.util.smali.toInstruction -import app.revanced.util.integrations.Constants.INTEGRATIONS_PATH import app.revanced.util.integrations.Constants.PATCHES_PATH import org.jf.dexlib2.builder.MutableMethodImplementation import org.jf.dexlib2.iface.Method @@ -28,17 +27,6 @@ internal fun MutableMethodImplementation.injectHideCall( ) } -internal fun MutableMethodImplementation.injectTheme( - index: Int, - register: Int, - method: String -) { - this.addInstruction( - index, - "invoke-static { v$register }, $INTEGRATIONS_PATH/utils/ThemeHelper;->$method(I)V".toInstruction() - ) -} - // TODO: populate this to all patches /** * Convert a [MethodFingerprint] to a [PatchResultError]. diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/settings/bytecode/patch/SettingsBytecodePatch.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/settings/bytecode/patch/SettingsBytecodePatch.kt index b66deb3af..661ef5b2a 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/misc/settings/bytecode/patch/SettingsBytecodePatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/settings/bytecode/patch/SettingsBytecodePatch.kt @@ -1,12 +1,11 @@ package app.revanced.patches.youtube.misc.settings.bytecode.patch -import app.revanced.extensions.findMutableMethodOf -import app.revanced.extensions.injectTheme import app.revanced.extensions.toErrorResult import app.revanced.patcher.annotation.Name import app.revanced.patcher.annotation.Version import app.revanced.patcher.data.BytecodeContext import app.revanced.patcher.extensions.addInstruction +import app.revanced.patcher.extensions.replaceInstruction import app.revanced.patcher.patch.BytecodePatch import app.revanced.patcher.patch.PatchResult import app.revanced.patcher.patch.PatchResultSuccess @@ -18,8 +17,6 @@ import app.revanced.patches.youtube.misc.resourceid.patch.SharedResourceIdPatch import app.revanced.patches.youtube.misc.settings.bytecode.fingerprints.ThemeSetterSystemFingerprint import app.revanced.util.bytecode.BytecodeHelper.injectInit import app.revanced.util.integrations.Constants.INTEGRATIONS_PATH -import org.jf.dexlib2.Opcode -import org.jf.dexlib2.iface.instruction.formats.Instruction31i @Name("settings-bytecode-patch") @DependsOn( @@ -34,61 +31,19 @@ import org.jf.dexlib2.iface.instruction.formats.Instruction31i class SettingsBytecodePatch : BytecodePatch( listOf(ThemeSetterSystemFingerprint) ) { - - // list of resource names to get the id of - private val resourceIds = arrayOf( - "Theme.YouTube.Light", - "Theme.YouTube.Light.DarkerPalette" - ).map { name -> - ResourceMappingPatch.resourceMappings.single { it.name == name }.id - } - override fun execute(context: BytecodeContext): PatchResult { - context.classes.forEach { classDef -> - classDef.methods.forEach { method -> - with(method.implementation) { - this?.instructions?.forEachIndexed { index, instruction -> - when (instruction.opcode) { - Opcode.CONST -> { - when ((instruction as Instruction31i).wideLiteral) { - resourceIds[0] -> { // primary theme - val insertIndex = index - 3 - val invokeInstruction = instructions.elementAt(insertIndex) - if (invokeInstruction.opcode != Opcode.IF_NE) return@forEachIndexed - - val mutableMethod = context.proxy(classDef).mutableClass.findMutableMethodOf(method) - - val viewRegister = (instructions.elementAt(index) as Instruction31i).registerA - mutableMethod.implementation!!.injectTheme(mutableMethod.implementation!!.instructions.size - 1 , viewRegister, "setPrimaryTheme") - } - - resourceIds[1] -> { // secondary theme - val insertIndex = index - 3 - val invokeInstruction = instructions.elementAt(insertIndex) - if (invokeInstruction.opcode != Opcode.IF_NE) return@forEachIndexed - - val mutableMethod = context.proxy(classDef).mutableClass.findMutableMethodOf(method) - - val viewRegister = (instructions.elementAt(index) as Instruction31i).registerA - mutableMethod.implementation!!.injectTheme(index + 2, viewRegister, "setSecondaryTheme") - } - } - } - else -> return@forEachIndexed - } - } - } - } - } - // apply the current theme of the settings page ThemeSetterSystemFingerprint.result?.let { with(it.mutableMethod) { - addInstruction( - it.scanResult.patternScanResult!!.startIndex, + val targetIndex = it.scanResult.patternScanResult!!.startIndex + replaceInstruction( + targetIndex, SET_THEME ) - + addInstruction( + targetIndex + 1, + "return-object v0" + ) addInstruction( this.implementation!!.instructions.size - 1, SET_THEME