Skip to content

A games launcher for GOG, Amazon and Epic Games for Linux, Windows and macOS.

6.3 KB
Raw Blame
import { addHandler, addListener } from 'backend/ipc'
import { SteamUser } from './user'
import { libraryManagerMap } from '..'
import {
  getSteamDownloadLimit,
  getSteamPlatformSwitchInstall,
  getSteamRuntimeStatus,
  runAurelia,
  setSteamDownloadLimit,
  setSteamPlatformSwitchInstall
} from './aurelia'
import { describeError, isSteamSupported } from './utils'
import {
  getSteamLinuxRuntimeSettings,
  setSteamLinuxRuntimeSetting,
  getSteamLinuxRuntime,
  setSteamLinuxRuntime
} from './linux_runtime'
import { logError, logWarning, LogPrefix } from 'backend/logger'
import { isLinux } from 'backend/constants/environment'
import { GlobalConfig } from 'backend/config'
import {
  installWineVersion,
  updateWineVersionInfos
} from 'backend/wine/manager/utils'
import { notify } from 'backend/dialog/dialog'
import { t } from 'i18next'
import { Repositorys } from 'common/types'
import type { WineVersionInfo } from 'common/types'
import { isGptkRunner, selectSteamRuntimeRunner } from './runtime_runner'

addHandler('getSteamUsers', () => SteamUser.getAccounts())

addHandler('isSteamSupported', () => isSteamSupported())

addHandler('getSteamDlcInfo', async (_e, appName, noCache) =>
  libraryManagerMap['steam'].getDLCInfo(appName, noCache)
)

addHandler('getSteamInstallLibraries', async () =>
  libraryManagerMap['steam'].getInstallLibraries()
)

addHandler('getSteamDownloadLimit', async () => getSteamDownloadLimit())

addHandler('setSteamDownloadLimit', async (_e, kbit) =>
  setSteamDownloadLimit(kbit)
)

// Only Linux has two versions to switch between
addHandler('getSteamPlatformSwitchInstall', async () => {
  if (!isLinux) return undefined
  try {
    return await getSteamPlatformSwitchInstall()
  } catch (error) {
    logError(
      ['Unable to read the platform switch setting', describeError(error)],
      LogPrefix.Steam
    )
    return undefined
  }
})

addHandler('setSteamPlatformSwitchInstall', async (_e, mode) => {
  try {
    return await setSteamPlatformSwitchInstall(mode)
  } catch (error) {
    logError(
      ['Unable to set the platform switch setting', describeError(error)],
      LogPrefix.Steam
    )
    throw error
  }
})

addHandler('getSteamCloudSyncStatus', (_e, appName) =>
  libraryManagerMap['steam'].getGame(appName).getCloudSyncStatus()
)

addHandler('runSteamCloudSync', async (_e, appName, direction) =>
  libraryManagerMap['steam'].getGame(appName).runCloudSync(direction)
)

addHandler('resolveSteamCloudSync', async (_e, appName, resolve) =>
  libraryManagerMap['steam'].getGame(appName).resolveCloudSync(resolve)
)

addHandler('listSteamCloudFiles', async (_e, appName) =>
  libraryManagerMap['steam'].getGame(appName).listCloudFiles()
)

addHandler('setSteamDlcEnabled', async (_e, dlcAppId, enabled) =>
  // setDlcEnabled operates on the DLC's app id, not a specific game instance.
  libraryManagerMap['steam'].getGame(dlcAppId).setDlcEnabled(dlcAppId, enabled)
)

addHandler('getSteamLinuxRuntimeSettings', async () =>
  getSteamLinuxRuntimeSettings()
)

addHandler('setSteamLinuxRuntimeSetting', async (_e, key, value) =>
  setSteamLinuxRuntimeSetting(key, value)
)

addHandler('getSteamLinuxRuntime', async (_e, appName) =>
  getSteamLinuxRuntime(appName)
)

addHandler('setSteamLinuxRuntime', async (_e, appName, choice) =>
  setSteamLinuxRuntime(appName, choice)
)

addHandler('getSteamLaunchTargets', async (_e, appName) =>
  libraryManagerMap['steam'].getGame(appName).getLaunchTargets()
)

addHandler('setSteamLaunchTarget', (_e, appName, targetId) =>
  libraryManagerMap['steam'].getGame(appName).setLaunchTarget(targetId)
)

addHandler('applySteamLaunchTarget', (_e, appName) =>
  libraryManagerMap['steam'].getGame(appName).applyLaunchTarget()
)

addHandler('getSteamRuntimeStatus', async () => getSteamRuntimeStatus())

async function latestWineStaging(): Promise<WineVersionInfo | undefined> {
  const releases = await updateWineVersionInfos(
    true,
    [Repositorys.WINESTAGINGMACOS],
    10
  )
  return releases
    .filter(
      (release) => release.type === 'Wine-Staging-macOS' && !!release.download
    )
    .sort((a, b) => Date.parse(b.date) - Date.parse(a.date))[0]
}

async function ensureSteamRuntimeRunner(): Promise<void> {
  const status = await getSteamRuntimeStatus()
  if (status.runnerConfigured && !isGptkRunner(status.runnerPath)) {
    return
  }

  let candidate = selectSteamRuntimeRunner(
    await GlobalConfig.get().getAlternativeWine()
  )
  if (!candidate?.bin) {
    const release = await latestWineStaging()
    if (!release) {
      throw new Error('No Wine Staging release is available to download.')
    }
    notify({ title: release.version, body: t('notify.install.startInstall') })
    const result = await installWineVersion(release, () => {})
    notify({
      title: release.version,
      body:
        result === 'success'
          ? t('notify.install.finished')
          : t('notify.install.error')
    })
    if (result !== 'success') {
      throw new Error(`Failed to install ${release.version}.`)
    }
    candidate = selectSteamRuntimeRunner(
      await GlobalConfig.get().getAlternativeWine()
    )
  }
  if (!candidate?.bin) {
    throw new Error(
      'Could not find a Wine build for the Windows Steam runtime.'
    )
  }
  logWarning(
    ['Configuring the Windows Steam runtime to use', candidate.bin],
    LogPrefix.Steam
  )
  await runAurelia(['config', 'steam-wine-runner', candidate.bin])
}

// Opens the Windows Steam installer under Wine
addHandler('installSteamWineRuntime', async () => {
  try {
    await ensureSteamRuntimeRunner()
    await runAurelia(['steam-wine', 'install'])
    return { status: 'done' as const }
  } catch (error) {
    logError(
      ['Failed to install the in-Wine Steam runtime', describeError(error)],
      LogPrefix.Steam
    )
    return { status: 'error' as const, error: describeError(error) }
  }
})

// Opens the Windows Steam client so the user can sign in
addHandler('loginSteamWineRuntime', async () => {
  try {
    await ensureSteamRuntimeRunner()
    await runAurelia(['steam-wine', 'login'])
    return { status: 'done' as const }
  } catch (error) {
    logError(
      ['Failed to open the in-Wine Steam sign-in', describeError(error)],
      LogPrefix.Steam
    )
    return { status: 'error' as const, error: describeError(error) }
  }
})

addListener('logoutSteamAccount', (_e, steamId) => {
  void SteamUser.logoutAccount(steamId)
})