mirror of
https://github.com/inotia00/revanced-patches.git
synced 2025-06-12 21:27:43 +02:00
refactor(Utils): appropriate message is displayed in the exception when the index is not found
This commit is contained in:
@ -340,10 +340,81 @@ inline fun <reified T : Reference> Instruction.getReference() =
|
||||
fun Method.indexOfFirstInstruction(predicate: Instruction.() -> Boolean) =
|
||||
this.implementation!!.instructions.indexOfFirst(predicate)
|
||||
|
||||
fun MutableMethod.getTargetIndex(opcode: Opcode) = getTargetIndex(0, opcode)
|
||||
fun MutableMethod.getTargetIndexOrThrow(opcode: Opcode) =
|
||||
getTargetIndexOrThrow(0, opcode)
|
||||
|
||||
fun MutableMethod.getTargetIndexReversed(opcode: Opcode) =
|
||||
getTargetIndexReversed(implementation!!.instructions.size - 1, opcode)
|
||||
fun MutableMethod.getTargetIndexOrThrow(startIndex: Int, opcode: Opcode) =
|
||||
checkIndex(getTargetIndex(startIndex, opcode), startIndex, opcode)
|
||||
|
||||
fun MutableMethod.getTargetIndexReversedOrThrow(opcode: Opcode) =
|
||||
getTargetIndexReversedOrThrow(implementation!!.instructions.lastIndex, opcode)
|
||||
|
||||
fun MutableMethod.getTargetIndexReversedOrThrow(startIndex: Int, opcode: Opcode) =
|
||||
checkIndex(getTargetIndexReversed(startIndex, opcode), startIndex, opcode)
|
||||
|
||||
fun Method.getTargetIndexWithFieldReferenceNameOrThrow(filedName: String) =
|
||||
checkIndex(getTargetIndexWithFieldReferenceName(filedName), 0, filedName)
|
||||
|
||||
fun MutableMethod.getTargetIndexWithFieldReferenceNameOrThrow(startIndex: Int, filedName: String) =
|
||||
checkIndex(getTargetIndexWithFieldReferenceName(startIndex, filedName), startIndex, filedName)
|
||||
|
||||
fun MutableMethod.getTargetIndexWithFieldReferenceNameReversedOrThrow(returnType: String) =
|
||||
getTargetIndexWithFieldReferenceNameReversedOrThrow(implementation!!.instructions.lastIndex, returnType)
|
||||
|
||||
fun MutableMethod.getTargetIndexWithFieldReferenceNameReversedOrThrow(startIndex: Int, returnType: String) =
|
||||
checkIndex(getTargetIndexWithFieldReferenceNameReversed(startIndex, returnType), startIndex, returnType)
|
||||
|
||||
fun Method.getTargetIndexWithFieldReferenceTypeOrThrow(returnType: String) =
|
||||
checkIndex(getTargetIndexWithFieldReferenceType(returnType), 0, returnType)
|
||||
|
||||
fun MutableMethod.getTargetIndexWithFieldReferenceTypeOrThrow(startIndex: Int, returnType: String) =
|
||||
checkIndex(getTargetIndexWithFieldReferenceType(startIndex, returnType), startIndex, returnType)
|
||||
|
||||
fun MutableMethod.getTargetIndexWithFieldReferenceTypeReversedOrThrow(returnType: String) =
|
||||
getTargetIndexWithFieldReferenceTypeReversedOrThrow(implementation!!.instructions.lastIndex, returnType)
|
||||
|
||||
fun MutableMethod.getTargetIndexWithFieldReferenceTypeReversedOrThrow(startIndex: Int, returnType: String) =
|
||||
checkIndex(getTargetIndexWithFieldReferenceTypeReversed(startIndex, returnType), startIndex, returnType)
|
||||
|
||||
fun Method.getTargetIndexWithMethodReferenceNameOrThrow(methodName: String) =
|
||||
checkIndex(getTargetIndexWithMethodReferenceName(methodName), 0, methodName)
|
||||
|
||||
fun MutableMethod.getTargetIndexWithMethodReferenceNameOrThrow(startIndex: Int, methodName: String) =
|
||||
checkIndex(getTargetIndexWithMethodReferenceName(startIndex, methodName), startIndex, methodName)
|
||||
|
||||
fun MutableMethod.getTargetIndexWithMethodReferenceNameReversedOrThrow(methodName: String) =
|
||||
getTargetIndexWithMethodReferenceNameReversedOrThrow(implementation!!.instructions.lastIndex, methodName)
|
||||
|
||||
fun MutableMethod.getTargetIndexWithMethodReferenceNameReversedOrThrow(startIndex: Int, methodName: String) =
|
||||
checkIndex(getTargetIndexWithMethodReferenceNameReversed(startIndex, methodName), startIndex, methodName)
|
||||
|
||||
fun Method.getTargetIndexWithReferenceOrThrow(reference: String) =
|
||||
checkIndex(getTargetIndexWithReference(reference), 0, reference)
|
||||
|
||||
fun MutableMethod.getTargetIndexWithReferenceOrThrow(startIndex: Int, reference: String) =
|
||||
checkIndex(getTargetIndexWithReference(startIndex, reference), startIndex, reference)
|
||||
|
||||
fun MutableMethod.getTargetIndexWithReferenceReversedOrThrow(reference: String) =
|
||||
getTargetIndexWithReferenceReversedOrThrow(implementation!!.instructions.lastIndex, reference)
|
||||
|
||||
fun MutableMethod.getTargetIndexWithReferenceReversedOrThrow(startIndex: Int, reference: String) =
|
||||
checkIndex(getTargetIndexWithReferenceReversed(startIndex, reference), startIndex, reference)
|
||||
|
||||
fun checkIndex(index: Int, startIndex: Int, opcode: Opcode): Int {
|
||||
if (index < 0) {
|
||||
throw PatchException("Target index not found. startIndex: $startIndex, opcode: $opcode")
|
||||
}
|
||||
return index
|
||||
}
|
||||
|
||||
fun checkIndex(index: Int, startIndex: Int, name: String): Int {
|
||||
if (index < 0) {
|
||||
throw PatchException("Target index not found. startIndex: $startIndex, name: $name")
|
||||
}
|
||||
return index
|
||||
}
|
||||
|
||||
fun MutableMethod.getTargetIndex(opcode: Opcode) = getTargetIndex(0, opcode)
|
||||
|
||||
fun MutableMethod.getTargetIndex(startIndex: Int, opcode: Opcode) =
|
||||
implementation!!.instructions.let {
|
||||
@ -352,6 +423,9 @@ fun MutableMethod.getTargetIndex(startIndex: Int, opcode: Opcode) =
|
||||
}
|
||||
}
|
||||
|
||||
fun MutableMethod.getTargetIndexReversed(opcode: Opcode) =
|
||||
getTargetIndexReversed(implementation!!.instructions.size - 1, opcode)
|
||||
|
||||
fun MutableMethod.getTargetIndexReversed(startIndex: Int, opcode: Opcode): Int {
|
||||
for (index in startIndex downTo 0) {
|
||||
if (getInstruction(index).opcode != opcode)
|
||||
@ -368,16 +442,16 @@ fun Method.getTargetIndexWithFieldReferenceName(filedName: String) = implementat
|
||||
}
|
||||
} ?: -1
|
||||
|
||||
fun MutableMethod.getTargetIndexWithFieldReferenceNameReversed(returnType: String) =
|
||||
getTargetIndexWithFieldReferenceTypeReversed(implementation!!.instructions.size - 1, returnType)
|
||||
|
||||
fun MutableMethod.getTargetIndexWithFieldReferenceName(startIndex: Int, filedName: String) =
|
||||
implementation!!.instructions.let {
|
||||
startIndex + it.subList(startIndex, it.size - 1).indexOfFirst { instruction ->
|
||||
startIndex + it.subList(startIndex, it.lastIndex).indexOfFirst { instruction ->
|
||||
instruction.getReference<FieldReference>()?.name == filedName
|
||||
}
|
||||
}
|
||||
|
||||
fun MutableMethod.getTargetIndexWithFieldReferenceNameReversed(returnType: String) =
|
||||
getTargetIndexWithFieldReferenceTypeReversed(implementation!!.instructions.lastIndex, returnType)
|
||||
|
||||
fun MutableMethod.getTargetIndexWithFieldReferenceNameReversed(
|
||||
startIndex: Int,
|
||||
filedName: String
|
||||
|
Reference in New Issue
Block a user