1
mirror of https://github.com/revanced/revanced-patcher synced 2025-10-08 13:32:13 +02:00

Compare commits

..

1 Commits

Author SHA1 Message Date
oSumAtrIX
1f0eab36d9 fix build 2024-10-27 04:32:40 +01:00
15 changed files with 388 additions and 464 deletions

View File

@@ -1,4 +1,7 @@
public final class app/revanced/patcher/Fingerprint { public final class app/revanced/patcher/Fingerprint {
public final fun getMatch ()Lapp/revanced/patcher/Match;
public final fun match (Lapp/revanced/patcher/patch/BytecodePatchContext;Lcom/android/tools/smali/dexlib2/iface/ClassDef;)Z
public final fun match (Lapp/revanced/patcher/patch/BytecodePatchContext;Lcom/android/tools/smali/dexlib2/iface/Method;)Z
} }
public final class app/revanced/patcher/FingerprintBuilder { public final class app/revanced/patcher/FingerprintBuilder {
@@ -15,17 +18,20 @@ public final class app/revanced/patcher/FingerprintBuilder {
public final class app/revanced/patcher/FingerprintKt { public final class app/revanced/patcher/FingerprintKt {
public static final fun fingerprint (ILkotlin/jvm/functions/Function1;)Lapp/revanced/patcher/Fingerprint; public static final fun fingerprint (ILkotlin/jvm/functions/Function1;)Lapp/revanced/patcher/Fingerprint;
public static final fun fingerprint (Lapp/revanced/patcher/patch/BytecodePatchBuilder;ILkotlin/jvm/functions/Function1;)Lapp/revanced/patcher/patch/BytecodePatchBuilder$InvokedFingerprint;
public static synthetic fun fingerprint$default (ILkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lapp/revanced/patcher/Fingerprint; public static synthetic fun fingerprint$default (ILkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lapp/revanced/patcher/Fingerprint;
public static synthetic fun fingerprint$default (Lapp/revanced/patcher/patch/BytecodePatchBuilder;ILkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lapp/revanced/patcher/patch/BytecodePatchBuilder$InvokedFingerprint;
} }
public abstract interface annotation class app/revanced/patcher/InternalApi : java/lang/annotation/Annotation { public abstract interface annotation class app/revanced/patcher/InternalApi : java/lang/annotation/Annotation {
} }
public final class app/revanced/patcher/Match { public final class app/revanced/patcher/Match {
public final fun getClassDef ()Lapp/revanced/patcher/util/proxy/mutableTypes/MutableClass; public fun <init> (Lcom/android/tools/smali/dexlib2/iface/Method;Lcom/android/tools/smali/dexlib2/iface/ClassDef;Lapp/revanced/patcher/Match$PatternMatch;Ljava/util/List;Lapp/revanced/patcher/patch/BytecodePatchContext;)V
public final fun getMethod ()Lapp/revanced/patcher/util/proxy/mutableTypes/MutableMethod; public final fun getClassDef ()Lcom/android/tools/smali/dexlib2/iface/ClassDef;
public final fun getOriginalClassDef ()Lcom/android/tools/smali/dexlib2/iface/ClassDef; public final fun getMethod ()Lcom/android/tools/smali/dexlib2/iface/Method;
public final fun getOriginalMethod ()Lcom/android/tools/smali/dexlib2/iface/Method; public final fun getMutableClass ()Lapp/revanced/patcher/util/proxy/mutableTypes/MutableClass;
public final fun getMutableMethod ()Lapp/revanced/patcher/util/proxy/mutableTypes/MutableMethod;
public final fun getPatternMatch ()Lapp/revanced/patcher/Match$PatternMatch; public final fun getPatternMatch ()Lapp/revanced/patcher/Match$PatternMatch;
public final fun getStringMatches ()Ljava/util/List; public final fun getStringMatches ()Ljava/util/List;
} }
@@ -57,8 +63,8 @@ public final class app/revanced/patcher/Patcher : java/io/Closeable {
} }
public final class app/revanced/patcher/PatcherConfig { public final class app/revanced/patcher/PatcherConfig {
public fun <init> (Ljava/io/File;Ljava/io/File;Ljava/lang/String;Ljava/lang/String;)V public fun <init> (Ljava/io/File;Ljava/io/File;Ljava/lang/String;Ljava/lang/String;Z)V
public synthetic fun <init> (Ljava/io/File;Ljava/io/File;Ljava/lang/String;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V public synthetic fun <init> (Ljava/io/File;Ljava/io/File;Ljava/lang/String;Ljava/lang/String;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V
} }
public final class app/revanced/patcher/PatcherContext : java/io/Closeable { public final class app/revanced/patcher/PatcherContext : java/io/Closeable {
@@ -129,28 +135,31 @@ public final class app/revanced/patcher/extensions/InstructionExtensions {
} }
public final class app/revanced/patcher/patch/BytecodePatch : app/revanced/patcher/patch/Patch { public final class app/revanced/patcher/patch/BytecodePatch : app/revanced/patcher/patch/Patch {
public final fun getExtensionInputStream ()Ljava/util/function/Supplier; public final fun getExtension ()Ljava/io/InputStream;
public final fun getFingerprints ()Ljava/util/Set;
public fun toString ()Ljava/lang/String; public fun toString ()Ljava/lang/String;
} }
public final class app/revanced/patcher/patch/BytecodePatchBuilder : app/revanced/patcher/patch/PatchBuilder { public final class app/revanced/patcher/patch/BytecodePatchBuilder : app/revanced/patcher/patch/PatchBuilder {
public synthetic fun build$revanced_patcher ()Lapp/revanced/patcher/patch/Patch; public synthetic fun build$revanced_patcher ()Lapp/revanced/patcher/patch/Patch;
public final fun extendWith (Ljava/lang/String;)Lapp/revanced/patcher/patch/BytecodePatchBuilder; public final fun extendWith (Ljava/lang/String;)Lapp/revanced/patcher/patch/BytecodePatchBuilder;
public final fun getExtensionInputStream ()Ljava/util/function/Supplier; public final fun getExtension ()Ljava/io/InputStream;
public final fun setExtensionInputStream (Ljava/util/function/Supplier;)V public final fun invoke (Lapp/revanced/patcher/Fingerprint;)Lapp/revanced/patcher/patch/BytecodePatchBuilder$InvokedFingerprint;
public final fun setExtension (Ljava/io/InputStream;)V
}
public final class app/revanced/patcher/patch/BytecodePatchBuilder$InvokedFingerprint {
public final fun getValue (Ljava/lang/Void;Lkotlin/reflect/KProperty;)Lapp/revanced/patcher/Match;
} }
public final class app/revanced/patcher/patch/BytecodePatchContext : app/revanced/patcher/patch/PatchContext, java/io/Closeable { public final class app/revanced/patcher/patch/BytecodePatchContext : app/revanced/patcher/patch/PatchContext, java/io/Closeable {
public final fun classBy (Lkotlin/jvm/functions/Function1;)Lapp/revanced/patcher/util/proxy/ClassProxy; public final fun classBy (Lkotlin/jvm/functions/Function1;)Lapp/revanced/patcher/util/proxy/ClassProxy;
public final fun classByType (Ljava/lang/String;)Lapp/revanced/patcher/util/proxy/ClassProxy;
public fun close ()V public fun close ()V
public synthetic fun get ()Ljava/lang/Object; public synthetic fun get ()Ljava/lang/Object;
public fun get ()Ljava/util/Set; public fun get ()Ljava/util/Set;
public final fun getClasses ()Lapp/revanced/patcher/util/ProxyClassList; public final fun getClasses ()Lapp/revanced/patcher/util/ProxyClassList;
public final fun getMatch (Lapp/revanced/patcher/Fingerprint;)Lapp/revanced/patcher/Match; public final fun navigate (Lcom/android/tools/smali/dexlib2/iface/Method;)Lapp/revanced/patcher/util/MethodNavigator;
public final fun getValue (Lapp/revanced/patcher/Fingerprint;Ljava/lang/Void;Lkotlin/reflect/KProperty;)Lapp/revanced/patcher/Match;
public final fun match (Lapp/revanced/patcher/Fingerprint;Lcom/android/tools/smali/dexlib2/iface/ClassDef;)Lapp/revanced/patcher/Match;
public final fun match (Lapp/revanced/patcher/Fingerprint;Lcom/android/tools/smali/dexlib2/iface/Method;)Lapp/revanced/patcher/Match;
public final fun navigate (Lcom/android/tools/smali/dexlib2/iface/reference/MethodReference;)Lapp/revanced/patcher/util/MethodNavigator;
public final fun proxy (Lcom/android/tools/smali/dexlib2/iface/ClassDef;)Lapp/revanced/patcher/util/proxy/ClassProxy; public final fun proxy (Lcom/android/tools/smali/dexlib2/iface/ClassDef;)Lapp/revanced/patcher/util/proxy/ClassProxy;
} }
@@ -277,7 +286,7 @@ public final class app/revanced/patcher/patch/Options : java/util/Map, kotlin/jv
} }
public abstract class app/revanced/patcher/patch/Patch { public abstract class app/revanced/patcher/patch/Patch {
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;ZLjava/util/Set;Ljava/util/Set;Ljava/util/Set;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;Lkotlin/jvm/internal/DefaultConstructorMarker;)V public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;ZLjava/util/Set;Ljava/util/Set;Ljava/util/Set;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun execute (Lapp/revanced/patcher/patch/PatchContext;)V public final fun execute (Lapp/revanced/patcher/patch/PatchContext;)V
public final fun finalize (Lapp/revanced/patcher/patch/PatchContext;)V public final fun finalize (Lapp/revanced/patcher/patch/PatchContext;)V
public final fun getCompatiblePackages ()Ljava/util/Set; public final fun getCompatiblePackages ()Ljava/util/Set;
@@ -294,13 +303,13 @@ public abstract class app/revanced/patcher/patch/PatchBuilder {
public final fun compatibleWith ([Ljava/lang/String;)V public final fun compatibleWith ([Ljava/lang/String;)V
public final fun compatibleWith ([Lkotlin/Pair;)V public final fun compatibleWith ([Lkotlin/Pair;)V
public final fun dependsOn ([Lapp/revanced/patcher/patch/Patch;)V public final fun dependsOn ([Lapp/revanced/patcher/patch/Patch;)V
public final fun execute (Lkotlin/jvm/functions/Function1;)V public final fun execute (Lkotlin/jvm/functions/Function2;)V
public final fun finalize (Lkotlin/jvm/functions/Function1;)V public final fun finalize (Lkotlin/jvm/functions/Function2;)V
protected final fun getCompatiblePackages ()Ljava/util/Set; protected final fun getCompatiblePackages ()Ljava/util/Set;
protected final fun getDependencies ()Ljava/util/Set; protected final fun getDependencies ()Ljava/util/Set;
protected final fun getDescription ()Ljava/lang/String; protected final fun getDescription ()Ljava/lang/String;
protected final fun getExecutionBlock ()Lkotlin/jvm/functions/Function1; protected final fun getExecutionBlock ()Lkotlin/jvm/functions/Function2;
protected final fun getFinalizeBlock ()Lkotlin/jvm/functions/Function1; protected final fun getFinalizeBlock ()Lkotlin/jvm/functions/Function2;
protected final fun getName ()Ljava/lang/String; protected final fun getName ()Ljava/lang/String;
protected final fun getOptions ()Ljava/util/Set; protected final fun getOptions ()Ljava/util/Set;
protected final fun getUse ()Z protected final fun getUse ()Z
@@ -308,8 +317,8 @@ public abstract class app/revanced/patcher/patch/PatchBuilder {
public final fun invoke (Ljava/lang/String;[Ljava/lang/String;)Lkotlin/Pair; public final fun invoke (Ljava/lang/String;[Ljava/lang/String;)Lkotlin/Pair;
protected final fun setCompatiblePackages (Ljava/util/Set;)V protected final fun setCompatiblePackages (Ljava/util/Set;)V
protected final fun setDependencies (Ljava/util/Set;)V protected final fun setDependencies (Ljava/util/Set;)V
protected final fun setExecutionBlock (Lkotlin/jvm/functions/Function1;)V protected final fun setExecutionBlock (Lkotlin/jvm/functions/Function2;)V
protected final fun setFinalizeBlock (Lkotlin/jvm/functions/Function1;)V protected final fun setFinalizeBlock (Lkotlin/jvm/functions/Function2;)V
} }
public abstract interface class app/revanced/patcher/patch/PatchContext : java/util/function/Supplier { public abstract interface class app/revanced/patcher/patch/PatchContext : java/util/function/Supplier {
@@ -386,13 +395,18 @@ public final class app/revanced/patcher/patch/ResourcePatchBuilder : app/revance
} }
public final class app/revanced/patcher/patch/ResourcePatchContext : app/revanced/patcher/patch/PatchContext { public final class app/revanced/patcher/patch/ResourcePatchContext : app/revanced/patcher/patch/PatchContext {
public final fun delete (Ljava/lang/String;)Z
public final fun document (Ljava/io/InputStream;)Lapp/revanced/patcher/util/Document;
public final fun document (Ljava/lang/String;)Lapp/revanced/patcher/util/Document;
public fun get ()Lapp/revanced/patcher/PatcherResult$PatchedResources; public fun get ()Lapp/revanced/patcher/PatcherResult$PatchedResources;
public synthetic fun get ()Ljava/lang/Object; public synthetic fun get ()Ljava/lang/Object;
public final fun get (Ljava/lang/String;Z)Ljava/io/File; public final fun get (Ljava/lang/String;Z)Ljava/io/File;
public static synthetic fun get$default (Lapp/revanced/patcher/patch/ResourcePatchContext;Ljava/lang/String;ZILjava/lang/Object;)Ljava/io/File; public static synthetic fun get$default (Lapp/revanced/patcher/patch/ResourcePatchContext;Ljava/lang/String;ZILjava/lang/Object;)Ljava/io/File;
public final fun getDocument ()Lapp/revanced/patcher/patch/ResourcePatchContext$DocumentOperatable;
public final fun stageDelete (Lkotlin/jvm/functions/Function1;)Z
}
public final class app/revanced/patcher/patch/ResourcePatchContext$DocumentOperatable {
public fun <init> (Lapp/revanced/patcher/patch/ResourcePatchContext;)V
public final fun get (Ljava/io/InputStream;)Lapp/revanced/patcher/util/Document;
public final fun get (Ljava/lang/String;)Lapp/revanced/patcher/util/Document;
} }
public final class app/revanced/patcher/util/Document : java/io/Closeable, org/w3c/dom/Document { public final class app/revanced/patcher/util/Document : java/io/Closeable, org/w3c/dom/Document {
@@ -471,9 +485,8 @@ public final class app/revanced/patcher/util/MethodNavigator {
public final fun at (ILkotlin/jvm/functions/Function1;)Lapp/revanced/patcher/util/MethodNavigator; public final fun at (ILkotlin/jvm/functions/Function1;)Lapp/revanced/patcher/util/MethodNavigator;
public final fun at ([I)Lapp/revanced/patcher/util/MethodNavigator; public final fun at ([I)Lapp/revanced/patcher/util/MethodNavigator;
public static synthetic fun at$default (Lapp/revanced/patcher/util/MethodNavigator;ILkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lapp/revanced/patcher/util/MethodNavigator; public static synthetic fun at$default (Lapp/revanced/patcher/util/MethodNavigator;ILkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lapp/revanced/patcher/util/MethodNavigator;
public final fun getValue (Ljava/lang/Void;Lkotlin/reflect/KProperty;)Lapp/revanced/patcher/util/proxy/mutableTypes/MutableMethod; public final fun immutable ()Lcom/android/tools/smali/dexlib2/iface/Method;
public final fun original ()Lcom/android/tools/smali/dexlib2/iface/Method; public final fun mutable ()Lapp/revanced/patcher/util/proxy/mutableTypes/MutableMethod;
public final fun stop ()Lapp/revanced/patcher/util/proxy/mutableTypes/MutableMethod;
} }
public final class app/revanced/patcher/util/ProxyClassList : java/util/List, kotlin/jvm/internal/markers/KMutableList { public final class app/revanced/patcher/util/ProxyClassList : java/util/List, kotlin/jvm/internal/markers/KMutableList {

View File

@@ -72,10 +72,6 @@ To start developing patches with ReVanced Patcher, you must prepare a developmen
Throughout the documentation, [ReVanced Patches](https://github.com/revanced/revanced-patches) will be used as an example project. Throughout the documentation, [ReVanced Patches](https://github.com/revanced/revanced-patches) will be used as an example project.
> [!NOTE]
> To start a fresh project,
> you can use the [ReVanced Patches template](https://github.com/revanced/revanced-patches-template).
1. Clone the repository 1. Clone the repository
```bash ```bash

View File

@@ -60,16 +60,14 @@
# 🔎 Fingerprinting # 🔎 Fingerprinting
In the context of ReVanced, a fingerprint is a partial description of a method. In the context of ReVanced, fingerprinting is primarily used to match methods with a limited amount of known information.
It is used to uniquely match a method by its characteristics.
Fingerprinting is used to match methods with a limited amount of known information.
Methods with obfuscated names that change with each update are primary candidates for fingerprinting. Methods with obfuscated names that change with each update are primary candidates for fingerprinting.
The goal of fingerprinting is to uniquely identify a method by capturing various attributes, such as the return type, The goal of fingerprinting is to uniquely identify a method by capturing various attributes, such as the return type,
access flags, an opcode pattern, strings, and more. access flags, an opcode pattern, strings, and more.
## ⛳️ Example fingerprint ## ⛳️ Example fingerprint
An example fingerprint is shown below: Throughout the documentation, the following example will be used to demonstrate the concepts of fingerprints:
```kt ```kt
@@ -81,11 +79,11 @@ fingerprint {
parameters("Z") parameters("Z")
opcodes(Opcode.RETURN) opcodes(Opcode.RETURN)
strings("pro") strings("pro")
custom { (method, classDef) -> classDef == "Lcom/some/app/ads/AdsLoader;" } custom { (method, classDef) -> method.definingClass == "Lcom/some/app/ads/AdsLoader;" }
} }
``` ```
## 🔎 Reconstructing the original code from the example fingerprint from above ## 🔎 Reconstructing the original code from a fingerprint
The following code is reconstructed from the fingerprint to understand how a fingerprint is created. The following code is reconstructed from the fingerprint to understand how a fingerprint is created.
@@ -109,7 +107,7 @@ The fingerprint contains the following information:
- Package and class name: - Package and class name:
```kt ```kt
custom { (method, classDef) -> classDef == "Lcom/some/app/ads/AdsLoader;" } custom = { (method, classDef) -> method.definingClass == "Lcom/some/app/ads/AdsLoader;"}
``` ```
With this information, the original code can be reconstructed: With this information, the original code can be reconstructed:
@@ -130,8 +128,6 @@ package com.some.app.ads;
} }
``` ```
Using that fingerprint, this method can be matched uniquely from all other methods.
> [!TIP] > [!TIP]
> A fingerprint should contain information about a method likely to remain the same across updates. > A fingerprint should contain information about a method likely to remain the same across updates.
> A method's name is not included in the fingerprint because it will likely change with each update in an obfuscated app. > A method's name is not included in the fingerprint because it will likely change with each update in an obfuscated app.
@@ -139,8 +135,8 @@ Using that fingerprint, this method can be matched uniquely from all other metho
## 🔨 How to use fingerprints ## 🔨 How to use fingerprints
A fingerprint is matched to a method, Fingerprints can be added to a patch by directly creating and adding them or by invoking them manually.
once the `match` property of the fingerprint is accessed in a patch's `execute` scope: Fingerprints added to a patch are matched by ReVanced Patcher before the patch is executed.
```kt ```kt
val fingerprint = fingerprint { val fingerprint = fingerprint {
@@ -148,53 +144,18 @@ val fingerprint = fingerprint {
} }
val patch = bytecodePatch { val patch = bytecodePatch {
execute { // Directly create and add a fingerprint.
val match = fingerprint.match!! fingerprint {
}
}
```
The fingerprint won't be matched again, if it has already been matched once.
This makes it useful, to share fingerprints between multiple patches, and let the first patch match the fingerprint:
```kt
// Either of these two patches will match the fingerprint first and the other patch can reuse the match:
val mainActivityPatch1 = bytecodePatch {
execute {
val match = mainActivityOnCreateFingerprint.match!!
}
}
val mainActivityPatch2 = bytecodePatch {
execute {
val match = mainActivityOnCreateFingerprint.match!!
}
}
```
A fingerprint match can also be delegated to a variable for convenience without the need to check for `null`:
```kt
val fingerprint = fingerprint {
// ... // ...
} }
val patch = bytecodePatch { // Add a fingerprint manually by invoking it.
execute { fingerprint()
// Alternative to fingerprint.match ?: throw PatchException("No match found")
val match by fingerprint.match
try {
match.method
} catch (e: PatchException) {
// Handle the exception for example.
}
}
} }
``` ```
> [!WARNING] > [!TIP]
> If the fingerprint can not be matched to any method, the match of a fingerprint is `null`. If such a match is delegated > Multiple patches can share fingerprints. If a fingerprint is matched once, it will not be matched again.
> to a variable, accessing it will raise an exception.
> [!TIP] > [!TIP]
> If a fingerprint has an opcode pattern, you can use the `fuzzyPatternScanThreshhold` parameter of the `opcode` > If a fingerprint has an opcode pattern, you can use the `fuzzyPatternScanThreshhold` parameter of the `opcode`
@@ -211,33 +172,52 @@ val patch = bytecodePatch {
> ) > )
>} >}
> ``` > ```
>
The match of a fingerprint contains references to the original method and class definition of the method: Once the fingerprint is matched, the match can be used in the patch:
```kt
val patch = bytecodePatch {
// Add a fingerprint and delegate its match to a variable.
val match by showAdsFingerprint()
val match2 by fingerprint {
// ...
}
execute {
val method = match.method
val method2 = match2.method
}
}
```
> [!WARNING]
> If the fingerprint can not be matched to any method, the match of a fingerprint is `null`. If such a match is delegated
> to a variable, accessing it will raise an exception.
The match of a fingerprint contains mutable and immutable references to the method and the class it matches to.
```kt ```kt
class Match( class Match(
val originalMethod: Method, val method: Method,
val originalClassDef: ClassDef, val classDef: ClassDef,
val patternMatch: Match.PatternMatch?, val patternMatch: Match.PatternMatch?,
val stringMatches: List<Match.StringMatch>?, val stringMatches: List<Match.StringMatch>?,
// ... // ...
) { ) {
val classDef by lazy { /* ... */ } val mutableClass by lazy { /* ... */ }
val method by lazy { /* ... */ } val mutableMethod by lazy { /* ... */ }
// ... // ...
} }
``` ```
The `classDef` and `method` properties can be used to make changes to the class or method. ## 🏹 Manual matching of fingerprints
They are lazy properties, so they are only computed
and will effectively replace the original method or class definition when accessed.
## 🏹 Manually matching fingerprints Unless a fingerprint is added to a patch, the fingerprint will not be matched automatically by ReVanced Patcher
before the patch is executed.
Instead, the fingerprint can be matched manually using various overloads of a fingerprint's `match` function.
By default, a fingerprint is matched automatically against all classes when the `match` property is accessed. You can match a fingerprint the following ways:
Instead, the fingerprint can be matched manually using various overloads of a fingerprint's `match` function:
- In a **list of classes**, if the fingerprint can match in a known subset of classes - In a **list of classes**, if the fingerprint can match in a known subset of classes
@@ -245,8 +225,10 @@ Instead, the fingerprint can be matched manually using various overloads of a fi
you can match the fingerprint on the list of classes: you can match the fingerprint on the list of classes:
```kt ```kt
execute { execute { context ->
val match = showAdsFingerprint.match(classes) ?: throw PatchException("No match found") val match = showAdsFingerprint.apply {
match(context, context.classes)
}.match ?: throw PatchException("No match found")
} }
``` ```
@@ -255,26 +237,30 @@ you can match the fingerprint on the list of classes:
If you know the fingerprint can match a method in a specific class, you can match the fingerprint in the class: If you know the fingerprint can match a method in a specific class, you can match the fingerprint in the class:
```kt ```kt
execute { execute { context ->
val adsLoaderClass = classes.single { it.name == "Lcom/some/app/ads/Loader;" } val adsLoaderClass = context.classes.single { it.name == "Lcom/some/app/ads/Loader;" }
val match = showAdsFingerprint.match(context, adsLoaderClass) ?: throw PatchException("No match found") val match = showAdsFingerprint.apply {
match(context, adsLoaderClass)
}.match ?: throw PatchException("No match found")
} }
``` ```
- Match a **single method**, to extract certain information about it - Match a **single method**, to extract certain information about it
The match of a fingerprint contains useful information about the method, The match of a fingerprint contains useful information about the method, such as the start and end index of an opcode pattern
such as the start and end index of an opcode pattern or the indices of the instructions with certain string references. or the indices of the instructions with certain string references.
A fingerprint can be leveraged to extract such information from a method instead of manually figuring it out: A fingerprint can be leveraged to extract such information from a method instead of manually figuring it out:
```kt ```kt
execute { execute { context ->
val currentPlanFingerprint = fingerprint { val proStringsFingerprint = fingerprint {
strings("free", "trial") strings("free", "trial")
} }
currentPlanFingerprint.match(adsFingerprintMatch.method)?.let { match -> proStringsFingerprint.apply {
match(context, adsFingerprintMatch.method)
}.match?.let { match ->
match.stringMatches.forEach { match -> match.stringMatches.forEach { match ->
println("The index of the string '${match.string}' is ${match.index}") println("The index of the string '${match.string}' is ${match.index}")
} }

View File

@@ -76,23 +76,23 @@ val disableAdsPatch = bytecodePatch(
) { ) {
compatibleWith("com.some.app"("1.0.0")) compatibleWith("com.some.app"("1.0.0"))
// Patches can depend on other patches, executing them first. // Resource patch disables ads by patching resource files.
dependsOn(disableAdsResourcePatch) dependsOn(disableAdsResourcePatch)
// Merge precompiled DEX files into the patched app, before the patch is executed. // Precompiled DEX file to be merged into the patched app.
extendWith("disable-ads.rve") extendWith("disable-ads.rve")
// Business logic of the patch to disable ads in the app.
execute {
// Fingerprint to find the method to patch. // Fingerprint to find the method to patch.
val showAdsMatch by showAdsFingerprint { val showAdsMatch by showAdsFingerprint {
// More about fingerprints on the next page of the documentation. // More about fingerprints on the next page of the documentation.
} }
// Business logic of the patch to disable ads in the app.
execute {
// In the method that shows ads, // In the method that shows ads,
// call DisableAdsPatch.shouldDisableAds() from the extension (precompiled DEX file) // call DisableAdsPatch.shouldDisableAds() from the extension (precompiled DEX file)
// to enable or disable ads. // to enable or disable ads.
showAdsMatch.method.addInstructions( showAdsMatch.mutableMethod.addInstructions(
0, 0,
""" """
invoke-static {}, LDisableAdsPatch;->shouldDisableAds()Z invoke-static {}, LDisableAdsPatch;->shouldDisableAds()Z
@@ -146,10 +146,10 @@ loadPatchesJar(patches).apply {
The type of an option can be obtained from the `type` property of the option: The type of an option can be obtained from the `type` property of the option:
```kt ```kt
option.type // The KType of the option. Captures the full type information of the option. option.type // The KType of the option.
``` ```
Options can be declared outside a patch and added to a patch manually: Options can be declared outside of a patch and added to a patch manually:
```kt ```kt
val option = stringOption(key = "option") val option = stringOption(key = "option")
@@ -184,8 +184,10 @@ and use it in a patch:
val patch = bytecodePatch(name = "Complex patch") { val patch = bytecodePatch(name = "Complex patch") {
extendWith("complex-patch.rve") extendWith("complex-patch.rve")
val match by methodFingerprint()
execute { execute {
fingerprint.match!!.mutableMethod.addInstructions(0, "invoke-static { }, LComplexPatch;->doSomething()V") match.mutableMethod.addInstructions(0, "invoke-static { }, LComplexPatch;->doSomething()V")
} }
} }
``` ```

View File

@@ -96,21 +96,21 @@ Example of patches:
@Surpress("unused") @Surpress("unused")
val bytecodePatch = bytecodePatch { val bytecodePatch = bytecodePatch {
execute { execute {
// More about this on the next page of the documentation. // TODO
} }
} }
@Surpress("unused") @Surpress("unused")
val rawResourcePatch = rawResourcePatch { val rawResourcePatch = rawResourcePatch {
execute { execute {
// More about this on the next page of the documentation. // TODO
} }
} }
@Surpress("unused") @Surpress("unused")
val resourcePatch = resourcePatch { val resourcePatch = resourcePatch {
execute { execute {
// More about this on the next page of the documentation. // TODO
} }
} }
``` ```

View File

@@ -4,107 +4,18 @@ A handful of APIs are available to make patch development easier and more effici
## 📙 Overview ## 📙 Overview
1. 👹 Create mutable replacements of classes with `proxy(ClassDef)` 1. 👹 Mutate classes with `context.proxy(ClassDef)`
2. 🔍 Find and create mutable replaces with `classBy(Predicate)` 2. 🔍 Find and proxy existing classes with `classBy(Predicate)` and `classByType(String)`
3. 🏃‍ Navigate method calls recursively by index with `navigate(Method)` 3. 🏃‍ Easily access referenced methods recursively by index with `MethodNavigator`
4. 💾 Read and write resource files with `get(String, Boolean)` and `delete(String)` 4. 🔨 Make use of extension functions from `BytecodeUtils` and `ResourceUtils` with certain applications
5. 📃 Read and write DOM files using `document(String)` and `document(InputStream)` (Available in ReVanced Patches)
5. 💾 Read and write (decoded) resources with `ResourcePatchContext.get(Path, Boolean)`
6. 📃 Read and write DOM files using `ResourcePatchContext.document`
### 🧰 APIs ### 🧰 APIs
#### 👹 `proxy(ClassDef)` > [!WARNING]
> This section is still under construction and may be incomplete.
By default, the classes are immutable, meaning they cannot be modified.
To make a class mutable, use the `proxy(ClassDef)` function.
This function creates a lazy mutable copy of the class definition.
Accessing the property will replace the original class definition with the mutable copy,
thus allowing you to make changes to the class. Subsequent accesses will return the same mutable copy.
```kt
execute {
val mutableClass = proxy(classDef)
mutableClass.methods.add(Method())
}
```
#### 🔍 `classBy(Predicate)`
The `classBy(Predicate)` function is an alternative to finding and creating mutable classes by a predicate.
It automatically proxies the class definition, making it mutable.
```kt
execute {
// Alternative to proxy(classes.find { it.name == "Lcom/example/MyClass;" })?.classDef
val classDef = classBy { it.name == "Lcom/example/MyClass;" }?.classDef
}
```
#### 🏃‍ `navigate(Method).at(index)`
The `navigate(Method)` function allows you to navigate method calls recursively by index.
```kt
execute {
// Sequentially navigate to the instructions at index 1 within 'someMethod'.
val method = navigate(someMethod).at(1).original() // original() returns the original immutable method.
// Further navigate to the second occurrence where the instruction's opcode is 'INVOKEVIRTUAL'.
// stop() returns the mutable copy of the method.
val method = navigate(someMethod).at(2) { instruction -> instruction.opcode == Opcode.INVOKEVIRTUAL }.stop()
// Alternatively, to stop(), you can delegate the method to a variable.
val method by navigate(someMethod).at(1)
// You can chain multiple calls to at() to navigate deeper into the method.
val method by navigate(someMethod).at(1).at(2, 3, 4).at(5)
}
```
#### 💾 `get(String, Boolean)` and `delete(String)`
The `get(String, Boolean)` function returns a `File` object that can be used to read and write resource files.
```kt
execute {
val file = get("res/values/strings.xml")
val content = file.readText()
file.writeText(content)
}
```
The `delete` function can mark files for deletion when the APK is rebuilt.
```kt
execute {
delete("res/values/strings.xml")
}
```
#### 📃 `document(String)` and `document(InputStream)`
The `document` function is used to read and write DOM files.
```kt
execute {
document("res/values/strings.xml").use { document ->
val element = doc.createElement("string").apply {
textContent = "Hello, World!"
}
document.documentElement.appendChild(element)
}
}
```
You can also read documents from an `InputStream`:
```kt
execute {
val inputStream = classLoader.getResourceAsStream("some.xml")
document(inputStream).use { document ->
// ...
}
}
```
## 🎉 Afterword ## 🎉 Afterword

File diff suppressed because it is too large Load Diff

View File

@@ -12,12 +12,16 @@ import java.util.logging.Logger
* @param temporaryFilesPath A path to a folder to store temporary files in. * @param temporaryFilesPath A path to a folder to store temporary files in.
* @param aaptBinaryPath A path to a custom aapt binary. * @param aaptBinaryPath A path to a custom aapt binary.
* @param frameworkFileDirectory A path to the directory to cache the framework file in. * @param frameworkFileDirectory A path to the directory to cache the framework file in.
* @param multithreadingDexFileWriter Whether to use multiple threads for writing dex files.
* This has impact on memory usage and performance.
*/ */
class PatcherConfig( class PatcherConfig(
internal val apkFile: File, internal val apkFile: File,
private val temporaryFilesPath: File = File("revanced-temporary-files"), private val temporaryFilesPath: File = File("revanced-temporary-files"),
aaptBinaryPath: String? = null, aaptBinaryPath: String? = null,
frameworkFileDirectory: String? = null, frameworkFileDirectory: String? = null,
@Deprecated("This is going to be removed in the future because it is not needed anymore.")
internal val multithreadingDexFileWriter: Boolean = false,
) { ) {
private val logger = Logger.getLogger(PatcherConfig::class.java.name) private val logger = Logger.getLogger(PatcherConfig::class.java.name)

View File

@@ -29,12 +29,12 @@ class PatcherResult internal constructor(
* @param resourcesApk The compiled resources.apk file. * @param resourcesApk The compiled resources.apk file.
* @param otherResources The directory containing other resources files. * @param otherResources The directory containing other resources files.
* @param doNotCompress List of files that should not be compressed. * @param doNotCompress List of files that should not be compressed.
* @param deleteResources List of resources that should be deleted. * @param deleteResources List of predicates about resources that should be deleted.
*/ */
class PatchedResources internal constructor( class PatchedResources internal constructor(
val resourcesApk: File?, val resourcesApk: File?,
val otherResources: File?, val otherResources: File?,
val doNotCompress: Set<String>, val doNotCompress: Set<String>,
val deleteResources: Set<String>, val deleteResources: Set<(String) -> Boolean>,
) )
} }

View File

@@ -1,6 +1,8 @@
package app.revanced.patcher.patch package app.revanced.patcher.patch
import app.revanced.patcher.* import app.revanced.patcher.InternalApi
import app.revanced.patcher.PatcherConfig
import app.revanced.patcher.PatcherResult
import app.revanced.patcher.extensions.InstructionExtensions.instructionsOrNull import app.revanced.patcher.extensions.InstructionExtensions.instructionsOrNull
import app.revanced.patcher.util.ClassMerger.merge import app.revanced.patcher.util.ClassMerger.merge
import app.revanced.patcher.util.MethodNavigator import app.revanced.patcher.util.MethodNavigator
@@ -12,7 +14,6 @@ import com.android.tools.smali.dexlib2.iface.ClassDef
import com.android.tools.smali.dexlib2.iface.DexFile import com.android.tools.smali.dexlib2.iface.DexFile
import com.android.tools.smali.dexlib2.iface.Method import com.android.tools.smali.dexlib2.iface.Method
import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction
import com.android.tools.smali.dexlib2.iface.reference.MethodReference
import com.android.tools.smali.dexlib2.iface.reference.StringReference import com.android.tools.smali.dexlib2.iface.reference.StringReference
import lanchon.multidexlib2.BasicDexFileNamer import lanchon.multidexlib2.BasicDexFileNamer
import lanchon.multidexlib2.DexIO import lanchon.multidexlib2.DexIO
@@ -22,7 +23,6 @@ import java.io.Closeable
import java.io.FileFilter import java.io.FileFilter
import java.util.* import java.util.*
import java.util.logging.Logger import java.util.logging.Logger
import kotlin.reflect.KProperty
/** /**
* A context for patches containing the current state of the bytecode. * A context for patches containing the current state of the bytecode.
@@ -53,49 +53,16 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi
).also { opcodes = it.opcodes }.classes.toMutableList(), ).also { opcodes = it.opcodes }.classes.toMutableList(),
) )
/**
* The match for this [Fingerprint]. Null if unmatched.
*/
val Fingerprint.match get() = match(this@BytecodePatchContext)
/**
* Match using a [ClassDef].
*
* @param classDef The class to match against.
* @return The [Match] if a match was found or if the fingerprint is already matched to a method, null otherwise.
*/
fun Fingerprint.match(classDef: ClassDef) = match(this@BytecodePatchContext, classDef)
/**
* Match using a [Method].
* The class is retrieved from the method.
*
* @param method The method to match against.
* @return The [Match] if a match was found or if the fingerprint is already matched to a method, null otherwise.
*/
fun Fingerprint.match(method: Method) = match(this@BytecodePatchContext, method)
/**
* Get the match for this [Fingerprint].
*
* @throws IllegalStateException If the [Fingerprint] has not been matched.
*/
operator fun Fingerprint.getValue(nothing: Nothing?, property: KProperty<*>): Match = _match
?: throw PatchException("No fingerprint match to delegate to \"${property.name}\".")
/** /**
* The lookup maps for methods and the class they are a member of from the [classes]. * The lookup maps for methods and the class they are a member of from the [classes].
*/ */
internal val lookupMaps by lazy { LookupMaps(classes) } internal val lookupMaps by lazy { LookupMaps(classes) }
/** /**
* Merge the extension of [bytecodePatch] into the [BytecodePatchContext]. * Merge the extension of this patch.
* If no extension is present, the function will return early.
*
* @param bytecodePatch The [BytecodePatch] to merge the extension of.
*/ */
internal fun mergeExtension(bytecodePatch: BytecodePatch) { internal fun BytecodePatch.mergeExtension() {
bytecodePatch.extensionInputStream?.get()?.use { extensionStream -> extension?.use { extensionStream ->
RawDexIO.readRawDexFile(extensionStream, 0, null).classes.forEach { classDef -> RawDexIO.readRawDexFile(extensionStream, 0, null).classes.forEach { classDef ->
val existingClass = lookupMaps.classesByType[classDef.type] ?: run { val existingClass = lookupMaps.classesByType[classDef.type] ?: run {
logger.fine("Adding class \"$classDef\"") logger.fine("Adding class \"$classDef\"")
@@ -118,9 +85,18 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi
classes += mergedClass classes += mergedClass
} }
} }
} ?: logger.fine("No extension to merge") } ?: return logger.fine("No extension to merge")
} }
/**
* Find a class by its type using a contains check.
*
* @param type The type of the class.
* @return A proxy for the first class that matches the type.
*/
@Deprecated("Use classBy { type in it.type } instead.", ReplaceWith("classBy { type in it.type }"))
fun classByType(type: String) = classBy { type in it.type }
/** /**
* Find a class with a predicate. * Find a class with a predicate.
* *
@@ -148,7 +124,7 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi
* *
* @return A [MethodNavigator] for the method. * @return A [MethodNavigator] for the method.
*/ */
fun navigate(method: MethodReference) = MethodNavigator(this@BytecodePatchContext, method) fun navigate(method: Method) = MethodNavigator(this@BytecodePatchContext, method)
/** /**
* Compile bytecode from the [BytecodePatchContext]. * Compile bytecode from the [BytecodePatchContext].
@@ -169,7 +145,7 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi
}.apply { }.apply {
MultiDexIO.writeDexFile( MultiDexIO.writeDexFile(
true, true,
-1, if (config.multithreadingDexFileWriter) -1 else 1,
this, this,
BasicDexFileNamer(), BasicDexFileNamer(),
object : DexFile { object : DexFile {

View File

@@ -2,6 +2,7 @@
package app.revanced.patcher.patch package app.revanced.patcher.patch
import app.revanced.patcher.Fingerprint
import app.revanced.patcher.Patcher import app.revanced.patcher.Patcher
import app.revanced.patcher.PatcherContext import app.revanced.patcher.PatcherContext
import dalvik.system.DexClassLoader import dalvik.system.DexClassLoader
@@ -13,8 +14,8 @@ import java.lang.reflect.Member
import java.lang.reflect.Method import java.lang.reflect.Method
import java.lang.reflect.Modifier import java.lang.reflect.Modifier
import java.net.URLClassLoader import java.net.URLClassLoader
import java.util.function.Supplier
import java.util.jar.JarFile import java.util.jar.JarFile
import kotlin.reflect.KProperty
typealias PackageName = String typealias PackageName = String
typealias VersionName = String typealias VersionName = String
@@ -45,10 +46,10 @@ sealed class Patch<C : PatchContext<*>>(
val dependencies: Set<Patch<*>>, val dependencies: Set<Patch<*>>,
val compatiblePackages: Set<Package>?, val compatiblePackages: Set<Package>?,
options: Set<Option<*>>, options: Set<Option<*>>,
private val executeBlock: (C) -> Unit, private val executeBlock: Patch<C>.(C) -> Unit,
// Must be internal and nullable, so that Patcher.invoke can check, // Must be internal and nullable, so that Patcher.invoke can check,
// if a patch has a finalizing block in order to not emit it twice. // if a patch has a finalizing block in order to not emit it twice.
internal var finalizeBlock: ((C) -> Unit)?, internal var finalizeBlock: (Patch<C>.(C) -> Unit)?,
) { ) {
/** /**
* The options of the patch. * The options of the patch.
@@ -56,35 +57,35 @@ sealed class Patch<C : PatchContext<*>>(
val options = Options(options) val options = Options(options)
/** /**
* Calls the execution block of the patch. * Runs the execution block of the patch.
* This function is called by [Patcher.invoke]. * Called by [Patcher].
* *
* @param context The [PatcherContext] to get the [PatchContext] from to execute the patch with. * @param context The [PatcherContext] to get the [PatchContext] from to execute the patch with.
*/ */
internal abstract fun execute(context: PatcherContext) internal abstract fun execute(context: PatcherContext)
/** /**
* Calls the execution block of the patch. * Runs the execution block of the patch.
* *
* @param context The [PatchContext] to execute the patch with. * @param context The [PatchContext] to execute the patch with.
*/ */
fun execute(context: C) = executeBlock(context) fun execute(context: C) = executeBlock(context)
/** /**
* Calls the finalizing block of the patch. * Runs the finalizing block of the patch.
* This function is called by [Patcher.invoke]. * Called by [Patcher].
* *
* @param context The [PatcherContext] to get the [PatchContext] from to finalize the patch with. * @param context The [PatcherContext] to get the [PatchContext] from to finalize the patch with.
*/ */
internal abstract fun finalize(context: PatcherContext) internal abstract fun finalize(context: PatcherContext)
/** /**
* Calls the finalizing block of the patch. * Runs the finalizing block of the patch.
* *
* @param context The [PatchContext] to finalize the patch with. * @param context The [PatchContext] to finalize the patch with.
*/ */
fun finalize(context: C) { fun finalize(context: C) {
finalizeBlock?.invoke(context) finalizeBlock?.invoke(this, context)
} }
override fun toString() = name ?: "Patch" override fun toString() = name ?: "Patch"
@@ -126,7 +127,8 @@ internal fun Iterable<Patch<*>>.forEachRecursively(
* If null, the patch is compatible with all packages. * If null, the patch is compatible with all packages.
* @param dependencies Other patches this patch depends on. * @param dependencies Other patches this patch depends on.
* @param options The options of the patch. * @param options The options of the patch.
* @property extensionInputStream Getter for the extension input stream of the patch. * @param fingerprints The fingerprints that are resolved before the patch is executed.
* @property extension An input stream of the extension resource this patch uses.
* An extension is a precompiled DEX file that is merged into the patched app before this patch is executed. * An extension is a precompiled DEX file that is merged into the patched app before this patch is executed.
* @param executeBlock The execution block of the patch. * @param executeBlock The execution block of the patch.
* @param finalizeBlock The finalizing block of the patch. Called after all patches have been executed, * @param finalizeBlock The finalizing block of the patch. Called after all patches have been executed,
@@ -141,9 +143,10 @@ class BytecodePatch internal constructor(
compatiblePackages: Set<Package>?, compatiblePackages: Set<Package>?,
dependencies: Set<Patch<*>>, dependencies: Set<Patch<*>>,
options: Set<Option<*>>, options: Set<Option<*>>,
val extensionInputStream: Supplier<InputStream>?, val fingerprints: Set<Fingerprint>,
executeBlock: (BytecodePatchContext) -> Unit, val extension: InputStream?,
finalizeBlock: ((BytecodePatchContext) -> Unit)?, executeBlock: Patch<BytecodePatchContext>.(BytecodePatchContext) -> Unit,
finalizeBlock: (Patch<BytecodePatchContext>.(BytecodePatchContext) -> Unit)?,
) : Patch<BytecodePatchContext>( ) : Patch<BytecodePatchContext>(
name, name,
description, description,
@@ -155,7 +158,14 @@ class BytecodePatch internal constructor(
finalizeBlock, finalizeBlock,
) { ) {
override fun execute(context: PatcherContext) = with(context.bytecodeContext) { override fun execute(context: PatcherContext) = with(context.bytecodeContext) {
mergeExtension(this@BytecodePatch) with(context.bytecodeContext) {
mergeExtension()
}
fingerprints.forEach {
it.match(this)
}
execute(this) execute(this)
} }
@@ -188,8 +198,8 @@ class RawResourcePatch internal constructor(
compatiblePackages: Set<Package>?, compatiblePackages: Set<Package>?,
dependencies: Set<Patch<*>>, dependencies: Set<Patch<*>>,
options: Set<Option<*>>, options: Set<Option<*>>,
executeBlock: (ResourcePatchContext) -> Unit, executeBlock: Patch<ResourcePatchContext>.(ResourcePatchContext) -> Unit,
finalizeBlock: ((ResourcePatchContext) -> Unit)?, finalizeBlock: (Patch<ResourcePatchContext>.(ResourcePatchContext) -> Unit)?,
) : Patch<ResourcePatchContext>( ) : Patch<ResourcePatchContext>(
name, name,
description, description,
@@ -231,8 +241,8 @@ class ResourcePatch internal constructor(
compatiblePackages: Set<Package>?, compatiblePackages: Set<Package>?,
dependencies: Set<Patch<*>>, dependencies: Set<Patch<*>>,
options: Set<Option<*>>, options: Set<Option<*>>,
executeBlock: (ResourcePatchContext) -> Unit, executeBlock: Patch<ResourcePatchContext>.(ResourcePatchContext) -> Unit,
finalizeBlock: ((ResourcePatchContext) -> Unit)?, finalizeBlock: (Patch<ResourcePatchContext>.(ResourcePatchContext) -> Unit)?,
) : Patch<ResourcePatchContext>( ) : Patch<ResourcePatchContext>(
name, name,
description, description,
@@ -277,8 +287,8 @@ sealed class PatchBuilder<C : PatchContext<*>>(
protected var dependencies = mutableSetOf<Patch<*>>() protected var dependencies = mutableSetOf<Patch<*>>()
protected val options = mutableSetOf<Option<*>>() protected val options = mutableSetOf<Option<*>>()
protected var executionBlock: ((C) -> Unit) = { } protected var executionBlock: (Patch<C>.(C) -> Unit) = { }
protected var finalizeBlock: ((C) -> Unit)? = null protected var finalizeBlock: (Patch<C>.(C) -> Unit)? = null
/** /**
* Add an option to the patch. * Add an option to the patch.
@@ -337,7 +347,7 @@ sealed class PatchBuilder<C : PatchContext<*>>(
* *
* @param block The execution block of the patch. * @param block The execution block of the patch.
*/ */
fun execute(block: C.() -> Unit) { fun execute(block: Patch<C>.(C) -> Unit) {
executionBlock = block executionBlock = block
} }
@@ -346,7 +356,7 @@ sealed class PatchBuilder<C : PatchContext<*>>(
* *
* @param block The finalizing block of the patch. * @param block The finalizing block of the patch.
*/ */
fun finalize(block: C.() -> Unit) { fun finalize(block: Patch<C>.(C) -> Unit) {
finalizeBlock = block finalizeBlock = block
} }
@@ -375,7 +385,8 @@ private fun <B : PatchBuilder<*>> B.buildPatch(block: B.() -> Unit = {}) = apply
* If null, the patch is named "Patch" and will not be loaded by [PatchLoader]. * If null, the patch is named "Patch" and will not be loaded by [PatchLoader].
* @param description The description of the patch. * @param description The description of the patch.
* @param use Weather or not the patch should be used. * @param use Weather or not the patch should be used.
* @property extensionInputStream Getter for the extension input stream of the patch. * @property fingerprints The fingerprints that are resolved before the patch is executed.
* @property extension An input stream of the extension resource this patch uses.
* An extension is a precompiled DEX file that is merged into the patched app before this patch is executed. * An extension is a precompiled DEX file that is merged into the patched app before this patch is executed.
* *
* @constructor Create a new [BytecodePatchBuilder] builder. * @constructor Create a new [BytecodePatchBuilder] builder.
@@ -385,9 +396,27 @@ class BytecodePatchBuilder internal constructor(
description: String?, description: String?,
use: Boolean, use: Boolean,
) : PatchBuilder<BytecodePatchContext>(name, description, use) { ) : PatchBuilder<BytecodePatchContext>(name, description, use) {
private val fingerprints = mutableSetOf<Fingerprint>()
/**
* Add the fingerprint to the patch.
*
* @return A wrapper for the fingerprint with the ability to delegate the match to the fingerprint.
*/
operator fun Fingerprint.invoke() = InvokedFingerprint(also { fingerprints.add(it) })
class InvokedFingerprint internal constructor(private val fingerprint: Fingerprint) {
// The reason getValue isn't extending the Fingerprint class is
// because delegating makes only sense if the fingerprint was previously added to the patch by invoking it.
// It may be likely to forget invoking it. By wrapping the fingerprint into this class,
// the compiler will throw an error if the fingerprint was not invoked if attempting to delegate the match.
operator fun getValue(nothing: Nothing?, property: KProperty<*>) = fingerprint.match
?: throw PatchException("No fingerprint match to delegate to \"${property.name}\".")
}
// Must be internal for the inlined function "extendWith". // Must be internal for the inlined function "extendWith".
@PublishedApi @PublishedApi
internal var extensionInputStream: Supplier<InputStream>? = null internal var extension: InputStream? = null
// Inlining is necessary to get the class loader that loaded the patch // Inlining is necessary to get the class loader that loaded the patch
// to load the extension from the resources. // to load the extension from the resources.
@@ -398,11 +427,8 @@ class BytecodePatchBuilder internal constructor(
*/ */
@Suppress("NOTHING_TO_INLINE") @Suppress("NOTHING_TO_INLINE")
inline fun extendWith(extension: String) = apply { inline fun extendWith(extension: String) = apply {
val classLoader = object {}.javaClass.classLoader this.extension = object {}.javaClass.classLoader.getResourceAsStream(extension)
?: throw PatchException("Extension \"$extension\" not found")
extensionInputStream = Supplier {
classLoader.getResourceAsStream(extension) ?: throw PatchException("Extension \"$extension\" not found")
}
} }
override fun build() = BytecodePatch( override fun build() = BytecodePatch(
@@ -412,7 +438,8 @@ class BytecodePatchBuilder internal constructor(
compatiblePackages, compatiblePackages,
dependencies, dependencies,
options, options,
extensionInputStream, fingerprints,
extension,
executionBlock, executionBlock,
finalizeBlock, finalizeBlock,
) )

View File

@@ -31,20 +31,15 @@ class ResourcePatchContext internal constructor(
) : PatchContext<PatcherResult.PatchedResources?> { ) : PatchContext<PatcherResult.PatchedResources?> {
private val logger = Logger.getLogger(ResourcePatchContext::class.java.name) private val logger = Logger.getLogger(ResourcePatchContext::class.java.name)
/**
* Read a document from an [InputStream].
*/
fun document(inputStream: InputStream) = Document(inputStream)
/** /**
* Read and write documents in the [PatcherConfig.apkFiles]. * Read and write documents in the [PatcherConfig.apkFiles].
*/ */
fun document(path: String) = Document(get(path)) val document = DocumentOperatable()
/** /**
* Set of resources from [PatcherConfig.apkFiles] to delete. * Predicate to delete resources from [PatcherConfig.apkFiles].
*/ */
private val deleteResources = mutableSetOf<String>() private val deleteResources = mutableSetOf<(String) -> Boolean>()
/** /**
* Decode resources of [PatcherConfig.apkFile]. * Decode resources of [PatcherConfig.apkFile].
@@ -206,11 +201,11 @@ class ResourcePatchContext internal constructor(
} }
/** /**
* Mark a file for deletion when the APK is rebuilt. * Stage a file to be deleted from [PatcherConfig.apkFile].
* *
* @param name The name of the file to delete. * @param shouldDelete The predicate to stage the file for deletion given its name.
*/ */
fun delete(name: String) = deleteResources.add(name) fun stageDelete(shouldDelete: (String) -> Boolean) = deleteResources.add(shouldDelete)
/** /**
* How to handle resources decoding and compiling. * How to handle resources decoding and compiling.
@@ -232,4 +227,10 @@ class ResourcePatchContext internal constructor(
*/ */
NONE, NONE,
} }
inner class DocumentOperatable {
operator fun get(inputStream: InputStream) = Document(inputStream)
operator fun get(path: String) = Document(this@ResourcePatchContext[path])
}
} }

View File

@@ -12,7 +12,6 @@ import com.android.tools.smali.dexlib2.iface.instruction.Instruction
import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction import com.android.tools.smali.dexlib2.iface.instruction.ReferenceInstruction
import com.android.tools.smali.dexlib2.iface.reference.MethodReference import com.android.tools.smali.dexlib2.iface.reference.MethodReference
import com.android.tools.smali.dexlib2.util.MethodUtil import com.android.tools.smali.dexlib2.util.MethodUtil
import kotlin.reflect.KProperty
/** /**
* A navigator for methods. * A navigator for methods.
@@ -28,7 +27,7 @@ import kotlin.reflect.KProperty
class MethodNavigator internal constructor(private val context: BytecodePatchContext, private var startMethod: MethodReference) { class MethodNavigator internal constructor(private val context: BytecodePatchContext, private var startMethod: MethodReference) {
private var lastNavigatedMethodReference = startMethod private var lastNavigatedMethodReference = startMethod
private val lastNavigatedMethodInstructions get() = with(original()) { private val lastNavigatedMethodInstructions get() = with(immutable()) {
instructionsOrNull ?: throw NavigateException("Method $definingClass.$name does not have an implementation.") instructionsOrNull ?: throw NavigateException("Method $definingClass.$name does not have an implementation.")
} }
@@ -77,22 +76,15 @@ class MethodNavigator internal constructor(private val context: BytecodePatchCon
* *
* @return The last navigated method mutably. * @return The last navigated method mutably.
*/ */
fun stop() = context.classBy(matchesCurrentMethodReferenceDefiningClass)!!.mutableClass.firstMethodBySignature fun mutable() = context.classBy(matchesCurrentMethodReferenceDefiningClass)!!.mutableClass.firstMethodBySignature
as MutableMethod as MutableMethod
/**
* Get the last navigated method mutably.
*
* @return The last navigated method mutably.
*/
operator fun getValue(nothing: Nothing?, property: KProperty<*>) = stop()
/** /**
* Get the last navigated method immutably. * Get the last navigated method immutably.
* *
* @return The last navigated method immutably. * @return The last navigated method immutably.
*/ */
fun original() = context.classes.first(matchesCurrentMethodReferenceDefiningClass).firstMethodBySignature fun immutable() = context.classes.first(matchesCurrentMethodReferenceDefiningClass).firstMethodBySignature
/** /**
* Predicate to match the class defining the current method reference. * Predicate to match the class defining the current method reference.

View File

@@ -3,21 +3,21 @@ package app.revanced.patcher
import app.revanced.patcher.patch.* import app.revanced.patcher.patch.*
import app.revanced.patcher.patch.BytecodePatchContext.LookupMaps import app.revanced.patcher.patch.BytecodePatchContext.LookupMaps
import app.revanced.patcher.util.ProxyClassList import app.revanced.patcher.util.ProxyClassList
import com.android.tools.smali.dexlib2.iface.ClassDef
import com.android.tools.smali.dexlib2.iface.Method
import com.android.tools.smali.dexlib2.immutable.ImmutableClassDef import com.android.tools.smali.dexlib2.immutable.ImmutableClassDef
import com.android.tools.smali.dexlib2.immutable.ImmutableMethod import com.android.tools.smali.dexlib2.immutable.ImmutableMethod
import io.mockk.every import io.mockk.every
import io.mockk.just import io.mockk.just
import io.mockk.mockk import io.mockk.mockk
import io.mockk.runs import io.mockk.runs
import jdk.internal.module.ModuleBootstrap.patcher
import kotlinx.coroutines.flow.toList import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.assertAll import org.junit.jupiter.api.assertDoesNotThrow
import java.util.logging.Logger import java.util.logging.Logger
import kotlin.test.* import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.test.assertTrue
internal object PatcherTest { internal object PatcherTest {
private lateinit var patcher: Patcher private lateinit var patcher: Patcher
@@ -151,15 +151,19 @@ internal object PatcherTest {
@Test @Test
fun `throws if unmatched fingerprint match is delegated`() { fun `throws if unmatched fingerprint match is delegated`() {
val patch = bytecodePatch { val patch = bytecodePatch {
execute {
// Fingerprint can never match. // Fingerprint can never match.
val match by fingerprint { } val match by fingerprint { }
// Manually add the fingerprint.
app.revanced.patcher.fingerprint { }()
execute {
// Throws, because the fingerprint can't be matched. // Throws, because the fingerprint can't be matched.
match.patternMatch match.patternMatch
} }
} }
assertEquals(2, patch.fingerprints.size)
assertTrue( assertTrue(
patch().exception != null, patch().exception != null,
"Expected an exception because the fingerprint can't match.", "Expected an exception because the fingerprint can't match.",
@@ -168,6 +172,44 @@ internal object PatcherTest {
@Test @Test
fun `matches fingerprint`() { fun `matches fingerprint`() {
mockClassWithMethod()
val patches = setOf(bytecodePatch { fingerprint { this returns "V" } })
assertNull(
patches.first().fingerprints.first().match,
"Expected fingerprint to be matched before execution.",
)
patches()
assertDoesNotThrow("Expected fingerprint to be matched.") {
assertEquals(
"V",
patches.first().fingerprints.first().match!!.method.returnType,
"Expected fingerprint to be matched.",
)
}
}
private operator fun Set<Patch<*>>.invoke(): List<PatchResult> {
every { patcher.context.executablePatches } returns toMutableSet()
every { patcher.context.bytecodeContext.lookupMaps } returns LookupMaps(patcher.context.bytecodeContext.classes)
every { with(patcher.context.bytecodeContext) { any<BytecodePatch>().mergeExtension() } } just runs
return runBlocking { patcher().toList() }
}
private operator fun Patch<*>.invoke() = setOf(this)().first()
private fun Any.setPrivateField(field: String, value: Any) {
this::class.java.getDeclaredField(field).apply {
this.isAccessible = true
set(this@setPrivateField, value)
}
}
private fun mockClassWithMethod() {
every { patcher.context.bytecodeContext.classes } returns ProxyClassList( every { patcher.context.bytecodeContext.classes } returns ProxyClassList(
mutableListOf( mutableListOf(
ImmutableClassDef( ImmutableClassDef(
@@ -193,50 +235,6 @@ internal object PatcherTest {
), ),
), ),
) )
every { with(patcher.context.bytecodeContext) { any<Fingerprint>().match } } answers { callOriginal() }
every { with(patcher.context.bytecodeContext) { any<Fingerprint>().match(any<ClassDef>()) } } answers { callOriginal() }
every { with(patcher.context.bytecodeContext) { any<Fingerprint>().match(any<Method>()) } } answers { callOriginal() }
every { patcher.context.bytecodeContext.classBy(any()) } answers { callOriginal() }
every { patcher.context.bytecodeContext.proxy(any()) } answers { callOriginal() }
val fingerprint = fingerprint { returns("V") }
val fingerprint2 = fingerprint { returns("V") }
val fingerprint3 = fingerprint { returns("V") }
val patches = setOf(
bytecodePatch {
execute {
fingerprint.match(classes.first().methods.first())
fingerprint2.match(classes.first())
fingerprint3.match
}
},
)
patches()
assertAll(
"Expected fingerprints to match.",
{ assertNotNull(fingerprint._match) },
{ assertNotNull(fingerprint2._match) },
{ assertNotNull(fingerprint3._match) },
)
}
private operator fun Set<Patch<*>>.invoke(): List<PatchResult> {
every { patcher.context.executablePatches } returns toMutableSet()
every { patcher.context.bytecodeContext.lookupMaps } returns LookupMaps(patcher.context.bytecodeContext.classes) every { patcher.context.bytecodeContext.lookupMaps } returns LookupMaps(patcher.context.bytecodeContext.classes)
every { with(patcher.context.bytecodeContext) { mergeExtension(any<BytecodePatch>()) } } just runs
return runBlocking { patcher().toList() }
}
private operator fun Patch<*>.invoke() = setOf(this)().first()
private fun Any.setPrivateField(field: String, value: Any) {
this::class.java.getDeclaredField(field).apply {
this.isAccessible = true
set(this@setPrivateField, value)
}
} }
} }

View File

@@ -1,5 +1,6 @@
package app.revanced.patcher.patch package app.revanced.patcher.patch
import app.revanced.patcher.fingerprint
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
@@ -23,6 +24,23 @@ internal object PatchTest {
assertEquals("compatible.package", patch.compatiblePackages!!.first().first) assertEquals("compatible.package", patch.compatiblePackages!!.first().first)
} }
@Test
fun `can create patch with fingerprints`() {
val externalFingerprint = fingerprint {}
val patch = bytecodePatch(name = "Test") {
val externalFingerprintMatch by externalFingerprint()
val internalFingerprintMatch by fingerprint {}
execute {
externalFingerprintMatch.method
internalFingerprintMatch.method
}
}
assertEquals(2, patch.fingerprints.size)
}
@Test @Test
fun `can create patch with dependencies`() { fun `can create patch with dependencies`() {
val patch = bytecodePatch(name = "Test") { val patch = bytecodePatch(name = "Test") {