revanced-patches/src/main/kotlin/app/revanced/patches/youtube/video/hdrbrightness/patch/HDRBrightnessPatch.kt

64 lines
3.0 KiB
Kotlin
Raw Normal View History

package app.revanced.patches.youtube.video.hdrbrightness.patch
import app.revanced.patcher.annotation.Description
import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.data.BytecodeContext
import app.revanced.patcher.extensions.addInstructions
import app.revanced.patcher.patch.BytecodePatch
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
import app.revanced.patcher.patch.annotations.Patch
import app.revanced.patches.youtube.video.hdrbrightness.annotations.HDRBrightnessCompatibility
import app.revanced.patches.youtube.video.hdrbrightness.fingerprints.HDRBrightnessFingerprint
2022-07-11 18:59:26 +02:00
import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch
import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch
import app.revanced.patches.shared.settings.preference.impl.StringResource
import app.revanced.patches.shared.settings.preference.impl.SwitchPreference
2022-07-11 18:59:26 +02:00
import org.jf.dexlib2.iface.instruction.ReferenceInstruction
import org.jf.dexlib2.iface.instruction.TwoRegisterInstruction
import org.jf.dexlib2.iface.reference.FieldReference
2022-07-31 12:15:26 +02:00
@Patch
2022-07-11 18:59:26 +02:00
@Name("hdr-auto-brightness")
@Description("Makes the brightness of HDR videos follow the system default.")
@HDRBrightnessCompatibility
2022-07-11 18:59:26 +02:00
@Version("0.0.2")
@DependsOn([IntegrationsPatch::class, SettingsPatch::class])
class HDRBrightnessPatch : BytecodePatch(
listOf(HDRBrightnessFingerprint)
) {
override fun execute(context: BytecodeContext): PatchResult {
SettingsPatch.PreferenceScreen.VIDEO.addPreferences(
SwitchPreference(
"revanced_hdr_auto_brightness",
StringResource("revanced_hdr_auto_brightness_title", "Enable auto HDR brightness"),
StringResource("revanced_hdr_auto_brightness_summary_on", "Auto HDR brightness is enabled"),
StringResource("revanced_hdr_auto_brightness_summary_off", "Auto HDR brightness is disabled")
)
)
val method = HDRBrightnessFingerprint.result!!.mutableMethod
method.implementation!!.instructions.filter { instruction ->
val fieldReference = (instruction as? ReferenceInstruction)?.reference as? FieldReference
fieldReference?.let { it.name == "screenBrightness" } == true
2022-07-11 18:59:26 +02:00
}.forEach { instruction ->
val brightnessRegisterIndex = method.implementation!!.instructions.indexOf(instruction)
2022-07-11 18:59:26 +02:00
val register = (instruction as TwoRegisterInstruction).registerA
val insertIndex = brightnessRegisterIndex + 1
2022-07-11 18:59:26 +02:00
method.addInstructions(
insertIndex,
"""
invoke-static {v$register}, Lapp/revanced/integrations/patches/HDRAutoBrightnessPatch;->getHDRBrightness(F)F
move-result v$register
"""
2022-07-11 18:59:26 +02:00
)
}
return PatchResultSuccess()
}
}