Server/project/src/loaders/BundleLoader.ts
TheSparta 418d9f2a8f Import path alias on the whole project (!157)
- Ability to use @spt-aki path alias on the whole project.
- Swapped all imports from relative paths, for imports using the path alias.

Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/157
Co-authored-by: TheSparta <thesparta@noreply.dev.sp-tarkov.com>
Co-committed-by: TheSparta <thesparta@noreply.dev.sp-tarkov.com>
2023-10-19 17:21:17 +00:00

93 lines
2.3 KiB
TypeScript

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";
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,
@inject("JsonUtil") protected jsonUtil: JsonUtil
)
{ }
/**
* Handle singleplayer/bundles
*/
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
{
const manifest = this.jsonUtil.deserialize<BundleManifest>(this.vfs.readFile(`${modpath}bundles.json`)).manifest;
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));
}
}
public addBundle(key: string, b: BundleInfo): void
{
this.bundles[key] = b;
}
}
export interface BundleManifest
{
manifest: Array<BundleManifestEntry>
}
export interface BundleManifestEntry
{
key: string
path: string
}