Skip to content

Commit 46b4398

Browse files
authored
feat(Crunchyroll): Add Hide ads patch (#5201)
1 parent 91842dc commit 46b4398

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

patches/api/patches.api

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ public final class app/revanced/patches/cieid/restrictions/root/BypassRootChecks
160160
public static final fun getBypassRootChecksPatch ()Lapp/revanced/patcher/patch/BytecodePatch;
161161
}
162162

163+
public final class app/revanced/patches/crunchyroll/ads/HideAdsPatchKt {
164+
public static final fun getHideAdsPatch ()Lapp/revanced/patcher/patch/BytecodePatch;
165+
}
166+
163167
public final class app/revanced/patches/duolingo/ad/DisableAdsPatchKt {
164168
public static final fun getDisableAdsPatch ()Lapp/revanced/patcher/patch/BytecodePatch;
165169
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package app.revanced.patches.crunchyroll.ads
2+
3+
import app.revanced.patcher.fingerprint
4+
5+
internal val videoUrlReadyToStringFingerprint = fingerprint {
6+
strings("VideoUrlReady(url=", ", enableAds=")
7+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package app.revanced.patches.crunchyroll.ads
2+
3+
import app.revanced.patcher.extensions.InstructionExtensions.addInstructions
4+
import app.revanced.patcher.extensions.InstructionExtensions.getInstruction
5+
import app.revanced.patcher.extensions.InstructionExtensions.instructions
6+
import app.revanced.patcher.patch.bytecodePatch
7+
import app.revanced.util.getReference
8+
import app.revanced.util.indexOfFirstInstruction
9+
import app.revanced.util.removeFlags
10+
import com.android.tools.smali.dexlib2.AccessFlags
11+
import com.android.tools.smali.dexlib2.Opcode
12+
import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction
13+
import com.android.tools.smali.dexlib2.iface.reference.FieldReference
14+
15+
@Suppress("unused")
16+
val hideAdsPatch = bytecodePatch(
17+
name = "Hide Ads"
18+
) {
19+
compatibleWith("com.crunchyroll.crunchyroid")
20+
21+
execute {
22+
// Get obfuscated "enableAds" field from toString method.
23+
val enableAdsField = videoUrlReadyToStringFingerprint.let {
24+
val strIndex = videoUrlReadyToStringFingerprint.stringMatches!!.last().index
25+
val fieldIndex = it.method.indexOfFirstInstruction(strIndex, Opcode.IGET_BOOLEAN)
26+
it.method.getInstruction<ReferenceInstruction>(fieldIndex).getReference<FieldReference>()!!
27+
}
28+
29+
// Remove final access flag on field.
30+
videoUrlReadyToStringFingerprint.classDef.fields
31+
.first { it.name == enableAdsField.name }
32+
.removeFlags(AccessFlags.FINAL)
33+
34+
// Override enableAds field in non-default constructor.
35+
val constructor = videoUrlReadyToStringFingerprint.classDef.methods.first {
36+
AccessFlags.CONSTRUCTOR.isSet(it.accessFlags) && it.parameters.isNotEmpty()
37+
}
38+
constructor.addInstructions(
39+
constructor.instructions.count() - 1,
40+
"""
41+
move-object/from16 v0, p0
42+
const/4 v1, 0x0
43+
iput-boolean v1, v0, $enableAdsField
44+
""")
45+
}
46+
}

patches/src/main/kotlin/app/revanced/util/BytecodeUtils.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import app.revanced.patcher.extensions.InstructionExtensions.removeInstruction
1010
import app.revanced.patcher.patch.BytecodePatchContext
1111
import app.revanced.patcher.patch.PatchException
1212
import app.revanced.patcher.util.proxy.mutableTypes.MutableClass
13+
import app.revanced.patcher.util.proxy.mutableTypes.MutableField
1314
import app.revanced.patcher.util.proxy.mutableTypes.MutableField.Companion.toMutable
1415
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
1516
import app.revanced.patcher.util.smali.ExternalLabel
@@ -1021,6 +1022,14 @@ private fun MutableMethod.overrideReturnValue(value: String, returnLate: Boolean
10211022
}
10221023
}
10231024

1025+
/**
1026+
* Remove the given AccessFlags from the field.
1027+
*/
1028+
internal fun MutableField.removeFlags(vararg flags: AccessFlags) {
1029+
val bitField = flags.map { it.value }.reduce { acc, flag -> acc and flag }
1030+
this.accessFlags = this.accessFlags and bitField.inv()
1031+
}
1032+
10241033
internal fun BytecodePatchContext.addStaticFieldToExtension(
10251034
className: String,
10261035
methodName: String,

0 commit comments

Comments
 (0)