1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-12-13 18:21:51 +00:00

Make shapez exports global

This commit is contained in:
tobspr 2022-01-14 19:12:50 +01:00
parent 2d3219373f
commit e12ed11c3e
3 changed files with 38 additions and 29 deletions

4
src/js/globals.d.ts vendored
View File

@ -22,6 +22,8 @@ declare const G_IS_RELEASE: boolean;
declare const G_CHINA_VERSION: boolean;
declare const G_WEGAME_VERSION: boolean;
declare const shapez: any;
// Polyfills
declare interface String {
replaceAll(search: string, replacement: string): string;
@ -95,6 +97,8 @@ declare interface Window {
registerMod: any;
anyModLoaded: any;
shapez: any;
webkitRequestAnimationFrame();
assert(condition: boolean, failureMessage: string);

View File

@ -1,4 +1,4 @@
registerMod(shapez => {
registerMod(() => {
class DemoModComponent extends shapez.Component {
static getId() {
return "DemoMod";

View File

@ -41,9 +41,41 @@ export class ModLoader {
return this.mods.length > 0;
}
exposeExports() {
if (G_IS_DEV || G_IS_STANDALONE) {
let exports = {};
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]) {
throw new Error("Duplicate export of " + member);
}
Object.defineProperty(exports, member, {
get() {
return module[member];
},
set(v) {
module[member] = v;
},
});
}
});
window.shapez = exports;
}
}
async initMods() {
LOG.log("hook:init");
this.exposeExports();
if (G_IS_STANDALONE || G_IS_DEV) {
try {
let mods = [];
@ -71,36 +103,9 @@ export class ModLoader {
}
}
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]) {
throw new Error("Duplicate export of " + member);
}
Object.defineProperty(exports, member, {
get() {
return module[member];
},
set(v) {
module[member] = v;
},
});
}
});
}
this.initialized = true;
this.modLoadQueue.forEach(modClass => {
const mod = new (modClass(exports))(this.app, this);
const mod = new (modClass())(this.app, this);
mod.init();
this.mods.push(mod);
});