Server/project/src/loaders/BundleLoader.ts

94 lines
2.3 KiB
TypeScript
Raw Normal View History

2023-03-03 16:23:46 +01:00
import { inject, injectable } from "tsyringe";
import { HttpServerHelper } from "@spt-aki/helpers/HttpServerHelper";
import { JsonUtil } from "@spt-aki/utils/JsonUtil";
import { VFS } from "@spt-aki/utils/VFS";
2023-03-03 16:23:46 +01:00
class BundleInfo
{
modPath: string;
key: string;
path: string;
filepath: string;
dependencyKeys: string[];
constructor(modpath: string, bundle: any, bundlePath: string, bundleFilepath: string)
{
this.modPath = modpath;
this.key = bundle.key;
this.path = bundlePath;
this.filepath = bundleFilepath;
this.dependencyKeys = bundle.dependencyKeys || [];
}
}
@injectable()
export class BundleLoader
{
protected bundles: Record<string, BundleInfo> = {};
constructor(
@inject("HttpServerHelper") protected httpServerHelper: HttpServerHelper,
@inject("VFS") protected vfs: VFS,
2023-11-13 17:10:44 +01:00
@inject("JsonUtil") protected jsonUtil: JsonUtil,
2023-03-03 16:23:46 +01:00
)
2023-11-13 17:10:44 +01:00
{}
2023-03-03 16:23:46 +01:00
2023-07-15 12:00:35 +02:00
/**
* Handle singleplayer/bundles
*/
2023-03-03 16:23:46 +01:00
public getBundles(local: boolean): BundleInfo[]
{
const result: BundleInfo[] = [];
for (const bundle in this.bundles)
{
result.push(this.getBundle(bundle, local));
}
return result;
}
public getBundle(key: string, local: boolean): BundleInfo
{
const bundle = this.jsonUtil.clone(this.bundles[key]);
if (local)
{
bundle.path = bundle.filepath;
}
delete bundle.filepath;
return bundle;
}
public addBundles(modpath: string): void
{
2023-11-13 17:10:44 +01:00
const manifest =
this.jsonUtil.deserialize<BundleManifest>(this.vfs.readFile(`${modpath}bundles.json`)).manifest;
2023-03-03 16:23:46 +01:00
for (const bundle of manifest)
{
const bundlePath = `${this.httpServerHelper.getBackendUrl()}/files/bundle/${bundle.key}`;
const bundleFilepath = bundle.path || `${modpath}bundles/${bundle.key}`.replace(/\\/g, "/");
this.addBundle(bundle.key, new BundleInfo(modpath, bundle, bundlePath, bundleFilepath));
2023-03-03 16:23:46 +01:00
}
}
2023-11-13 17:10:44 +01:00
public addBundle(key: string, b: BundleInfo): void
2023-07-15 12:00:35 +02:00
{
this.bundles[key] = b;
}
2023-03-03 16:23:46 +01:00
}
export interface BundleManifest
{
2023-11-13 17:10:44 +01:00
manifest: Array<BundleManifestEntry>;
2023-03-03 16:23:46 +01:00
}
export interface BundleManifestEntry
{
2023-11-13 17:10:44 +01:00
key: string;
path: string;
}