refactor: Only check literal instead of additionally the opcode

This commit is contained in:
oSumAtrIX
2023-10-14 00:59:58 +02:00
parent 5ace3bb568
commit c4cb1e6f99
7 changed files with 24 additions and 25 deletions

View File

@ -7,7 +7,6 @@ import app.revanced.patcher.patch.PatchException
import app.revanced.patcher.util.proxy.mutableTypes.MutableClass
import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod
import app.revanced.patches.shared.mapping.misc.ResourceMappingPatch
import com.android.tools.smali.dexlib2.Opcode
import com.android.tools.smali.dexlib2.iface.Method
import com.android.tools.smali.dexlib2.iface.instruction.WideLiteralInstruction
import com.android.tools.smali.dexlib2.util.MethodUtil
@ -58,38 +57,38 @@ fun MutableMethod.injectHideViewCall(
)
/**
* Find the index of the first constant instruction with the id of the given resource name.
* Find the index of the first instruction with the id of the given resource name.
*
* @param resourceName the name of the resource to find the id for.
* @return the index of the first constant instruction with the id of the given resource name, or -1 if not found.
* @return the index of the first instruction with the id of the given resource name, or -1 if not found.
*/
fun Method.findIndexForIdResource(resourceName: String): Int {
fun getIdResourceId(resourceName: String) = ResourceMappingPatch.resourceMappings.single {
it.type == "id" && it.name == resourceName
}.id
return indexOfFirstConstantInstructionValue(getIdResourceId(resourceName))
return indexOfFirstWideLiteralInstructionValue(getIdResourceId(resourceName))
}
/**
* Find the index of the first constant instruction with the given value.
* Find the index of the first wide literal instruction with the given value.
*
* @return the first constant instruction with the value, or -1 if not found.
* @return the first literal instruction with the value, or -1 if not found.
*/
fun Method.indexOfFirstConstantInstructionValue(constantValue: Long) = implementation?.let {
fun Method.indexOfFirstWideLiteralInstructionValue(literal: Long) = implementation?.let {
it.instructions.indexOfFirst { instruction ->
instruction.opcode == Opcode.CONST && (instruction as WideLiteralInstruction).wideLiteral == constantValue
(instruction as? WideLiteralInstruction)?.wideLiteral == literal
}
} ?: -1
/**
* Check if the method contains a constant with the given value.
* Check if the method contains a literal with the given value.
*
* @return if the method contains a constant with the given value.
* @return if the method contains a literal with the given value.
*/
fun Method.containsConstantInstructionValue(constantValue: Long) =
indexOfFirstConstantInstructionValue(constantValue) >= 0
fun Method.containsWideLiteralInstructionValue(literal: Long) =
indexOfFirstWideLiteralInstructionValue(literal) >= 0
/**