2024-03-29 18:43:36 +00:00
|
|
|
import path from "node:path";
|
2024-05-21 17:59:04 +00:00
|
|
|
import { HttpServerHelper } from "@spt/helpers/HttpServerHelper";
|
|
|
|
import { BundleHashCacheService } from "@spt/services/cache/BundleHashCacheService";
|
|
|
|
import { JsonUtil } from "@spt/utils/JsonUtil";
|
|
|
|
import { VFS } from "@spt/utils/VFS";
|
2024-07-23 11:12:53 -04:00
|
|
|
import { ICloner } from "@spt/utils/cloners/ICloner";
|
|
|
|
import { inject, injectable } from "tsyringe";
|
2023-03-03 15:23:46 +00:00
|
|
|
|
2024-07-23 11:12:53 -04:00
|
|
|
export class BundleInfo {
|
2024-03-29 18:43:36 +00:00
|
|
|
modpath: string;
|
|
|
|
filename: string;
|
|
|
|
crc: number;
|
|
|
|
dependencies: string[];
|
2023-03-03 15:23:46 +00:00
|
|
|
|
2024-07-23 11:12:53 -04:00
|
|
|
constructor(modpath: string, bundle: BundleManifestEntry, bundleHash: number) {
|
2024-03-29 18:43:36 +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()
|
2024-07-23 11:12:53 -04:00
|
|
|
export class BundleLoader {
|
2023-03-03 15:23:46 +00:00
|
|
|
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,
|
2024-03-29 18:43:36 +00:00
|
|
|
@inject("BundleHashCacheService") protected bundleHashCacheService: BundleHashCacheService,
|
2024-05-28 14:04:20 +00:00
|
|
|
@inject("PrimaryCloner") protected cloner: ICloner,
|
2024-07-23 11:12:53 -04:00
|
|
|
) {}
|
2023-03-03 15:23:46 +00:00
|
|
|
|
2023-07-15 11:00:35 +01:00
|
|
|
/**
|
|
|
|
* Handle singleplayer/bundles
|
|
|
|
*/
|
2024-07-23 11:12:53 -04:00
|
|
|
public getBundles(): BundleInfo[] {
|
2023-03-03 15:23:46 +00:00
|
|
|
const result: BundleInfo[] = [];
|
|
|
|
|
2024-07-23 11:12:53 -04:00
|
|
|
for (const bundle in this.bundles) {
|
2024-03-29 18:43:36 +00:00
|
|
|
result.push(this.getBundle(bundle));
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-07-23 11:12:53 -04:00
|
|
|
public getBundle(key: string): BundleInfo {
|
2024-05-13 17:58:17 +00:00
|
|
|
return this.cloner.clone(this.bundles[key]);
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 11:12:53 -04:00
|
|
|
public addBundles(modpath: string): void {
|
2024-05-17 15:32:41 -04:00
|
|
|
const bundleManifestArr = this.jsonUtil.deserialize<BundleManifest>(
|
|
|
|
this.vfs.readFile(`${modpath}bundles.json`),
|
|
|
|
).manifest;
|
2023-03-03 15:23:46 +00:00
|
|
|
|
2024-07-23 11:12:53 -04:00
|
|
|
for (const bundleManifest of bundleManifestArr) {
|
2024-07-31 14:14:56 +00:00
|
|
|
const relativeModPath = modpath.slice(0, -1).replace(/\\/g, "/");
|
2024-03-29 18:43:36 +00:00
|
|
|
const bundleLocalPath = `${modpath}bundles/${bundleManifest.key}`.replace(/\\/g, "/");
|
|
|
|
|
2024-07-23 11:12:53 -04:00
|
|
|
if (!this.bundleHashCacheService.calculateAndMatchHash(bundleLocalPath)) {
|
2024-03-29 18:43:36 +00:00
|
|
|
this.bundleHashCacheService.calculateAndStoreHash(bundleLocalPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
const bundleHash = this.bundleHashCacheService.getStoredValue(bundleLocalPath);
|
|
|
|
|
2024-07-31 14:14:56 +00:00
|
|
|
this.addBundle(bundleManifest.key, new BundleInfo(relativeModPath, bundleManifest, bundleHash));
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-23 09:50:52 +00:00
|
|
|
|
2024-07-23 11:12:53 -04:00
|
|
|
public addBundle(key: string, b: BundleInfo): void {
|
2023-04-23 09:50:52 +00:00
|
|
|
this.bundles[key] = b;
|
|
|
|
}
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 11:12:53 -04:00
|
|
|
export interface BundleManifest {
|
|
|
|
manifest: BundleManifestEntry[];
|
2023-03-03 15:23:46 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 11:12:53 -04:00
|
|
|
export interface BundleManifestEntry {
|
|
|
|
key: string;
|
|
|
|
dependencyKeys: string[];
|
2023-11-15 20:35:05 -05:00
|
|
|
}
|