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 { join, sep } from 'path'
import { existsSync } from 'graceful-fs'
import { logError, logWarning, LogPrefix } from 'backend/logger'
import { isFlatpak, isMac, isWindows } from 'backend/constants/environment'
import { getAureliaBin } from 'backend/utils'
import type { InstallPlatform } from 'common/types'
import type { SteamDataKind } from 'common/types/steam'
import type { AureliaLibraryGame } from './aurelia_types'
import { sendFrontendMessage } from 'backend/ipc'
import { showDialogBoxModalAuto } from 'backend/dialog/dialog'
import { t } from 'i18next'
import { AureliaError } from './aurelia'
/** Returned by every unsupported command. */
export const STEAM_UNSUPPORTED = 'Steam support is not available'
function hasAureliaBinary(): boolean {
const { dir, bin } = getAureliaBin()
return existsSync(dir ? join(dir, bin) : bin)
}
// macOS needs the Aurelia binary; Flatpak is unsupported.
export function isSteamSupported(): boolean {
return !isFlatpak && (!isMac || hasAureliaBinary())
}
export function describeError(error: unknown): string {
if (!(error instanceof AureliaError)) return String(error)
// Aurelia's hint says what to do.
return error.hint ? `${error.message} (${error.hint})` : error.message
}
/** Aurelia refuses while Steam runs. */
export function isSteamRunningError(error: unknown): boolean {
const text = error instanceof Error ? error.message : String(error)
return text.includes('Steam is running')
}
/** Aurelia found a second install in the destination library. */
export function isDuplicateInstallError(
error: unknown,
kind?: string
): boolean {
// Typed since Aurelia v0.5.3.
if (kind === 'already_installed') return true
// Text fallback for older binaries.
const text = error instanceof Error ? error.message : String(error)
return text.includes('already installed in')
}
export function steamRunningMessage(): string {
return t(
'box.error.steam-running',
'Steam is running. Close Steam, then try again.'
)
}
/** Dialog when Steam blocks the change. */
export function reportSteamRunning(error: unknown): void {
if (!isSteamRunningError(error)) return
showDialogBoxModalAuto({
title: t('box.error.title', 'Error'),
message: steamRunningMessage(),
type: 'ERROR'
})
}
/** Aurelia failed for lack of login. */
function isAuthRequired(error: unknown): boolean {
return error instanceof AureliaError && error.kind === 'auth_required'
}
/** Platform string recorded in `InstalledInfo`. */
const installPlatform = isWindows ? 'windows' : isMac ? 'osx' : 'linux'
/** Matched against Aurelia's `oslist` field. */
export const currentOsList = isWindows ? 'windows' : isMac ? 'macos' : 'linux'
/** Aurelia's `-p` value, or undefined when unsupported. */
export function aureliaPlatform(platform: string): string | undefined {
const lc = String(platform).toLowerCase()
if (lc.startsWith('win')) return 'windows'
if (lc.startsWith('lin')) return 'linux'
return undefined
}
/**
* The platform Heroic should record for an installed Steam game
*/
export function installedPlatformFor(
gamePlatform?: string | null
): InstallPlatform {
switch (String(gamePlatform).toLowerCase()) {
case 'windows':
return 'windows'
case 'macos':
case 'osx':
return 'osx'
case 'linux':
return 'linux'
default:
return installPlatform
}
}
/** `-p <platform>` argument pair, or empty. */
export function platformArgs(platform: string): string[] {
const mapped = aureliaPlatform(platform)
return mapped ? ['-p', mapped] : []
}
/** Apps shown in the library and search. */
export function isLibraryApp(game: Pick<AureliaLibraryGame, 'type'>): boolean {
// Soundtracks aren't games
if (game.type === 'Soundtrack') return false
// Betas may be unowned
return game.type !== 'Beta'
}
/**
* Normalises a chosen install path to the Steam **library root**
*/
export function toSteamLibraryRoot(path?: string): string | undefined {
if (!path) return undefined
const trimmed = path.replace(/[/\\]+$/, '')
const segments = trimmed.split(/[/\\]/)
const steamappsIdx = segments.findIndex(
(segment) => segment.toLowerCase() === 'steamapps'
)
const root =
steamappsIdx === -1 ? trimmed : segments.slice(0, steamappsIdx).join(sep)
return root || undefined
}
interface SteamCallOptions<T> {
/** Returned when disabled or on failure. */
fallback: T
/** Logged alongside the error text. */
failure: string
/** Warn instead of error. */
level?: 'error' | 'warning'
/** Tell the game page what failed. */
report?: { appId: string; data: SteamDataKind }
}
/**
* Runs an Aurelia-backed task on supported platforms
*/
export async function steamCall<T>(
task: () => Promise<T>,
{ fallback, failure, level = 'error', report }: SteamCallOptions<T>
): Promise<T> {
if (!isSteamSupported()) return fallback
try {
return await task()
} catch (error) {
const log = level === 'warning' ? logWarning : logError
log([failure, describeError(error)], LogPrefix.Steam)
if (report) {
sendFrontendMessage('steamDataFetchFailed', {
...report,
authRequired: isAuthRequired(error)
})
}
return fallback
}
}
/** A launch target's `play` flags without `--platform <value>`. */
export function withoutPlatformFlag(flags: string[]): string[] {
const at = flags.indexOf('--platform')
if (at < 0) return flags
return [...flags.slice(0, at), ...flags.slice(at + 2)]
}
interface SelectableTarget {
id: string
available: boolean
missing: string | null
}
/**
* The user's pick, if Play can use it. With an enforced platform, Aurelia
* downloads a missing build itself, so that pick stays valid.
*/
export function pickLaunchTarget<T extends SelectableTarget>(
targets: T[],
selected: string | undefined,
pinsPlatform: boolean
): T | undefined {
return targets.find(
(target) =>
target.id === selected &&
(target.available || (pinsPlatform && target.missing === 'build'))
)
}