forked from ReVanced/revanced-patches-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(twitter): make
hide-promoted-ads
patch compatible with any version
Signed-off-by: oSumAtrIX <[email protected]>
- Loading branch information
Showing
9 changed files
with
182 additions
and
98 deletions.
There are no files selected for viewing
18 changes: 0 additions & 18 deletions
18
...p/revanced/patches/twitter/ad/timeline/fingerprints/TimelineTweetJsonParserFingerprint.kt
This file was deleted.
Oops, something went wrong.
77 changes: 0 additions & 77 deletions
77
src/main/kotlin/app/revanced/patches/twitter/ad/timeline/patch/TimelineAdsPatch.kt
This file was deleted.
Oops, something went wrong.
9 changes: 9 additions & 0 deletions
9
...tlin/app/revanced/patches/twitter/misc/hook/json/fingerprints/JsonHookPatchFingerprint.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package app.revanced.patches.twitter.misc.hook.json.fingerprints | ||
|
||
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint | ||
import org.jf.dexlib2.Opcode | ||
|
||
object JsonHookPatchFingerprint : MethodFingerprint( | ||
customFingerprint = { methodDef -> methodDef.name == "<clinit>" }, | ||
opcodes = listOf(Opcode.IGET_OBJECT) | ||
) |
10 changes: 10 additions & 0 deletions
10
...in/app/revanced/patches/twitter/misc/hook/json/fingerprints/JsonInputStreamFingerprint.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package app.revanced.patches.twitter.misc.hook.json.fingerprints | ||
|
||
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint | ||
|
||
object JsonInputStreamFingerprint : MethodFingerprint( | ||
customFingerprint = { methodDef -> | ||
if (methodDef.parameterTypes.size == 0) false | ||
else methodDef.parameterTypes.first() == "Ljava/io/InputStream;" | ||
} | ||
) |
7 changes: 7 additions & 0 deletions
7
...kotlin/app/revanced/patches/twitter/misc/hook/json/fingerprints/LoganSquareFingerprint.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package app.revanced.patches.twitter.misc.hook.json.fingerprints | ||
|
||
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprint | ||
|
||
object LoganSquareFingerprint : MethodFingerprint( | ||
customFingerprint = { methodDef -> methodDef.definingClass.endsWith("LoganSquare;") } | ||
) |
114 changes: 114 additions & 0 deletions
114
src/main/kotlin/app/revanced/patches/twitter/misc/hook/json/patch/JsonHookPatch.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package app.revanced.patches.twitter.misc.hook.json.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.fingerprint.method.impl.MethodFingerprint.Companion.resolve | ||
import app.revanced.patcher.fingerprint.method.impl.MethodFingerprintResult | ||
import app.revanced.patcher.patch.* | ||
import app.revanced.patches.twitter.misc.hook.json.fingerprints.JsonHookPatchFingerprint | ||
import app.revanced.patches.twitter.misc.hook.json.fingerprints.JsonInputStreamFingerprint | ||
import app.revanced.patches.twitter.misc.hook.json.fingerprints.LoganSquareFingerprint | ||
import java.io.InvalidClassException | ||
|
||
@Name("json-hook") | ||
@Description("Hooks the stream which reads JSON responses.") | ||
@Version("0.0.1") | ||
class JsonHookPatch : BytecodePatch( | ||
listOf(LoganSquareFingerprint) | ||
) { | ||
override fun execute(context: BytecodeContext): PatchResult { | ||
// Make sure the integrations are present. | ||
val jsonHookPatch = context.findClass { it.type == JSON_HOOK_PATCH_CLASS_DESCRIPTOR } | ||
?: return PatchResultError("Could not find integrations.") | ||
|
||
// Allow patch to inject hooks into the patches integrations. | ||
jsonHookPatchFingerprintResult = JsonHookPatchFingerprint.also { | ||
it.resolve(context, jsonHookPatch.immutableClass) | ||
}.result ?: return PatchResultError("Unexpected integrations.") | ||
|
||
// Conveniently find the type to hook a method in, via a named field. | ||
val jsonFactory = LoganSquareFingerprint.result | ||
?.classDef | ||
?.fields | ||
?.firstOrNull { it.name == "JSON_FACTORY" } | ||
?.type | ||
.let { type -> | ||
context.findClass { it.type == type }?.mutableClass | ||
} ?: return PatchResultError("Could not find required class.") | ||
|
||
// Hook the methods first parameter. | ||
JsonInputStreamFingerprint | ||
.also { it.resolve(context, jsonFactory) } | ||
.result | ||
?.mutableMethod | ||
?.addInstructions( | ||
0, | ||
""" | ||
invoke-static { p1 }, $JSON_HOOK_PATCH_CLASS_DESCRIPTOR->parseJsonHook(Ljava/io/InputStream;)Ljava/io/InputStream; | ||
move-result-object p1 | ||
""" | ||
) ?: return PatchResultError("Could not find method to hook.") | ||
|
||
return PatchResultSuccess() | ||
} | ||
|
||
/** | ||
* Create a hook class. | ||
* The class has to extend on **JsonHook**. | ||
* The class has to be a Kotlin object class, or at least have an INSTANCE field of itself. | ||
* | ||
* @param context The [BytecodeContext] of the current patch. | ||
* @param descriptor The class descriptor of the hook. | ||
*/ | ||
internal class Hook(context: BytecodeContext, private val descriptor: String) { | ||
private var added = false | ||
|
||
/** | ||
* Add the hook. | ||
*/ | ||
internal fun add() { | ||
if (added) return | ||
|
||
jsonHookPatchFingerprintResult.apply { | ||
mutableMethod.apply { | ||
addInstructions( | ||
scanResult.patternScanResult!!.startIndex, | ||
""" | ||
sget-object v1, $descriptor->INSTANCE:$descriptor | ||
invoke-virtual {v0, v1}, Lkotlin/collections/builders/ListBuilder;->add(Ljava/lang/Object;)Z | ||
""" | ||
) | ||
} | ||
} | ||
|
||
added = true | ||
} | ||
|
||
init { | ||
context.findClass { it.type == descriptor }?.let { | ||
it.mutableClass.also { classDef -> | ||
if ( | ||
classDef.superclass != JSON_HOOK_CLASS_DESCRIPTOR || | ||
!classDef.fields.any { field -> field.name == "INSTANCE" } | ||
) throw InvalidClassException(classDef.type, "Not a hook class") | ||
|
||
} | ||
} ?: throw ClassNotFoundException("Failed to find hook class") | ||
} | ||
} | ||
|
||
private companion object { | ||
const val JSON_HOOK_CLASS_NAMESPACE = "app/revanced/twitter/patches/hook/json" | ||
|
||
const val JSON_HOOK_PATCH_CLASS_DESCRIPTOR = "L$JSON_HOOK_CLASS_NAMESPACE/JsonHookPatch;" | ||
|
||
const val BASE_PATCH_CLASS_NAME = "BaseJsonHook" | ||
|
||
const val JSON_HOOK_CLASS_DESCRIPTOR = "L$JSON_HOOK_CLASS_NAMESPACE/$BASE_PATCH_CLASS_NAME;" | ||
|
||
private lateinit var jsonHookPatchFingerprintResult: MethodFingerprintResult | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/app/revanced/patches/twitter/misc/hook/patch/BaseHookPatchPatch.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package app.revanced.patches.twitter.misc.hook.patch | ||
|
||
import app.revanced.patcher.data.BytecodeContext | ||
import app.revanced.patcher.patch.BytecodePatch | ||
import app.revanced.patcher.patch.PatchResultError | ||
import app.revanced.patcher.patch.PatchResultSuccess | ||
import app.revanced.patcher.patch.annotations.DependsOn | ||
import app.revanced.patches.twitter.misc.hook.json.patch.JsonHookPatch | ||
|
||
@DependsOn([JsonHookPatch::class]) | ||
abstract class BaseHookPatchPatch(private val hookClassDescriptor: String) : BytecodePatch() { | ||
override fun execute(context: BytecodeContext) = try { | ||
PatchResultSuccess().also { JsonHookPatch.Hook(context, hookClassDescriptor).add() } | ||
} catch (ex: Exception) { | ||
PatchResultError(ex) | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
...e/annotations/TimelineAdsCompatibility.kt → ...h/ads/annotations/HideAdsCompatibility.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
package app.revanced.patches.twitter.ad.timeline.annotations | ||
package app.revanced.patches.twitter.misc.hook.patch.ads.annotations | ||
|
||
import app.revanced.patcher.annotation.Compatibility | ||
import app.revanced.patcher.annotation.Package | ||
|
||
@Compatibility( | ||
[Package( | ||
"com.twitter.android", arrayOf("9.65.3-release.0") | ||
"com.twitter.android" | ||
)] | ||
) | ||
@Target(AnnotationTarget.CLASS) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
internal annotation class TimelineAdsCompatibility | ||
internal annotation class HideAdsCompatibility |
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/app/revanced/patches/twitter/misc/hook/patch/ads/patch/HideAdsPatch.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package app.revanced.patches.twitter.misc.hook.patch.ads.patch | ||
|
||
import app.revanced.patcher.annotation.Description | ||
import app.revanced.patcher.annotation.Name | ||
import app.revanced.patcher.annotation.Version | ||
import app.revanced.patcher.patch.annotations.DependsOn | ||
import app.revanced.patcher.patch.annotations.Patch | ||
import app.revanced.patches.twitter.misc.hook.json.patch.JsonHookPatch | ||
import app.revanced.patches.twitter.misc.hook.patch.BaseHookPatchPatch | ||
import app.revanced.patches.twitter.misc.hook.patch.ads.annotations.HideAdsCompatibility | ||
|
||
@Patch | ||
@Name("hide-ads") | ||
@DependsOn([JsonHookPatch::class]) | ||
@Description("Hides ads.") | ||
@HideAdsCompatibility | ||
@Version("0.0.1") | ||
class HideAdsPatch : BaseHookPatchPatch(HOOK_CLASS_DESCRIPTOR) { | ||
private companion object { | ||
const val HOOK_CLASS_DESCRIPTOR = "Lapp/revanced/twitter/patches/hook/patch/ads/AdsHook;" | ||
} | ||
} |