revanced-patches/src/main/kotlin/app/revanced/patches/youtube/misc/settings/bytecode/patch/SettingsPatch.kt

91 lines
3.7 KiB
Kotlin
Raw Normal View History

2022-07-31 12:15:26 +02:00
package app.revanced.patches.youtube.misc.settings.bytecode.patch
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.data.impl.BytecodeData
import app.revanced.patcher.extensions.addInstruction
import app.revanced.patcher.extensions.addInstructions
import app.revanced.patcher.patch.PatchResult
import app.revanced.patcher.patch.PatchResultSuccess
2022-08-03 03:53:35 +02:00
import app.revanced.patcher.patch.annotations.DependsOn
2022-07-31 12:15:26 +02:00
import app.revanced.patcher.patch.annotations.Patch
import app.revanced.patcher.patch.impl.BytecodePatch
import app.revanced.patcher.util.smali.toInstruction
2022-07-31 12:15:26 +02:00
import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch
import app.revanced.patches.youtube.misc.mapping.patch.ResourceIdMappingProviderResourcePatch
2022-07-31 12:15:26 +02:00
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
2022-07-31 12:15:26 +02:00
import app.revanced.patches.youtube.misc.settings.resource.patch.SettingsResourcePatch
@Patch
2022-08-03 03:53:35 +02:00
@DependsOn(
[
IntegrationsPatch::class,
SettingsResourcePatch::class,
ResourceIdMappingProviderResourcePatch::class
]
)
2022-07-31 12:15:26 +02:00
@Name("settings")
@Description("Adds settings for ReVanced to YouTube.")
@SettingsCompatibility
@Version("0.0.1")
class SettingsPatch : BytecodePatch(
listOf(LicenseActivityFingerprint, ReVancedSettingsActivityFingerprint, ThemeSetterFingerprint)
2022-07-31 12:15:26 +02:00
) {
override fun execute(data: BytecodeData): PatchResult {
val licenseActivityResult = LicenseActivityFingerprint.result!!
val settingsResult = ReVancedSettingsActivityFingerprint.result!!
val themeSetterResult = ThemeSetterFingerprint.result!!
2022-07-31 12:15:26 +02:00
val licenseActivityClass = licenseActivityResult.mutableClass
val settingsClass = settingsResult.mutableClass
val onCreate = licenseActivityResult.mutableMethod
val setThemeMethodName = "setTheme"
val initializeSettings = settingsResult.mutableMethod
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.
2022-07-31 12:15:26 +02:00
onCreate.addInstructions(
1,
"""
invoke-static { p0 }, ${settingsClass.type}->${initializeSettings.name}(${licenseActivityClass.type})V
return-void
"""
)
// add the initializeSettings call to the onCreate method.
2022-07-31 12:15:26 +02:00
onCreate.addInstruction(
0,
"invoke-static { p0 }, ${settingsClass.type}->$setThemeMethodName(${licenseActivityClass.type})V"
)
2022-07-31 12:15:26 +02:00
return PatchResultSuccess()
}
internal companion object {
val appearanceStringId = ResourceIdMappingProviderResourcePatch.resourceMappings.find {
it.type == "string" && it.name == "app_theme_appearance_dark"
}!!.id
}
2022-07-31 12:15:26 +02:00
}