diff --git a/src/js/mods/mod.ts b/src/js/mods/mod.ts index 4b756b60..bc1b9582 100644 --- a/src/js/mods/mod.ts +++ b/src/js/mods/mod.ts @@ -1,5 +1,5 @@ import { Application } from "@/application"; -import { ModInterface } from "./mod_interface"; +import { ModInterfaceV2 } from "./mod_interface_v2"; import { FrozenModMetadata, ModMetadata } from "./mod_metadata"; import { MOD_SIGNALS } from "./mod_signals"; import { ModLoader } from "./modloader"; @@ -20,7 +20,7 @@ export abstract class Mod { // TODO: Review what properties are necessary while improving ModInterface protected readonly app: Application; protected readonly modLoader: ModLoader; - protected readonly modInterface: ModInterface; + protected readonly modInterface: ModInterfaceV2; protected readonly signals = MOD_SIGNALS; // Exposed for convenience @@ -31,11 +31,12 @@ export abstract class Mod { constructor(metadata: ModMetadata, app: Application, modLoader: ModLoader) { this.app = app; this.modLoader = modLoader; - // TODO: ModInterface should accept the mod instance - this.modInterface = new ModInterface(modLoader); this.id = metadata.id; this.metadata = freezeMetadata(metadata); + + // ModInterfaceV2 assumes id to be set + this.modInterface = new ModInterfaceV2(this, modLoader); } abstract init(): void | Promise; diff --git a/src/js/mods/mod_interface_v2.ts b/src/js/mods/mod_interface_v2.ts new file mode 100644 index 00000000..f67c056c --- /dev/null +++ b/src/js/mods/mod_interface_v2.ts @@ -0,0 +1,38 @@ +import { Mod } from "./mod"; +import { ModInterface } from "./mod_interface"; +import { ModLoader } from "./modloader"; + +export class ModInterfaceV2 extends ModInterface { + private readonly mod: Mod; + private readonly baseUrl: string; + + constructor(mod: Mod, modLoader: ModLoader) { + super(modLoader); + this.mod = mod; + this.baseUrl = `mod://${mod.id}`; + } + + resolve(path: string) { + path = path + .split("/") + .map(p => encodeURIComponent(p)) + .join("/"); + + if (!path.startsWith("./")) { + // Assume relative if not specified + path = `./${path}`; + } + + // Cannot use import.meta in webpack context + return new URL(path, this.baseUrl).toString(); + } + + addStylesheet(path: string) { + const link = document.createElement("link"); + link.rel = "stylesheet"; + link.href = this.resolve(path); + link.setAttribute("data-mod-id", this.mod.id); + + document.head.append(link); + } +}