Skip to content

Commit

Permalink
perf: fix high battery consumption due to chromecast not working with…
Browse files Browse the repository at this point in the history
… `microg`
  • Loading branch information
oSumAtrIX committed Jun 12, 2022
1 parent 9a063d4 commit dd8b01a
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,11 @@ import app.revanced.patches.youtube.misc.microg.patch.resource.MicroGResourcePat
import app.revanced.patches.youtube.misc.microg.patch.resource.enum.StringReplaceMode
import app.revanced.patches.youtube.misc.microg.shared.Constants.BASE_MICROG_PACKAGE_NAME
import app.revanced.patches.youtube.misc.microg.shared.Constants.REVANCED_PACKAGE_NAME
import app.revanced.patches.youtube.misc.microg.signatures.GooglePlayUtilitySignature
import app.revanced.patches.youtube.misc.microg.signatures.IntegrityCheckSignature
import app.revanced.patches.youtube.misc.microg.signatures.PrimeSignature
import app.revanced.patches.youtube.misc.microg.signatures.ServiceCheckSignature
import app.revanced.patches.youtube.misc.microg.signatures.*
import org.jf.dexlib2.Opcode
import org.jf.dexlib2.builder.MutableMethodImplementation
import org.jf.dexlib2.builder.instruction.BuilderInstruction21c
import org.jf.dexlib2.builder.instruction.BuilderInstruction21s
import org.jf.dexlib2.iface.instruction.formats.Instruction21c
import org.jf.dexlib2.iface.reference.StringReference
import org.jf.dexlib2.immutable.reference.ImmutableStringReference
Expand All @@ -39,19 +37,36 @@ import org.jf.dexlib2.immutable.reference.ImmutableStringReference
@Version("0.0.1")
class MicroGBytecodePatch : BytecodePatch(
listOf(
IntegrityCheckSignature, ServiceCheckSignature, GooglePlayUtilitySignature, PrimeSignature
IntegrityCheckSignature,
ServiceCheckSignature,
GooglePlayUtilitySignature,
CastDynamiteModuleSignature,
CastDynamiteModuleV2Signature,
CastContextFetchSignature,
PrimeSignature,
)
) {
override fun execute(data: BytecodeData): PatchResult {
// smali patches
disablePlayServiceChecks()
disablePlayServiceChecksAndFixCastIssues()
data.classes.forEach { classDef ->
var proxiedClass: MutableClass? = null

classDef.methods.forEach methodLoop@{ method ->
val implementation = method.implementation ?: return@methodLoop

var proxiedImplementation: MutableMethodImplementation? = null

// disable cast button since it is unsupported by microg and causes battery issues
// the code is here instead of the fixCastIssues method because we do not need a signature this way
if (classDef.type.endsWith("MediaRouteButton;") && method.name == "setVisibility") {
proxiedClass = data.proxy(classDef).resolve()
proxiedImplementation = proxiedClass!!.methods.first { it.name == "setVisibility" }.implementation

proxiedImplementation!!.replaceInstruction(
0, BuilderInstruction21s(Opcode.CONST_16, 1, 8) // 8 == HIDDEN
)
}

implementation.instructions.forEachIndexed { i, instruction ->
if (instruction.opcode != Opcode.CONST_STRING) return@forEachIndexed

Expand Down Expand Up @@ -111,10 +126,11 @@ class MicroGBytecodePatch : BytecodePatch(
}
}

signatures.last()
return PatchResultSuccess()
}

private fun disablePlayServiceChecks() {
private fun disablePlayServiceChecksAndFixCastIssues() {
for (i in 0 until signatures.count() - 1) {
val result = signatures.elementAt(i).result!!
val stringInstructions = when (result.immutableMethod.returnType.first()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package app.revanced.patches.youtube.misc.microg.signatures

import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.signature.implementation.method.MethodSignature
import app.revanced.patcher.signature.implementation.method.annotation.DirectPatternScanMethod
import app.revanced.patcher.signature.implementation.method.annotation.MatchingMethod
import app.revanced.patches.youtube.misc.microg.annotations.MicroGPatchCompatibility

@Name("cast-context-fetch-signature")
@MatchingMethod(
"Lvvz;", "a"
)
@DirectPatternScanMethod
@MicroGPatchCompatibility
@Version("0.0.1")
object CastContextFetchSignature : MethodSignature(
null, null, null, null,
listOf("Error fetching CastContext.")
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package app.revanced.patches.youtube.misc.microg.signatures

import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.signature.implementation.method.MethodSignature
import app.revanced.patcher.signature.implementation.method.annotation.DirectPatternScanMethod
import app.revanced.patcher.signature.implementation.method.annotation.MatchingMethod
import app.revanced.patches.youtube.misc.microg.annotations.MicroGPatchCompatibility

@Name("cast-module-signature")
@MatchingMethod(
"Llqh;", "c"
)
@DirectPatternScanMethod
@MicroGPatchCompatibility
@Version("0.0.1")
object CastDynamiteModuleSignature : MethodSignature(
null, null, null, null,
listOf("com.google.android.gms.cast.framework.internal.CastDynamiteModuleImpl")
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package app.revanced.patches.youtube.misc.microg.signatures

import app.revanced.patcher.annotation.Name
import app.revanced.patcher.annotation.Version
import app.revanced.patcher.signature.implementation.method.MethodSignature
import app.revanced.patcher.signature.implementation.method.annotation.DirectPatternScanMethod
import app.revanced.patcher.signature.implementation.method.annotation.MatchingMethod
import app.revanced.patches.youtube.misc.microg.annotations.MicroGPatchCompatibility

@Name("cast-context-fetch-signature")
@MatchingMethod(
"Lmcf;", "c"
)
@DirectPatternScanMethod
@MicroGPatchCompatibility
@Version("0.0.1")
object CastDynamiteModuleV2Signature : MethodSignature(
null, null, null, null,
listOf("Failed to load module via V2: ")
)

0 comments on commit dd8b01a

Please sign in to comment.