Skip to content

Commit

Permalink
fix the hacky requireNativeComponent double register
Browse files Browse the repository at this point in the history
  • Loading branch information
pylixonly committed Feb 11, 2024
1 parent 2fae296 commit e1e5560
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
23 changes: 20 additions & 3 deletions src/lib/metro/filters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MetroModules, PropsFinder, PropsFinderAll } from "@types";
import { instead } from "@lib/patcher";

// Metro require
declare const __r: (moduleId: number) => any;
Expand All @@ -25,6 +26,22 @@ for (const key in window.modules) {
}
}

const checked = new WeakSet();
const onModuleCheck = (exports: any) => {
if (typeof exports !== "object" || checked.has(exports)) return;
checked.add(exports);

if (exports?.default?.name === "requireNativeComponent") {
instead("default", exports, (args, orig) => {
try {
return orig(...args);
} catch {
return args[0];
}
})
}
}

// Function to filter through modules
const filterModules = (modules: MetroModules, single = false) => (filter: (m: any) => boolean) => {
const found = [];
Expand All @@ -33,19 +50,19 @@ const filterModules = (modules: MetroModules, single = false) => (filter: (m: an
const id = Number(key);
const module = modules[id]?.publicModule?.exports;

// HACK: Override the function used to report fatal JavaScript errors (that crash the app) to prevent module-requiring side effects
// Credit to @pylixonly (492949202121261067) for the initial version of this fix
if (!modules[id].isInitialized) try {
window.ErrorUtils.setGlobalHandler(() => {});
__r(id);
window.ErrorUtils.setGlobalHandler(originalHandler);
} catch {}
} catch { }

if (!module) {
blacklist(id);
continue;
}

onModuleCheck(module);

if (module.default && module.__esModule && filter(module.default)) {
if (single) return module.default;
found.push(module.default);
Expand Down
21 changes: 1 addition & 20 deletions src/lib/preinit.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,12 @@
import { initThemes } from "@lib/themes";
import { instead } from "@lib/patcher";

// Hoist required modules
// This used to be in filters.ts, but things became convoluted

const basicFind = (filter: (m: any) => any | string) => {
for (const key in window.modules) {
const exp = window.modules[key]?.publicModule.exports;
if (exp && filter(exp)) return exp;
}
}

const requireNativeComponent = basicFind(m => m?.default?.name === "requireNativeComponent");

if (requireNativeComponent) {
// > "Tried to register two views with the same name DCDVisualEffectView"
// This serves as a workaround for the crashing You tab on Android starting from version 192.x
// How? We simply ignore it.
instead("default", requireNativeComponent, (args, orig) => {
try {
return orig(...args);
} catch {
return args[0];
}
})
}

// Hoist React on window
window.React = basicFind(m => m.createElement) as typeof import("react");

Expand All @@ -38,7 +19,7 @@ export const chroma = basicFind(m => m.brewer) as typeof import("chroma-js");
// Themes
if (window.__vendetta_loader?.features.themes) {
try {
initThemes();
require("@lib/themes").initThemes();
} catch (e) {
console.error("[Vendetta] Failed to initialize themes...", e);
}
Expand Down

0 comments on commit e1e5560

Please sign in to comment.