this repo has no description
at v1.0.1 2.6 kB view raw
1import { 2 ExtensionManifest, 3 DetectedExtension, 4 ExtensionLoadSource, 5 constants 6} from "@moonlight-mod/types"; 7import { readConfig } from "./config"; 8import requireImport from "./util/import"; 9import { getCoreExtensionsPath, getExtensionsPath } from "./util/data"; 10 11function loadDetectedExtensions( 12 dir: string, 13 type: ExtensionLoadSource 14): DetectedExtension[] { 15 const fs = requireImport("fs"); 16 const path = requireImport("path"); 17 const ret: DetectedExtension[] = []; 18 19 const glob = require("glob"); 20 const manifests = glob.sync(dir + "/**/manifest.json"); 21 22 for (const manifestPath of manifests) { 23 if (!fs.existsSync(manifestPath)) continue; 24 const dir = path.dirname(manifestPath); 25 26 const manifest: ExtensionManifest = JSON.parse( 27 fs.readFileSync(manifestPath, "utf8") 28 ); 29 30 const webPath = path.join(dir, "index.js"); 31 const nodePath = path.join(dir, "node.js"); 32 const hostPath = path.join(dir, "host.js"); 33 34 // if none exist (empty manifest) don't give a shit 35 if ( 36 !fs.existsSync(webPath) && 37 !fs.existsSync(nodePath) && 38 !fs.existsSync(hostPath) 39 ) { 40 continue; 41 } 42 43 const web = fs.existsSync(webPath) 44 ? fs.readFileSync(webPath, "utf8") 45 : undefined; 46 47 let url: string | undefined = undefined; 48 const urlPath = path.join(dir, constants.repoUrlFile); 49 if (type == ExtensionLoadSource.Normal && fs.existsSync(urlPath)) { 50 url = fs.readFileSync(urlPath, "utf8"); 51 } 52 53 ret.push({ 54 id: manifest.id, 55 manifest, 56 source: { 57 type, 58 url 59 }, 60 scripts: { 61 web, 62 webPath: web != null ? webPath : undefined, 63 nodePath: fs.existsSync(nodePath) ? nodePath : undefined, 64 hostPath: fs.existsSync(hostPath) ? hostPath : undefined 65 } 66 }); 67 } 68 69 return ret; 70} 71 72function getExtensionsNative(): DetectedExtension[] { 73 const config = readConfig(); 74 const res = []; 75 76 res.push( 77 ...loadDetectedExtensions(getCoreExtensionsPath(), ExtensionLoadSource.Core) 78 ); 79 80 res.push( 81 ...loadDetectedExtensions(getExtensionsPath(), ExtensionLoadSource.Normal) 82 ); 83 84 for (const devSearchPath of config.devSearchPaths ?? []) { 85 res.push( 86 ...loadDetectedExtensions(devSearchPath, ExtensionLoadSource.Developer) 87 ); 88 } 89 90 return res; 91} 92 93export function getExtensions(): DetectedExtension[] { 94 webPreload: { 95 return moonlightNode.extensions; 96 } 97 98 nodePreload: { 99 return getExtensionsNative(); 100 } 101 102 injector: { 103 return getExtensionsNative(); 104 } 105 106 throw new Error("Called getExtensions() outside of node-preload/web-preload"); 107}