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

78 lines
2.2 KiB
JavaScript
Raw Normal View History

2022-01-13 20:20:42 +00:00
/* typehints:start */
import { Application } from "../application";
import { ModLoader } from "./modloader";
/* typehints:end */
import { createLogger } from "../core/logging";
import { AtlasSprite, SpriteAtlasLink } from "../core/sprites";
import { Mod } from "./mod";
const LOG = createLogger("mod-interface");
export class ModInterface {
/**
*
* @param {ModLoader} modLoader
* @param {Mod} mod
*/
constructor(modLoader, mod) {
/**
* @param {Application} app
*/
this.app = undefined;
this.modLoader = modLoader;
this.mod = mod;
}
registerCss(cssString) {
const element = document.createElement("style");
element.textContent = cssString;
element.setAttribute("data-mod-id", this.mod.metadata.id);
element.setAttribute("data-mod-name", this.mod.metadata.name);
document.head.appendChild(element);
}
registerSprite(spriteId, base64string) {
assert(base64string.startsWith("data:image"));
const img = new Image();
img.src = base64string;
const sprite = new AtlasSprite(spriteId);
const link = new SpriteAtlasLink({
w: img.width,
h: img.height,
atlas: img,
packOffsetX: 0,
packOffsetY: 0,
packedW: img.width,
packedH: img.height,
packedX: 0,
packedY: 0,
});
sprite.linksByResolution["0.25"] = link;
sprite.linksByResolution["0.5"] = link;
sprite.linksByResolution["0.75"] = link;
// @ts-ignore
sprite.modSource = this.mod;
const oldSprite = this.modLoader.lazySprites.get(spriteId);
if (oldSprite) {
LOG.error(
"Sprite '" +
spriteId +
"' is provided twice, once by mod '" +
// @ts-ignore
oldSprite.modSource.metadata.name +
"' and once by mod '" +
this.mod.metadata.name +
"'. This could cause artifacts."
);
}
this.modLoader.lazySprites.set(spriteId, sprite);
}
}