-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathinjector.js
375 lines (341 loc) · 12.9 KB
/
injector.js
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
globalThis.ModAPIVersion = "v2.7";
globalThis.doEaglerforge = true;
document.querySelector("title").innerText = `EaglerForge Injector ${ModAPIVersion}`;
document.querySelector("h1").innerText = `EaglerForge Injector ${ModAPIVersion}`;
function wait(ms) {
return new Promise((resolve, reject) => {
setTimeout(() => { resolve(); }, ms);
});
}
function _status(x) {
document.querySelector("#status").innerText = x;
}
function entriesToStaticVariableProxy(entries, prefix, clinitList) {
prefix = prefix.replace(
"var ",
""
);
if (entries.length === 0) {
return `ModAPI.hooks._rippedStaticProperties[\`${prefix}\`]={};`;
}
if (clinitList.includes(prefix + "_$callClinit")) {
entries.push({
name: "$callClinit",
variable: prefix + "_$callClinit"
});
}
var getComponents = "";
entries.forEach((entry) => {
getComponents += `
case \`${entry.name}\`: return ${entry.variable};`;
});
getComponents += `
default: return Reflect.get(a,b,c);`
var setComponents = "";
entries.forEach((entry) => {
setComponents += `
case \`${entry.name}\`: ${entry.variable} = c; break;`;
});
setComponents += ` default: a[b]=c;`
/*/
ModAPI.hooks._rippedStaticIndexer[\`${prefix.replace(
"var ",
""
)}\`] = [${entries
.flatMap((x) => {
return '"' + x.name + '"';
})
.join(",")}];
/*/
var proxy = `ModAPI.hooks._rippedStaticProperties[\`${prefix.replace(
"var ",
""
)}\`] = new Proxy({${entries
.flatMap((x) => {
return '"' + x.name + '"';
})
.join(":null,") + (entries.length > 0 ? ":null" : "")
}}, {
get: function (a,b,c) {
switch (b) {
${getComponents}
}
},
set: function (a,b,c) {
switch (b) {
${setComponents}
}
return true;
}
});`;
return proxy;
}
async function processClasses(string) {
if (globalThis.doShronk) {
if (!confirm("The minify step is extremely slow, especially on lower-end devices, and can take upwards of 15 minutes.")) {
return;
}
}
_status("Beginning patch process...");
await wait(50);
var patchedFile = string;
patchedFile = patchedFile.replaceAll(
`(function(root, module) {`,
`${modapi_preinit}
(function(root, module) {`
);
patchedFile = patchedFile.replaceAll(
`var main;(function(){`,
`${modapi_preinit}
var main;(function(){`
);
_status("Patching threads and reflect metadata...");
await wait(50);
patchedFile = patchedFile
.replace("\r", "")
.replace(
"var main;\n(function() {",
modapi_preinit + "var main;\n(function() {"
);
patchedFile = patchedFile.replace(
/function \$rt_metadata\(data\)( ?){/gm,
`function $rt_metadata(data) {
/*/EaglerForge Client Patch/*/
ModAPI.hooks._rippedData.push(data);
/*/EaglerForge Client Patch/*/`
);
patchedFile = patchedFile.replaceAll(
`return thread != null && thread.isResuming()`,
(match) => {
return freezeCallstack + match;
}
);
patchedFile = patchedFile.replaceAll(
`return thread != null && thread.isSuspending();`,
(match) => {
return freezeCallstack + match;
}
);
patchedFile = patchedFile.replaceAll(
`return $rt_currentNativeThread;`,
(match) => {
return (
`if(ModAPI.hooks.freezeCallstack){return {isResuming: ()=>{false}, isSuspending: ()=>{false}, push: (a)=>{}, pop: ()=>{console.warn("Frozen stack was popped, context is now unstable.")}}};` +
match
);
}
);
patchedFile = patchedFile.replaceAll("function TeaVMThread(", "globalThis.ModAPI.hooks.TeaVMThread = TeaVMThread;\nfunction TeaVMThread(");
_status("Getting clinit list...");
var clinitList = [...patchedFile.matchAll(/^[\t ]*function \S+?_\S+?_\$callClinit\(/gm)].map(x => x[0].replaceAll("function ", "").replaceAll("(", "").trim());
console.log(clinitList);
_status("Extracting constructors and methods...");
await wait(50);
const extractConstructorRegex =
/^\s*function (\S*?)__init_\d*?\((?!\$)/gm;
const extractConstructorFullNameRegex =
/function (\S*?)__init_[0-9]*/gm;
patchedFile = patchedFile.replaceAll(
extractConstructorRegex,
(match) => {
var fullName = match.match(extractConstructorFullNameRegex);
fullName = fullName[0].replace("function ", "");
return (
`ModAPI.hooks._rippedConstructors[\`${fullName}\`] = ${fullName};
` + match
);
}
);
const extractInternalConstructorRegex =
/^\s*function (\S*?)__init_\d*?\(\$this/gm; //same as extract constructor regex, but only allow $this as first argument
patchedFile = patchedFile.replaceAll(
extractInternalConstructorRegex,
(match) => {
var fullName = match.match(extractConstructorFullNameRegex);
fullName = fullName[0].replace("function ", "");
return (
`ModAPI.hooks._rippedInternalConstructors[\`${fullName}\`] = ${fullName};
` + match
);
}
);
const extractInstanceMethodRegex =
/^[\t ]*function \S+?_\S+?_\S+?\((\$this)?/gm; // /^[\t ]*function \S+?_\S+?_\S+?\(\$this/gm
const extractInstanceMethodFullNameRegex = /function (\S*?)\(/gm; // /function (\S*?)\(\$this/gm
patchedFile = patchedFile.replaceAll(
extractInstanceMethodRegex,
(match) => {
if (
match.includes("__init_") ||
match.includes("__clinit_") ||
match.includes("_$callClinit")
) {
return match;
}
var fullName = match.match(extractInstanceMethodFullNameRegex);
fullName = fullName[0].replace("function ", "").replace("(", "");
return (
`function ${fullName}(...args) {
return ModAPI.hooks.methods[\`${fullName}\`].apply(this, args);
}
ModAPI.hooks._rippedMethodTypeMap[\`${fullName}\`] = \`${match.endsWith("($this") ? "instance" : "static"
}\`;
ModAPI.hooks.methods[\`${fullName}\`]=` +
match.replace(fullName + "(", "(")
);
}
);
var staticVariables = [
...patchedFile.matchAll(/var \S+?_\S+?_\S+? = /gm),
].flatMap((x) => {
return x[0];
}).filter(x => {
return (!x.includes("$_clinit_$")) && (!x.includes("$lambda$"))
});
//Also stores classes from $rt_classWithoutFields(0)
patchedFile = patchedFile.replaceAll(
/var \S+?_\S+? = \$rt_classWithoutFields\(\S*?\);/gm,
function (match) {
var prefix = match.replaceAll(
/ = \$rt_classWithoutFields\(\S*?\);/gm,
""
);
var entries = [];
staticVariables.forEach((entry) => {
if (entry.startsWith(prefix)) {
var variableName = entry
.replace("var ", "")
.replace(" = ", "");
var segments = variableName.split("_");
segments.splice(0, 2);
var name = segments.join("_");
entries.push({
name: name,
variable: variableName,
});
}
});
var proxy = entriesToStaticVariableProxy(entries, prefix, clinitList);
var shortPrefix = prefix.replace(
"var ",
""
);
return match + `ModAPI.hooks._rippedInterfaceMap[\`${shortPrefix}\`]=${shortPrefix};` + proxy;
}
);
//Edge cases. sigh
//Done: add support for static properties on classes with constructors like this: function nmcg_GuiMainMenu() {
patchedFile = patchedFile.replaceAll(
/function [a-z]+?_([a-zA-Z0-9\$]+?)\(\) \{/gm,
(match) => {
var prefix = "var " + match.replace("function ", "").replace("() {", "");
var entries = [];
staticVariables.forEach((entry) => {
if (entry.startsWith(prefix)) {
var variableName = entry
.replace("var ", "")
.replace(" = ", "");
var segments = variableName.split("_");
segments.splice(0, 2);
var name = segments.join("_");
entries.push({
name: name,
variable: variableName,
});
}
});
var proxy = entriesToStaticVariableProxy(entries, prefix, clinitList);
return proxy + "\n" + match;
}
);
_status("Extracting teavm internals...");
await wait(50);
patchedFile = patchedFile.replaceAll(
/function \$rt_\S+?\(/gm,
(match) => {
var name = match.replace("function ", "");
name = name.substring(0, name.length - 1);
return (
`ModAPI.hooks._teavm[\`${name}\`]=${name};
` + match
);
}
);
_status("Applying bonus patches from patch registry...");
await wait(50);
patchedFile = PatchesRegistry.patchFile(patchedFile);
if (globalThis.doShronk) {
_status("Shrinking file...");
await wait(50);
patchedFile = await shronk(patchedFile);
}
_status("Injecting scripts...");
await wait(50);
patchedFile = patchedFile.replace(
` id="game_frame">`,
` id="game_frame">
\<script id="modapi_patchesreg_events"\>${PatchesRegistry.getEventInjectorCode()};\<\/script\>
\<script id="modapi_postinit"\>${globalThis.modapi_postinit.replace("__modapi_version_code__", ModAPIVersion)}\<\/script\>
\<script id="modapi_modloader"\>${globalThis.modapi_modloader}\<\/script\>
\<script id="modapi_guikit"\>${globalThis.modapi_guikit}\<\/script\>
\<script id="modapi_postinit_data"\>globalThis.modapi_postinit = \`${globalThis.modapi_postinit.replaceAll("\\", "\\\\")}\`\<\/script\>
\<script id="libserverside"\>{"._|_libserverside_|_."}\<\/script\>
\<script id="__eaglerforgeinjector_installation_flag__"\>console.log("Thank you for using EaglerForge!");\<\/script\>`
);
patchedFile = patchedFile.replace(`<title>EaglercraftX 1.8</title>`, `<title>EFI ${globalThis.ModAPIVersion}</title>`);
patchedFile = patchedFile.replaceAll(/main\(\);\s*?}/gm, (match) => {
return match.replace("main();", "main();ModAPI.hooks._postInit();");
});
_status("Done, awaiting input...");
await wait(50);
return patchedFile;
}
document.querySelector("#giveme").addEventListener("click", () => {
if (
!document.querySelector("input").files ||
!document.querySelector("input").files[0]
) {
return;
}
// @type File
var file = document.querySelector("input").files[0];
var fileType = file.name.split(".");
fileType = fileType[fileType.length - 1];
file.text().then(async (string) => {
var patchedFile = string;
if (globalThis.doEaglerforge) {
if (string.includes("__eaglerforgeinjector_installation_flag__")) {
return alert("this file already has eaglerforge injected in it, you nonce.\nif you're trying to update, you need a vanilla file.");
}
patchedFile = await processClasses(patchedFile);
} else if (globalThis.doShronk) {
patchedFile = await shronk(patchedFile);
}
patchedFile.replace(`{"._|_libserverside_|_."}`, "");
var blob = new Blob([patchedFile], { type: file.type });
saveAs(blob, "processed." + fileType);
});
});
document.querySelector("#givemeserver").addEventListener("click", () => {
if (
!document.querySelector("input").files ||
!document.querySelector("input").files[0]
) {
return;
}
// @type File
var file = document.querySelector("input").files[0];
var fileType = file.name.split(".");
fileType = fileType[fileType.length - 1];
file.text().then(async (string) => {
var patchedFile = string;
if (globalThis.doEaglerforge) {
patchedFile = await processClasses(patchedFile);
} else if (globalThis.doShronk) {
patchedFile = await shronk(patchedFile);
}
patchedFile.replace(`{"._|_libserverside_|_."}`, `(${EFServer.toString()})()`);
var blob = new Blob([patchedFile], { type: file.type });
saveAs(blob, "efserver." + fileType);
});
});