defered addBundles after PostDBModLoader is run

- defered addBundles after PostDBModLoader is run
- removed unused injected class instances
This commit is contained in:
TheSparta 2024-03-24 17:52:38 +00:00
parent fdc6be4dc7
commit 5e8fb7b303
3 changed files with 39 additions and 45 deletions

View File

@ -1,6 +1,5 @@
import { DependencyContainer, inject, injectable } from "tsyringe";
import { BundleLoader } from "@spt-aki/loaders/BundleLoader";
import { ModTypeCheck } from "@spt-aki/loaders/ModTypeCheck";
import { PreAkiModLoader } from "@spt-aki/loaders/PreAkiModLoader";
import { IPostAkiLoadMod } from "@spt-aki/models/external/IPostAkiLoadMod";
@ -8,15 +7,14 @@ import { IPostAkiLoadModAsync } from "@spt-aki/models/external/IPostAkiLoadModAs
import { IModLoader } from "@spt-aki/models/spt/mod/IModLoader";
import { ILogger } from "@spt-aki/models/spt/utils/ILogger";
import { LocalisationService } from "@spt-aki/services/LocalisationService";
import { VFS } from "@spt-aki/utils/VFS";
@injectable()
export class PostAkiModLoader implements IModLoader
{
protected container: DependencyContainer;
constructor(
@inject("WinstonLogger") protected logger: ILogger,
@inject("BundleLoader") protected bundleLoader: BundleLoader,
@inject("VFS") protected vfs: VFS,
@inject("PreAkiModLoader") protected preAkiModLoader: PreAkiModLoader,
@inject("LocalisationService") protected localisationService: LocalisationService,
@inject("ModTypeCheck") protected modTypeCheck: ModTypeCheck,
@ -32,12 +30,12 @@ export class PostAkiModLoader implements IModLoader
{
if (globalThis.G_MODS_ENABLED)
{
await this.executeMods(this.preAkiModLoader.getContainer());
this.addBundles();
this.container = this.preAkiModLoader.getContainer();
await this.executeModsAsync();
}
}
protected async executeMods(container: DependencyContainer): Promise<void>
protected async executeModsAsync(): Promise<void>
{
const mods = this.preAkiModLoader.sortModsLoadOrder();
for (const modName of mods)
@ -54,7 +52,7 @@ export class PostAkiModLoader implements IModLoader
{
try
{
await (mod.mod as IPostAkiLoadModAsync).postAkiLoadAsync(container);
await (mod.mod as IPostAkiLoadModAsync).postAkiLoadAsync(this.container);
}
catch (err)
{
@ -69,21 +67,7 @@ export class PostAkiModLoader implements IModLoader
if (this.modTypeCheck.isPostAkiLoad(mod.mod))
{
(mod.mod as IPostAkiLoadMod).postAkiLoad(container);
}
}
}
protected addBundles(): void
{
const mods = this.preAkiModLoader.sortModsLoadOrder();
for (const modName of mods)
{
// add mod bundles
const modpath = this.preAkiModLoader.getModPath(modName);
if (this.vfs.exists(`${modpath}bundles.json`))
{
this.bundleLoader.addBundles(modpath);
(mod.mod as IPostAkiLoadMod).postAkiLoad(this.container);
}
}
}

View File

@ -1,6 +1,7 @@
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";
@ -11,8 +12,11 @@ import { LocalisationService } from "@spt-aki/services/LocalisationService";
@injectable()
export class PostDBModLoader implements OnLoad
{
protected container: DependencyContainer;
constructor(
@inject("WinstonLogger") protected logger: ILogger,
@inject("BundleLoader") protected bundleLoader: BundleLoader,
@inject("PreAkiModLoader") protected preAkiModLoader: PreAkiModLoader,
@inject("LocalisationService") protected localisationService: LocalisationService,
@inject("ModTypeCheck") protected modTypeCheck: ModTypeCheck,
@ -23,7 +27,9 @@ export class PostDBModLoader implements OnLoad
{
if (globalThis.G_MODS_ENABLED)
{
await this.executeMods(this.preAkiModLoader.getContainer());
this.container = this.preAkiModLoader.getContainer();
await this.executeModsAsync();
this.addBundles();
}
}
@ -37,7 +43,7 @@ export class PostDBModLoader implements OnLoad
return this.preAkiModLoader.getModPath(mod);
}
protected async executeMods(container: DependencyContainer): Promise<void>
protected async executeModsAsync(): Promise<void>
{
const mods = this.preAkiModLoader.sortModsLoadOrder();
for (const modName of mods)
@ -54,7 +60,7 @@ export class PostDBModLoader implements OnLoad
{
try
{
await (mod.mod as IPostDBLoadModAsync).postDBLoadAsync(container);
await (mod.mod as IPostDBLoadModAsync).postDBLoadAsync(this.container);
}
catch (err)
{
@ -69,7 +75,21 @@ export class PostDBModLoader implements OnLoad
if (this.modTypeCheck.isPostDBAkiLoad(mod.mod))
{
(mod.mod as IPostDBLoadMod).postDBLoad(container);
(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);
}
}
}

View File

@ -4,7 +4,6 @@ import path from "node:path";
import semver from "semver";
import { DependencyContainer, inject, injectable } from "tsyringe";
import { BundleLoader } from "@spt-aki/loaders/BundleLoader";
import { ModLoadOrder } from "@spt-aki/loaders/ModLoadOrder";
import { ModTypeCheck } from "@spt-aki/loaders/ModTypeCheck";
import { ModDetails } from "@spt-aki/models/eft/profile/IAkiProfile";
@ -24,7 +23,7 @@ import { VFS } from "@spt-aki/utils/VFS";
@injectable()
export class PreAkiModLoader implements IModLoader
{
protected static container: DependencyContainer;
protected container: DependencyContainer;
protected readonly basepath = "user/mods/";
protected readonly modOrderPath = "user/mods/order.json";
@ -39,7 +38,6 @@ export class PreAkiModLoader implements IModLoader
@inject("VFS") protected vfs: VFS,
@inject("JsonUtil") protected jsonUtil: JsonUtil,
@inject("ModCompilerService") protected modCompilerService: ModCompilerService,
@inject("BundleLoader") protected bundleLoader: BundleLoader,
@inject("LocalisationService") protected localisationService: LocalisationService,
@inject("ConfigServer") protected configServer: ConfigServer,
@inject("ModLoadOrder") protected modLoadOrder: ModLoadOrder,
@ -57,9 +55,9 @@ export class PreAkiModLoader implements IModLoader
{
if (globalThis.G_MODS_ENABLED)
{
PreAkiModLoader.container = container;
this.container = container;
await this.importModsAsync();
await this.executeModsAsync(container);
await this.executeModsAsync();
}
}
@ -363,10 +361,9 @@ export class PreAkiModLoader implements IModLoader
/**
* Execute each mod found in this.imported
* @param container Dependence container to give to mod when it runs
* @returns void promise
*/
protected async executeModsAsync(container: DependencyContainer): Promise<void>
protected async executeModsAsync(): Promise<void>
{
// Sort mods load order
const source = this.sortModsLoadOrder();
@ -401,7 +398,7 @@ export class PreAkiModLoader implements IModLoader
{
try
{
await (requiredMod.mod as IPreAkiLoadModAsync).preAkiLoadAsync(container);
await (requiredMod.mod as IPreAkiLoadModAsync).preAkiLoadAsync(this.container);
globalThis[mod] = requiredMod;
}
catch (err)
@ -420,7 +417,7 @@ export class PreAkiModLoader implements IModLoader
// Perform sync load of mod
if (this.modTypeCheck.isPreAkiLoad(requiredMod.mod))
{
(requiredMod.mod as IPreAkiLoadMod).preAkiLoad(container);
(requiredMod.mod as IPreAkiLoadMod).preAkiLoad(this.container);
globalThis[mod] = requiredMod;
}
}
@ -450,13 +447,6 @@ export class PreAkiModLoader implements IModLoader
{
const modPath = this.getModPath(mod);
const isBundleMod = pkg.isBundleMod ?? false;
if (isBundleMod)
{
this.bundleLoader.addBundles(modPath);
}
const typeScriptFiles = this.vfs.getFilesOfType(`${modPath}src`, ".ts");
if (typeScriptFiles.length > 0)
@ -736,9 +726,9 @@ export class PreAkiModLoader implements IModLoader
public getContainer(): DependencyContainer
{
if (PreAkiModLoader.container)
if (this.container)
{
return PreAkiModLoader.container;
return this.container;
}
throw new Error(this.localisationService.getText("modloader-dependency_container_not_initalized"));