GoonCord/src/entry.ts

89 lines
2.7 KiB
TypeScript
Raw Normal View History

import { version } from "bunny-build";
2024-03-06 01:57:59 +08:00
import { instead } from "spitroast";
2024-02-23 01:27:10 +08:00
// @ts-ignore - shut up fr
globalThis.window = globalThis;
async function initializeBunny() {
2024-02-23 01:27:10 +08:00
try {
// Make 'freeze' and 'seal' do nothing
Object.freeze = Object.seal = Object;
await require("@metro/caches").initMetroCache();
2024-05-20 15:36:33 +08:00
await require(".").default();
2024-02-23 01:27:10 +08:00
} catch (e) {
const { ClientInfoManager } = require("@lib/api/native/modules");
2024-02-23 01:27:10 +08:00
const stack = e instanceof Error ? e.stack : undefined;
console.log(stack ?? e?.toString?.() ?? e);
alert([
"Failed to load Bunny!\n",
2024-02-23 01:27:10 +08:00
`Build Number: ${ClientInfoManager.Build}`,
`Bunny: ${version}`,
2024-02-23 01:27:10 +08:00
stack || e?.toString?.(),
].join("\n"));
}
}
2024-02-23 01:27:10 +08:00
// @ts-ignore
if (typeof globalThis.__r !== "undefined") {
initializeBunny();
2024-02-23 01:27:10 +08:00
} else {
// We hold calls from the native side
function onceIndexRequired(originalRequire: any) {
const batchedBridge = window.__fbBatchedBridge;
const callQueue = new Array<any>;
const unpatchHook = instead("callFunctionReturnFlushedQueue", batchedBridge, (args, orig) => {
2024-02-23 01:27:10 +08:00
if (args[0] === "AppRegistry" || !batchedBridge.getCallableModule(args[0])) {
callQueue.push(args);
return batchedBridge.flushedQueue();
}
return orig.apply(batchedBridge, args);
});
const startDiscord = async () => {
await initializeBunny();
unpatchHook();
2024-02-23 01:27:10 +08:00
originalRequire(0);
2024-03-06 01:57:59 +08:00
callQueue.forEach(arg =>
batchedBridge.getCallableModule(arg[0])
2024-02-23 01:27:10 +08:00
&& batchedBridge.__callFunction(...arg));
};
startDiscord();
}
2024-03-06 01:57:59 +08:00
2024-02-23 01:27:10 +08:00
var _requireFunc: any; // We can't set properties to 'this' during __r set for some reason
Object.defineProperties(globalThis, {
__r: {
configurable: true,
get: () => _requireFunc,
set(v) {
_requireFunc = function patchedRequire(a: number) {
// Initializing index.ts(x)
if (a === 0) {
onceIndexRequired(v);
_requireFunc = v;
} else return v(a);
2024-03-06 01:57:59 +08:00
};
2024-02-23 01:27:10 +08:00
}
},
__d: {
configurable: true,
get() {
// @ts-ignore - I got an error where 'Object' is undefined *sometimes*, which is literally never supposed to happen
if (window.Object && !window.modules) {
window.modules = window.__c?.();
}
return this.value;
},
2024-03-06 01:57:59 +08:00
set(v) { this.value = v; }
2024-02-23 01:27:10 +08:00
}
});
2024-03-06 01:57:59 +08:00
}