Server/project/src/loaders/BundleLoader.ts

97 lines
2.7 KiB
TypeScript
Raw Normal View History

import path from "node:path";
2023-03-03 15:23:46 +00:00
import { inject, injectable } from "tsyringe";
import { HttpServerHelper } from "@spt/helpers/HttpServerHelper";
import { BundleHashCacheService } from "@spt/services/cache/BundleHashCacheService";
import { ICloner } from "@spt/utils/cloners/ICloner";
import { JsonUtil } from "@spt/utils/JsonUtil";
import { VFS } from "@spt/utils/VFS";
2023-03-03 15:23:46 +00:00
2024-03-24 00:49:53 +00:00
export class BundleInfo
2023-03-03 15:23:46 +00:00
{
modpath: string;
filename: string;
crc: number;
dependencies: string[];
2023-03-03 15:23:46 +00:00
constructor(modpath: string, bundle: BundleManifestEntry, bundleHash: number)
2023-03-03 15:23:46 +00:00
{
this.modpath = modpath;
this.filename = bundle.key;
this.crc = bundleHash;
this.dependencies = bundle.dependencyKeys || [];
2023-03-03 15:23:46 +00:00
}
}
@injectable()
export class BundleLoader
{
protected bundles: Record<string, BundleInfo> = {};
constructor(
@inject("HttpServerHelper") protected httpServerHelper: HttpServerHelper,
@inject("VFS") protected vfs: VFS,
2023-11-15 20:35:05 -05:00
@inject("JsonUtil") protected jsonUtil: JsonUtil,
@inject("BundleHashCacheService") protected bundleHashCacheService: BundleHashCacheService,
@inject("PrimaryCloner") protected cloner: ICloner,
2023-03-03 15:23:46 +00:00
)
2023-11-15 20:35:05 -05:00
{}
2023-03-03 15:23:46 +00:00
2023-07-15 11:00:35 +01:00
/**
* Handle singleplayer/bundles
*/
public getBundles(): BundleInfo[]
2023-03-03 15:23:46 +00:00
{
const result: BundleInfo[] = [];
for (const bundle in this.bundles)
{
result.push(this.getBundle(bundle));
2023-03-03 15:23:46 +00:00
}
return result;
}
public getBundle(key: string): BundleInfo
2023-03-03 15:23:46 +00:00
{
return this.cloner.clone(this.bundles[key]);
2023-03-03 15:23:46 +00:00
}
public addBundles(modpath: string): void
{
const bundleManifestArr = this.jsonUtil.deserialize<BundleManifest>(
this.vfs.readFile(`${modpath}bundles.json`),
).manifest;
2023-03-03 15:23:46 +00:00
for (const bundleManifest of bundleManifestArr)
2023-03-03 15:23:46 +00:00
{
const absoluteModPath = path.join(process.cwd(), modpath).slice(0, -1).replace(/\\/g, "/");
const bundleLocalPath = `${modpath}bundles/${bundleManifest.key}`.replace(/\\/g, "/");
if (!this.bundleHashCacheService.calculateAndMatchHash(bundleLocalPath))
{
this.bundleHashCacheService.calculateAndStoreHash(bundleLocalPath);
}
const bundleHash = this.bundleHashCacheService.getStoredValue(bundleLocalPath);
this.addBundle(bundleManifest.key, new BundleInfo(absoluteModPath, bundleManifest, bundleHash));
2023-03-03 15:23:46 +00:00
}
}
2023-11-15 20:35:05 -05:00
public addBundle(key: string, b: BundleInfo): void
2023-07-15 11:00:35 +01:00
{
this.bundles[key] = b;
}
2023-03-03 15:23:46 +00:00
}
export interface BundleManifest
{
manifest: BundleManifestEntry[]
2023-03-03 15:23:46 +00:00
}
export interface BundleManifestEntry
{
key: string
dependencyKeys: string[]
2023-11-15 20:35:05 -05:00
}