Skip to content

Commit

Permalink
Merge pull request #118 from project-fika/auto_restart
Browse files Browse the repository at this point in the history
Server-side changes for automatic headless client restart
  • Loading branch information
ArchangelWTF authored Feb 24, 2025
2 parents e3d3a89 + 5f7e473 commit c401a74
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 16 deletions.
10 changes: 10 additions & 0 deletions assets/scripts/_TEMPLATE.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@ECHO OFF
IF NOT EXIST ".\\BepInEx\\plugins\\Fika.Headless.dll" GOTO DLL_NOT_EXIST

:START_HEADLESS
EscapeFromTarkov.exe -token=${profileId} -config={'BackendUrl':'${backendUrl}','Version':'live'} -batchmode -nographics --enable-console true
GOTO START_HEADLESS

:DLL_NOT_EXIST
ECHO Could not find 'Fika.Headless.dll'. Please install the Headless plugin before starting the client and make sure the .bat file is in the Headless installation directory where 'EscapeFromTarkov.exe' is.
PAUSE
4 changes: 4 additions & 0 deletions src/callbacks/FikaClientCallbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export class FikaClientCallbacks {
public handleNatPunchServerConfig(_url: string, _info: IFikaRaidServerIdRequestData, _sessionID: string): string {
return this.httpResponseUtil.noBody(this.fikaClientController.handleNatPunchServerConfig());
}
/** Handle /fika/headless/config */
public handleHeadlessConfig(_url: string, _info: IFikaRaidServerIdRequestData, _sessionID: string): string {
return this.httpResponseUtil.noBody(this.fikaClientController.handleHeadlessConfig());
}

/** Handle /fika/client/check/mods */
public handleCheckMods(_url: string, info: IFikaCheckModRequestData, sessionID: string): string {
Expand Down
8 changes: 8 additions & 0 deletions src/controllers/FikaClientController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { ILogger } from "@spt/models/spt/utils/ILogger";
import { SaveServer } from "@spt/servers/SaveServer";

import { FikaClientModHashesHelper } from "../helpers/FikaClientModHashesHelper";
import { IFikaConfigHeadless } from "../models/fika/config/IFikaConfigHeadless";
import { IFikaConfigNatPunchServer } from "../models/fika/config/IFikaConfigNatPunchServer";
import { IFikaCheckModRequestData } from "../models/fika/routes/client/check/IFikaCheckModRequestData";
import { IFikaCheckModResponse, IVersionCheckResponse } from "../models/fika/routes/client/check/IFikaCheckModResponse";
Expand Down Expand Up @@ -46,6 +47,13 @@ export class FikaClientController {
return this.fikaClientService.getNatPunchServerConfig();
}

/**
* Handle /fika/headless/config
*/
public handleHeadlessConfig(): IFikaConfigHeadless {
return this.fikaClientService.getHeadlessConfig();
}

/**
* Handle /fika/client/check/mods
*/
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/FikaHeadlessHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ export class FikaHeadlessHelper {

headlessClients.push(availableHeadlessClient);
}

return headlessClients;
}

return headlessClients;
}
}
1 change: 1 addition & 0 deletions src/models/fika/config/IFikaConfigHeadless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export interface IFikaConfigHeadless {
};
/** If this is true, sets the headless's average level to that of the entire lobby, if set to false it will take the level of the requester. */
setLevelToAverageOfLobby: boolean;
restartAfterAmountOfRaids: number;
}
3 changes: 3 additions & 0 deletions src/routers/static/FikaClientStaticRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export class FikaClientStaticRouter extends StaticRouter {
new RouteAction("/fika/natpunchserver/config", async (url: string, info: IFikaRaidServerIdRequestData, sessionID: string, _output: string): Promise<string> => {
return this.fikaClientCallbacks.handleNatPunchServerConfig(url, info, sessionID);
}),
new RouteAction("/fika/headless/config", async (url: string, info: IFikaRaidServerIdRequestData, sessionID: string, _output: string): Promise<string> => {
return this.fikaClientCallbacks.handleHeadlessConfig(url, info, sessionID);
}),
new RouteAction("/fika/client/check/mods", async (url: string, info: IFikaCheckModRequestData, sessionID: string, _output: string): Promise<string> => {
return this.fikaClientCallbacks.handleCheckMods(url, info, sessionID);
}),
Expand Down
5 changes: 5 additions & 0 deletions src/services/FikaClientService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { ILogger } from "@spt/models/spt/utils/ILogger";
import { SaveServer } from "@spt/servers/SaveServer";

import { FikaClientModHashesHelper } from "../helpers/FikaClientModHashesHelper";
import { IFikaConfigHeadless } from "../models/fika/config/IFikaConfigHeadless";
import { IFikaConfigNatPunchServer } from "../models/fika/config/IFikaConfigNatPunchServer";
import { IFikaConfigServer } from "../models/fika/config/IFikaConfigServer";
import { IFikaCheckModRequestData } from "../models/fika/routes/client/check/IFikaCheckModRequestData";
Expand Down Expand Up @@ -55,6 +56,10 @@ export class FikaClientService {
return this.fikaConfig.getConfig().natPunchServer;
}

public getHeadlessConfig(): IFikaConfigHeadless {
return this.fikaConfig.getConfig().headless;
}

public getVersion(): IVersionCheckResponse {
const version = this.fikaConfig.getVersion();
return { version };
Expand Down
25 changes: 11 additions & 14 deletions src/services/headless/FikaHeadlessProfileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class FikaHeadlessProfileService {
const backendUrl = `https://${ip}:${port}`;

for (const profile of createdProfiles) {
await this.generateLaunchScript(profile, backendUrl, this.scriptsPath);
await this.generateLaunchScript(profile.info.id, backendUrl, this.scriptsPath);
}
}
}
Expand Down Expand Up @@ -164,22 +164,19 @@ export class FikaHeadlessProfileService {
return profile;
}

private async generateLaunchScript(profile: ISptProfile, backendUrl: string, targetFolderPath: string): Promise<void> {
const scriptName = `Start_${profile.info.username}.bat`;
const scriptPath = path.join(targetFolderPath, scriptName);
const scriptContent = `@echo off
if NOT EXIST ".\\BepInEx\\plugins\\Fika.Headless.dll" (
echo Could not find 'Fika.Headless.dll', please install the Headless plugin before starting the client and make sure the .bat file is in the Headless installation directory where 'EscapeFromTarkov.exe' is.
pause
) else (
start "" EscapeFromTarkov.exe -token=${profile.info.id} -config={'BackendUrl':'${backendUrl}','Version':'live'} -batchmode -nographics --enable-console true & exit
)`;

private async generateLaunchScript(profileId: string, backendUrl: string, scriptsFolderPath: string): Promise<void> {
try {
if (!(await this.fileSystem.exists(targetFolderPath))) {
await this.fileSystem.ensureDir(targetFolderPath);
if (!(await this.fileSystem.exists(scriptsFolderPath))) {
await this.fileSystem.ensureDir(scriptsFolderPath);
}

const scriptName = `Start_${profileId}.bat`;
const scriptPath = path.join(scriptsFolderPath, scriptName);

const templatePath = path.join(scriptsFolderPath, "_TEMPLATE.bat");
const templateContent = await this.fileSystem.read(templatePath);
const scriptContent = templateContent.replace("${profileId}", profileId).replace("${backendUrl}", backendUrl);

await this.fileSystem.write(scriptPath, scriptContent);

this.logger.success(`Generated launch script: /fika-server/assets/scripts/${scriptName}`);
Expand Down
1 change: 1 addition & 0 deletions src/utils/FikaConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export class FikaConfig {
forceIp: "",
},
setLevelToAverageOfLobby: true,
restartAfterAmountOfRaids: 3,
},
background: {
enable: true,
Expand Down

0 comments on commit c401a74

Please sign in to comment.