fix(settings): dark theme not applied in ReVanced settings https://github.com/inotia00/ReVanced_Extended/issues/690

This commit is contained in:
inotia00
2023-04-11 06:03:04 +09:00
parent 2680cfe849
commit c534fc49f6
2 changed files with 8 additions and 65 deletions

View File

@ -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