From 0dae5b17e5b9ef0c86f6426ca2a03ea3789cb030 Mon Sep 17 00:00:00 2001 From: amsyarasyiq <82711525+amsyarasyiq@users.noreply.github.com> Date: Sat, 13 May 2023 01:02:11 +0800 Subject: [PATCH] [Entry] Hook first module factory to init bundle --- src/entry.ts | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/entry.ts b/src/entry.ts index 1725164..42109a2 100644 --- a/src/entry.ts +++ b/src/entry.ts @@ -1,15 +1,25 @@ import { ClientInfoManager } from "@lib/native"; +import { after } from "@lib/patcher"; // This logs in the native logging implementation, e.g. logcat console.log("Hello from Vendetta!"); -import(".").then((m) => m.default()).catch((e) => { - console.log(e?.stack ?? e.toString()); - alert([ - "Failed to load Vendetta!\n", - `Build Number: ${ClientInfoManager.Build}`, - // @ts-expect-error, replaced in build script - `Vendetta: ${__vendettaVersion}`, - e?.stack || e.toString(), - ].join("\n")); -}); +async function initBundle() { + try { + await import(".").then(i => i.default()); + } catch (e: any) { + console.error(e?.stack ?? e?.toString()); + alert([ + "Failed to load Vendetta!\n", + `Build Number: ${ClientInfoManager.Build}`, + // @ts-expect-error, replaced in build script + `Vendetta: ${__vendettaVersion}`, + e?.stack ?? e?.toString(), + ].join("\n")); + } +} + +// Discord iOS 178.0 (43557) introduced some changes that caused our bundle to load too early, resulting in the app crashing. +// As a workaround, we hook into the factory function of the first module that loads after the bundle to init the bundle. +// This delays our bundle initialization to a certain point where window.modules is ready. +after("factory", Object.values(window.modules).find(m => m.factory), initBundle, true);