From ba9cbaf127c4b24890535997ce28882458e03e78 Mon Sep 17 00:00:00 2001 From: Canny <94744045+Canny1913@users.noreply.github.com> Date: Sat, 22 Oct 2022 13:45:10 +0300 Subject: [PATCH] fix(youtube/integrations): set context for remaining activities (#828) --- .../fingerprints/ServiceFingerprint.kt | 13 +++++ .../StandalonePlayerFingerprint.kt | 16 ++++++ .../integrations/patch/IntegrationsPatch.kt | 54 +++++++++---------- 3 files changed, 53 insertions(+), 30 deletions(-) create mode 100644 src/main/kotlin/app/revanced/patches/youtube/misc/integrations/fingerprints/ServiceFingerprint.kt create mode 100644 src/main/kotlin/app/revanced/patches/youtube/misc/integrations/fingerprints/StandalonePlayerFingerprint.kt diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/integrations/fingerprints/ServiceFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/integrations/fingerprints/ServiceFingerprint.kt new file mode 100644 index 000000000..0d1312bed --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/integrations/fingerprints/ServiceFingerprint.kt @@ -0,0 +1,13 @@ +package app.revanced.patches.youtube.misc.integrations.fingerprints + +import app.revanced.patcher.annotation.Name +import app.revanced.patcher.annotation.Version +import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint +import app.revanced.patches.youtube.misc.integrations.annotations.IntegrationsCompatibility + +@Name("service-fingerprint") +@IntegrationsCompatibility +@Version("0.0.1") +object ServiceFingerprint : MethodFingerprint( + customFingerprint = { methodDef -> methodDef.definingClass.endsWith("ApiPlayerService;") && methodDef.name == "" } +) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/integrations/fingerprints/StandalonePlayerFingerprint.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/integrations/fingerprints/StandalonePlayerFingerprint.kt new file mode 100644 index 000000000..5bd664de0 --- /dev/null +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/integrations/fingerprints/StandalonePlayerFingerprint.kt @@ -0,0 +1,16 @@ +package app.revanced.patches.youtube.misc.integrations.fingerprints + +import app.revanced.patcher.annotation.Name +import app.revanced.patcher.annotation.Version +import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint +import app.revanced.patches.youtube.misc.integrations.annotations.IntegrationsCompatibility + +@Name("standalone-player-fingerprint") +@IntegrationsCompatibility +@Version("0.0.1") +object StandalonePlayerFingerprint : MethodFingerprint( + strings = listOf( + "Invalid PlaybackStartDescriptor. Returning the instance itself.", + "com.google.android.music", + ) +) \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/youtube/misc/integrations/patch/IntegrationsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/misc/integrations/patch/IntegrationsPatch.kt index 6f4ddd164..f7eaff302 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/misc/integrations/patch/IntegrationsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/misc/integrations/patch/IntegrationsPatch.kt @@ -4,6 +4,7 @@ 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.MethodFingerprintExtensions.name import app.revanced.patcher.extensions.addInstruction import app.revanced.patcher.extensions.or import app.revanced.patcher.patch.BytecodePatch @@ -14,6 +15,8 @@ import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod.Companion.toMu import app.revanced.patcher.util.smali.toInstructions import app.revanced.patches.youtube.misc.integrations.annotations.IntegrationsCompatibility import app.revanced.patches.youtube.misc.integrations.fingerprints.InitFingerprint +import app.revanced.patches.youtube.misc.integrations.fingerprints.ServiceFingerprint +import app.revanced.patches.youtube.misc.integrations.fingerprints.StandalonePlayerFingerprint import org.jf.dexlib2.AccessFlags import org.jf.dexlib2.immutable.ImmutableMethod import org.jf.dexlib2.immutable.ImmutableMethodImplementation @@ -24,42 +27,33 @@ import org.jf.dexlib2.immutable.ImmutableMethodImplementation @Version("0.0.1") class IntegrationsPatch : BytecodePatch( listOf( - InitFingerprint + InitFingerprint, StandalonePlayerFingerprint, ServiceFingerprint ) ) { + companion object { + private const val INTEGRATIONS_DESCRIPTOR = "Lapp/revanced/integrations/utils/ReVancedUtils;" + } + override fun execute(context: BytecodeContext): PatchResult { - if (context.findClass("Lapp/revanced/integrations/utils/ReVancedUtils") == null) - return PatchResultError("Integrations have not been merged yet. This patch can not succeed without the integrations.") + if (context.findClass(INTEGRATIONS_DESCRIPTOR) == null) + return PatchResultError("Integrations have not been merged yet. This patch can not succeed without merging the integrations.") - val result = InitFingerprint.result!! + arrayOf(InitFingerprint, StandalonePlayerFingerprint, ServiceFingerprint).map { + it to (it.result ?: return PatchResultError("${it.name} failed to resolve")) + }.forEach { (fingerprint, result) -> + with(result.mutableMethod) { + // parameter which holds the context + val contextParameter = if (fingerprint == ServiceFingerprint) parameters.size else 1 + // register which holds the context + val contextRegister = implementation!!.registerCount - contextParameter - val method = result.mutableMethod - val implementation = method.implementation!! - val count = implementation.registerCount - 1 - - method.addInstruction( - 0, "sput-object v$count, Lapp/revanced/integrations/utils/ReVancedUtils;->context:Landroid/content/Context;" - ) - - val classDef = result.mutableClass - classDef.methods.add( - ImmutableMethod( - classDef.type, - "getAppContext", - null, - "Landroid/content/Context;", - AccessFlags.PUBLIC or AccessFlags.STATIC, - null, - null, - ImmutableMethodImplementation( - 1, """ - invoke-static { }, Lapp/revanced/integrations/utils/ReVancedUtils;->getAppContext()Landroid/content/Context; - move-result-object v0 - return-object v0 - """.toInstructions(), null, null + addInstruction( + 0, + "sput-object v$contextRegister, $INTEGRATIONS_DESCRIPTOR->context:Landroid/content/Context;" ) - ).toMutable() - ) + } + } + return PatchResultSuccess() } } \ No newline at end of file