Formatting for loader classes.

This commit is contained in:
Refringe 2023-11-13 11:10:44 -05:00
parent 8586447d21
commit 3f2d36a2fc
No known key found for this signature in database
GPG Key ID: 64E03E5F892C6F9E
6 changed files with 153 additions and 84 deletions

View File

@ -1,4 +1,3 @@
import { inject, injectable } from "tsyringe";
import { HttpServerHelper } from "@spt-aki/helpers/HttpServerHelper";
@ -31,7 +30,7 @@ export class BundleLoader
constructor(
@inject("HttpServerHelper") protected httpServerHelper: HttpServerHelper,
@inject("VFS") protected vfs: VFS,
@inject("JsonUtil") protected jsonUtil: JsonUtil
@inject("JsonUtil") protected jsonUtil: JsonUtil,
)
{}
@ -65,7 +64,8 @@ export class BundleLoader
public addBundles(modpath: string): void
{
const manifest = this.jsonUtil.deserialize<BundleManifest>(this.vfs.readFile(`${modpath}bundles.json`)).manifest;
const manifest =
this.jsonUtil.deserialize<BundleManifest>(this.vfs.readFile(`${modpath}bundles.json`)).manifest;
for (const bundle of manifest)
{
@ -83,11 +83,11 @@ export class BundleLoader
export interface BundleManifest
{
manifest: Array<BundleManifestEntry>
manifest: Array<BundleManifestEntry>;
}
export interface BundleManifestEntry
{
key: string
path: string
key: string;
path: string;
}

View File

@ -13,7 +13,7 @@ export class ModLoadOrder
constructor(
@inject("WinstonLogger") protected logger: ILogger,
@inject("LocalisationService") protected localisationService: LocalisationService
@inject("LocalisationService") protected localisationService: LocalisationService,
)
{}
@ -143,7 +143,12 @@ export class ModLoadOrder
{
if (this.modsAvailable.get(modAfter)?.loadAfter?.includes(mod))
{
throw new Error(this.localisationService.getText("modloader-load_order_conflict", {modOneName: mod, modTwoName: modAfter}));
throw new Error(
this.localisationService.getText("modloader-load_order_conflict", {
modOneName: mod,
modTwoName: modAfter,
}),
);
}
dependencies.add(modAfter);

View File

@ -10,7 +10,6 @@ import { IPreAkiLoadModAsync } from "@spt-aki/models/external/IPreAkiLoadModAsyn
@injectable()
export class ModTypeCheck
{
/**
* Use defined safe guard to check if the mod is a IPreAkiLoadMod
* @returns boolean
@ -77,6 +76,5 @@ export class ModTypeCheck
this.isPreAkiLoadAsync(mod) ||
this.isPostAkiLoadAsync(mod) ||
this.isPostDBAkiLoadAsync(mod);
}
}

View File

@ -19,7 +19,7 @@ export class PostAkiModLoader implements IModLoader
@inject("VFS") protected vfs: VFS,
@inject("PreAkiModLoader") protected preAkiModLoader: PreAkiModLoader,
@inject("LocalisationService") protected localisationService: LocalisationService,
@inject("ModTypeCheck") protected modTypeCheck: ModTypeCheck
@inject("ModTypeCheck") protected modTypeCheck: ModTypeCheck,
)
{}
@ -43,7 +43,9 @@ export class PostAkiModLoader implements IModLoader
for (const modName of mods)
{
// // import class
const filepath = `${this.preAkiModLoader.getModPath(modName)}${this.preAkiModLoader.getImportedModDetails()[modName].main}`;
const filepath = `${this.preAkiModLoader.getModPath(modName)}${
this.preAkiModLoader.getImportedModDetails()[modName].main
}`;
const modpath = `${process.cwd()}/${filepath}`;
// eslint-disable-next-line @typescript-eslint/no-var-requires
const mod = require(modpath);
@ -56,7 +58,12 @@ export class PostAkiModLoader implements IModLoader
}
catch (err)
{
this.logger.error(this.localisationService.getText("modloader-async_mod_error", `${err?.message ?? ""}\n${err.stack ?? ""}`));
this.logger.error(
this.localisationService.getText(
"modloader-async_mod_error",
`${err?.message ?? ""}\n${err.stack ?? ""}`,
),
);
}
}

View File

@ -15,7 +15,7 @@ export class PostDBModLoader implements OnLoad
@inject("WinstonLogger") protected logger: ILogger,
@inject("PreAkiModLoader") protected preAkiModLoader: PreAkiModLoader,
@inject("LocalisationService") protected localisationService: LocalisationService,
@inject("ModTypeCheck") protected modTypeCheck: ModTypeCheck
@inject("ModTypeCheck") protected modTypeCheck: ModTypeCheck,
)
{}
@ -32,7 +32,6 @@ export class PostDBModLoader implements OnLoad
return "aki-mods";
}
public getModPath(mod: string): string
{
return this.preAkiModLoader.getModPath(mod);
@ -43,8 +42,10 @@ export class PostDBModLoader implements OnLoad
const mods = this.preAkiModLoader.sortModsLoadOrder();
for (const modName of mods)
{
// // import class
const filepath = `${this.preAkiModLoader.getModPath(modName)}${this.preAkiModLoader.getImportedModDetails()[modName].main}`;
// import class
const filepath = `${this.preAkiModLoader.getModPath(modName)}${
this.preAkiModLoader.getImportedModDetails()[modName].main
}`;
const modpath = `${process.cwd()}/${filepath}`;
// eslint-disable-next-line @typescript-eslint/no-var-requires
const mod = require(modpath);
@ -57,7 +58,12 @@ export class PostDBModLoader implements OnLoad
}
catch (err)
{
this.logger.error(this.localisationService.getText("modloader-async_mod_error", `${err?.message ?? ""}\n${err.stack ?? ""}`));
this.logger.error(
this.localisationService.getText(
"modloader-async_mod_error",
`${err?.message ?? ""}\n${err.stack ?? ""}`,
),
);
}
}

View File

@ -43,7 +43,7 @@ export class PreAkiModLoader implements IModLoader
@inject("LocalisationService") protected localisationService: LocalisationService,
@inject("ConfigServer") protected configServer: ConfigServer,
@inject("ModLoadOrder") protected modLoadOrder: ModLoadOrder,
@inject("ModTypeCheck") protected modTypeCheck: ModTypeCheck
@inject("ModTypeCheck") protected modTypeCheck: ModTypeCheck,
)
{
this.akiConfig = this.configServer.getConfig<ICoreConfig>(ConfigTypes.CORE);
@ -96,10 +96,10 @@ export class PreAkiModLoader implements IModLoader
for (const modName in modsGroupedByName)
{
const modDatas = modsGroupedByName[modName];
const modVersions = modDatas.map(x => x.version);
const modVersions = modDatas.map((x) => x.version);
const highestVersion = semver.maxSatisfying(modVersions, "*");
const chosenVersion = modDatas.find(x => x.name === modName && x.version === highestVersion);
const chosenVersion = modDatas.find((x) => x.name === modName && x.version === highestVersion);
if (!chosenVersion)
{
continue;
@ -175,7 +175,10 @@ export class PreAkiModLoader implements IModLoader
}
// if the mod has library dependencies check if these dependencies are bundled in the server, if not install them
if (modToValidate.dependencies && Object.keys(modToValidate.dependencies).length > 0 && !this.vfs.exists(`${this.basepath}${modFolderName}/node_modules`))
if (
modToValidate.dependencies && Object.keys(modToValidate.dependencies).length > 0 &&
!this.vfs.exists(`${this.basepath}${modFolderName}/node_modules`)
)
{
this.autoInstallDependencies(`${this.basepath}${modFolderName}`, modToValidate);
}
@ -398,7 +401,12 @@ export class PreAkiModLoader implements IModLoader
}
catch (err)
{
this.logger.error(this.localisationService.getText("modloader-async_mod_error", `${err?.message ?? ""}\n${err.stack ?? ""}`));
this.logger.error(
this.localisationService.getText(
"modloader-async_mod_error",
`${err?.message ?? ""}\n${err.stack ?? ""}`,
),
);
}
continue;
@ -457,7 +465,7 @@ export class PreAkiModLoader implements IModLoader
else
{
// rename the mod entry point to .ts if it's set to .js because G_MODS_TRANSPILE_TS is set to false
pkg.main = (pkg.main).replace(".js", ".ts");
pkg.main = pkg.main.replace(".js", ".ts");
}
}
@ -466,7 +474,13 @@ export class PreAkiModLoader implements IModLoader
// Add mod to imported list
this.imported[mod] = {...pkg, dependencies: pkg.modDependencies};
this.logger.info(this.localisationService.getText("modloader-loaded_mod", {name: pkg.name, version: pkg.version, author: pkg.author}));
this.logger.info(
this.localisationService.getText("modloader-loaded_mod", {
name: pkg.name,
version: pkg.version,
author: pkg.author,
}),
);
}
/**
@ -509,12 +523,17 @@ export class PreAkiModLoader implements IModLoader
// If this feature flag is set to false, we warn the user he has a mod that requires extra dependencies and might not work, point them in the right direction on how to enable this feature.
if (!this.akiConfig.features.autoInstallModDependencies)
{
this.logger.warning(this.localisationService.getText("modloader-installing_external_dependencies_disabled", {
this.logger.warning(
this.localisationService.getText("modloader-installing_external_dependencies_disabled", {
name: pkg.name,
author: pkg.author,
configPath: path.join(globalThis.G_RELEASE_CONFIGURATION ? "Aki_Data/Server/configs" : "assets/configs", "core.json"),
configOption: "autoInstallModDependencies"
}));
configPath: path.join(
globalThis.G_RELEASE_CONFIGURATION ? "Aki_Data/Server/configs" : "assets/configs",
"core.json",
),
configOption: "autoInstallModDependencies",
}),
);
this.skippedMods.add(`${pkg.author}-${pkg.name}`);
return;
@ -524,9 +543,18 @@ export class PreAkiModLoader implements IModLoader
this.vfs.rename(`${modPath}/package.json`, `${modPath}/package.json.bak`);
this.vfs.writeFile(`${modPath}/package.json`, "{}");
this.logger.info(this.localisationService.getText("modloader-installing_external_dependencies", {name: pkg.name, author: pkg.author}));
this.logger.info(
this.localisationService.getText("modloader-installing_external_dependencies", {
name: pkg.name,
author: pkg.author,
}),
);
const pnpmPath = path.join(process.cwd(), (globalThis.G_RELEASE_CONFIGURATION ? "Aki_Data/Server/@pnpm/exe" : "node_modules/@pnpm/exe"), (os.platform() === "win32" ? "pnpm.exe" : "pnpm"));
const pnpmPath = path.join(
process.cwd(),
globalThis.G_RELEASE_CONFIGURATION ? "Aki_Data/Server/@pnpm/exe" : "node_modules/@pnpm/exe",
os.platform() === "win32" ? "pnpm.exe" : "pnpm",
);
let command = `${pnpmPath} install `;
command += dependenciesToInstall.map(([depName, depVersion]) => `${depName}@${depVersion}`).join(" ");
execSync(command, {cwd: modPath});
@ -550,13 +578,25 @@ export class PreAkiModLoader implements IModLoader
// Raise dependency version incompatible if the dependency is not found in the mod list
if (!loadedMods.has(modDependency))
{
this.logger.error(this.localisationService.getText("modloader-missing_dependency", {mod: modName, modDependency: modDependency}));
this.logger.error(
this.localisationService.getText("modloader-missing_dependency", {
mod: modName,
modDependency: modDependency,
}),
);
return false;
}
if (!semver.satisfies(loadedMods.get(modDependency).version, requiredVersion))
{
this.logger.error(this.localisationService.getText("modloader-outdated_dependency", {mod: modName, modDependency: modDependency, currentVersion: loadedMods.get(modDependency).version, requiredVersion: requiredVersion}));
this.logger.error(
this.localisationService.getText("modloader-outdated_dependency", {
mod: modName,
modDependency: modDependency,
currentVersion: loadedMods.get(modDependency).version,
requiredVersion: requiredVersion,
}),
);
return false;
}
}
@ -577,7 +617,13 @@ export class PreAkiModLoader implements IModLoader
// Raise dependency version incompatible if any incompatible mod is found
if (loadedMods.has(incompatibleModName))
{
this.logger.error(this.localisationService.getText("modloader-incompatible_mod_found", {author: mod.author, modName: mod.name, incompatibleModName: incompatibleModName}));
this.logger.error(
this.localisationService.getText("modloader-incompatible_mod_found", {
author: mod.author,
modName: mod.name,
incompatibleModName: incompatibleModName,
}),
);
return false;
}
}
@ -599,7 +645,7 @@ export class PreAkiModLoader implements IModLoader
const modIsCalledSrc = modName.toLowerCase() === "src";
const modIsCalledDb = modName.toLowerCase() === "db";
const hasBepinExFolderStructure = this.vfs.exists(`${modPath}/plugins`);
const containsDll = this.vfs.getFiles(`${modPath}`).find(x => x.includes(".dll"));
const containsDll = this.vfs.getFiles(`${modPath}`).find((x) => x.includes(".dll"));
if (modIsCalledSrc || modIsCalledDb || modIsCalledUser)
{
@ -629,7 +675,12 @@ export class PreAkiModLoader implements IModLoader
{
if (!(check in config))
{
this.logger.error(this.localisationService.getText("modloader-missing_package_json_property", {modName: modName, prop: check}));
this.logger.error(
this.localisationService.getText("modloader-missing_package_json_property", {
modName: modName,
prop: check,
}),
);
issue = true;
}
}
@ -642,23 +693,23 @@ export class PreAkiModLoader implements IModLoader
if ("main" in config)
{
if (config.main.split(".").pop() !== "js") // expects js file as entry
{
if (config.main.split(".").pop() !== "js")
{ // expects js file as entry
this.logger.error(this.localisationService.getText("modloader-main_property_not_js", modName));
issue = true;
}
if (!this.vfs.exists(`${modPath}/${config.main}`))
{
// If TS file exists with same name, dont perform check as we'll generate JS from TS file
const tsFileName = config.main.replace(".js", ".ts");
const tsFileExists = this.vfs.exists(`${modPath}/${tsFileName}`);
if (!tsFileExists)
{
this.logger.error(this.localisationService.getText("modloader-main_property_points_to_nothing", modName));
this.logger.error(
this.localisationService.getText("modloader-main_property_points_to_nothing", modName),
);
issue = true;
}
}
@ -666,7 +717,9 @@ export class PreAkiModLoader implements IModLoader
if (config.incompatibilities && !Array.isArray(config.incompatibilities))
{
this.logger.error(this.localisationService.getText("modloader-incompatibilities_not_string_array", modName));
this.logger.error(
this.localisationService.getText("modloader-incompatibilities_not_string_array", modName),
);
issue = true;
}