shapez/src/js/mods/modloader.js

108 lines
2.5 KiB
JavaScript

/* typehints:start */
import { Application } from "../application";
/* typehints:end */
import { createLogger } from "../core/logging";
import { Mod } from "./mod";
import { ModInterface } from "./mod_interface";
import { MOD_SIGNALS } from "./mod_signals";
const LOG = createLogger("mods");
/**
* @typedef {{
* name: string;
* version: string;
* author: string;
* website: string;
* description: string;
* id: string;
* minimumGameVersion?: string;
* settings: [];
* doesNotAffectSavegame?: boolean
* }} ModMetadata
*/
export class ModLoader {
constructor() {
LOG.log("modloader created");
/**
* @type {Application}
*/
this.app = undefined;
/** @type {Mod[]} */
this.mods = [];
this.modInterface = new ModInterface(this);
/** @type {({ meta: ModMetadata, modClass: typeof Mod})[]} */
this.modLoadQueue = [];
this.initialized = false;
this.signals = MOD_SIGNALS;
}
linkApp(app) {
this.app = app;
}
anyModsActive() {
return this.mods.length > 0;
}
/**
*
* @returns {import("../savegame/savegame_typedefs").SavegameStoredMods}
*/
getModsListForSavegame() {
return this.mods
.filter(mod => !mod.metadata.doesNotAffectSavegame)
.map(mod => ({
id: mod.metadata.id,
version: mod.metadata.version,
website: mod.metadata.website,
name: mod.metadata.name,
author: mod.metadata.author,
}));
}
/**
*
* @param {import("../savegame/savegame_typedefs").SavegameStoredMods} originalMods
*/
computeModDifference(originalMods) {
/**
* @type {import("../savegame/savegame_typedefs").SavegameStoredMods}
*/
let missing = [];
const current = this.getModsListForSavegame();
originalMods.forEach(mod => {
for (let i = 0; i < current.length; ++i) {
const currentMod = current[i];
if (currentMod.id === mod.id && currentMod.version === mod.version) {
current.splice(i, 1);
return;
}
}
missing.push(mod);
});
return {
missing,
extra: current,
};
}
exposeExports() {}
async initMods() {
this.initialized = true;
}
}
export const MODS = new ModLoader();