diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/settings/bytecode/fingerprints/ThemeSetterFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/settings/bytecode/fingerprints/ThemeSetterFingerprint.kt new file mode 100644 index 000000000..148590939 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/settings/bytecode/fingerprints/ThemeSetterFingerprint.kt @@ -0,0 +1,29 @@ +package app.revanced.patches.youtube.misc.settings.bytecode.fingerprints + +import app.revanced.patcher.annotation.Name +import app.revanced.patcher.annotation.Version +import app.revanced.patcher.fingerprint.method.annotation.MatchingMethod +import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint +import app.revanced.patches.youtube.layout.returnyoutubedislike.annotations.ReturnYouTubeDislikeCompatibility +import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch +import org.jf.dexlib2.Opcode +import org.jf.dexlib2.iface.instruction.WideLiteralInstruction + +@Name("theme-setter-fingerprint") +@MatchingMethod( + "Lfyq;", "a" +) +@ReturnYouTubeDislikeCompatibility +@Version("0.0.1") +object ThemeSetterFingerprint : MethodFingerprint( + "L", + null, + null, + listOf(Opcode.RETURN_OBJECT), + null, + { methodDef -> + methodDef.implementation?.instructions?.any { + it.opcode.ordinal == Opcode.CONST.ordinal && (it as WideLiteralInstruction).wideLiteral == SettingsPatch.appearanceStringId + } == true + } +) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/settings/bytecode/patch/SettingsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/settings/bytecode/patch/SettingsPatch.kt index 4cc15349f..ad253bc65 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/misc/settings/bytecode/patch/SettingsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/settings/bytecode/patch/SettingsPatch.kt @@ -11,10 +11,13 @@ import app.revanced.patcher.patch.PatchResultSuccess import app.revanced.patcher.patch.annotations.Dependencies import app.revanced.patcher.patch.annotations.Patch import app.revanced.patcher.patch.impl.BytecodePatch +import app.revanced.patcher.util.smali.toInstruction import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch +import app.revanced.patches.youtube.misc.mapping.patch.ResourceIdMappingProviderResourcePatch import app.revanced.patches.youtube.misc.settings.annotations.SettingsCompatibility import app.revanced.patches.youtube.misc.settings.bytecode.fingerprints.LicenseActivityFingerprint import app.revanced.patches.youtube.misc.settings.bytecode.fingerprints.ReVancedSettingsActivityFingerprint +import app.revanced.patches.youtube.misc.settings.bytecode.fingerprints.ThemeSetterFingerprint import app.revanced.patches.youtube.misc.settings.resource.patch.SettingsResourcePatch @Patch @@ -24,11 +27,12 @@ import app.revanced.patches.youtube.misc.settings.resource.patch.SettingsResourc @SettingsCompatibility @Version("0.0.1") class SettingsPatch : BytecodePatch( - listOf(LicenseActivityFingerprint, ReVancedSettingsActivityFingerprint) + listOf(LicenseActivityFingerprint, ReVancedSettingsActivityFingerprint, ThemeSetterFingerprint) ) { override fun execute(data: BytecodeData): PatchResult { val licenseActivityResult = LicenseActivityFingerprint.result!! val settingsResult = ReVancedSettingsActivityFingerprint.result!! + val themeSetterResult = ThemeSetterFingerprint.result!! val licenseActivityClass = licenseActivityResult.mutableClass val settingsClass = settingsResult.mutableClass @@ -37,7 +41,25 @@ class SettingsPatch : BytecodePatch( val setThemeMethodName = "setTheme" val initializeSettings = settingsResult.mutableMethod - // First add the setTheme call to the onCreate method to not affect the offsets. + val setThemeInstruction = + "invoke-static {v0}, Lapp/revanced/integrations/utils/ThemeHelper;->setTheme(Ljava/lang/Object;)V".toInstruction( + themeSetterResult.mutableMethod + ) + + // add instructions to set the theme of the settings activity + themeSetterResult.mutableMethod.implementation!!.let { + it.addInstruction( + themeSetterResult.patternScanResult!!.startIndex, + setThemeInstruction + ) + + it.addInstruction( + it.instructions.size - 1, // add before return + setThemeInstruction + ) + } + + // add the setTheme call to the onCreate method to not affect the offsets. onCreate.addInstructions( 1, """ @@ -46,11 +68,18 @@ class SettingsPatch : BytecodePatch( """ ) - // Add the initializeSettings call to the onCreate method. + // add the initializeSettings call to the onCreate method. onCreate.addInstruction( 0, "invoke-static { p0 }, ${settingsClass.type}->$setThemeMethodName(${licenseActivityClass.type})V" ) + return PatchResultSuccess() } + + internal companion object { + val appearanceStringId = ResourceIdMappingProviderResourcePatch.resourceMappings.find { + it.type == "string" && it.name == "app_theme_appearance_dark" + }!!.id + } } \ No newline at end of file