this repo has no description

Merge pull request #24 from ImLvna/injector-desktop-support

injector: support other patch methods

Eva 28f297cb de36c965

Changed files
+58 -42
packages
core
src
util
core-extensions
src
disableSentry
injector
src
node-preload
src
types
+19 -17
packages/core-extensions/src/disableSentry/host.ts
···
const logger = moonlightHost.getLogger("disableSentry");
-
try {
-
const hostSentryPath = require.resolve(
-
join(moonlightHost.asarPath, "node_modules", "@sentry", "electron")
-
);
-
require.cache[hostSentryPath] = new Module(
-
hostSentryPath,
-
require.cache[require.resolve(moonlightHost.asarPath)]
-
);
-
require.cache[hostSentryPath]!.exports = {
-
init: () => {},
-
captureException: () => {},
-
setTag: () => {},
-
setUser: () => {}
-
};
-
logger.debug("Stubbed Sentry host side!");
-
} catch (err) {
-
logger.error("Failed to stub Sentry host side:", err);
+
if (moonlightHost.asarPath !== "moonlightDesktop") {
+
try {
+
const hostSentryPath = require.resolve(
+
join(moonlightHost.asarPath, "node_modules", "@sentry", "electron")
+
);
+
require.cache[hostSentryPath] = new Module(
+
hostSentryPath,
+
require.cache[require.resolve(moonlightHost.asarPath)]
+
);
+
require.cache[hostSentryPath]!.exports = {
+
init: () => {},
+
captureException: () => {},
+
setTag: () => {},
+
setUser: () => {}
+
};
+
logger.debug("Stubbed Sentry host side!");
+
} catch (err) {
+
logger.error("Failed to stub Sentry host side:", err);
+
}
}
moonlightHost.events.on("window-created", (window: BrowserWindow) => {
+19 -17
packages/core-extensions/src/disableSentry/node.ts
···
const logger = moonlightNode.getLogger("disableSentry");
-
const preloadPath = ipcRenderer.sendSync(constants.ipcGetOldPreloadPath);
-
try {
-
const sentryPath = require.resolve(
-
resolve(preloadPath, "..", "node_modules", "@sentry", "electron")
-
);
-
require.cache[sentryPath] = new Module(
-
sentryPath,
-
require.cache[require.resolve(preloadPath)]
-
);
-
require.cache[sentryPath]!.exports = {
-
init: () => {},
-
setTag: () => {},
-
setUser: () => {}
-
};
-
logger.debug("Stubbed Sentry node side!");
-
} catch (err) {
-
logger.error("Failed to stub Sentry:", err);
+
if (!ipcRenderer.sendSync(constants.ipcGetIsMoonlightDesktop)) {
+
const preloadPath = ipcRenderer.sendSync(constants.ipcGetOldPreloadPath);
+
try {
+
const sentryPath = require.resolve(
+
resolve(preloadPath, "..", "node_modules", "@sentry", "electron")
+
);
+
require.cache[sentryPath] = new Module(
+
sentryPath,
+
require.cache[require.resolve(preloadPath)]
+
);
+
require.cache[sentryPath]!.exports = {
+
init: () => {},
+
setTag: () => {},
+
setUser: () => {}
+
};
+
logger.debug("Stubbed Sentry node side!");
+
} catch (err) {
+
logger.error("Failed to stub Sentry:", err);
+
}
}
+10 -4
packages/core/src/util/data.ts
···
const fs = requireImport("fs");
const path = requireImport("path");
+
let configPath = "";
+
const buildInfoPath = path.join(process.resourcesPath, "build_info.json");
-
const buildInfo: BuildInfo = JSON.parse(
-
fs.readFileSync(buildInfoPath, "utf8")
-
);
+
if (!fs.existsSync(buildInfoPath)) {
+
configPath = path.join(dir, "desktop.json");
+
} else {
+
const buildInfo: BuildInfo = JSON.parse(
+
fs.readFileSync(buildInfoPath, "utf8")
+
);
+
configPath = path.join(dir, buildInfo.releaseChannel + ".json");
+
}
-
const configPath = path.join(dir, buildInfo.releaseChannel + ".json");
return configPath;
}
+8 -2
packages/injector/src/index.ts
···
const logger = new Logger("injector");
-
let oldPreloadPath = "";
+
let oldPreloadPath: string | undefined;
let corsAllow: string[] = [];
+
let isMoonlightDesktop = false;
ipcMain.on(constants.ipcGetOldPreloadPath, (e) => {
e.returnValue = oldPreloadPath;
···
ipcMain.on(constants.ipcGetAppData, (e) => {
e.returnValue = app.getPath("appData");
});
+
ipcMain.on(constants.ipcGetIsMoonlightDesktop, (e) => {
+
e.returnValue = isMoonlightDesktop;
+
});
ipcMain.handle(constants.ipcMessageBox, (_, opts) => {
electron.dialog.showMessageBoxSync(opts);
});
···
class BrowserWindow extends ElectronBrowserWindow {
constructor(opts: BrowserWindowConstructorOptions) {
-
oldPreloadPath = opts.webPreferences!.preload!;
+
oldPreloadPath = opts.webPreferences!.preload;
opts.webPreferences!.preload = require.resolve("./node-preload.js");
moonlightHost.events.emit("window-options", opts);
···
// "aight i'm writing exclusively C# from now on and never touching JavaScript again"
export async function inject(asarPath: string) {
+
isMoonlightDesktop = asarPath === "moonlightDesktop";
try {
const config = readConfig();
const extensions = getExtensions();
···
logger.error("Failed to inject", e);
}
+
if (isMoonlightDesktop) return;
// Need to do this instead of require() or it breaks require.main
// @ts-expect-error why are you not documented
Module._load(asarPath, Module, true);
+1 -2
packages/node-preload/src/index.ts
···
extensions: getExtensions(),
processedExtensions: processed,
nativesCache: {},
-
getConfig,
getConfigOption: <T>(ext: string, name: string) => {
const config = getConfig(ext);
···
}
// Let Discord start even if we fail
-
require(oldPreloadPath);
+
if (oldPreloadPath) require(oldPreloadPath);
}
const oldPreloadPath: string = ipcRenderer.sendSync(
+1
packages/types/src/constants.ts
···
export const ipcGetOldPreloadPath = "_moonlight_getOldPreloadPath";
export const ipcGetAppData = "_moonlight_getAppData";
+
export const ipcGetIsMoonlightDesktop = "_moonlight_getIsMoonlightDesktop";
export const ipcMessageBox = "_moonlight_messageBox";
export const ipcSetCorsList = "_moonlight_setCorsList";