Skip to content

Commit

Permalink
refactor: Change ProxyList to a map, remove now redundant class looku…
Browse files Browse the repository at this point in the history
…p map. Encapsulate ProxyList into PatchClasses.
  • Loading branch information
LisoUseInAIKyrios committed Jan 28, 2025
1 parent 142aa71 commit cc4f158
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 110 deletions.
50 changes: 13 additions & 37 deletions api/revanced-patcher.api
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,15 @@ public final class app/revanced/patcher/patch/BytecodePatchBuilder : app/revance
}

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/mutableTypes/MutableClass;
public final fun classBy (Ljava/lang/String;)Lcom/android/tools/smali/dexlib2/iface/ClassDef;
public final fun classBy (Lkotlin/jvm/functions/Function1;)Lcom/android/tools/smali/dexlib2/iface/ClassDef;
public fun close ()V
public synthetic fun get ()Ljava/lang/Object;
public fun get ()Ljava/util/Set;
public final fun getClasses ()Lapp/revanced/patcher/util/ProxyClassList;
public final fun getClasses ()Lapp/revanced/patcher/util/PatchClasses;
public final fun mutableClassBy (Lcom/android/tools/smali/dexlib2/iface/ClassDef;)Lapp/revanced/patcher/util/proxy/mutableTypes/MutableClass;
public final fun mutableClassBy (Ljava/lang/String;)Lapp/revanced/patcher/util/proxy/mutableTypes/MutableClass;
public final fun mutableClassBy (Lkotlin/jvm/functions/Function1;)Lapp/revanced/patcher/util/proxy/mutableTypes/MutableClass;
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/mutableTypes/MutableClass;
}
Expand Down Expand Up @@ -645,41 +649,13 @@ public final class app/revanced/patcher/util/MethodNavigator {
public static synthetic fun to$default (Lapp/revanced/patcher/util/MethodNavigator;ILkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lapp/revanced/patcher/util/MethodNavigator;
}

public final class app/revanced/patcher/util/ProxyClassList : java/util/List, kotlin/jvm/internal/markers/KMutableList {
public fun add (ILcom/android/tools/smali/dexlib2/iface/ClassDef;)V
public synthetic fun add (ILjava/lang/Object;)V
public fun add (Lcom/android/tools/smali/dexlib2/iface/ClassDef;)Z
public synthetic fun add (Ljava/lang/Object;)Z
public fun addAll (ILjava/util/Collection;)Z
public fun addAll (Ljava/util/Collection;)Z
public fun clear ()V
public fun contains (Lcom/android/tools/smali/dexlib2/iface/ClassDef;)Z
public final fun contains (Ljava/lang/Object;)Z
public fun containsAll (Ljava/util/Collection;)Z
public fun get (I)Lcom/android/tools/smali/dexlib2/iface/ClassDef;
public synthetic fun get (I)Ljava/lang/Object;
public fun getSize ()I
public fun indexOf (Lcom/android/tools/smali/dexlib2/iface/ClassDef;)I
public final fun indexOf (Ljava/lang/Object;)I
public fun isEmpty ()Z
public fun iterator ()Ljava/util/Iterator;
public fun lastIndexOf (Lcom/android/tools/smali/dexlib2/iface/ClassDef;)I
public final fun lastIndexOf (Ljava/lang/Object;)I
public fun listIterator ()Ljava/util/ListIterator;
public fun listIterator (I)Ljava/util/ListIterator;
public final fun remove (I)Lcom/android/tools/smali/dexlib2/iface/ClassDef;
public synthetic fun remove (I)Ljava/lang/Object;
public fun remove (Lcom/android/tools/smali/dexlib2/iface/ClassDef;)Z
public final fun remove (Ljava/lang/Object;)Z
public fun removeAll (Ljava/util/Collection;)Z
public fun removeAt (I)Lcom/android/tools/smali/dexlib2/iface/ClassDef;
public fun retainAll (Ljava/util/Collection;)Z
public fun set (ILcom/android/tools/smali/dexlib2/iface/ClassDef;)Lcom/android/tools/smali/dexlib2/iface/ClassDef;
public synthetic fun set (ILjava/lang/Object;)Ljava/lang/Object;
public final fun size ()I
public fun subList (II)Ljava/util/List;
public fun toArray ()[Ljava/lang/Object;
public fun toArray ([Ljava/lang/Object;)[Ljava/lang/Object;
public final class app/revanced/patcher/util/PatchClasses {
public final fun classBy (Ljava/lang/String;)Lcom/android/tools/smali/dexlib2/iface/ClassDef;
public final fun classBy (Lkotlin/jvm/functions/Function1;)Lcom/android/tools/smali/dexlib2/iface/ClassDef;
public final fun forEach (Lkotlin/jvm/functions/Function1;)V
public final fun mutableClassBy (Lcom/android/tools/smali/dexlib2/iface/ClassDef;)Lapp/revanced/patcher/util/proxy/mutableTypes/MutableClass;
public final fun mutableClassBy (Ljava/lang/String;)Lapp/revanced/patcher/util/proxy/mutableTypes/MutableClass;
public final fun mutableClassBy (Lkotlin/jvm/functions/Function1;)Lapp/revanced/patcher/util/proxy/mutableTypes/MutableClass;
}

public final class app/revanced/patcher/util/proxy/mutableTypes/MutableAnnotation : com/android/tools/smali/dexlib2/base/BaseAnnotation {
Expand Down
80 changes: 51 additions & 29 deletions src/main/kotlin/app/revanced/patcher/patch/BytecodePatchContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import app.revanced.patcher.PatcherResult
import app.revanced.patcher.extensions.InstructionExtensions.instructionsOrNull
import app.revanced.patcher.util.ClassMerger.merge
import app.revanced.patcher.util.MethodNavigator
import app.revanced.patcher.util.ProxyClassList
import app.revanced.patcher.util.PatchClasses
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.Opcodes
import com.android.tools.smali.dexlib2.iface.ClassDef
Expand Down Expand Up @@ -43,20 +43,20 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi
/**
* The list of classes.
*/
val classes = ProxyClassList(
val classes = PatchClasses(
MultiDexIO.readDexFile(
true,
config.apkFile,
BasicDexFileNamer(),
null,
null,
).also { opcodes = it.opcodes }.classes.toMutableList(),
).also { opcodes = it.opcodes }.classes.associateBy { it.type }.toMutableMap(),
)

/**
* 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.pool.values) }

/**
* Merge the extension of [bytecodePatch] into the [BytecodePatchContext].
Expand All @@ -67,11 +67,10 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi
internal fun mergeExtension(bytecodePatch: BytecodePatch) {
bytecodePatch.extensionInputStream?.get()?.use { extensionStream ->
RawDexIO.readRawDexFile(extensionStream, 0, null).classes.forEach { classDef ->
val existingClass = lookupMaps.classesByType[classDef.type] ?: run {
val existingClass = classes.classBy(classDef.type) ?: run {
logger.fine { "Adding class \"$classDef\"" }

classes += classDef
lookupMaps.classesByType[classDef.type] = classDef
classes.addClass(classDef)

return@forEach
}
Expand All @@ -84,29 +83,59 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi
return@let
}

classes -= existingClass
classes += mergedClass
classes.addClass(mergedClass)
}
}
} ?: logger.fine("No extension to merge")
}

/**
* Find a class with a predicate.
*
* @param classType The full classname.
* @return An immutable instance of the class type.
* @see mutableClassBy
*/
fun classBy(classType: String) = classes.classBy(classType)

/**
* Find a class with a predicate.
*
* @param predicate A predicate to match the class.
* @return A proxy for the first class that matches the predicate.
* @return An immutable instance of the class type.
* @see mutableClassBy
*/
fun classBy(predicate: (ClassDef) -> Boolean) = classes.proxy(predicate)
fun classBy(predicate: (ClassDef) -> Boolean) = classes.classBy(predicate)

/**
* Proxy the class to allow mutation.
* Find a class with a predicate.
*
* @param classDef The class to proxy.
* @param classType The full classname.
* @return A mutable version of the class type.
*/
fun mutableClassBy(classType: String) = classes.mutableClassBy(classType)

/**
* Find a class with a predicate.
*
* @return A proxy for the class.
* @param classDef An immutable class.
* @return A mutable version of the class definition.
*/
fun proxy(classDef: ClassDef) = classes.proxy(classDef)
fun mutableClassBy(classDef: ClassDef) = classes.mutableClassBy(classDef)

/**
* Find a class with a predicate.
*
* @param predicate A predicate to match the class.
* @return A mutable class that matches the predicate.
*/
fun mutableClassBy(predicate: (ClassDef) -> Boolean) = classes.mutableClassBy(predicate)

/**
* @return The mutable instance of an immutable class.
*/
@Deprecated("Instead use `mutableClassBy(String)`, `mutableClassBy(ClassDef)`, or `mutableClassBy(predicate)`")
fun proxy(classDef: ClassDef) = classes.mutableClassBy(classDef)

/**
* Navigate a method.
Expand Down Expand Up @@ -140,7 +169,7 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi
this,
BasicDexFileNamer(),
object : DexFile {
override fun getClasses() = this@BytecodePatchContext.classes.toSet()
override fun getClasses() = this@BytecodePatchContext.classes.pool.values.toSet()

override fun getOpcodes() = this@BytecodePatchContext.opcodes
},
Expand All @@ -156,25 +185,22 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi
}

/**
* A lookup map for methods and the class they are a member of and classes.
* A lookup map for strings and the methods they are a member of.
*
* @param classes The list of classes to create the lookup maps from.
*/
internal class LookupMaps internal constructor(classes: List<ClassDef>) : Closeable {
internal class LookupMaps internal constructor(classes: Collection<ClassDef>) : Closeable {
/**
* Methods associated by strings referenced in them.
*/
internal val methodsByStrings = MethodClassPairsLookupMap()

// Lookup map for fast checking if a class exists by its type.
val classesByType = mutableMapOf<String, ClassDef>().apply {
classes.forEach { classDef -> put(classDef.type, classDef) }
}

init {
classes.forEach { classDef ->
classDef.methods.forEach { method ->
val methodClassPair: MethodClassPair = method to classDef
val methodClassPair: MethodClassPair by lazy {
method to classDef
}

// Add strings contained in the method as the key.
method.instructionsOrNull?.forEach { instruction ->
Expand All @@ -186,22 +212,18 @@ class BytecodePatchContext internal constructor(private val config: PatcherConfi

methodsByStrings[string] = methodClassPair
}

// In the future, the class type could be added to the lookup map.
// This would require MethodFingerprint to be changed to include the class type.
}
}
}

override fun close() {
methodsByStrings.clear()
classesByType.clear()
}
}

override fun close() {
lookupMaps.close()
classes.clear()
classes.close()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/app/revanced/patcher/util/ClassMerger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ internal object ClassMerger {
callback(targetClass)

targetClass.superclass ?: return
this.classBy { targetClass.superclass == it.type }?.let {
this.mutableClassBy { targetClass.superclass == it.type }?.let {
traverseClassHierarchy(it, callback)
}
}
Expand Down
11 changes: 2 additions & 9 deletions src/main/kotlin/app/revanced/patcher/util/MethodNavigator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class MethodNavigator internal constructor(
*
* @return The last navigated method mutably.
*/
fun stop() = classBy(matchesCurrentMethodReferenceDefiningClass)!!.firstMethodBySignature
fun stop() = mutableClassBy(lastNavigatedMethodReference.definingClass).firstMethodBySignature
as MutableMethod

/**
Expand All @@ -95,14 +95,7 @@ class MethodNavigator internal constructor(
*
* @return The last navigated method immutably.
*/
fun original(): Method = classes.first(matchesCurrentMethodReferenceDefiningClass).firstMethodBySignature

/**
* Predicate to match the class defining the current method reference.
*/
private val matchesCurrentMethodReferenceDefiningClass = { classDef: ClassDef ->
classDef.type == lastNavigatedMethodReference.definingClass
}
fun original(): Method = classes.classBy(lastNavigatedMethodReference.definingClass)!!.firstMethodBySignature

/**
* Find the first [lastNavigatedMethodReference] in the class.
Expand Down
93 changes: 93 additions & 0 deletions src/main/kotlin/app/revanced/patcher/util/PatchClasses.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package app.revanced.patcher.util

import app.revanced.patcher.patch.PatchException
import app.revanced.patcher.util.proxy.mutableTypes.MutableClass
import com.android.tools.smali.dexlib2.iface.ClassDef
import kotlin.collections.mutableMapOf

@Deprecated("Instead use PatchClasses")
typealias ProxyClassList = PatchClasses

/**
* A set of all classes for the target app and any extension classes.
*/
class PatchClasses internal constructor(
/**
* Pool of both immutable and mutable classes.
*/
internal val pool: MutableMap<String, ClassDef>
) {
/**
* Mutable classes. All instances are also found in [pool].
*/
private val mutablePool = mutableMapOf<String, MutableClass>()

internal fun addClass(classDef: ClassDef) {
pool[classDef.type] = classDef
}

internal fun close() {
pool.clear()
mutablePool.clear()
}

/**
* Iterate over all classes.
*/
fun forEach(action: (ClassDef) -> Unit) {
pool.values.forEach(action)
}

/**
* Find a class with a predicate.
*
* @param classType The full classname.
* @return An immutable instance of the class type.
* @see mutableClassBy
*/
fun classBy(classType: String) = pool[classType]

/**
* Find a class with a predicate.
*
* @param predicate A predicate to match the class.
* @return An immutable instance of the class type.
*/
fun classBy(predicate: (ClassDef) -> Boolean) = pool.values.find(predicate)

/**
* Find a class with a predicate.
*
* @param classDefType The full classname.
* @return A mutable version of the class type.
*/
fun mutableClassBy(classDefType: String) =
mutablePool[classDefType] ?: MutableClass(
pool.get(classDefType) ?: throw PatchException("Could not find class: $classDefType")
).also {
mutablePool[classDefType] = it
pool[classDefType] = it
}

/**
* Find a class with a predicate.
*
* @param classDef An immutable class.
* @return A mutable version of the class definition.
*/
fun mutableClassBy(classDef: ClassDef) =
mutablePool[classDef.type] ?: MutableClass(classDef).also {
val classType = classDef.type
mutablePool[classType] = it
pool[classType] = it
}

/**
* Find a class with a predicate.
*
* @param predicate A predicate to match the class.
* @return A mutable class that matches the predicate.
*/
fun mutableClassBy(predicate: (ClassDef) -> Boolean) =
mutablePool.values.find { predicate(it) } ?: pool.values.find(predicate)?.let { mutableClassBy(it) }
}
Loading

0 comments on commit cc4f158

Please sign in to comment.