Callback.java
7.15 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* This file is part of LiteLoader.
* Copyright (C) 2012-16 Adam Mummery-Smith
* All Rights Reserved.
*/
package com.mumfrey.liteloader.transformers;
import java.util.ArrayList;
import java.util.List;
import com.mumfrey.liteloader.core.runtime.Obf;
/**
* Target information for injected callback methods
*
* @author Adam Mummery-Smith
*/
public class Callback
{
/**
* Type of callback to inject
*/
public enum CallbackType
{
/**
* Redirect callbacks are injected at the start of a method and
* immediately return, thus short-circuiting the method
*/
REDIRECT(true),
/**
* Event callbacks are injected at the start of a method but do not
* alter the normal method behaviour.
*/
EVENT(false),
/**
* Return callbacks are injected immediately prior to every RETURN
* opcode in a particular method. Callback handlers must have the SAME
* return type as the method containing the injected callback
*/
RETURN(false),
/**
* A profiler callback injected at a profiler "startSection" invocation
*/
PROFILER_STARTSECTION(Obf.startSection, true),
/**
* A profiler callback injected at a profiler "endSection" invocation
*/
PROFILER_ENDSECTION(Obf.endSection, false),
/**
* A profiler callback injected at a profiler "endStartSection"
* invocation
*/
PROFILER_ENDSTARTSECTION(Obf.endStartSection, true);
/**
*
*/
private final boolean injectReturn;
/**
*
*/
private final boolean isProfilerCallback;
/**
*
*/
private final boolean sectionRequired;
/**
*
*/
private final Obf profilerMethod;
private CallbackType(boolean returnFrom)
{
this.injectReturn = returnFrom;
this.isProfilerCallback = false;
this.profilerMethod = null;
this.sectionRequired = false;
}
private CallbackType(Obf profilerMethod, boolean sectionRequired)
{
this.injectReturn = false;
this.isProfilerCallback = true;
this.profilerMethod = profilerMethod;
this.sectionRequired = sectionRequired;
}
boolean injectReturn()
{
return this.injectReturn;
}
boolean isProfilerCallback()
{
return this.isProfilerCallback;
}
String getProfilerMethod(int obfType)
{
return this.profilerMethod != null ? this.profilerMethod.names[obfType] : "";
}
String getProfilerMethodSignature()
{
return this.sectionRequired ? "(Ljava/lang/String;)V" : "()V";
}
boolean isSectionRequired()
{
return this.sectionRequired;
}
public String getSignature()
{
if (this == CallbackType.EVENT || this == CallbackType.REDIRECT)
{
return "head";
}
return this.name().toString().toLowerCase();
}
}
/**
*
*/
private final CallbackType callbackType;
/**
*
*/
private final String sectionName;
/**
*
*/
private final String profilerMethod;
/**
* Callback class reference
*/
private final String callbackClass;
/**
* Callback method name
*/
private final String callbackMethod;
/**
* Return callbacks are injected before every RETURN opcode in the method,
* each RETURN is thus allocated a sequential refNumber which is passed to
* the callback method so that the callback handler can choose which RETURN
* it wishes to handle.
*/
int refNumber;
/**
*
*/
private final List<Callback> chainedCallbacks;
public Callback(CallbackType callbackType, String callbackMethod, String callbackClass)
{
this(callbackType, callbackMethod, callbackClass, null, 0);
}
/**
* A new callback method in the specified class
*
* @param callbackMethod Method to call, must be public, static and have the
* appropriate signature for the type of injected callback
* @param callbackClass Fully qualified name of the class containing the
* callback method, must also be public or visible from the calling
* package
*/
public Callback(CallbackType callbackType, String callbackMethod, String callbackClass, String section, int obfType)
{
if (section == null && callbackType.isSectionRequired())
{
throw new RuntimeException(String.format("Callback of type %s requires a section name but no section name was provided",
callbackType.name()));
}
this.callbackType = callbackType;
this.callbackClass = callbackClass.replace('.', '/');
this.callbackMethod = callbackMethod;
this.sectionName = section;
this.chainedCallbacks = new ArrayList<Callback>();
this.profilerMethod = callbackType.getProfilerMethod(obfType);
}
private Callback(Callback other, int refNumber)
{
this.callbackType = other.callbackType;
this.callbackClass = other.callbackClass;
this.callbackMethod = other.callbackMethod;
this.sectionName = other.sectionName;
this.chainedCallbacks = other.chainedCallbacks;
this.profilerMethod = other.profilerMethod;
this.refNumber = refNumber;
}
public CallbackType getType()
{
return this.callbackType;
}
public String getCallbackClass()
{
return this.callbackClass;
}
public String getCallbackMethod()
{
return this.callbackMethod;
}
public boolean injectReturn()
{
return this.callbackType.injectReturn();
}
public boolean isProfilerCallback()
{
return this.callbackType.isProfilerCallback();
}
public String getSectionName()
{
return this.sectionName;
}
public String getProfilerMethod()
{
return this.profilerMethod;
}
public String getProfilerMethodSignature()
{
return this.callbackType.getProfilerMethodSignature();
}
public Callback getNextCallback()
{
return new Callback(this, this.refNumber++);
}
void addChainedCallback(Callback chained)
{
this.chainedCallbacks.add(chained);
}
public List<Callback> getChainedCallbacks()
{
return this.chainedCallbacks;
}
@Override
public String toString()
{
return this.callbackMethod;
}
@Override
public boolean equals(Object other)
{
if (other == null || !(other instanceof Callback)) return false;
Callback callback = (Callback)other;
return callback.callbackClass.equals(this.callbackClass) && callback.callbackMethod.equals(this.callbackMethod)
&& callback.callbackType == this.callbackType;
}
@Override
public int hashCode()
{
return super.hashCode();
}
}