Skip to content

Commit

Permalink
Merge pull request #203 from moonlight-mod/notnite/node-events
Browse files Browse the repository at this point in the history
  • Loading branch information
Cynosphere authored Jan 28, 2025
2 parents 027f865 + 3719cec commit fb3ddc7
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 16 deletions.
4 changes: 4 additions & 0 deletions packages/browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { getConfig, getConfigOption, getManifest, setConfigOption } from "@moonl
import { IndexedDB } from "@zenfs/dom";
import { configureSingle } from "@zenfs/core";
import * as fs from "@zenfs/core/promises";
import { NodeEventPayloads, NodeEventType } from "@moonlight-mod/types/core/event";
import { createEventEmitter } from "@moonlight-mod/core/util/event";

function getParts(path: string) {
if (path.startsWith("/")) path = path.substring(1);
Expand Down Expand Up @@ -110,6 +112,7 @@ window._moonlightBrowserInit = async () => {
processedExtensions,
nativesCache: {},
isBrowser: true,
events: createEventEmitter<NodeEventType, NodeEventPayloads>(),

version: MOONLIGHT_VERSION,
branch: MOONLIGHT_BRANCH as MoonlightBranch,
Expand Down Expand Up @@ -141,6 +144,7 @@ window._moonlightBrowserInit = async () => {
async writeConfig(newConfig) {
await writeConfig(newConfig);
config = newConfig;
this.events.dispatchEvent(NodeEventType.ConfigSaved, newConfig);
}
};

Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/extension/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { registerPatch, registerWebpackModule } from "../patch";
import calculateDependencies from "../util/dependency";
import { createEventEmitter } from "../util/event";
import { registerStyles } from "../styles";
import { EventPayloads, EventType } from "@moonlight-mod/types/core/event";
import { WebEventPayloads, WebEventType } from "@moonlight-mod/types/core/event";

const logger = new Logger("core/extension/loader");

Expand Down Expand Up @@ -202,7 +202,7 @@ export async function loadExtensions(exts: DetectedExtension[]): Promise<Process
}

export async function loadProcessedExtensions({ extensions, dependencyGraph }: ProcessedExtensions) {
const eventEmitter = createEventEmitter<EventType, EventPayloads>();
const eventEmitter = createEventEmitter<WebEventType, WebEventPayloads>();
const finished: Set<string> = new Set();

logger.trace(
Expand All @@ -224,11 +224,11 @@ export async function loadProcessedExtensions({ extensions, dependencyGraph }: P
}

function done() {
eventEmitter.removeEventListener(EventType.ExtensionLoad, cb);
eventEmitter.removeEventListener(WebEventType.ExtensionLoad, cb);
r();
}

eventEmitter.addEventListener(EventType.ExtensionLoad, cb);
eventEmitter.addEventListener(WebEventType.ExtensionLoad, cb);
if (finished.has(dep)) done();
})
);
Expand All @@ -242,7 +242,7 @@ export async function loadProcessedExtensions({ extensions, dependencyGraph }: P
await loadExt(ext);

finished.add(ext.id);
eventEmitter.dispatchEvent(EventType.ExtensionLoad, ext.id);
eventEmitter.dispatchEvent(WebEventType.ExtensionLoad, ext.id);
logger.debug(`Loaded "${ext.id}"`);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "@moonlight-mod/types";
import Logger from "./util/logger";
import calculateDependencies, { Dependency } from "./util/dependency";
import { EventType } from "@moonlight-mod/types/core/event";
import { WebEventType } from "@moonlight-mod/types/core/event";
import { processFind, processReplace, testFind } from "./util/patch";

const logger = new Logger("core/patch");
Expand Down Expand Up @@ -402,7 +402,7 @@ export async function installWebpackPatcher() {
const realPush = jsonp.push;
if (jsonp.push.__moonlight !== true) {
jsonp.push = (items) => {
moonlight.events.dispatchEvent(EventType.ChunkLoad, {
moonlight.events.dispatchEvent(WebEventType.ChunkLoad, {
chunkId: items[0],
modules: items[1],
require: items[2]
Expand Down Expand Up @@ -450,7 +450,7 @@ export async function installWebpackPatcher() {
set(modules: any) {
const { stack } = new Error();
if (stack!.includes("/assets/") && !Array.isArray(modules)) {
moonlight.events.dispatchEvent(EventType.ChunkLoad, {
moonlight.events.dispatchEvent(WebEventType.ChunkLoad, {
modules: modules
});
patchModules(modules);
Expand Down
4 changes: 4 additions & 0 deletions packages/node-preload/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { loadExtensions, loadProcessedExtensions } from "@moonlight-mod/core/ext
import createFS from "@moonlight-mod/core/fs";
import { registerCors, registerBlocked, getDynamicCors } from "@moonlight-mod/core/cors";
import { getConfig, getConfigOption, getManifest, setConfigOption } from "@moonlight-mod/core/util/config";
import { NodeEventPayloads, NodeEventType } from "@moonlight-mod/types/core/event";
import { createEventEmitter } from "@moonlight-mod/core/util/event";

let initialized = false;
let logger: Logger;
Expand Down Expand Up @@ -51,6 +53,7 @@ async function injectGlobals() {
processedExtensions,
nativesCache: {},
isBrowser: false,
events: createEventEmitter<NodeEventType, NodeEventPayloads>(),

version: MOONLIGHT_VERSION,
branch: MOONLIGHT_BRANCH as MoonlightBranch,
Expand All @@ -69,6 +72,7 @@ async function injectGlobals() {
async writeConfig(newConfig) {
await writeConfig(newConfig);
config = newConfig;
this.events.dispatchEvent(NodeEventType.ConfigSaved, newConfig);
},

getNatives: (ext: string) => global.moonlightNode.nativesCache[ext],
Expand Down
17 changes: 13 additions & 4 deletions packages/types/src/core/event.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Config } from "../config";
import { WebpackModuleFunc, WebpackRequireType } from "../discord";

export interface MoonlightEventEmitter<EventId extends string = string, EventData = Record<EventId, any>> {
Expand All @@ -6,16 +7,24 @@ export interface MoonlightEventEmitter<EventId extends string = string, EventDat
removeEventListener: <Id extends keyof EventData>(id: Id, cb: (data: EventData[Id]) => void) => void;
}

export enum EventType {
export enum WebEventType {
ChunkLoad = "chunkLoad",
ExtensionLoad = "extensionLoad"
}

export type EventPayloads = {
[EventType.ChunkLoad]: {
export type WebEventPayloads = {
[WebEventType.ChunkLoad]: {
chunkId?: number[];
modules: { [id: string]: WebpackModuleFunc };
require?: (require: WebpackRequireType) => any;
};
[EventType.ExtensionLoad]: string;
[WebEventType.ExtensionLoad]: string;
};

export enum NodeEventType {
ConfigSaved = "configSaved"
}

export type NodeEventPayloads = {
[NodeEventType.ConfigSaved]: Config;
};
11 changes: 9 additions & 2 deletions packages/types/src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import type { DetectedExtension, IdentifiedPatch, IdentifiedWebpackModule, Proce
import type EventEmitter from "events";
import type LunAST from "@moonlight-mod/lunast";
import type Moonmap from "@moonlight-mod/moonmap";
import type { EventPayloads, EventType, MoonlightEventEmitter } from "./core/event";
import type {
WebEventPayloads,
WebEventType,
MoonlightEventEmitter,
NodeEventType,
NodeEventPayloads
} from "./core/event";
import { MoonlightFS } from "./fs";

export type MoonlightHost = {
Expand Down Expand Up @@ -34,6 +40,7 @@ export type MoonlightNode = {
processedExtensions: ProcessedExtensions;
nativesCache: Record<string, any>;
isBrowser: boolean;
events: MoonlightEventEmitter<NodeEventType, NodeEventPayloads>;

version: string;
branch: MoonlightBranch;
Expand All @@ -60,7 +67,7 @@ export type MoonlightWeb = {
unpatched: Set<IdentifiedPatch>;
pendingModules: Set<IdentifiedWebpackModule>;
enabledExtensions: Set<string>;
events: MoonlightEventEmitter<EventType, EventPayloads>;
events: MoonlightEventEmitter<WebEventType, WebEventPayloads>;
patchingInternals: {
onModuleLoad: (moduleId: string | string[], callback: (moduleId: string) => void) => void;
registerPatch: (patch: IdentifiedPatch) => void;
Expand Down
4 changes: 2 additions & 2 deletions packages/web-preload/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import LunAST from "@moonlight-mod/lunast";
import Moonmap from "@moonlight-mod/moonmap";
import loadMappings from "@moonlight-mod/mappings";
import { createEventEmitter } from "@moonlight-mod/core/util/event";
import { EventPayloads, EventType } from "@moonlight-mod/types/core/event";
import { WebEventPayloads, WebEventType } from "@moonlight-mod/types/core/event";

async function load() {
delete window._moonlightWebLoad;
Expand All @@ -20,7 +20,7 @@ async function load() {
pendingModules: new Set(),
enabledExtensions: new Set(),

events: createEventEmitter<EventType, EventPayloads>(),
events: createEventEmitter<WebEventType, WebEventPayloads>(),
patchingInternals: {
onModuleLoad,
registerPatch,
Expand Down

0 comments on commit fb3ddc7

Please sign in to comment.