From 97217929af83684a2dadba7303f307f06bffaab6 Mon Sep 17 00:00:00 2001 From: OxrxL <108184954+OxrxL@users.noreply.github.com> Date: Wed, 9 Nov 2022 06:21:56 +0100 Subject: [PATCH] refactor(youtube/general-ads-patch): squash extension classes (#1006) --- .../app/revanced/extensions/Extensions.kt | 11 ---- .../bytecode/extensions/MethodExtensions.kt | 45 ---------------- .../bytecode/patch/GeneralBytecodeAdsPatch.kt | 54 +++++++++++++++++-- .../ad/general/bytecode/utils/MethodUtils.kt | 20 ------- 4 files changed, 49 insertions(+), 81 deletions(-) delete mode 100644 src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/extensions/MethodExtensions.kt delete mode 100644 src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/utils/MethodUtils.kt diff --git a/src/main/kotlin/app/revanced/extensions/Extensions.kt b/src/main/kotlin/app/revanced/extensions/Extensions.kt index 5b31631a..6833bdea 100644 --- a/src/main/kotlin/app/revanced/extensions/Extensions.kt +++ b/src/main/kotlin/app/revanced/extensions/Extensions.kt @@ -18,17 +18,6 @@ import org.jf.dexlib2.immutable.reference.ImmutableMethodReference import org.w3c.dom.Node import java.nio.file.Files -// TODO: this method does not make sense here -internal fun MutableMethodImplementation.injectHideCall( - index: Int, - register: Int -) { - this.addInstruction( - index, - "invoke-static { v$register }, Lapp/revanced/integrations/patches/HideHomeAdsPatch;->HideHomeAds(Landroid/view/View;)V".toInstruction() - ) -} - /** * traverse the class hierarchy starting from the given root class * diff --git a/src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/extensions/MethodExtensions.kt b/src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/extensions/MethodExtensions.kt deleted file mode 100644 index b06e5af8..00000000 --- a/src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/extensions/MethodExtensions.kt +++ /dev/null @@ -1,45 +0,0 @@ -package app.revanced.patches.youtube.ad.general.bytecode.extensions - -import app.revanced.patcher.extensions.softCompareTo -import app.revanced.patcher.patch.PatchResultError -import app.revanced.patcher.util.proxy.mutableTypes.MutableClass -import org.jf.dexlib2.iface.Method -import org.jf.dexlib2.iface.instruction.Instruction -import org.jf.dexlib2.iface.instruction.ReferenceInstruction -import org.jf.dexlib2.iface.reference.FieldReference -import org.jf.dexlib2.iface.reference.MethodReference -import org.jf.dexlib2.iface.reference.Reference -import org.jf.dexlib2.immutable.reference.ImmutableMethodReference - -internal object MethodExtensions { - internal fun MutableClass.findMutableMethodOf( - method: Method - ) = this.methods.first { - it.softCompareTo( - ImmutableMethodReference( - method.definingClass, method.name, method.parameters, method.returnType - ) - ) - } - - internal inline fun Instruction.toDescriptor(): String { - val reference = (this as ReferenceInstruction).reference - return when (T::class) { - MethodReference::class -> { - val methodReference = reference as MethodReference - "${methodReference.definingClass}->${methodReference.name}(${ - methodReference.parameterTypes.joinToString( - "" - ) { it } - })${methodReference.returnType}" - } - - FieldReference::class -> { - val fieldReference = reference as FieldReference - "${fieldReference.definingClass}->${fieldReference.name}:${fieldReference.type}" - } - - else -> throw PatchResultError("Unsupported reference type") - } - } -} \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/patch/GeneralBytecodeAdsPatch.kt b/src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/patch/GeneralBytecodeAdsPatch.kt index 10366dd0..6df993f9 100644 --- a/src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/patch/GeneralBytecodeAdsPatch.kt +++ b/src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/patch/GeneralBytecodeAdsPatch.kt @@ -1,12 +1,12 @@ package app.revanced.patches.youtube.ad.general.bytecode.patch -import app.revanced.extensions.injectHideCall 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.extensions.instruction +import app.revanced.patcher.extensions.softCompareTo import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint.Companion.resolve import app.revanced.patcher.patch.BytecodePatch @@ -18,9 +18,8 @@ import app.revanced.patcher.patch.annotations.Patch import app.revanced.patcher.util.proxy.mutableTypes.MutableClass import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod import app.revanced.patcher.util.smali.ExternalLabel +import app.revanced.patcher.util.smali.toInstruction import app.revanced.patches.youtube.ad.general.annotation.GeneralAdsCompatibility -import app.revanced.patches.youtube.ad.general.bytecode.extensions.MethodExtensions.findMutableMethodOf -import app.revanced.patches.youtube.ad.general.bytecode.extensions.MethodExtensions.toDescriptor import app.revanced.patches.youtube.ad.general.resource.patch.GeneralResourceAdsPatch import app.revanced.patches.youtube.misc.integrations.patch.IntegrationsPatch import app.revanced.patches.youtube.misc.mapping.patch.ResourceMappingResourcePatch @@ -28,13 +27,18 @@ import app.revanced.patches.youtube.misc.settings.bytecode.patch.SettingsPatch import app.revanced.patches.youtube.misc.settings.framework.components.impl.StringResource import app.revanced.patches.youtube.misc.settings.framework.components.impl.SwitchPreference import org.jf.dexlib2.Opcode +import org.jf.dexlib2.builder.MutableMethodImplementation import org.jf.dexlib2.builder.instruction.BuilderInstruction10x -import org.jf.dexlib2.dexbacked.instruction.DexBackedInstruction21c +import org.jf.dexlib2.iface.Method +import org.jf.dexlib2.iface.instruction.Instruction import org.jf.dexlib2.iface.instruction.OneRegisterInstruction +import org.jf.dexlib2.iface.instruction.ReferenceInstruction import org.jf.dexlib2.iface.instruction.formats.* import org.jf.dexlib2.iface.reference.FieldReference import org.jf.dexlib2.iface.reference.MethodReference +import org.jf.dexlib2.iface.reference.Reference import org.jf.dexlib2.iface.reference.StringReference +import org.jf.dexlib2.immutable.reference.ImmutableMethodReference @Patch @DependsOn([ResourceMappingResourcePatch::class, IntegrationsPatch::class, SettingsPatch::class, GeneralResourceAdsPatch::class]) @@ -205,7 +209,6 @@ class GeneralBytecodeAdsPatch : BytecodePatch() { // insert hide call to hide the view corresponding to the resource val viewRegister = (invokeInstruction as Instruction35c).registerC mutableMethod!!.implementation!!.injectHideCall(insertIndex, viewRegister) - } resourceIds[1] -> { // reel ads @@ -312,4 +315,45 @@ class GeneralBytecodeAdsPatch : BytecodePatch() { instruction.opcode == Opcode.CONST && (instruction as Instruction31i).narrowLiteral == lithoConstant } ?: false } + + private fun MutableClass.findMutableMethodOf( + method: Method + ) = this.methods.first { + it.softCompareTo( + ImmutableMethodReference( + method.definingClass, method.name, method.parameters, method.returnType + ) + ) + } + + private inline fun Instruction.toDescriptor(): String { + val reference = (this as ReferenceInstruction).reference + return when (T::class) { + MethodReference::class -> { + val methodReference = reference as MethodReference + "${methodReference.definingClass}->${methodReference.name}(${ + methodReference.parameterTypes.joinToString( + "" + ) { it } + })${methodReference.returnType}" + } + + FieldReference::class -> { + val fieldReference = reference as FieldReference + "${fieldReference.definingClass}->${fieldReference.name}:${fieldReference.type}" + } + + else -> throw PatchResultError("Unsupported reference type") + } + } + + private fun MutableMethodImplementation.injectHideCall( + index: Int, + register: Int + ) { + this.addInstruction( + index, + "invoke-static { v$register }, Lapp/revanced/integrations/patches/HideHomeAdsPatch;->HideHomeAds(Landroid/view/View;)V".toInstruction() + ) + } } diff --git a/src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/utils/MethodUtils.kt b/src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/utils/MethodUtils.kt deleted file mode 100644 index f9b0c00a..00000000 --- a/src/main/kotlin/app/revanced/patches/youtube/ad/general/bytecode/utils/MethodUtils.kt +++ /dev/null @@ -1,20 +0,0 @@ -package app.revanced.patches.youtube.ad.general.bytecode.utils - -import app.revanced.patcher.extensions.or -import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod.Companion.toMutable -import org.jf.dexlib2.AccessFlags -import org.jf.dexlib2.iface.MethodImplementation -import org.jf.dexlib2.immutable.ImmutableMethod -import org.jf.dexlib2.immutable.ImmutableMethodParameter - -internal object MethodUtils { - internal fun createMutableMethod( - definingClass: String, name: String, returnType: String, parameter: String, implementation: MethodImplementation - ) = ImmutableMethod( - definingClass, name, listOf( - ImmutableMethodParameter( - parameter, null, null - ) - ), returnType, AccessFlags.PRIVATE or AccessFlags.STATIC, null, null, implementation - ).toMutable() -} \ No newline at end of file