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

Create ModInterfaceV2 for cool stuff

For now it only includes a helper function to resolve file URLs and a
method that adds a CSS stylesheet to the document.
This commit is contained in:
Даниїл Григор'єв 2025-04-13 02:58:41 +03:00
parent 7b4cb25d5d
commit c9d2a16ada
No known key found for this signature in database
GPG Key ID: B890DF16341D8C1D
2 changed files with 43 additions and 4 deletions

View File

@ -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<void>;

View File

@ -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);
}
}