A games launcher for GOG, Amazon and Epic Games for Linux, Windows and macOS.
TypeScript amazon-primeelectronepic-games-launcherepic-games-storeepic-storegaminggoggog-galaxyheroic-games-launcherlinuxplay-gamesprotonsteamwine
import type LogWriter from 'backend/logger/log_writer'
import { logInfo, logWarning, LogPrefix } from 'backend/logger'
import { sendGameStatusUpdate } from 'backend/utils'
import type { CallRunnerOptions, OutputStream } from 'common/types'
import type {
SteamLinuxRuntimeChoice,
SteamLinuxRuntimeGame,
SteamLinuxRuntimeSettings
} from 'common/types/steam'
import { aureliaHasFeature, runAurelia } from './aurelia'
import type {
AureliaGameLinuxRuntimeResponse,
AureliaLinuxRuntimeAutoInstallResponse,
AureliaLinuxRuntimeEvent,
AureliaLinuxRuntimeResponse
} from './aurelia_types'
/** Parse one output line as a runtime event. */
export function parseLinuxRuntimeEvent(
line: string
): AureliaLinuxRuntimeEvent | undefined {
if (!line.includes('"linux_runtime"')) return undefined
try {
const value = JSON.parse(line.trim()) as Partial<AureliaLinuxRuntimeEvent>
if (value?.event !== 'linux_runtime' || typeof value.name !== 'string') {
return undefined
}
return value as AureliaLinuxRuntimeEvent
} catch {
return undefined
}
}
/**
* Follows `aurelia play --json` output for Steam Linux Runtime events:
* logs the runtime in use and shows a runtime download as a pre-launch step.
*/
export function linuxRuntimeOutputHandler(
appName: string,
logWriter: LogWriter
): (output: string, stream?: OutputStream) => void {
// Streams interleave, so buffer separately
const pending: Record<OutputStream, string> = { stdout: '', stderr: '' }
const show = (context?: string) =>
sendGameStatusUpdate({
appName,
runner: 'steam',
status: context === undefined ? 'playing' : 'redist',
context
})
const handle = (event: AureliaLinuxRuntimeEvent) => {
switch (event.state) {
case 'applied':
void logWriter.logInfo(`Running in ${event.name}`)
break
case 'installing':
void logWriter.logInfo(`Installing ${event.name}`)
show(event.name)
break
case 'progress':
show(
event.percent === undefined
? event.name
: `${event.name} · ${Math.round(event.percent)}%`
)
break
case 'installed':
void logWriter.logInfo(`Installed ${event.name}`)
show()
break
case 'install_failed':
void logWriter.logWarning(
`Could not install ${event.name}: ${event.error ?? 'unknown error'}`
)
show()
break
}
}
return (output: string, stream: OutputStream = 'stderr') => {
pending[stream] += output
const lines = pending[stream].split('\n')
pending[stream] = lines.pop() ?? ''
for (const line of lines) {
const event = parseLinuxRuntimeEvent(line)
if (event) handle(event)
}
}
}
/** Adds runtime events to `play` output. */
export function withLinuxRuntimeEvents(
commandParts: string[],
options: CallRunnerOptions = {}
): CallRunnerOptions {
const { abortId, logWriters, onOutput } = options
// Play runs use the app id
if (commandParts[0] !== 'play' || !abortId || !logWriters?.length) {
return options
}
const handleRuntime = linuxRuntimeOutputHandler(abortId, logWriters[0])
return {
...options,
onOutput: (output, child, stream) => {
handleRuntime(output, stream)
onOutput?.(output, child, stream)
}
}
}
/** Aurelia's Steam Linux Runtime switches. */
export async function getSteamLinuxRuntimeSettings(): Promise<SteamLinuxRuntimeSettings> {
const off = { supported: false, enabled: false, autoInstall: false }
if (!(await aureliaHasFeature('play.linux_runtime'))) return off
try {
const [runtime, autoInstall] = await Promise.all([
runAurelia<AureliaLinuxRuntimeResponse>(['config', 'linux-runtime']),
runAurelia<AureliaLinuxRuntimeAutoInstallResponse>([
'config',
'linux-runtime-auto-install'
])
])
return {
supported: true,
enabled: runtime.linux_runtime ?? true,
autoInstall: autoInstall.linux_runtime_auto_install ?? true
}
} catch (error) {
logWarning(
['Unable to read Steam Linux Runtime settings', error],
LogPrefix.Steam
)
return off
}
}
/** Set one Steam Linux Runtime switch. */
export async function setSteamLinuxRuntimeSetting(
key: 'enabled' | 'autoInstall',
value: boolean
): Promise<SteamLinuxRuntimeSettings> {
const command =
key === 'enabled' ? 'linux-runtime' : 'linux-runtime-auto-install'
await runAurelia(['config', command, value ? 'on' : 'off'])
logInfo(['Steam Linux Runtime', command, 'set to', value], LogPrefix.Steam)
return getSteamLinuxRuntimeSettings()
}
/** Read a game's runtime without changing it. */
export async function getSteamLinuxRuntime(
appId: string
): Promise<SteamLinuxRuntimeGame> {
if (!(await aureliaHasFeature('play.linux_runtime'))) {
return { supported: false }
}
try {
const result = await runAurelia<AureliaGameLinuxRuntimeResponse>([
'config',
'game',
appId
])
return {
supported: true,
choice: result.linux_runtime,
effective: result.linux_runtime_effective
}
} catch (error) {
logWarning(
['Unable to read the Steam Linux Runtime of', appId, error],
LogPrefix.Steam
)
return { supported: true }
}
}
/** Store a game's choice; return its state. */
export async function setSteamLinuxRuntime(
appId: string,
choice: SteamLinuxRuntimeChoice
): Promise<SteamLinuxRuntimeGame> {
if (!(await aureliaHasFeature('play.linux_runtime'))) {
return { supported: false }
}
const result = await runAurelia<AureliaGameLinuxRuntimeResponse>([
'config',
'game',
appId,
'--linux-runtime',
choice
])
return {
supported: true,
choice: result.linux_runtime,
effective: result.linux_runtime_effective
}
}
/** Push a game's runtime choice to Aurelia. */
export async function applySteamLinuxRuntime(
appId: string,
choice: SteamLinuxRuntimeChoice
): Promise<boolean> {
if (!(await aureliaHasFeature('play.linux_runtime'))) return false
await runAurelia(['config', 'game', appId, '--linux-runtime', choice])
return true
}