this repo has no description
at v1.1.0 2.7 kB view raw
1import { webFrame, ipcRenderer, contextBridge } from "electron"; 2import fs from "fs"; 3import path from "path"; 4 5import { readConfig, writeConfig } from "@moonlight-mod/core/config"; 6import { constants } from "@moonlight-mod/types"; 7import { getExtensions } from "@moonlight-mod/core/extension"; 8import { getExtensionsPath } from "@moonlight-mod/core/util/data"; 9import Logger from "@moonlight-mod/core/util/logger"; 10import { 11 loadExtensions, 12 loadProcessedExtensions 13} from "@moonlight-mod/core/extension/loader"; 14 15async function injectGlobals() { 16 const config = readConfig(); 17 const extensions = getExtensions(); 18 const processed = await loadExtensions(extensions); 19 20 function getConfig(ext: string) { 21 const val = config.extensions[ext]; 22 if (val == null || typeof val === "boolean") return undefined; 23 return val.config; 24 } 25 26 global.moonlightNode = { 27 config, 28 extensions: getExtensions(), 29 processedExtensions: processed, 30 nativesCache: {}, 31 getConfig, 32 getConfigOption: <T>(ext: string, name: string) => { 33 const config = getConfig(ext); 34 if (config == null) return undefined; 35 const option = config[name]; 36 if (option == null) return undefined; 37 return option as T; 38 }, 39 getNatives: (ext: string) => global.moonlightNode.nativesCache[ext], 40 getLogger: (id: string) => { 41 return new Logger(id); 42 }, 43 44 getExtensionDir: (ext: string) => { 45 const extPath = getExtensionsPath(); 46 return path.join(extPath, ext); 47 }, 48 writeConfig 49 }; 50 51 await loadProcessedExtensions(processed); 52 contextBridge.exposeInMainWorld("moonlightNode", moonlightNode); 53 54 const extCors = moonlightNode.processedExtensions.extensions 55 .map((x) => x.manifest.cors ?? []) 56 .flat(); 57 58 for (const repo of moonlightNode.config.repositories) { 59 const url = new URL(repo); 60 url.pathname = "/"; 61 extCors.push(url.toString()); 62 } 63 64 ipcRenderer.invoke(constants.ipcSetCorsList, extCors); 65} 66 67async function loadPreload() { 68 const webPreloadPath = path.join(__dirname, "web-preload.js"); 69 const webPreload = fs.readFileSync(webPreloadPath, "utf8"); 70 await webFrame.executeJavaScript(webPreload); 71} 72 73async function init(oldPreloadPath: string) { 74 try { 75 await injectGlobals(); 76 await loadPreload(); 77 } catch (e) { 78 const message = e instanceof Error ? e.stack : e; 79 await ipcRenderer.invoke(constants.ipcMessageBox, { 80 title: "moonlight node-preload error", 81 message: message 82 }); 83 } 84 85 // Let Discord start even if we fail 86 if (oldPreloadPath) require(oldPreloadPath); 87} 88 89const oldPreloadPath: string = ipcRenderer.sendSync( 90 constants.ipcGetOldPreloadPath 91); 92init(oldPreloadPath);