refactor: config override

- fix: hidden sc plus features
This commit is contained in:
rhunk
2023-10-27 01:05:26 +02:00
parent 233d043d45
commit 2b0b14a60f
5 changed files with 137 additions and 31 deletions

View File

@ -20,9 +20,9 @@ class MapperContext(
return classMap[name.toString()]
}
private val mappings = mutableMapOf<String, Any>()
private val mappings = mutableMapOf<String, Any?>()
fun addMapping(key: String, vararg array: Pair<String, Any>) {
fun addMapping(key: String, vararg array: Pair<String, Any?>) {
mappings[key] = array.toMap()
}

View File

@ -1,10 +1,11 @@
package me.rhunk.snapenhance.mapper.impl
import me.rhunk.snapenhance.mapper.AbstractClassMapper
import me.rhunk.snapenhance.mapper.ext.findConstString
import me.rhunk.snapenhance.mapper.ext.getClassName
import me.rhunk.snapenhance.mapper.ext.hasStaticConstructorString
import me.rhunk.snapenhance.mapper.ext.isEnum
import me.rhunk.snapenhance.mapper.ext.*
import org.jf.dexlib2.iface.instruction.formats.Instruction21c
import org.jf.dexlib2.iface.instruction.formats.Instruction35c
import org.jf.dexlib2.iface.reference.FieldReference
import org.jf.dexlib2.iface.reference.MethodReference
import java.lang.reflect.Modifier
class CompositeConfigurationProviderMapper : AbstractClassMapper() {
@ -32,7 +33,35 @@ class CompositeConfigurationProviderMapper : AbstractClassMapper() {
it.parameterTypes[2] == enumType.type
}
val hasExperimentMethodReference = observePropertyMethod.implementation?.instructions?.firstOrNull { instruction ->
if (instruction !is Instruction35c) return@firstOrNull false
(instruction.reference as? MethodReference)?.let { methodRef ->
methodRef.returnType == "Z" && methodRef.parameterTypes.size == 1 && methodRef.parameterTypes[0] == configEnumInterface.type
} == true
}?.let { (it as Instruction35c).reference as MethodReference }
val getBooleanAppExperimentClass = classDef.methods.first {
// search for observeBoolean method
it.parameterTypes.size == 1 &&
it.parameterTypes[0] == configEnumInterface.type &&
it.implementation?.findConstString("observeBoolean") == true
}.let { method ->
// search for static field invocation of GetBooleanAppExperiment class
val getBooleanAppExperimentClassFieldInstruction = method.implementation?.instructions?.firstOrNull { instruction ->
if (instruction !is Instruction21c) return@firstOrNull false
val fieldReference = instruction.reference as? FieldReference ?: return@firstOrNull false
getClass(fieldReference.definingClass)?.methods?.any {
it.returnType == "Ljava/lang/Object;" &&
it.parameterTypes.size == 2 &&
(0..1).all { i -> it.parameterTypes[i] == "Ljava/lang/Object;" }
} == true
}?.let { (it as Instruction21c).reference as FieldReference }
getClass(getBooleanAppExperimentClassFieldInstruction?.definingClass)?.getClassName()
}
val enumGetDefaultValueMethod = configEnumInterface.methods.first { getClass(it.returnType)?.interfaces?.contains("Ljava/io/Serializable;") == true }
val enumGetCategoryMethod = configEnumInterface.methods.first { it.parameterTypes.size == 0 && getClass(it.returnType)?.isEnum() == true }
val defaultValueField = getClass(enumGetDefaultValueMethod.returnType)!!.fields.first {
Modifier.isFinal(it.accessFlags) &&
Modifier.isPublic(it.accessFlags) &&
@ -46,8 +75,16 @@ class CompositeConfigurationProviderMapper : AbstractClassMapper() {
"enum" to mapOf(
"class" to configEnumInterface.getClassName(),
"getValue" to enumGetDefaultValueMethod.name,
"getCategory" to enumGetCategoryMethod.name,
"defaultValueField" to defaultValueField.name
)
),
"appExperimentProvider" to (hasExperimentMethodReference?.let {
mapOf(
"class" to getClass(it.definingClass)?.getClassName(),
"GetBooleanAppExperimentClass" to getBooleanAppExperimentClass,
"hasExperimentMethod" to hasExperimentMethodReference.name
)
})
)
return@mapper
}