From 749709faaa75df872da0b0a5492636c50947604c Mon Sep 17 00:00:00 2001 From: NotNite Date: Wed, 9 Oct 2024 16:37:23 -0400 Subject: [PATCH 1/2] Fix wrong baseUrl imports --- packages/core-extensions/src/moonbase/types.ts | 2 +- packages/core-extensions/src/noHideToken/index.ts | 2 +- packages/core/src/fs.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/core-extensions/src/moonbase/types.ts b/packages/core-extensions/src/moonbase/types.ts index 2b14937..40f106b 100644 --- a/packages/core-extensions/src/moonbase/types.ts +++ b/packages/core-extensions/src/moonbase/types.ts @@ -1,4 +1,4 @@ -import { DetectedExtension, ExtensionManifest } from "types/src"; +import { DetectedExtension, ExtensionManifest } from "@moonlight-mod/types"; export type MoonbaseNatives = { checkForMoonlightUpdate(): Promise; diff --git a/packages/core-extensions/src/noHideToken/index.ts b/packages/core-extensions/src/noHideToken/index.ts index 6e45492..f58cd38 100644 --- a/packages/core-extensions/src/noHideToken/index.ts +++ b/packages/core-extensions/src/noHideToken/index.ts @@ -1,4 +1,4 @@ -import { Patch } from "types/src"; +import { Patch } from "@moonlight-mod/types"; export const patches: Patch[] = [ { diff --git a/packages/core/src/fs.ts b/packages/core/src/fs.ts index 0b9be09..70eb8fe 100644 --- a/packages/core/src/fs.ts +++ b/packages/core/src/fs.ts @@ -1,4 +1,4 @@ -import { MoonlightFS } from "types/src"; +import { MoonlightFS } from "@moonlight-mod/types"; import requireImport from "./util/import"; export default function getFS(): MoonlightFS { From 01d483b3852cf931bf1b47f986ca21f401a01393 Mon Sep 17 00:00:00 2001 From: NotNite Date: Wed, 9 Oct 2024 16:52:34 -0400 Subject: [PATCH 2/2] Exposing FS to web is bad actually --- packages/browser/src/index.ts | 4 +- .../core-extensions/src/moonbase/native.ts | 21 +++-- .../src/moonbase/webpackModules/stores.ts | 2 +- packages/core/src/config.ts | 15 +-- packages/core/src/extension.ts | 72 +++++++-------- packages/core/src/fs.ts | 92 +++++++++---------- packages/core/src/util/data.ts | 28 +++--- packages/injector/src/index.ts | 3 + packages/node-preload/src/index.ts | 6 +- packages/types/src/globals.ts | 3 +- packages/types/src/index.ts | 2 +- 11 files changed, 124 insertions(+), 124 deletions(-) diff --git a/packages/browser/src/index.ts b/packages/browser/src/index.ts index 4b98643..1d48a61 100644 --- a/packages/browser/src/index.ts +++ b/packages/browser/src/index.ts @@ -26,7 +26,7 @@ window._moonlightBrowserInit = async () => { } }); - window._moonlightBrowserFS = { + window.moonlightFS = { async readFile(path) { return new Uint8Array(await fs.readFile(path)); }, @@ -108,7 +108,7 @@ window._moonlightBrowserInit = async () => { extensions, processedExtensions, nativesCache: {}, - fs: window._moonlightBrowserFS, + isBrowser: true, version: MOONLIGHT_VERSION, branch: MOONLIGHT_BRANCH as MoonlightBranch, diff --git a/packages/core-extensions/src/moonbase/native.ts b/packages/core-extensions/src/moonbase/native.ts index 6ac49aa..24b87bf 100644 --- a/packages/core-extensions/src/moonbase/native.ts +++ b/packages/core-extensions/src/moonbase/native.ts @@ -8,7 +8,6 @@ export const nightlyRefUrl = "https://moonlight-mod.github.io/moonlight/ref"; export const userAgent = `moonlight/${moonlightNode.version} (https://github.com/moonlight-mod/moonlight)`; export default function getNatives(): MoonbaseNatives { - const fs = moonlightNode.fs; const logger = moonlightNode.getLogger("moonbase/natives"); return { @@ -71,25 +70,29 @@ export default function getNatives(): MoonbaseNatives { const dir = moonlightNode.getExtensionDir(manifest.id); // remake it in case of updates - if (await fs.exists(dir)) await fs.rmdir(dir); - await fs.mkdir(dir); + if (await moonlightFS.exists(dir)) await moonlightFS.rmdir(dir); + await moonlightFS.mkdir(dir); const buffer = await req.arrayBuffer(); const files = extractAsar(buffer); for (const [file, buf] of Object.entries(files)) { - const fullFile = fs.join(dir, file); - const fullDir = fs.dirname(fullFile); + const fullFile = moonlightFS.join(dir, file); + const fullDir = moonlightFS.dirname(fullFile); - if (!(await fs.exists(fullDir))) await fs.mkdir(fullDir); - await fs.writeFile(fs.join(dir, file), buf); + if (!(await moonlightFS.exists(fullDir))) + await moonlightFS.mkdir(fullDir); + await moonlightFS.writeFile(moonlightFS.join(dir, file), buf); } - await fs.writeFileString(fs.join(dir, repoUrlFile), repo); + await moonlightFS.writeFileString( + moonlightFS.join(dir, repoUrlFile), + repo + ); }, async deleteExtension(id) { const dir = moonlightNode.getExtensionDir(id); - await fs.rmdir(dir); + await moonlightFS.rmdir(dir); }, getExtensionConfig(id, key) { diff --git a/packages/core-extensions/src/moonbase/webpackModules/stores.ts b/packages/core-extensions/src/moonbase/webpackModules/stores.ts index 2cfe0e4..209ece9 100644 --- a/packages/core-extensions/src/moonbase/webpackModules/stores.ts +++ b/packages/core-extensions/src/moonbase/webpackModules/stores.ts @@ -8,7 +8,7 @@ import { mainRepo } from "@moonlight-mod/types/constants"; const logger = moonlight.getLogger("moonbase"); let natives: MoonbaseNatives = moonlight.getNatives("moonbase"); -if (!natives) natives = getNatives(); +if (moonlightNode.isBrowser) natives = getNatives(); class MoonbaseSettingsStore extends Store { private origConfig: Config; diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index e05e8c7..b824cc1 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -1,7 +1,6 @@ import { Config } from "@moonlight-mod/types"; import { getConfigPath } from "./util/data"; import * as constants from "@moonlight-mod/types/constants"; -import getFS from "./fs"; import Logger from "./util/logger"; const logger = new Logger("core/config"); @@ -18,9 +17,11 @@ const defaultConfig: Config = { export async function writeConfig(config: Config) { try { - const fs = getFS(); const configPath = await getConfigPath(); - await fs.writeFileString(configPath, JSON.stringify(config, null, 2)); + await moonlightFS.writeFileString( + configPath, + JSON.stringify(config, null, 2) + ); } catch (e) { logger.error("Failed to write config", e); } @@ -31,15 +32,15 @@ export async function readConfig(): Promise { return moonlightNode.config; } - const fs = getFS(); - const configPath = await getConfigPath(); - if (!(await fs.exists(configPath))) { + if (!(await moonlightFS.exists(configPath))) { await writeConfig(defaultConfig); return defaultConfig; } else { try { - let config: Config = JSON.parse(await fs.readFileString(configPath)); + let config: Config = JSON.parse( + await moonlightFS.readFileString(configPath) + ); // Assign the default values if they don't exist (newly added) config = { ...defaultConfig, ...config }; await writeConfig(config); diff --git a/packages/core/src/extension.ts b/packages/core/src/extension.ts index 7e1bb84..19e9744 100644 --- a/packages/core/src/extension.ts +++ b/packages/core/src/extension.ts @@ -2,28 +2,26 @@ import { ExtensionManifest, DetectedExtension, ExtensionLoadSource, - constants, - MoonlightFS + constants } from "@moonlight-mod/types"; import { readConfig } from "./config"; import { getCoreExtensionsPath, getExtensionsPath } from "./util/data"; import Logger from "./util/logger"; -import getFS from "./fs"; const logger = new Logger("core/extension"); -async function findManifests(fs: MoonlightFS, dir: string): Promise { +async function findManifests(dir: string): Promise { const ret = []; - if (await fs.exists(dir)) { - for (const file of await fs.readdir(dir)) { - const path = fs.join(dir, file); + if (await moonlightFS.exists(dir)) { + for (const file of await moonlightFS.readdir(dir)) { + const path = moonlightFS.join(dir, file); if (file === "manifest.json") { ret.push(path); } - if (!(await fs.isFile(path))) { - ret.push(...(await findManifests(fs, path))); + if (!(await moonlightFS.isFile(path))) { + ret.push(...(await findManifests(path))); } } } @@ -32,50 +30,58 @@ async function findManifests(fs: MoonlightFS, dir: string): Promise { } async function loadDetectedExtensions( - fs: MoonlightFS, dir: string, type: ExtensionLoadSource ): Promise { const ret: DetectedExtension[] = []; - const manifests = await findManifests(fs, dir); + const manifests = await findManifests(dir); for (const manifestPath of manifests) { try { - if (!(await fs.exists(manifestPath))) continue; - const dir = fs.dirname(manifestPath); + if (!(await moonlightFS.exists(manifestPath))) continue; + const dir = moonlightFS.dirname(manifestPath); const manifest: ExtensionManifest = JSON.parse( - await fs.readFileString(manifestPath) + await moonlightFS.readFileString(manifestPath) ); - const webPath = fs.join(dir, "index.js"); - const nodePath = fs.join(dir, "node.js"); - const hostPath = fs.join(dir, "host.js"); + const webPath = moonlightFS.join(dir, "index.js"); + const nodePath = moonlightFS.join(dir, "node.js"); + const hostPath = moonlightFS.join(dir, "host.js"); // if none exist (empty manifest) don't give a shit - if (!fs.exists(webPath) && !fs.exists(nodePath) && !fs.exists(hostPath)) { + if ( + !moonlightFS.exists(webPath) && + !moonlightFS.exists(nodePath) && + !moonlightFS.exists(hostPath) + ) { continue; } - const web = (await fs.exists(webPath)) - ? await fs.readFileString(webPath) + const web = (await moonlightFS.exists(webPath)) + ? await moonlightFS.readFileString(webPath) : undefined; let url: string | undefined = undefined; - const urlPath = fs.join(dir, constants.repoUrlFile); - if (type === ExtensionLoadSource.Normal && (await fs.exists(urlPath))) { - url = await fs.readFileString(urlPath); + const urlPath = moonlightFS.join(dir, constants.repoUrlFile); + if ( + type === ExtensionLoadSource.Normal && + (await moonlightFS.exists(urlPath)) + ) { + url = await moonlightFS.readFileString(urlPath); } const wpModules: Record = {}; - const wpModulesPath = fs.join(dir, "webpackModules"); - if (await fs.exists(wpModulesPath)) { - const wpModulesFile = await fs.readdir(wpModulesPath); + const wpModulesPath = moonlightFS.join(dir, "webpackModules"); + if (await moonlightFS.exists(wpModulesPath)) { + const wpModulesFile = await moonlightFS.readdir(wpModulesPath); for (const wpModuleFile of wpModulesFile) { if (wpModuleFile.endsWith(".js")) { wpModules[wpModuleFile.replace(".js", "")] = - await fs.readFileString(fs.join(wpModulesPath, wpModuleFile)); + await moonlightFS.readFileString( + moonlightFS.join(wpModulesPath, wpModuleFile) + ); } } } @@ -91,8 +97,8 @@ async function loadDetectedExtensions( web, webPath: web != null ? webPath : undefined, webpackModules: wpModules, - nodePath: (await fs.exists(nodePath)) ? nodePath : undefined, - hostPath: (await fs.exists(hostPath)) ? hostPath : undefined + nodePath: (await moonlightFS.exists(nodePath)) ? nodePath : undefined, + hostPath: (await moonlightFS.exists(hostPath)) ? hostPath : undefined } }); } catch (e) { @@ -106,11 +112,9 @@ async function loadDetectedExtensions( async function getExtensionsNative(): Promise { const config = await readConfig(); const res = []; - const fs = getFS(); res.push( ...(await loadDetectedExtensions( - fs, getCoreExtensionsPath(), ExtensionLoadSource.Core )) @@ -118,7 +122,6 @@ async function getExtensionsNative(): Promise { res.push( ...(await loadDetectedExtensions( - fs, await getExtensionsPath(), ExtensionLoadSource.Normal )) @@ -127,7 +130,6 @@ async function getExtensionsNative(): Promise { for (const devSearchPath of config.devSearchPaths ?? []) { res.push( ...(await loadDetectedExtensions( - fs, devSearchPath, ExtensionLoadSource.Developer )) @@ -176,11 +178,9 @@ async function getExtensionsBrowser(): Promise { }); } - const fs = getFS(); - if (await fs.exists("/extensions")) { + if (await moonlightFS.exists("/extensions")) { ret.push( ...(await loadDetectedExtensions( - fs, "/extensions", ExtensionLoadSource.Normal )) diff --git a/packages/core/src/fs.ts b/packages/core/src/fs.ts index 70eb8fe..123b4e5 100644 --- a/packages/core/src/fs.ts +++ b/packages/core/src/fs.ts @@ -1,56 +1,50 @@ -import { MoonlightFS } from "@moonlight-mod/types"; +import type { MoonlightFS } from "@moonlight-mod/types"; import requireImport from "./util/import"; -export default function getFS(): MoonlightFS { - browser: { - return window._moonlightBrowserFS!; - } +export default function createFS(): MoonlightFS { + const fs = requireImport("fs"); + const path = requireImport("path"); - nodeTarget: { - const fs = requireImport("fs"); - const path = requireImport("path"); + return { + async readFile(path) { + const file = fs.readFileSync(path); + return new Uint8Array(file); + }, + async readFileString(path) { + return fs.readFileSync(path, "utf8"); + }, + async writeFile(path, data) { + fs.writeFileSync(path, Buffer.from(data)); + }, + async writeFileString(path, data) { + fs.writeFileSync(path, data, "utf8"); + }, + async unlink(path) { + fs.unlinkSync(path); + }, - return { - async readFile(path) { - const file = fs.readFileSync(path); - return new Uint8Array(file); - }, - async readFileString(path) { - return fs.readFileSync(path, "utf8"); - }, - async writeFile(path, data) { - fs.writeFileSync(path, Buffer.from(data)); - }, - async writeFileString(path, data) { - fs.writeFileSync(path, data, "utf8"); - }, - async unlink(path) { - fs.unlinkSync(path); - }, + async readdir(path) { + return fs.readdirSync(path); + }, + async mkdir(path) { + fs.mkdirSync(path, { recursive: true }); + }, + async rmdir(path) { + fs.rmSync(path, { recursive: true }); + }, - async readdir(path) { - return fs.readdirSync(path); - }, - async mkdir(path) { - fs.mkdirSync(path, { recursive: true }); - }, - async rmdir(path) { - fs.rmSync(path, { recursive: true }); - }, + async exists(path) { + return fs.existsSync(path); + }, + async isFile(path) { + return fs.statSync(path).isFile(); + }, - async exists(path) { - return fs.existsSync(path); - }, - async isFile(path) { - return fs.statSync(path).isFile(); - }, - - join(...parts) { - return path.join(...parts); - }, - dirname(dir) { - return path.dirname(dir); - } - }; - } + join(...parts) { + return path.join(...parts); + }, + dirname(dir) { + return path.dirname(dir); + } + }; } diff --git a/packages/core/src/util/data.ts b/packages/core/src/util/data.ts index aa922f8..7f51ef4 100644 --- a/packages/core/src/util/data.ts +++ b/packages/core/src/util/data.ts @@ -1,5 +1,4 @@ import { constants } from "@moonlight-mod/types"; -import getFS from "../fs"; export async function getMoonlightDir() { browser: { @@ -7,7 +6,6 @@ export async function getMoonlightDir() { } const electron = require("electron"); - const fs = getFS(); let appData = ""; injector: { @@ -18,8 +16,8 @@ export async function getMoonlightDir() { appData = electron.ipcRenderer.sendSync(constants.ipcGetAppData); } - const dir = fs.join(appData, "moonlight-mod"); - if (!(await fs.exists(dir))) await fs.mkdir(dir); + const dir = moonlightFS.join(appData, "moonlight-mod"); + if (!(await moonlightFS.exists(dir))) await moonlightFS.mkdir(dir); return dir; } @@ -34,19 +32,21 @@ export async function getConfigPath() { return "/config.json"; } - const fs = getFS(); const dir = await getMoonlightDir(); let configPath = ""; - const buildInfoPath = fs.join(process.resourcesPath, "build_info.json"); - if (!(await fs.exists(buildInfoPath))) { - configPath = fs.join(dir, "desktop.json"); + const buildInfoPath = moonlightFS.join( + process.resourcesPath, + "build_info.json" + ); + if (!(await moonlightFS.exists(buildInfoPath))) { + configPath = moonlightFS.join(dir, "desktop.json"); } else { const buildInfo: BuildInfo = JSON.parse( - await fs.readFileString(buildInfoPath) + await moonlightFS.readFileString(buildInfoPath) ); - configPath = fs.join(dir, buildInfo.releaseChannel + ".json"); + configPath = moonlightFS.join(dir, buildInfo.releaseChannel + ".json"); } return configPath; @@ -54,10 +54,9 @@ export async function getConfigPath() { async function getPathFromMoonlight(...names: string[]) { const dir = await getMoonlightDir(); - const fs = getFS(); - const target = fs.join(dir, ...names); - if (!(await fs.exists(target))) await fs.mkdir(target); + const target = moonlightFS.join(dir, ...names); + if (!(await moonlightFS.exists(target))) await moonlightFS.mkdir(target); return target; } @@ -67,6 +66,5 @@ export async function getExtensionsPath() { } export function getCoreExtensionsPath(): string { - const fs = getFS(); - return fs.join(__dirname, constants.coreExtensionsDir); + return moonlightFS.join(__dirname, constants.coreExtensionsDir); } diff --git a/packages/injector/src/index.ts b/packages/injector/src/index.ts index a7eea23..5212f0a 100644 --- a/packages/injector/src/index.ts +++ b/packages/injector/src/index.ts @@ -16,6 +16,7 @@ import { import EventEmitter from "node:events"; import { join, resolve } from "node:path"; import persist from "@moonlight-mod/core/persist"; +import createFS from "@moonlight-mod/core/fs"; const logger = new Logger("injector"); @@ -201,6 +202,8 @@ Object.defineProperty(BrowserWindow, "name", { export async function inject(asarPath: string) { isMoonlightDesktop = asarPath === "moonlightDesktop"; + global.moonlightFS = createFS(); + try { const config = await readConfig(); initLogger(config); diff --git a/packages/node-preload/src/index.ts b/packages/node-preload/src/index.ts index 4828734..6f04d56 100644 --- a/packages/node-preload/src/index.ts +++ b/packages/node-preload/src/index.ts @@ -14,9 +14,11 @@ import { loadExtensions, loadProcessedExtensions } from "@moonlight-mod/core/extension/loader"; -import getFS from "@moonlight-mod/core/fs"; +import createFS from "core/src/fs"; async function injectGlobals() { + global.moonlightFS = createFS(); + const config = await readConfig(); initLogger(config); const extensions = await getExtensions(); @@ -35,7 +37,7 @@ async function injectGlobals() { extensions, processedExtensions, nativesCache: {}, - fs: getFS(), + isBrowser: false, version: MOONLIGHT_VERSION, branch: MOONLIGHT_BRANCH as MoonlightBranch, diff --git a/packages/types/src/globals.ts b/packages/types/src/globals.ts index 076dc41..be31d44 100644 --- a/packages/types/src/globals.ts +++ b/packages/types/src/globals.ts @@ -14,7 +14,6 @@ import type { EventType, MoonlightEventEmitter } from "./core/event"; -import type { MoonlightFS } from "./fs"; export type MoonlightHost = { asarPath: string; @@ -36,7 +35,7 @@ export type MoonlightNode = { extensions: DetectedExtension[]; processedExtensions: ProcessedExtensions; nativesCache: Record; - fs: MoonlightFS; + isBrowser: boolean; version: string; branch: MoonlightBranch; diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index 7414426..ebce9a4 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -37,8 +37,8 @@ declare global { var moonlightHost: MoonlightHost; var moonlightNode: MoonlightNode; var moonlight: MoonlightWeb; + var moonlightFS: MoonlightFS; var _moonlightBrowserInit: () => Promise; var _moonlightBrowserLoad: () => Promise; - var _moonlightBrowserFS: MoonlightFS; }