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";
|
2023-08-09 10:49:45 +00:00
|
|
|
import { ILogger } from "../models/spt/utils/ILogger";
|
2023-03-03 15:23:46 +00:00
|
|
|
|
|
|
|
@injectable()
|
|
|
|
export class ConfigServer
|
|
|
|
{
|
|
|
|
protected configs: Record<string, any> = {};
|
2023-08-09 10:49:45 +00:00
|
|
|
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...");
|
|
|
|
|
2023-08-09 10:49:45 +00:00
|
|
|
// 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);
|
|
|
|
|
2023-08-09 10:49:45 +00:00
|
|
|
// Add file content to result
|
2023-03-03 15:23:46 +00:00
|
|
|
for (const file of files)
|
|
|
|
{
|
2023-08-09 10:49:45 +00:00
|
|
|
if (this.acceptableFileExtensions.includes(this.vfs.getFileExtension(file.toLowerCase())))
|
2023-03-03 15:23:46 +00:00
|
|
|
{
|
2023-08-09 10:49:45 +00:00
|
|
|
const fileName = this.vfs.stripExtension(file);
|
2023-03-03 15:23:46 +00:00
|
|
|
const filePathAndName = `${filepath}${file}`;
|
2023-08-09 10:49:45 +00:00
|
|
|
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"}`);
|
|
|
|
}
|
|
|
|
}
|