-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchattriggers-asmLib.js
More file actions
258 lines (217 loc) · 9.64 KB
/
Copy pathchattriggers-asmLib.js
File metadata and controls
258 lines (217 loc) · 9.64 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
const ASMAt = Java.type('dev.falsehonesty.asmhelper.dsl.At');
const ASMInjectionPoint = Java.type('dev.falsehonesty.asmhelper.dsl.InjectionPoint');
const ASMDescriptor = Java.type('dev.falsehonesty.asmhelper.dsl.instructions.Descriptor');
const asmInjectHelper = Java.type('com.chattriggers.ctjs.engine.langs.js.JSLoader').INSTANCE.asmInjectHelper;
const asmRemoveHelper = Java.type('com.chattriggers.ctjs.engine.langs.js.JSLoader').INSTANCE.asmRemoveHelper;
const asmFieldHelper = Java.type('com.chattriggers.ctjs.engine.langs.js.JSLoader').INSTANCE.asmFieldHelper;
const ASMMethodKt = Java.type('dev.falsehonesty.asmhelper.dsl.Method');
const proxyInsnList = $ => {
const proxy = new Proxy({builder: $}, {
get(target, key) {
// Here is where new methods are "added". Currently, we only
// add one method: invokeJS.
if (key === 'invokeJS') {
// We have to return a function to make it callable. Inside
// the function, we do our logic (the invoke dynamic calls).
// It is important that we are sure to bind all function calls
// on the InsnListBuilder to that builder. Otherwise, thisObj
// will be set to the global scope.
return functionId => {
let handle = target.builder.handle.bind(target.builder)(
target.builder.H_INVOKESTATIC,
"com/chattriggers/ctjs/launch/IndySupport",
"bootstrapInvokeJS",
"(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/invoke/CallSite;"
);
target.builder.invokeDynamic.bind(target.builder)(
"",
"invokeJSFunction",
"([Ljava/lang/Object;)Ljava/lang/Object;",
handle,
ASM.currentModule,
functionId
);
// We return this proxy, which is like returning the
// original InsnListBuilder for method chaining
return proxy;
};
} else {
// We delegate the call to the InsnListBuilder. However,
// most of the methods on InsnListBuilder will return an
// instance of itself, not our special proxy object. So
// here, we return another proxy which will execute the
// function call, and then return our InsnListBuilder
// proxy
return new Proxy(target.builder[key], {
apply(innerTarget, thisArg, argArray) {
// If any arguments are functions, they are single arguments
// functions that take the plain InsnListBuidler. Make sure
// they actually receive our modified builder
for (let i = 0; i < argArray.length; i++) {
const arg = argArray[i];
if (typeof arg === 'function') {
argArray[i] = $ => arg(proxyInsnList($));
}
}
// Make sure that the target function has the original
// InsnListBuilder object as it's thisObj
const result = innerTarget.bind($)(...argArray);
// The following methods always return something other
// than the builder
if (key === 'makeLabel' || key === 'handle') {
return result;
}
// The following methods only return something other
// than the builder on a particular overload (an overload
// that takes no arguments)
if (argArray.length === 0 && ['astore', 'fstore', 'istore', 'dstore', 'lstore'].includes(key)) {
return result;
}
// The method we've called just returns the builder,
// so instead return our modified proxy
return proxy;
}
})
}
}
});
return proxy;
}
class ASMBuilder {
constructor(className, methodName, descriptor, at) {
this.className = className;
this.methodName = methodName;
this.descriptor = descriptor;
this.at = at;
}
}
class RemoveBuilder extends ASMBuilder {
constructor(className, methodName, descriptor, at) {
super(className, methodName, descriptor, at);
this._methodMaps = {};
}
methodMaps(obj) {
this._methodMaps = obj;
return this;
}
numberToRemove(numberToRemove) {
this._numberToRemove = numberToRemove;
return this;
}
execute() {
asmRemoveHelper(this.className, this.at, this.methodName, this.descriptor, this._methodMaps, this._numberToRemove);
}
}
class FieldBuilder {
constructor(className, fieldName, descriptor, accessTypes) {
this.className = className;
this.fieldName = fieldName;
this.descriptor = descriptor;
this.accessTypes = accessTypes ?? [];
}
initialValue(obj) {
this._initialValue = obj;
return this;
}
execute() {
asmFieldHelper(this.className, this.fieldName, this.descriptor, this._initialValue, this.accessTypes);
}
}
class InjectBuilder extends ASMBuilder {
constructor(className, methodName, descriptor, at) {
super(className, methodName, descriptor, at);
this._methodMaps = {};
this._fieldMaps = {};
}
methodMaps(obj) {
this._methodMaps = obj;
return this;
}
fieldMaps(obj) {
this._fieldMaps = obj;
return this;
}
instructions(insnList) {
// Wrap the insnList in a proxy so we can "add" methods to the
// InsnListBuilder provided by ASMHelper. This proxy delegates
// all gets and calls to the target handler except those specified
// in the 'get' trap
this.insnList = $ => insnList(proxyInsnList($));
return this;
}
execute() {
if (this.insnList === undefined) {
throw new Error('InjectBuilder requires a call to instructions()');
}
asmInjectHelper(this.className, this.at, this.methodName, this.descriptor, this._fieldMaps, this._methodMaps, this.insnList);
}
}
export default class ASM {
static INTEGER = 'java/lang/Integer';
static DOUBLE = 'java/lang/Double';
static LONG = 'java/lang/Long';
static BOOLEAN = 'java/lang/Boolean';
static SHORT = 'java/lang/Short';
static CHARACTER = 'java/lang/Character';
static BYTE = 'java/lang/Byte';
static OBJECT = 'java/lang/Object';
static STRING = 'java/lang/String';
static MINECRAFT = 'net/minecraft/client/Minecraft';
static ENTITY = 'net/minecraft/entity/Entity';
static ENTITY_FX = 'net/minecraft/client/particle/EntityFX';
static ENTITY_ITEM = 'net/minecraft/entity/item/EntityItem';
static ENTITY_PLAYER = 'net/minecraft/entity/player/EntityPlayer';
static FILE = 'java/io/File';
static FRAME_BUFFER = 'net/minecraft/client/shader/Framebuffer';
static ICHATCOMPONENT = 'net/minecraft/util/IChatComponent';
static INVENTORY_PLAYER = 'net/minecraft/entity/player/InventoryPlayer';
static ITEM_STACK = 'net/minecraft/item/ItemStack';
static PACKET = 'net/minecraft/network/Packet';
static BLOCK_POS = 'net/minecraft/util/BlockPos';
static ENUM_FACING = 'net/minecraft/util/EnumFacing';
static CANCELLABLE_EVENT = 'com/chattriggers/ctjs/minecraft/listeners/CancellableEvent';
static TRIGGER_TYPE = 'com/chattriggers/ctjs/triggers/TriggerType';
static currentModule = "";
static JumpCondition = Java.type('dev.falsehonesty.asmhelper.dsl.instructions.JumpCondition');
static AccessType = Java.type('dev.falsehonesty.asmhelper.dsl.writers.AccessType');
static ARRAY(o) {
return `[${o}`;
}
static L(o) {
return `L${o};`;
}
static At(injectionPoint, before = true, shift = 0) {
return new ASMAt(injectionPoint, before, shift);
}
static desc(returnType, ...paramTypes) {
return `(${paramTypes.join('')})${returnType}`;
}
static injectBuilder(className, methodName, descriptor, at) {
return new InjectBuilder(className, methodName, descriptor, at);
}
static removeBuilder(className, methodName, descriptor, at) {
return new RemoveBuilder(className, methodName, descriptor, at);
}
static fieldBuilder(className, fieldName, descriptor, ...accessTypes) {
return new FieldBuilder(className, fieldName, descriptor, accessTypes);
}
static modify(className, block) {
ASMMethodKt.modify(className, block);
}
}
ASM.At.HEAD = ASMInjectionPoint.HEAD.INSTANCE;
ASM.At.RETURN = function (ordinal = null) {
return new ASMInjectionPoint.RETURN(ordinal);
};
ASM.At.INVOKE = function (
owner = throw new Error('ASM.At.INVOKE requires an owner parameter'),
name = throw new Error('ASM.At.INVOKE requires a name parameter'),
descriptor = throw new Error('ASM.At.INVOKE requires a descriptor parameter'),
ordinal = null
) {
return new ASMInjectionPoint.INVOKE(new ASMDescriptor(owner, name, descriptor), ordinal)
};
ASM.At.TAIL = ASMInjectionPoint.TAIL.INSTANCE;
ASM.At.CUSTOM = function (finder = () => ([])) {
return new ASMInjectionPoint.CUSTOM(finder);
};