2022-01-13 20:20:42 +00:00
|
|
|
import { createLogger } from "../core/logging";
|
|
|
|
|
import { Mod } from "./mod";
|
|
|
|
|
import { ModInterface } from "./mod_interface";
|
2022-01-14 06:18:25 +00:00
|
|
|
import { MOD_SIGNALS } from "./mod_signals";
|
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);
|
|
|
|
|
|
2022-01-14 06:37:26 +00:00
|
|
|
/** @type {((Object) => (new (Application, ModLoader) => Mod))[]} */
|
2022-01-13 21:14:49 +00:00
|
|
|
this.modLoadQueue = [];
|
2022-01-13 20:20:42 +00:00
|
|
|
|
|
|
|
|
this.initialized = false;
|
2022-01-13 21:14:49 +00:00
|
|
|
|
2022-01-14 06:18:25 +00:00
|
|
|
this.signals = MOD_SIGNALS;
|
2022-01-14 06:05:46 +00:00
|
|
|
|
2022-01-14 06:37:26 +00:00
|
|
|
this.registerMod(/** @type {any} */ (require("./demo_mod").default));
|
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");
|
2022-01-14 06:51:48 +00:00
|
|
|
|
|
|
|
|
let exports = {};
|
|
|
|
|
|
|
|
|
|
if (G_IS_DEV || G_IS_STANDALONE) {
|
|
|
|
|
const modules = require.context("../", true, /\.js$/);
|
|
|
|
|
|
|
|
|
|
Array.from(modules.keys()).forEach(key => {
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
const module = modules(key);
|
|
|
|
|
for (const member in module) {
|
|
|
|
|
if (member === "default") {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (exports[member]) {
|
2022-01-14 06:53:32 +00:00
|
|
|
throw new Error("Duplicate export of " + member);
|
2022-01-14 06:51:48 +00:00
|
|
|
}
|
|
|
|
|
Object.defineProperty(exports, member, {
|
|
|
|
|
get() {
|
|
|
|
|
return module[member];
|
|
|
|
|
},
|
|
|
|
|
set(v) {
|
|
|
|
|
module[member] = v;
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-13 20:20:42 +00:00
|
|
|
this.initialized = true;
|
2022-01-13 21:14:49 +00:00
|
|
|
this.modLoadQueue.forEach(modClass => {
|
2022-01-14 06:51:48 +00:00
|
|
|
const mod = new (modClass(exports))(this.app, this);
|
2022-01-13 21:14:49 +00:00
|
|
|
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-14 06:37:26 +00:00
|
|
|
* @param {(Object) => (new (Application, 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();
|