this repo has no description
at v1.0.6 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 32 getConfig, 33 getConfigOption: <T>(ext: string, name: string) => { 34 const config = getConfig(ext); 35 if (config == null) return undefined; 36 const option = config[name]; 37 if (option == null) return undefined; 38 return option as T; 39 }, 40 getNatives: (ext: string) => global.moonlightNode.nativesCache[ext], 41 getLogger: (id: string) => { 42 return new Logger(id); 43 }, 44 45 getExtensionDir: (ext: string) => { 46 const extPath = getExtensionsPath(); 47 return path.join(extPath, ext); 48 }, 49 writeConfig 50 }; 51 52 await loadProcessedExtensions(processed); 53 contextBridge.exposeInMainWorld("moonlightNode", moonlightNode); 54 55 const extCors = moonlightNode.processedExtensions.extensions 56 .map((x) => x.manifest.cors ?? []) 57 .flat(); 58 59 for (const repo of moonlightNode.config.repositories) { 60 const url = new URL(repo); 61 url.pathname = "/"; 62 extCors.push(url.toString()); 63 } 64 65 ipcRenderer.invoke(constants.ipcSetCorsList, extCors); 66} 67 68async function loadPreload() { 69 const webPreloadPath = path.join(__dirname, "web-preload.js"); 70 const webPreload = fs.readFileSync(webPreloadPath, "utf8"); 71 await webFrame.executeJavaScript(webPreload); 72} 73 74async function init(oldPreloadPath: string) { 75 try { 76 await injectGlobals(); 77 await loadPreload(); 78 } catch (e) { 79 const message = e instanceof Error ? e.stack : e; 80 await ipcRenderer.invoke(constants.ipcMessageBox, { 81 title: "moonlight node-preload error", 82 message: message 83 }); 84 } 85 86 // Let Discord start even if we fail 87 require(oldPreloadPath); 88} 89 90const oldPreloadPath: string = ipcRenderer.sendSync( 91 constants.ipcGetOldPreloadPath 92); 93init(oldPreloadPath);