0
0
mirror of https://github.com/sp-tarkov/server.git synced 2025-02-13 09:50:43 -05:00
server/project/src/utils/Watermark.ts

197 lines
6.1 KiB
TypeScript
Raw Normal View History

2023-03-03 15:23:46 +00:00
import { inject, injectable } from "tsyringe";
import { ConfigTypes } from "@spt-aki/models/enums/ConfigTypes";
import { ICoreConfig } from "@spt-aki/models/spt/config/ICoreConfig";
import { LogTextColor } from "@spt-aki/models/spt/logging/LogTextColor";
import { ILogger } from "@spt-aki/models/spt/utils/ILogger";
import { ConfigServer } from "@spt-aki/servers/ConfigServer";
import { LocalisationService } from "@spt-aki/services/LocalisationService";
2023-03-03 15:23:46 +00:00
@injectable()
export class WatermarkLocale
{
protected description: string[];
protected warning: string[];
protected modding: string[];
2023-11-15 20:35:05 -05:00
constructor(@inject("LocalisationService") protected localisationService: LocalisationService)
{
this.description = [
2023-03-03 15:23:46 +00:00
this.localisationService.getText("watermark-discord_url"),
"",
this.localisationService.getText("watermark-free_of_charge"),
this.localisationService.getText("watermark-paid_scammed"),
2023-11-15 20:35:05 -05:00
this.localisationService.getText("watermark-commercial_use_prohibited"),
];
this.warning = [
2023-03-03 15:23:46 +00:00
"",
this.localisationService.getText("watermark-testing_build"),
this.localisationService.getText("watermark-no_support"),
"",
`${this.localisationService.getText("watermark-report_issues_to")}:`,
this.localisationService.getText("watermark-issue_tracker_url"),
"",
2023-11-15 20:35:05 -05:00
this.localisationService.getText("watermark-use_at_own_risk"),
];
this.modding = [
2023-03-03 15:23:46 +00:00
"",
this.localisationService.getText("watermark-modding_disabled"),
"",
this.localisationService.getText("watermark-not_an_issue"),
2023-11-15 20:35:05 -05:00
this.localisationService.getText("watermark-do_not_report"),
];
}
2023-03-03 15:23:46 +00:00
public getDescription(): string[]
{
return this.description;
2023-03-03 15:23:46 +00:00
}
public getWarning(): string[]
{
return this.warning;
2023-03-03 15:23:46 +00:00
}
public getModding(): string[]
{
return this.modding;
2023-03-03 15:23:46 +00:00
}
}
@injectable()
export class Watermark
{
protected akiConfig: ICoreConfig;
protected text: string[] = [];
protected versionLabel = "";
2023-03-03 15:23:46 +00:00
constructor(
@inject("WinstonLogger") protected logger: ILogger,
@inject("ConfigServer") protected configServer: ConfigServer,
@inject("LocalisationService") protected localisationService: LocalisationService,
2023-11-15 20:35:05 -05:00
@inject("WatermarkLocale") protected watermarkLocale?: WatermarkLocale,
2023-03-03 15:23:46 +00:00
)
{
this.akiConfig = this.configServer.getConfig<ICoreConfig>(ConfigTypes.CORE);
}
public initialize(): void
{
const description = this.watermarkLocale.getDescription();
const warning = this.watermarkLocale.getWarning();
const modding = this.watermarkLocale.getModding();
const versionTag = this.getVersionTag();
this.versionLabel = `${this.akiConfig.projectName} ${versionTag}`;
this.text = [this.versionLabel];
this.text = [...this.text, ...description];
if (globalThis.G_DEBUG_CONFIGURATION)
{
this.text = this.text.concat([...warning]);
}
if (!globalThis.G_MODS_ENABLED)
{
this.text = this.text.concat([...modding]);
}
this.setTitle();
this.resetCursor();
this.draw();
2023-03-03 15:23:46 +00:00
}
/**
* Get a version string (x.x.x) or (x.x.x-BLEEDINGEDGE) OR (X.X.X (18xxx))
* @param withEftVersion Include the eft version this spt version was made for
* @returns string
*/
public getVersionTag(withEftVersion = false): string
{
const akiVersion = globalThis.G_AKIVERSION || this.akiConfig.akiVersion;
2023-03-03 15:23:46 +00:00
const versionTag = (globalThis.G_DEBUG_CONFIGURATION)
? `${akiVersion} - ${this.localisationService.getText("bleeding_edge_build")}`
: akiVersion;
2023-03-03 15:23:46 +00:00
if (withEftVersion)
{
const tarkovVersion = this.akiConfig.compatibleTarkovVersion.split(".").pop();
return `${versionTag} (${tarkovVersion})`;
}
return versionTag;
}
/**
2023-07-15 14:49:25 +01:00
* Handle singleplayer/settings/version
2023-03-03 15:23:46 +00:00
* Get text shown in game on screen, can't be translated as it breaks bsgs client when certian characters are used
* @returns string
*/
public getInGameVersionLabel(): string
{
const akiVersion = globalThis.G_AKIVERSION || this.akiConfig.akiVersion;
2023-03-03 15:23:46 +00:00
const versionTag = (globalThis.G_DEBUG_CONFIGURATION)
? `${akiVersion} - BLEEDINGEDGE ${globalThis.G_COMMIT?.slice(0, 6) ?? ""}`
: `${akiVersion} - ${globalThis.G_COMMIT?.slice(0, 6) ?? ""}`;
2023-03-03 15:23:46 +00:00
return `${this.akiConfig.projectName} ${versionTag}`;
}
/** Set window title */
2023-07-15 14:49:25 +01:00
protected setTitle(): void
2023-03-03 15:23:46 +00:00
{
process.title = this.versionLabel;
}
/** Reset console cursor to top */
2023-07-15 14:49:25 +01:00
protected resetCursor(): void
2023-03-03 15:23:46 +00:00
{
process.stdout.write("\u001B[2J\u001B[0;0f");
}
/** Draw the watermark */
2023-07-15 14:49:25 +01:00
protected draw(): void
2023-03-03 15:23:46 +00:00
{
const result = [];
// Calculate size, add 10% for spacing to the right
2023-03-03 15:23:46 +00:00
const longestLength = this.text.reduce((a, b) =>
{
return a.length > b.length ? a : b;
}).length * 1.1;
2023-03-03 15:23:46 +00:00
// Create line of - to add top/bottom of watermark
2023-03-03 15:23:46 +00:00
let line = "";
for (let i = 0; i < longestLength; ++i)
{
line += "─";
}
// Opening line
2023-03-03 15:23:46 +00:00
result.push(`┌─${line}─┐`);
// Add content of watermark to screen
for (const watermarkText of this.text)
2023-03-03 15:23:46 +00:00
{
const spacingSize = longestLength - watermarkText.length;
let textWithRightPadding = watermarkText;
2023-03-03 15:23:46 +00:00
for (let i = 0; i < spacingSize; ++i)
{
textWithRightPadding += " ";
2023-03-03 15:23:46 +00:00
}
result.push(`${textWithRightPadding}`);
2023-03-03 15:23:46 +00:00
}
// Closing line
2023-03-03 15:23:46 +00:00
result.push(`└─${line}─┘`);
// Log watermark to screen
2023-03-03 15:23:46 +00:00
for (const text of result)
{
this.logger.logWithColor(text, LogTextColor.YELLOW);
}
}
}