Server/project/src/servers/ConfigServer.ts
chomp c1a4c544bc Add JSONC support to server configs + use by modders (!112)
Replaced calls (where possible) to JSON.parse/stringify with use of `jsonUtil` functions
`VFS.ts` was tricky, it can't be updated as it'd create a circular dependency

Also add json5 to package.json for modders to have access to

Co-authored-by: Dev <dev@dev.sp-tarkov.com>
Reviewed-on: https://dev.sp-tarkov.com/SPT-AKI/Server/pulls/112
2023-08-09 10:49:45 +00:00

58 lines
1.8 KiB
TypeScript

import { JsonUtil } from "../utils/JsonUtil";
import { VFS } from "../utils/VFS";
import { inject, injectable } from "tsyringe";
import { ConfigTypes } from "../models/enums/ConfigTypes";
import { ICoreConfig } from "../models/spt/config/ICoreConfig";
import { ILogger } from "../models/spt/utils/ILogger";
@injectable()
export class ConfigServer
{
protected configs: Record<string, any> = {};
protected readonly acceptableFileExtensions: string[] = ["json", "jsonc"];
constructor(
@inject("WinstonLogger") protected logger: ILogger,
@inject("VFS") protected vfs: VFS,
@inject("JsonUtil") protected jsonUtil: JsonUtil
)
{
this.initialize();
}
public getConfig<T>(configType: ConfigTypes): T
{
return this.configs[configType];
}
public getConfigByString<T>(configType: string): T
{
return this.configs[configType];
}
public initialize(): void
{
this.logger.debug("Importing configs...");
// Get all filepaths
const filepath = (globalThis.G_RELEASE_CONFIGURATION)
? "Aki_Data/Server/configs/"
: "./assets/configs/";
const files = this.vfs.getFiles(filepath);
// Add file content to result
for (const file of files)
{
if (this.acceptableFileExtensions.includes(this.vfs.getFileExtension(file.toLowerCase())))
{
const fileName = this.vfs.stripExtension(file);
const filePathAndName = `${filepath}${file}`;
this.configs[`aki-${fileName}`] = this.jsonUtil.deserializeJsonC<any>(this.vfs.readFile(filePathAndName), filePathAndName);
}
}
this.logger.info(`Commit hash: ${(this.configs[ConfigTypes.CORE] as ICoreConfig).commit || "DEBUG"}`);
}
}