This repository was archived by the owner on Jun 14, 2024. It is now read-only.
generated from kernel-addons/PluginTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.js
More file actions
372 lines (311 loc) · 10.1 KB
/
renderer.js
File metadata and controls
372 lines (311 loc) · 10.1 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
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
import commonModules from "./common.js";
import "./polyfills.js";
/**
* Discord's webpack cache global name.
*/
const CHUNK_NAME = "webpackChunkdiscord_app";
/**
* Discord's __webpack_require__ instance.
*/
let __webpack_require__ = null;
/**
* Bool if the webpack cache is initialized.
*/
let ready = false;
/**
* Events for the webpack module. Available events:
* `ready` | `push`
*/
const Events = new EventTarget();
/**
* Promise for when discord's webpack cache is fully initialized and ready to use.
*/
const whenReady = new Promise(resolve => {
Events.addEventListener("ready", () => {
resolve();
ready = true;
}, {once: true});
});
/**
* Promise for when discord's global was found.
*/
const globalPromise = new Promise(async onExists => {
while (!Array.isArray(window[CHUNK_NAME])) {
await new Promise(setImmediate);
}
onExists();
});
const Filters = {
byProps(...props) {
const types = ["number", "string"];
return (m) => m && !~types.indexOf(typeof m) && props.every(prop => prop in m);
},
byDisplayName(displayName, defaultExports = false) {
return defaultExports
? (m) => m?.default?.displayName === displayName
: (m) => m?.displayName === displayName;
},
byPrototypes(...protos) {
return (m) => typeof m === "function" && protos.every(p => p in m.prototype);
},
byFunctionStrings(...strings) {
return (m) => typeof m?.default === "function" &&
strings.every(string => ~m.default.toString().indexOf(string));
}
};
/**
* Grabs discord's __webpack_require__ including the cache.
* @returns {any}
*/
const require = function () {
if (__webpack_require__) return __webpack_require__;
const chunk = [[Symbol("webpack")], {}, _ => _];
__webpack_require__ = window[CHUNK_NAME].push(chunk);
window[CHUNK_NAME].splice(window[CHUNK_NAME].indexOf(chunk), 1);
return __webpack_require__;
};
/**
* Finds modules inside the webpack cache.
* @param {(module: any, id: number) => boolean} filter Filter function that validates modules.
* @param {{
* all: boolean,
* force: boolean,
* default: boolean
* }} options
* @returns
*/
const findModule = function (filter, {all = false, force = false, default: defaultExports = true} = {}) {
if (typeof (filter) !== "function") return void 0;
const __webpack_require__ = require();
const found = [];
let hasError = null;
if (!__webpack_require__) return;
const wrapFilter = function (module, index) {
try {return filter(module, index);}
catch (error) {
hasError ??= error;
return false;
}
};
for (const id in __webpack_require__.c) {
const module = __webpack_require__.c[id].exports;
if (!module || module === window) continue;
switch (typeof module) {
case "object": {
if (wrapFilter(module, id)) {
if (!all) return module;
found.push(module);
}
if (module.__esModule &&
module.default != null &&
typeof module.default !== "number" &&
wrapFilter(module.default, id)
) {
const exports = defaultExports ? module.default : module;
if (!all) return exports;
found.push(exports);
}
if (force && module.__esModule) for (const key in module) {
if (!module[key]) continue;
if (wrapFilter(module[key], id)) {
if (!all) return module[key];
found.push(module[key]);
}
}
break;
}
case "function": {
if (wrapFilter(module, id)) {
if (!all) return module;
found.push(module);
}
break;
}
}
}
if (hasError) {
console.warn("[Webpack] filter threw an error. This can cause lag spikes at the user's end. Please fix asap.\n", hasError);
}
return all ? found : found[0];
};
/**
* Alias for findModule.
* @param {(module: any, id: number) => boolean} filter Filter function that validates modules.
* @param {} options
* @returns
*/
const findModules = function (filter, options) {
return findModule(filter, Object.assign({}, options, {all: true}));
};
/**
* Waits for modules.
* @param {(module: any, id: number) => boolean} filter Filter function that validates modules.
* @param {{
* all: boolean,
* force: boolean,
* default: boolean
* }} options
* @returns {Promise<any>}
*/
const waitFor = async function (filter, {retries = 100, all = false, forever = false, delay = 50} = {}) {
for (let i = 0; (i < retries) || forever; i++) {
const module = findModule(filter, {all});
if (module) return module;
await new Promise(res => setTimeout(res, delay));
}
};
/**
* Bulk fetches modules from Webpack. This is a faster way of fetching multiple modules at once.
* Valid options: {wait: boolean, wrap: boolean}
* @param {...any} options Filters & options for the module searcher.
* @returns {any[] | Promise<any>[]}
*/
const bulk = function (...options) {
const [filters, {wait = false, wrap = false, ...rest}] = parseOptions(options);
const found = new Array(filters.length);
const searchFunction = wait ? waitFor : findModule;
const wrappedFilters = wrap ? filters.map(filter => {
if (Array.isArray(filter)) filter = Filters.byProps(...filter);
if (typeof (filter) === "string") filter = Filters.byDisplayName(filter);
return (m) => {
try {return filter(m);}
catch (error) {return false;}
};
}) : filters;
const returnValue = searchFunction((module) => {
for (let i = 0; i < wrappedFilters.length; i++) {
const filter = wrappedFilters[i];
if (typeof filter !== "function" || !filter(module) || found[i] != null) continue;
found[i] = module;
}
return found.filter(Boolean).length === filters.length;
}, rest);
if (wait) return returnValue.then(() => found);
return found;
};
/**
* Finds modules by props.
* @returns {any | Promise<any>}
*/
const findByProps = function (...options) {
const [props, {bulk: findBulk = false, wait = false, ...rest}] = parseOptions(options);
if (!findBulk && !wait) {
return findModule(Filters.byProps(...props), rest);
}
if (wait && !findBulk) {
return waitFor(Filters.byProps(...props), rest);
}
if (findBulk) {
const filters = props.map((actualProps) => Array.isArray(actualProps)
? Filters.byProps(...actualProps)
: Filters.byProps(actualProps)
);
filters.push({wait, ...rest});
return bulk(...filters);
}
return null;
};
/**
* Finds modules by displayName.
* @returns {any | Promise<any>}
*/
const findByDisplayName = function (...options) {
const [displayNames, {bulk: findBulk = false, wait = false, ...rest}] = parseOptions(options);
if (!findBulk && !wait) {
return findModule(Filters.byDisplayName(displayNames[0], rest.default), rest);
}
if (wait && !findBulk) {
return waitFor(Filters.byDisplayName(displayNames[0]), rest);
}
if (findBulk) {
const filters = displayNames.map(Filters.byDisplayName);
filters.push({wait, ...rest});
return bulk(...filters);
}
return null;
};
/**
* Finds the index of a module inside webpack cache.
* @param {(module: any) => boolean} filter Filter to validate modules.
* @returns {number}
*/
const findIndex = function (filter) {
let foundIndex = -1;
findModule((module, index) => {
if (filter(module)) foundIndex = index;
});
return foundIndex;
};
/**
* Gets the exports of a module at a specific index.
* @param {number} index Index inside the webpack cache.
* @returns {any}
*/
const atIndex = function (index) {
return require()?.c[index];
};
/**
* Parses arguments and finds the options.
* @param {any[]} args A list of arguments.
* @param {(module: any) => boolean} filter filter that validates that options exist in the args tree.
* @returns {any[]}
*/
const parseOptions = function (args, filter = thing => (typeof (thing) === "object" && thing != null && !Array.isArray(thing))) {
return [args, filter(args.at(-1)) ? args.pop() : {}];
};
{
const InitializeEvents = ["START_SESSION", "CONNECTION_OPEN"];
(async () => {
await globalPromise;
const Dispatcher = await findByProps("dispatch", "isDispatching", {wait: true, forever: true});
for (const event of InitializeEvents) {
const listener = function () {
Dispatcher.unsubscribe(event, listener);
Events.dispatchEvent(new Event("ready"));
};
Dispatcher.subscribe(event, listener);
}
})();
}
export const Webpack = window.Webpack = {
findModule,
findByDisplayName,
findByProps,
findIndex,
waitFor,
bulk,
Events,
whenReady,
require,
atIndex,
findModules,
Filters,
globalPromise,
// Aliases
getByProps: findByProps,
getByDisplayName: findByDisplayName,
getModule: findModule,
getModules: findModules,
getByIndex: atIndex,
getIndex: findIndex,
get ready() {return ready;},
get common() {return commonCache;}
};
const descriptors = Object.getOwnPropertyDescriptors(commonModules);
const commonCache = {};
for (const name in descriptors) {
const descriptor = descriptors[name];
const originalGet = descriptor.get;
if (typeof originalGet !== "function") continue;
descriptor.get = () => {
const result = originalGet();
Object.defineProperty(commonCache, name, {
value: result,
enumerable: true,
writable: true,
configurable: true
});
return result;
};
Object.defineProperty(commonCache, name, descriptor);
}