Skip to content

Commit 28f297c

Browse files
authored
Merge pull request #24 from ImLvna/injector-desktop-support
injector: support other patch methods
2 parents de36c96 + 100edb3 commit 28f297c

File tree

6 files changed

+58
-42
lines changed

6 files changed

+58
-42
lines changed

packages/core-extensions/src/disableSentry/host.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,25 @@ import { BrowserWindow } from "electron";
44

55
const logger = moonlightHost.getLogger("disableSentry");
66

7-
try {
8-
const hostSentryPath = require.resolve(
9-
join(moonlightHost.asarPath, "node_modules", "@sentry", "electron")
10-
);
11-
require.cache[hostSentryPath] = new Module(
12-
hostSentryPath,
13-
require.cache[require.resolve(moonlightHost.asarPath)]
14-
);
15-
require.cache[hostSentryPath]!.exports = {
16-
init: () => {},
17-
captureException: () => {},
18-
setTag: () => {},
19-
setUser: () => {}
20-
};
21-
logger.debug("Stubbed Sentry host side!");
22-
} catch (err) {
23-
logger.error("Failed to stub Sentry host side:", err);
7+
if (moonlightHost.asarPath !== "moonlightDesktop") {
8+
try {
9+
const hostSentryPath = require.resolve(
10+
join(moonlightHost.asarPath, "node_modules", "@sentry", "electron")
11+
);
12+
require.cache[hostSentryPath] = new Module(
13+
hostSentryPath,
14+
require.cache[require.resolve(moonlightHost.asarPath)]
15+
);
16+
require.cache[hostSentryPath]!.exports = {
17+
init: () => {},
18+
captureException: () => {},
19+
setTag: () => {},
20+
setUser: () => {}
21+
};
22+
logger.debug("Stubbed Sentry host side!");
23+
} catch (err) {
24+
logger.error("Failed to stub Sentry host side:", err);
25+
}
2426
}
2527

2628
moonlightHost.events.on("window-created", (window: BrowserWindow) => {

packages/core-extensions/src/disableSentry/node.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,23 @@ import { constants } from "@moonlight-mod/types";
55

66
const logger = moonlightNode.getLogger("disableSentry");
77

8-
const preloadPath = ipcRenderer.sendSync(constants.ipcGetOldPreloadPath);
9-
try {
10-
const sentryPath = require.resolve(
11-
resolve(preloadPath, "..", "node_modules", "@sentry", "electron")
12-
);
13-
require.cache[sentryPath] = new Module(
14-
sentryPath,
15-
require.cache[require.resolve(preloadPath)]
16-
);
17-
require.cache[sentryPath]!.exports = {
18-
init: () => {},
19-
setTag: () => {},
20-
setUser: () => {}
21-
};
22-
logger.debug("Stubbed Sentry node side!");
23-
} catch (err) {
24-
logger.error("Failed to stub Sentry:", err);
8+
if (!ipcRenderer.sendSync(constants.ipcGetIsMoonlightDesktop)) {
9+
const preloadPath = ipcRenderer.sendSync(constants.ipcGetOldPreloadPath);
10+
try {
11+
const sentryPath = require.resolve(
12+
resolve(preloadPath, "..", "node_modules", "@sentry", "electron")
13+
);
14+
require.cache[sentryPath] = new Module(
15+
sentryPath,
16+
require.cache[require.resolve(preloadPath)]
17+
);
18+
require.cache[sentryPath]!.exports = {
19+
init: () => {},
20+
setTag: () => {},
21+
setUser: () => {}
22+
};
23+
logger.debug("Stubbed Sentry node side!");
24+
} catch (err) {
25+
logger.error("Failed to stub Sentry:", err);
26+
}
2527
}

packages/core/src/util/data.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@ export function getConfigPath(): string {
3131
const fs = requireImport("fs");
3232
const path = requireImport("path");
3333

34+
let configPath = "";
35+
3436
const buildInfoPath = path.join(process.resourcesPath, "build_info.json");
35-
const buildInfo: BuildInfo = JSON.parse(
36-
fs.readFileSync(buildInfoPath, "utf8")
37-
);
37+
if (!fs.existsSync(buildInfoPath)) {
38+
configPath = path.join(dir, "desktop.json");
39+
} else {
40+
const buildInfo: BuildInfo = JSON.parse(
41+
fs.readFileSync(buildInfoPath, "utf8")
42+
);
43+
configPath = path.join(dir, buildInfo.releaseChannel + ".json");
44+
}
3845

39-
const configPath = path.join(dir, buildInfo.releaseChannel + ".json");
4046
return configPath;
4147
}
4248

packages/injector/src/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@ import EventEmitter from "events";
1717

1818
const logger = new Logger("injector");
1919

20-
let oldPreloadPath = "";
20+
let oldPreloadPath: string | undefined;
2121
let corsAllow: string[] = [];
22+
let isMoonlightDesktop = false;
2223

2324
ipcMain.on(constants.ipcGetOldPreloadPath, (e) => {
2425
e.returnValue = oldPreloadPath;
2526
});
2627
ipcMain.on(constants.ipcGetAppData, (e) => {
2728
e.returnValue = app.getPath("appData");
2829
});
30+
ipcMain.on(constants.ipcGetIsMoonlightDesktop, (e) => {
31+
e.returnValue = isMoonlightDesktop;
32+
});
2933
ipcMain.handle(constants.ipcMessageBox, (_, opts) => {
3034
electron.dialog.showMessageBoxSync(opts);
3135
});
@@ -72,7 +76,7 @@ function patchCsp(headers: Record<string, string[]>) {
7276

7377
class BrowserWindow extends ElectronBrowserWindow {
7478
constructor(opts: BrowserWindowConstructorOptions) {
75-
oldPreloadPath = opts.webPreferences!.preload!;
79+
oldPreloadPath = opts.webPreferences!.preload;
7680
opts.webPreferences!.preload = require.resolve("./node-preload.js");
7781

7882
moonlightHost.events.emit("window-options", opts);
@@ -114,6 +118,7 @@ Object.defineProperty(BrowserWindow, "name", {
114118
// "aight i'm writing exclusively C# from now on and never touching JavaScript again"
115119

116120
export async function inject(asarPath: string) {
121+
isMoonlightDesktop = asarPath === "moonlightDesktop";
117122
try {
118123
const config = readConfig();
119124
const extensions = getExtensions();
@@ -157,6 +162,7 @@ export async function inject(asarPath: string) {
157162
logger.error("Failed to inject", e);
158163
}
159164

165+
if (isMoonlightDesktop) return;
160166
// Need to do this instead of require() or it breaks require.main
161167
// @ts-expect-error why are you not documented
162168
Module._load(asarPath, Module, true);

packages/node-preload/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ async function injectGlobals() {
2828
extensions: getExtensions(),
2929
processedExtensions: processed,
3030
nativesCache: {},
31-
3231
getConfig,
3332
getConfigOption: <T>(ext: string, name: string) => {
3433
const config = getConfig(ext);
@@ -84,7 +83,7 @@ async function init(oldPreloadPath: string) {
8483
}
8584

8685
// Let Discord start even if we fail
87-
require(oldPreloadPath);
86+
if (oldPreloadPath) require(oldPreloadPath);
8887
}
8988

9089
const oldPreloadPath: string = ipcRenderer.sendSync(

packages/types/src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ export const repoUrlFile = ".moonlight-repo-url";
55

66
export const ipcGetOldPreloadPath = "_moonlight_getOldPreloadPath";
77
export const ipcGetAppData = "_moonlight_getAppData";
8+
export const ipcGetIsMoonlightDesktop = "_moonlight_getIsMoonlightDesktop";
89
export const ipcMessageBox = "_moonlight_messageBox";
910
export const ipcSetCorsList = "_moonlight_setCorsList";

0 commit comments

Comments
 (0)