Server/project/src/loaders/PostDBModLoader.ts

97 lines
3.2 KiB
TypeScript
Raw Normal View History

2023-03-03 15:23:46 +00:00
import { DependencyContainer, inject, injectable } from "tsyringe";
import { OnLoad } from "@spt-aki/di/OnLoad";
import { BundleLoader } from "@spt-aki/loaders/BundleLoader";
import { ModTypeCheck } from "@spt-aki/loaders/ModTypeCheck";
import { PreAkiModLoader } from "@spt-aki/loaders/PreAkiModLoader";
import { IPostDBLoadMod } from "@spt-aki/models/external/IPostDBLoadMod";
import { IPostDBLoadModAsync } from "@spt-aki/models/external/IPostDBLoadModAsync";
import { ILogger } from "@spt-aki/models/spt/utils/ILogger";
import { LocalisationService } from "@spt-aki/services/LocalisationService";
2023-03-03 15:23:46 +00:00
@injectable()
export class PostDBModLoader implements OnLoad
{
protected container: DependencyContainer;
2023-03-03 15:23:46 +00:00
constructor(
@inject("WinstonLogger") protected logger: ILogger,
@inject("BundleLoader") protected bundleLoader: BundleLoader,
2023-03-03 15:23:46 +00:00
@inject("PreAkiModLoader") protected preAkiModLoader: PreAkiModLoader,
@inject("LocalisationService") protected localisationService: LocalisationService,
2023-11-13 11:10:44 -05:00
@inject("ModTypeCheck") protected modTypeCheck: ModTypeCheck,
2023-03-03 15:23:46 +00:00
)
2023-11-13 11:10:44 -05:00
{}
2023-03-03 15:23:46 +00:00
public async onLoad(): Promise<void>
{
if (globalThis.G_MODS_ENABLED)
{
this.container = this.preAkiModLoader.getContainer();
await this.executeModsAsync();
this.addBundles();
2023-03-03 15:23:46 +00:00
}
}
2023-11-13 11:10:44 -05:00
2023-03-03 15:23:46 +00:00
public getRoute(): string
{
return "aki-mods";
}
public getModPath(mod: string): string
{
return this.preAkiModLoader.getModPath(mod);
}
protected async executeModsAsync(): Promise<void>
2023-03-03 15:23:46 +00:00
{
const mods = this.preAkiModLoader.sortModsLoadOrder();
for (const modName of mods)
{
2023-11-13 11:10:44 -05:00
// import class
const filepath = `${this.preAkiModLoader.getModPath(modName)}${
this.preAkiModLoader.getImportedModDetails()[modName].main
}`;
2023-03-03 15:23:46 +00:00
const modpath = `${process.cwd()}/${filepath}`;
// eslint-disable-next-line @typescript-eslint/no-var-requires
const mod = require(modpath);
if (this.modTypeCheck.isPostDBAkiLoadAsync(mod.mod))
2023-03-03 15:23:46 +00:00
{
2023-11-13 11:10:44 -05:00
try
{
await (mod.mod as IPostDBLoadModAsync).postDBLoadAsync(this.container);
}
2023-11-13 11:10:44 -05:00
catch (err)
{
2023-11-13 11:10:44 -05:00
this.logger.error(
this.localisationService.getText(
"modloader-async_mod_error",
`${err?.message ?? ""}\n${err.stack ?? ""}`,
),
);
}
2023-03-03 15:23:46 +00:00
}
if (this.modTypeCheck.isPostDBAkiLoad(mod.mod))
2023-03-03 15:23:46 +00:00
{
(mod.mod as IPostDBLoadMod).postDBLoad(this.container);
}
}
}
protected addBundles(): void
{
const importedMods = this.preAkiModLoader.getImportedModDetails();
for (const [mod, pkg] of Object.entries(importedMods))
{
const modPath = this.preAkiModLoader.getModPath(mod);
if (pkg.isBundleMod ?? false)
{
this.bundleLoader.addBundles(modPath);
2023-03-03 15:23:46 +00:00
}
}
}
2023-11-13 11:10:44 -05:00
}