Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static Program Properties & Simplified Entry Points #1010

Merged
merged 2 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion project/Server.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
{
"name": "Debug",
"type": "node",
"program": "${workspaceFolder}/src/ide/TestEntry.ts",
"program": "${workspaceFolder}/src/entry/run.ts",
"runtimeVersion": "22.12.0",
"runtimeExecutable": "tsx",
"request": "launch",
Expand Down
36 changes: 19 additions & 17 deletions project/gulpfile.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ const serverExeName = "SPT.Server.exe";
const serverExe = path.join(buildDir, serverExeName);
const pkgConfig = "pkgconfig.json";
const entries = {
release: path.join("obj", "ide", "ReleaseEntry.js"),
debug: path.join("obj", "ide", "DebugEntry.js"),
bleeding: path.join("obj", "ide", "BleedingEdgeEntry.js"),
bleedingmods: path.join("obj", "ide", "BleedingEdgeModsEntry.js"),
release: "RELEASE",
debug: "DEBUG",
bleeding: "BLEEDING_EDGE",
bleedingmods: "BLEEDING_EDGE_MODS",
};
const licenseFile = "../LICENSE.md";

Expand Down Expand Up @@ -64,7 +64,7 @@ const compile = async () => {
// Packaging
const fetchPackageImage = async () => {
try {
const output = "./.pkg-cache/v6.2";
const output = "./.pkg-cache/v3.5";
const fetchedPkg = await pkgfetch.need({
arch: targetArch,
nodeRange: nodeVersion,
Expand Down Expand Up @@ -153,7 +153,7 @@ const copyLicense = () => gulp.src([licenseFile]).pipe(rename("LICENSE-Server.tx
/**
* Writes the latest build data to the core.json and build.json configuration files.
*/
const writeBuildDataToJSON = async () => {
const writeBuildDataToJSON = async (entryType) => {
try {
// Fetch the latest Git commit hash
const gitResult = await exec("git rev-parse HEAD", { stdout: "pipe" });
Expand All @@ -168,12 +168,13 @@ const writeBuildDataToJSON = async () => {
await fs.writeFile(coreJSONPath, JSON.stringify(coreParsed, null, 4));

// Write build.json
const buildJsonPath = path.join("obj", "ide", "build.json");
const buildInfo = {};
const buildJsonPath = path.join("obj", "entry", "build.json");

const buildInfo = {};
buildInfo.entryType = entryType;
buildInfo.sptVersion = coreParsed.sptVersion;
buildInfo.commit = coreParsed.commit;
buildInfo.buildTime = coreParsed.buildTime;
buildInfo.sptVersion = coreParsed.sptVersion;
await fs.writeFile(buildJsonPath, JSON.stringify(buildInfo, null, 4));
} catch (error) {
throw new Error(`Failed to write commit hash to core.json: ${error.message}`);
Expand All @@ -191,7 +192,8 @@ const createHashFile = async () => {
};

// Combine all tasks into addAssets
const addAssets = gulp.series(copyAssets, downloadPnpm, copyLicense, writeBuildDataToJSON, createHashFile);
const addAssets = (entryType) =>
gulp.series(copyAssets, downloadPnpm, copyLicense, () => writeBuildDataToJSON(entryType), createHashFile);

/**
* Cleans the build directory.
Expand Down Expand Up @@ -284,14 +286,14 @@ const loadRecursiveAsync = async (filepath) => {
};

// Main Tasks Generation
const build = (packagingType) => {
const anonPackaging = () => packaging(entries[packagingType]);
anonPackaging.displayName = `packaging-${packagingType}`;
const build = (entryType) => {
const anonPackaging = () => packaging(entries[entryType]);
anonPackaging.displayName = `packaging-${entryType}`;
const tasks = [
cleanBuild,
validateJSONs,
compile,
addAssets,
addAssets(entries[entryType]),
fetchPackageImage,
anonPackaging,
updateBuildProperties,
Expand All @@ -301,11 +303,11 @@ const build = (packagingType) => {
};

// Packaging Arguments
const packaging = async (entry) => {
const packaging = async (entryType) => {
const target = `${nodeVersion}-${targetPlatform}-${targetArch}`;
try {
await pkg.exec([
entry,
path.join("obj", "entry", "run.js"),
"--compress",
"GZip",
"--target",
Expand All @@ -330,5 +332,5 @@ gulp.task("run:build", async () => await exec(serverExeName, { stdio, cwd: build
gulp.task("run:profiler", async () => {
await cleanCompiled();
await compile();
await exec("node --prof --inspect --trace-warnings obj/ide/TestEntry.js", { stdio });
await exec("node --prof --inspect --trace-warnings obj/entry/run.js", { stdio });
});
1 change: 1 addition & 0 deletions project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"@pnpm/exe": "8.15.9",
"@swc/cli": "~0.4",
"@swc/core": "~1.7",
"@types/fs-extra": "11.0.4",
"@types/i18n": "~0.13",
"@types/node": "22.10.2",
"@types/proper-lockfile": "~4.1",
Expand Down
80 changes: 77 additions & 3 deletions project/src/Program.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
import { ErrorHandler } from "@spt/ErrorHandler";
import { Container } from "@spt/di/Container";
import buildInfo from "@spt/entry/build.json" assert { type: "json" };
import type { PreSptModLoader } from "@spt/loaders/PreSptModLoader";
import { App } from "@spt/utils/App";
import { Watermark } from "@spt/utils/Watermark";
import { container } from "tsyringe";

export enum EntryType {
LOCAL = "LOCAL",
DEBUG = "DEBUG",
RELEASE = "RELEASE",
BLEEDING_EDGE = "BLEEDING_EDGE",
BLEEDING_EDGE_MODS = "BLEEDING_EDGE_MODS",
}

export class Program {
private static _ENTRY_TYPE: EntryType;

private static _DEBUG: boolean;
private static _COMPILED: boolean;
private static _MODS: boolean;

private static _SPT_VERSION: string;
private static _COMMIT: string;
private static _BUILD_TIME: number;

private errorHandler: ErrorHandler;

constructor() {
// set window properties
process.stdout.setEncoding("utf8");
process.title = "SPT Server";

this.errorHandler = new ErrorHandler();
}

Expand All @@ -27,8 +47,62 @@ export class Program {

Container.registerPostLoadTypes(container, childContainer);
childContainer.resolve<App>("App").load();
} catch (err: any) {
this.errorHandler.handleCriticalError(err instanceof Error ? err : new Error(err));
} catch (err: unknown) {
this.errorHandler.handleCriticalError(err instanceof Error ? err : new Error(String(err)));
}
}

public static initialize(): void {
Program._ENTRY_TYPE = buildInfo.entryType as EntryType;
Program._SPT_VERSION = buildInfo.sptVersion ?? "";
Program._COMMIT = buildInfo.commit ?? "";
Program._BUILD_TIME = buildInfo.buildTime ?? 0;

switch (Program._ENTRY_TYPE) {
case EntryType.RELEASE:
Program._DEBUG = false;
Program._COMPILED = true;
Program._MODS = true;
break;
case EntryType.BLEEDING_EDGE:
Program._DEBUG = true;
Program._COMPILED = true;
Program._MODS = false;
break;
case EntryType.DEBUG:
case EntryType.BLEEDING_EDGE_MODS:
Program._DEBUG = true;
Program._COMPILED = true;
Program._MODS = true;
break;
default: // EntryType.LOCAL
Program._DEBUG = true;
Program._COMPILED = false;
Program._MODS = true;
break;
}
}

// Public Static Getters
public static get ENTRY_TYPE(): EntryType {
return Program._ENTRY_TYPE;
}
public static get DEBUG(): boolean {
return Program._DEBUG;
}
public static get COMPILED(): boolean {
return Program._COMPILED;
}
public static get MODS(): boolean {
return Program._MODS;
}
public static get SPT_VERSION(): string {
return Program._SPT_VERSION;
}
public static get COMMIT(): string {
return Program._COMMIT;
}
public static get BUILD_TIME(): number {
return Program._BUILD_TIME;
}
}
8 changes: 5 additions & 3 deletions project/src/callbacks/ClientLogCallbacks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EntryType, Program } from "@spt/Program";
import { ClientLogController } from "@spt/controllers/ClientLogController";
import { ModLoadOrder } from "@spt/loaders/ModLoadOrder";
import { INullResponseData } from "@spt/models/eft/httpResponse/INullResponseData";
Expand Down Expand Up @@ -34,7 +35,7 @@ export class ClientLogCallbacks {
public releaseNotes(): string {
const data: IRelease = this.configServer.getConfig<ICoreConfig>(ConfigTypes.CORE).release;

data.betaDisclaimerText = globalThis.G_MODS_ENABLED
data.betaDisclaimerText = Program.MODS
? this.localisationService.getText("release-beta-disclaimer-mods-enabled")
: this.localisationService.getText("release-beta-disclaimer");

Expand All @@ -47,8 +48,9 @@ export class ClientLogCallbacks {
data.illegalPluginsExceptionText = this.localisationService.getText("release-illegal-plugins-exception");
data.releaseSummaryText = this.localisationService.getText("release-summary");

data.isBeta = globalThis.G_WATERMARK_ENABLED;
data.isModdable = globalThis.G_MODS_ENABLED;
data.isBeta =
Program.ENTRY_TYPE === EntryType.BLEEDING_EDGE || Program.ENTRY_TYPE === EntryType.BLEEDING_EDGE_MODS;
data.isModdable = Program.MODS;
data.isModded = this.modLoadOrder.getLoadOrder().length > 0;

return this.httpResponse.noBody(data);
Expand Down
3 changes: 2 additions & 1 deletion project/src/callbacks/ModCallbacks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Program } from "@spt/Program";
import { OnLoad } from "@spt/di/OnLoad";
import { PostSptModLoader } from "@spt/loaders/PostSptModLoader";
import { ConfigTypes } from "@spt/models/enums/ConfigTypes";
Expand Down Expand Up @@ -25,7 +26,7 @@ export class ModCallbacks implements OnLoad {
}

public async onLoad(): Promise<void> {
if (globalThis.G_MODS_ENABLED) {
if (Program.MODS) {
await this.postSptModLoader.load();
}
}
Expand Down
9 changes: 4 additions & 5 deletions project/src/controllers/GameController.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Program } from "@spt/Program";
import { ApplicationContext } from "@spt/context/ApplicationContext";
import { ContextVariableType } from "@spt/context/ContextVariableType";
import { HideoutHelper } from "@spt/helpers/HideoutHelper";
Expand Down Expand Up @@ -565,11 +566,9 @@ export class GameController {

protected logProfileDetails(fullProfile: ISptProfile): void {
this.logger.debug(`Profile made with: ${fullProfile.spt.version}`);
this.logger.debug(
`Server version: ${globalThis.G_SPTVERSION || this.coreConfig.sptVersion} ${globalThis.G_COMMIT}`,
);
this.logger.debug(`Debug enabled: ${globalThis.G_DEBUG_CONFIGURATION}`);
this.logger.debug(`Mods enabled: ${globalThis.G_MODS_ENABLED}`);
this.logger.debug(`Server version: ${Program.SPT_VERSION || this.coreConfig.sptVersion} ${Program.COMMIT}`);
this.logger.debug(`Debug enabled: ${Program.DEBUG}`);
this.logger.debug(`Mods enabled: ${Program.MODS}`);
}

public getSurvey(sessionId: string): ISurveyResponseData {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"entryType": "LOCAL",
"sptVersion": "",
"commit": "",
"buildTime": 0
Expand Down
7 changes: 7 additions & 0 deletions project/src/entry/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import "reflect-metadata";
import "source-map-support/register";
import { Program } from "@spt/Program";

Program.initialize();
const program = new Program();
program.start();
14 changes: 0 additions & 14 deletions project/src/global.d.ts

This file was deleted.

19 changes: 0 additions & 19 deletions project/src/ide/BleedingEdgeEntry.ts

This file was deleted.

19 changes: 0 additions & 19 deletions project/src/ide/BleedingEdgeModsEntry.ts

This file was deleted.

19 changes: 0 additions & 19 deletions project/src/ide/DebugEntry.ts

This file was deleted.

19 changes: 0 additions & 19 deletions project/src/ide/ReleaseEntry.ts

This file was deleted.

Loading
Loading