dobrograd-13-06-2022/octolib/core/gameServer.ts

52 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-11-16 15:01:19 +05:00
import '../scripts/generate-env'
import childProcess from 'child_process'
import { info, ok, fail } from '../core/log'
import config from '../../config'
export const start = () => {
info('Launching game server...')
const exePath = process.platform === 'win32' ? '../srcds.exe' : '../srcds_run'
const gameServer = childProcess.spawn(exePath, [
'-allowlocalhttp',
'-console',
'-game', 'garrysmod',
'-tickrate', config.tickrate.toString(),
'-port', config.port.toString(),
'+sv_setsteamaccount', config.serverAccount,
'+host_workshop_collection', config.workshopCollection.toString(),
'+gmod_language', config.language,
'+gamemode', config.gamemode,
'+map', config.map,
'+maxplayers', config.maxPlayers.toString(),
'+sv_hibernate_think', config.hibernateThink ? '1' : '0',
], {
stdio: 'inherit',
})
gameServer.on('close', code => {
if (code === 0)
ok('Game server shut down')
else
fail('Game server exited with non-ok code ' + code)
})
const killServer = () => {
if (!gameServer.killed) {
gameServer.kill()
ok('Killed game server')
}
}
process.on('beforeExit', killServer)
process.on('uncaughtException', error => {
killServer()
throw error
})
if (gameServer.stdin)
process.stdin.pipe(gameServer.stdin)
return gameServer
}