Server/project/src/servers/ConfigServer.ts

58 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-03-03 15:23:46 +00:00
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";
2023-03-03 15:23:46 +00:00
@injectable()
export class ConfigServer
{
protected configs: Record<string, any> = {};
protected readonly acceptableFileExtensions: string[] = ["json", "jsonc"];
2023-03-03 15:23:46 +00:00
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/";
2023-03-03 15:23:46 +00:00
const files = this.vfs.getFiles(filepath);
// Add file content to result
2023-03-03 15:23:46 +00:00
for (const file of files)
{
if (this.acceptableFileExtensions.includes(this.vfs.getFileExtension(file.toLowerCase())))
2023-03-03 15:23:46 +00:00
{
const fileName = this.vfs.stripExtension(file);
2023-03-03 15:23:46 +00:00
const filePathAndName = `${filepath}${file}`;
this.configs[`aki-${fileName}`] = this.jsonUtil.deserializeJsonC<any>(this.vfs.readFile(filePathAndName), filePathAndName);
2023-03-03 15:23:46 +00:00
}
}
this.logger.info(`Commit hash: ${(this.configs[ConfigTypes.CORE] as ICoreConfig).commit || "DEBUG"}`);
}
}