1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-12-16 11:41:50 +00:00
tobspr_shapez.io/src/js/mods/modloader.js

67 lines
1.8 KiB
JavaScript
Raw Normal View History

2022-01-13 20:20:42 +00:00
import { createLogger } from "../core/logging";
2022-01-13 21:14:49 +00:00
import { Signal } from "../core/signal";
2022-01-13 20:20:42 +00:00
import { DemoMod } from "./demo_mod";
import { Mod } from "./mod";
import { ModInterface } from "./mod_interface";
import { BaseHUDPart } from "../game/hud/base_hud_part";
2022-01-13 20:20:42 +00:00
const LOG = createLogger("mods");
export class ModLoader {
constructor() {
LOG.log("modloader created");
/** @type {Mod[]} */
this.mods = [];
2022-01-13 21:14:49 +00:00
this.modInterface = new ModInterface(this);
/** @type {(new (ModLoader) => Mod)[]} */
this.modLoadQueue = [];
2022-01-13 20:20:42 +00:00
this.initialized = false;
2022-01-13 21:14:49 +00:00
this.signals = {
postInit: new Signal(),
injectSprites: new Signal(),
preprocessTheme: /** @type {TypedSignal<[Object]>} */ (new Signal()),
modifyLevelDefinitions: /** @type {TypedSignal<[Array[Object]]>} */ (new Signal()),
hudElementInitialized: /** @type {TypedSignal<[BaseHUDPart]>} */ (new Signal()),
hudElementFinalized: /** @type {TypedSignal<[BaseHUDPart]>} */ (new Signal()),
2022-01-13 21:14:49 +00:00
};
this.registerMod(DemoMod);
this.initMods();
2022-01-13 20:20:42 +00:00
}
linkApp(app) {
this.app = app;
}
2022-01-13 21:14:49 +00:00
initMods() {
2022-01-13 20:20:42 +00:00
LOG.log("hook:init");
this.initialized = true;
2022-01-13 21:14:49 +00:00
this.modLoadQueue.forEach(modClass => {
const mod = new modClass(this);
mod.init();
this.mods.push(mod);
2022-01-13 20:20:42 +00:00
});
2022-01-13 21:14:49 +00:00
this.modLoadQueue = [];
this.signals.postInit.dispatch();
2022-01-13 20:20:42 +00:00
}
/**
*
2022-01-13 21:14:49 +00:00
* @param {new (ModLoader) => Mod} mod
2022-01-13 20:20:42 +00:00
*/
registerMod(mod) {
if (this.initialized) {
throw new Error("Mods are already initialized, can not add mod afterwards.");
}
2022-01-13 21:14:49 +00:00
this.modLoadQueue.push(mod);
2022-01-13 20:20:42 +00:00
}
}
export const MODS = new ModLoader();