Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

injector: support other patch methods #24

Merged
merged 2 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions packages/core-extensions/src/disableSentry/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@ import { BrowserWindow } from "electron";

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) => {
Expand Down
36 changes: 19 additions & 17 deletions packages/core-extensions/src/disableSentry/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ import { constants } from "@moonlight-mod/types";

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);
}
}
14 changes: 10 additions & 4 deletions packages/core/src/util/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,18 @@ export function getConfigPath(): string {
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;
}

Expand Down
10 changes: 8 additions & 2 deletions packages/injector/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ import EventEmitter from "events";

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);
});
Expand Down Expand Up @@ -72,7 +76,7 @@ function patchCsp(headers: Record<string, string[]>) {

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);
Expand Down Expand Up @@ -114,6 +118,7 @@ Object.defineProperty(BrowserWindow, "name", {
// "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();
Expand Down Expand Up @@ -157,6 +162,7 @@ export async function inject(asarPath: string) {
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);
Expand Down
3 changes: 1 addition & 2 deletions packages/node-preload/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ async function injectGlobals() {
extensions: getExtensions(),
processedExtensions: processed,
nativesCache: {},

getConfig,
getConfigOption: <T>(ext: string, name: string) => {
const config = getConfig(ext);
Expand Down Expand Up @@ -84,7 +83,7 @@ async function init(oldPreloadPath: string) {
}

// Let Discord start even if we fail
require(oldPreloadPath);
if (oldPreloadPath) require(oldPreloadPath);
}

const oldPreloadPath: string = ipcRenderer.sendSync(
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export const repoUrlFile = ".moonlight-repo-url";

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";