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 { dialog, app } from 'electron'
import { logError, logInfo, LogPrefix } from './logger'
import i18next from 'i18next'
import { GameInfo, LaunchOption, Runner } from 'common/types'
import { getMainWindow } from './main_window'
import { sendFrontendMessage } from './ipc'
import { libraryManagerMap } from './storeManagers'
import { launchEventCallback } from './launcher'
import { z } from 'zod'
import { windowIcon } from './constants/paths'
import { Path } from './schemas'
import { isCLINoGui } from './constants/environment'
import { GlobalConfig } from './config'
// Every runner that gets heroic:// shortcuts, which excludes steam
const RUNNERS = z.enum([
'legendary',
'gog',
'sideload',
'nile',
'zoom',
'itchio'
])
function parseHeroicUrl(args: string[]): URL | undefined {
const urlStr = args.find((arg) => arg.startsWith('heroic://'))
if (!urlStr) return
try {
return new URL(urlStr)
} catch {
return
}
}
function urlRequestsNoGui(url: URL): boolean {
const guiParam = url.searchParams.get('gui')
return guiParam === 'false' || guiParam === '0' || guiParam === 'no'
}
// Returns true when a `heroic://launch/...` URL in `args` should suppress
// the main window: either the URL carries `gui=false` (or `0`/`no`), or the
// user enabled the `hideWindowOnProtocolLaunch` setting.
export function shouldHideWindowForProtocolArgs(args: string[]): boolean {
const url = parseHeroicUrl(args)
if (!url || url.hostname !== 'launch') return false
if (urlRequestsNoGui(url)) return true
try {
return GlobalConfig.get().getSettings().hideWindowOnProtocolLaunch === true
} catch {
return false
}
}
// Returns true when `args` represent a `heroic://launch/...` request, i.e.
// starting a game rather than just reopening the app. Used so that starting
// a game never forces the main window to show: whatever state the window
// was already in (open, minimized, or hidden) is left untouched, unless the
// user explicitly opted into hiding it (see `shouldHideWindowForProtocolArgs`).
export function isGameLaunchArgs(args: string[]): boolean {
const url = parseHeroicUrl(args)
return url?.hostname === 'launch'
}
export function handleProtocol(args: string[]) {
const url = parseHeroicUrl(args)
if (!url) return
logInfo(['Received', url.href], LogPrefix.ProtocolHandler)
switch (url.hostname) {
case 'ping':
return handlePing(url)
case 'launch':
return handleLaunch(url)
default:
return
}
}
function handlePing(url: URL) {
logInfo(['Received ping! Args:', url.searchParams], LogPrefix.ProtocolHandler)
}
async function handleLaunch(url: URL) {
let appName
let runnerStr
let args: string[] = []
let altExe: Path | undefined = undefined
// Windows automatically adds a trailing / to shortcuts
if (url.pathname && url.pathname !== '/') {
// Old-style pathname URLs:
// - `heroic://launch/Quail`
// - `heroic://launch/legendary/Quail`
const splitPath = url.pathname.split('/').filter(Boolean)
appName = splitPath.pop()
runnerStr = splitPath.pop()
} else {
// New-style params URL:
// `heroic://launch?appName=Quail&runner=legendary&arg=foo&arg=bar`
appName = url.searchParams.get('appName')
runnerStr = url.searchParams.get('runner')
args = url.searchParams.getAll('arg').map(decodeURIComponent)
const altExeParameter = url.searchParams.get('altExe')
if (altExeParameter) {
const altExeParse = Path.safeParse(decodeURIComponent(altExeParameter))
if (altExeParse.success) altExe = altExeParse.data
}
}
if (!appName) {
logError('No appName in protocol URL', LogPrefix.ProtocolHandler)
return
}
let runner: Runner | undefined
const runnerParse = RUNNERS.safeParse(runnerStr)
if (runnerParse.success) {
runner = runnerParse.data
}
const gameInfo = findGame(appName, runner)
if (!gameInfo) {
return logError(
`Could not receive game data for ${appName}!`,
LogPrefix.ProtocolHandler
)
}
const { is_installed, title } = gameInfo
const settings = await libraryManagerMap[gameInfo.runner]
.getGame(appName)
.getSettings()
const hideForThisLaunch =
urlRequestsNoGui(url) ||
GlobalConfig.get().getSettings().hideWindowOnProtocolLaunch === true
if (is_installed) {
let launchOption: LaunchOption | undefined = undefined
if (altExe)
launchOption = {
type: 'altExe',
executable: altExe
}
if (hideForThisLaunch) {
const mainWindow = getMainWindow()
if (mainWindow?.isVisible()) {
logInfo(
'Hiding main window for protocol launch',
LogPrefix.ProtocolHandler
)
mainWindow.hide()
}
}
return launchEventCallback({
appName: appName,
runner: gameInfo.runner,
skipVersionCheck: settings.ignoreGameUpdates,
args,
launchArguments: launchOption
})
}
logInfo(`"${title}" not installed.`, LogPrefix.ProtocolHandler)
const mainWindow = getMainWindow()
if (!mainWindow) return
const { response } = await dialog.showMessageBox(mainWindow, {
buttons: [i18next.t('box.yes'), i18next.t('box.no')],
cancelId: 1,
message: `${title} ${i18next.t(
'box.protocol.install.not_installed',
'Is Not Installed, do you wish to Install it?'
)}`,
title: title,
icon: windowIcon
})
if (response === 0) {
if (isCLINoGui || hideForThisLaunch) {
logInfo(
'Window was hidden but user wants to install, showing GUI',
LogPrefix.ProtocolHandler
)
mainWindow.show()
}
sendFrontendMessage('installGame', appName, gameInfo.runner)
} else if (response === 1) {
logInfo('Not installing game', LogPrefix.ProtocolHandler)
if (isCLINoGui) {
logInfo('--no-gui flag detected, exiting app', LogPrefix.ProtocolHandler)
app.quit()
}
}
}
function findGame(
appName?: string | null,
runner?: Runner
): GameInfo | undefined {
if (!appName) return
// If a runner is specified, search for the game in that runner and return it (if found)
if (runner) return libraryManagerMap[runner].getGame(appName).getGameInfo()
// If no runner is specified, search for the game in all runners and return the first one found
for (const runner of RUNNERS.options) {
const maybeGameInfo = libraryManagerMap[runner]
.getGame(appName)
.getGameInfo()
if (maybeGameInfo.app_name) return maybeGameInfo
}
return
}