From 8cf5233e94988e85fda3ce7a9825770a067aea21 Mon Sep 17 00:00:00 2001 From: CanadaHonk Date: Mon, 10 Apr 2023 20:59:54 +0100 Subject: [PATCH] rewrite: replace requireNative with globalPaths workaround Added Module.globalPaths workaround (reintroduce native usage instead of our own util using it) is needed as Discord's overlay code depends on their own implementation of this. Also removed requireNative and replace with just require since no longer needed with this hack anyway. --- src/bootstrap.js | 2 +- src/index.js | 8 +++++++- src/utils/requireNative.js | 6 ------ 3 files changed, 8 insertions(+), 8 deletions(-) delete mode 100644 src/utils/requireNative.js diff --git a/src/bootstrap.js b/src/bootstrap.js index 90054f3..e701b0d 100644 --- a/src/bootstrap.js +++ b/src/bootstrap.js @@ -48,7 +48,7 @@ const startCore = () => { }); }); - desktopCore = require('./utils/requireNative')('discord_desktop_core'); + desktopCore = require('discord_desktop_core'); desktopCore.startup({ splashScreen: splash, diff --git a/src/index.js b/src/index.js index 70328df..42b5875 100644 --- a/src/index.js +++ b/src/index.js @@ -26,9 +26,15 @@ if (process.platform === 'win32') try { for (const m of require('fs').readdirSync(b)) M.globalPaths.push(join(b, m)); // For each module dir, add to globalPaths } catch { log('Init', 'Failed to QS globalPaths') } +// inject Module.globalPaths into resolve lookups as it was removed in Electron >=17 and Discord depend on this workaround +const rlp = M._resolveLookupPaths; +M._resolveLookupPaths = (request, parent) => { + if (parent?.paths?.length > 0) parent.paths = parent.paths.concat(M.globalPaths); + return rlp(request, parent); +}; if (process.argv.includes('--overlay-host')) { // If overlay - require('./utils/requireNative')('discord_overlay2', 'standalone_host.js'); // Start overlay + require('discord_overlay2/standalone_host.js'); // Start overlay } else { require('./bootstrap')(); // Start bootstrap } \ No newline at end of file diff --git a/src/utils/requireNative.js b/src/utils/requireNative.js deleted file mode 100644 index e5832b8..0000000 --- a/src/utils/requireNative.js +++ /dev/null @@ -1,6 +0,0 @@ -// Custom requireNative as Electron >=17 breaks Module.globalPaths for some reason -// For Updater v2: get direct path in globalPaths (g[0]) -// For Module Updater: get root path for all modules in globalPaths - -const g = require('module').globalPaths.reverse(); -module.exports = (n, e = '') => require(require('path').join(g.find(x => x.includes(n)) ?? g[0], n, e)); \ No newline at end of file