forked from ReVanced/revanced-patcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MethodSignature.kt
102 lines (95 loc) 路 3.52 KB
/
MethodSignature.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package app.revanced.patcher.signature
import app.revanced.patcher.MethodNotFoundException
import org.jf.dexlib2.Opcode
/**
* Represents the [MethodSignature] for a method.
* @param metadata Metadata for this [MethodSignature].
* @param returnType The return type of the method.
* @param accessFlags The access flags of the method.
* @param methodParameters The parameters of the method.
* @param opcodes The list of opcodes of the method.
*/
class MethodSignature(
val metadata: MethodSignatureMetadata,
internal val returnType: String?,
internal val accessFlags: Int?,
internal val methodParameters: Iterable<String>?,
internal val opcodes: Iterable<Opcode>?
) {
/**
* The result of the signature
*/
var result: SignatureResolverResult? = null // TODO: figure out how to get rid of nullable
get() {
return field ?: throw MethodNotFoundException(
"Could not resolve required signature ${metadata.name}"
)
}
val resolved: Boolean
get() {
return result != null
}
}
/**
* Metadata about a [MethodSignature].
* @param name A suggestive name for the [MethodSignature].
* @param methodMetadata Metadata about the method for the [MethodSignature].
* @param patternScanMethod The pattern scanning method the pattern scanner should rely on.
* Can either be [PatternScanMethod.Fuzzy] or [PatternScanMethod.Direct].
* @param description An optional description of the [MethodSignature].
* @param compatiblePackages The list of packages the [MethodSignature] is compatible with.
* @param version The version of this signature.
*/
data class MethodSignatureMetadata(
val name: String,
val methodMetadata: MethodMetadata,
val patternScanMethod: PatternScanMethod,
val compatiblePackages: Iterable<String>,
val description: String?,
val version: String
)
/**
* Metadata about the method for a [MethodSignature].
* @param definingClass The defining class name of the method.
* @param name A suggestive name for the method which the [MethodSignature] was created for.
*/
data class MethodMetadata(
val definingClass: String?,
val name: String?
)
/**
* The method, the patcher should rely on when scanning the opcode pattern of a [MethodSignature]
*/
interface PatternScanMethod {
/**
* When comparing the signature, if one or more of the opcodes do not match, skip.
*/
class Direct : PatternScanMethod
/**
* When comparing the signature, if [threshold] or more of the opcodes do not match, skip.
*/
class Fuzzy(internal val threshold: Int) : PatternScanMethod {
/**
* A list of warnings the resolver found.
*
* This list will be allocated when the signature has been found.
* Meaning, if the signature was not found,
* or the signature was not yet resolved,
* the list will be null.
*/
lateinit var warnings: List<Warning>
/**
* Represents a resolver warning.
* @param expected The opcode the signature expected it to be.
* @param actual The actual opcode it was. Always different from [expected].
* @param expectedIndex The index for [expected]. Relative to the instruction list.
* @param actualIndex The index for [actual]. Relative to the pattern list from the signature.
*/
data class Warning(
val expected: Opcode,
val actual: Opcode,
val expectedIndex: Int,
val actualIndex: Int,
)
}
}