Skip to content

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

9.9 KB
Raw Blame
import type { TFunction } from 'i18next'

import type { InstallPlatform, WineInstallation } from './types'

/**
 * Presets for the "Add Game" dialog.
 *
 * A preset is only metadata: the artwork that ships with Heroic, the official
 * vendor installer URL and a list of places where the installed executable is
 * usually found. Presets never bundle third-party installers, everything is
 * downloaded from the vendor's own HTTPS endpoint at the user's request.
 */
type SideloadPresetKind = 'web' | 'launcher'

export type SideloadPresetPlatform = 'linux' | 'win32' | 'darwin'

interface SideloadPresetInstaller {
  url: string
  fileName: string
  /** Arguments for the regular, interactive installer. */
  args?: string[]
  /**
   * Arguments for a silent/unattended install, when the vendor supports it.
   * The UI offers a toggle and falls back to the interactive install.
   */
  silentArgs?: string[]
  /** Environment variables applied only while the installer runs. */
  env?: Record<string, string>
}

interface SideloadPresetWebApp {
  url: string
  userAgent?: string
  launchFullScreen?: boolean
}

export interface SideloadPresetLauncher {
  installer: SideloadPresetInstaller
  /**
   * Environment variables applied on every launch, for example Wine quirks
   * the launcher needs to render its own window.
   */
  launchEnv?: Record<string, string>
  /**
   * Extra arguments passed to the launcher when starting it, per host
   * platform. Used for the Chromium switches some launchers need under Wine.
   */
  launchArgs?: Partial<Record<SideloadPresetPlatform, string[]>>
  /**
   * Wine flavour the launcher is known to install and run with, per host
   * platform. Only used as the default pick; the user can choose another one.
   */
  preferredWineType?: Partial<
    Record<SideloadPresetPlatform, WineInstallation['type']>
  >
  /**
   * Executable paths relative to the Windows drive root (`drive_c` in a Wine
   * prefix), ordered by likelihood. The first existing file wins.
   */
  exeCandidates: string[]
  /**
   * Some installers use versioned folders (the EA app does). The launcher can
   * look for these names directly below the root and one folder deeper.
   */
  exeSearch?: {
    root: string
    names: string[]
  }
}

type SideloadScanSource = 'ubisoft' | 'ea' | 'battlenet'

export interface MissingSideloadGame {
  appName: string
  title: string
  storeGameId?: string
}

export interface ScannedSideloadGame {
  /** Stable id used to deduplicate and re-add scanned games. */
  id: string
  source: SideloadScanSource
  title: string
  installDir?: string
  executable?: string
  /** Protocol URL used to launch through the store client. */
  launchUrl?: string
  /** Already present in the Heroic library (scanned from this preset). */
  added: boolean
}

export interface SideloadPresetArtwork {
  square: string
  hero: string
}

export interface SideloadPreset {
  id: string
  kind: SideloadPresetKind
  title: string
  /**
   * Platforms where the vendor installer opens the app itself at the end. On
   * macOS that window can render blank because the installer does not apply
   * Heroic's graphics settings, so the install step warns about it.
   */
  installerOpensApp?: SideloadPresetPlatform[]
  /**
   * Platforms where this launcher only works with the commercial CrossOver
   * build. Free Wine builds may install it but cannot display its window.
   */
  requiresCrossover?: SideloadPresetPlatform[]
  platforms: SideloadPresetPlatform[]
  artwork: SideloadPresetArtwork
  web?: SideloadPresetWebApp
  launcher?: SideloadPresetLauncher
}

const ALL_DESKTOP_PLATFORMS: SideloadPresetPlatform[] = [
  'linux',
  'win32',
  'darwin'
]

const CHROME_USER_AGENT =
  'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36'

/**
 * Order matters: it is the order the preset picker renders.
 */
export const SIDELOAD_PRESETS: SideloadPreset[] = [
  {
    id: 'battlenet',
    kind: 'launcher',
    title: 'Battle.net',
    installerOpensApp: ['darwin'],
    platforms: ALL_DESKTOP_PLATFORMS,
    artwork: {
      square: 'battlenet-square.jpg',
      hero: 'battlenet-hero.jpg'
    },
    launcher: {
      installer: {
        // Use the direct downloader endpoint: the account/download page URL
        // redirects and returns 403 to the downloader used by Heroic.
        url: 'https://downloader.battle.net/download/getInstaller?os=win&installer=Battle.net-Setup.exe',
        fileName: 'Battle.net-Setup.exe',
        // Keep Battle.net's install location predictable and avoid the
        // installer creating a second prefix entry.
        args: [
          '--installpath=C:\\Program Files (x86)\\Battle.net',
          '--lang=enUS'
        ],
        env: {
          PROTON_ENABLE_WAYLAND: '0',
          WINE_SIMULATE_WRITECOPY: '1'
        }
      },
      // Apple's Game Porting Toolkit ships an old Wine that fails while
      // downloading the Battle.net Agent, so default to Wine Staging there.
      preferredWineType: {
        darwin: 'wine'
      },
      launchEnv: {
        WINE_SIMULATE_WRITECOPY: '1'
      },
      // On macOS the CEF login window stays blank when it renders through the
      // D3D GPU process; these switches keep it on the CPU renderer.
      launchArgs: {
        darwin: [
          '--from-launcher',
          '--disable-gpu-compositing',
          '--in-process-gpu',
          '--use-gl=swiftshader'
        ]
      },
      exeCandidates: [
        'Program Files (x86)/Battle.net/Battle.net.exe',
        'Program Files (x86)/Battle.net/Battle.net Launcher.exe',
        'Program Files/Battle.net/Battle.net.exe',
        'Program Files/Battle.net/Battle.net Launcher.exe'
      ]
    }
  },
  {
    id: 'ea-app',
    kind: 'launcher',
    title: 'EA app',
    platforms: ALL_DESKTOP_PLATFORMS,
    artwork: {
      square: 'ea-square.jpg',
      hero: 'ea-hero.jpg'
    },
    launcher: {
      installer: {
        url: 'https://origin-a.akamaihd.net/EA-Desktop-Client-Download/installer-releases/EAappInstaller.exe',
        fileName: 'EAappInstaller.exe',
        // Same flags used for the EA app that ships with Epic-managed games.
        args: ['EAX_LAUNCH_CLIENT=0', 'IGNORE_INSTALLED=1'],
        env: {
          PROTON_ENABLE_WAYLAND: '0'
        }
      },
      exeCandidates: [
        'Program Files/Electronic Arts/EA Desktop/EA Desktop/EALauncher.exe',
        'Program Files/Electronic Arts/EA Desktop/EA Desktop/EADesktop.exe',
        'Program Files (x86)/Electronic Arts/EA Desktop/EA Desktop/EALauncher.exe'
      ],
      exeSearch: {
        root: 'Program Files/Electronic Arts/EA Desktop',
        names: ['EALauncher.exe', 'EADesktop.exe']
      }
    }
  },
  {
    id: 'ubisoft-connect',
    kind: 'launcher',
    title: 'Ubisoft Connect',
    requiresCrossover: ['darwin'],
    platforms: ALL_DESKTOP_PLATFORMS,
    artwork: {
      square: 'ubisoft-square.jpg',
      hero: 'ubisoft-hero.jpg'
    },
    launcher: {
      installer: {
        url: 'https://static3.cdn.ubi.com/orbit/launcher_installer/UbisoftConnectInstaller.exe',
        fileName: 'UbisoftConnectInstaller.exe',
        silentArgs: ['/S'],
        env: {
          PROTON_ENABLE_WAYLAND: '0'
        }
      },
      exeCandidates: [
        'Program Files (x86)/Ubisoft/Ubisoft Game Launcher/UbisoftConnect.exe',
        'Program Files/Ubisoft/Ubisoft Game Launcher/UbisoftConnect.exe'
      ]
    }
  },
  {
    id: 'xbox-cloud',
    kind: 'web',
    title: 'Xbox Cloud Gaming',
    platforms: ALL_DESKTOP_PLATFORMS,
    artwork: {
      square: 'xbox-square.jpg',
      hero: 'xbox-hero.jpg'
    },
    web: {
      url: 'https://www.xbox.com/play',
      userAgent: CHROME_USER_AGENT,
      launchFullScreen: true
    }
  }
]

export function getSideloadPreset(
  presetId: string | undefined
): SideloadPreset | undefined {
  if (!presetId) {
    return undefined
  }
  return SIDELOAD_PRESETS.find((preset) => preset.id === presetId)
}

/**
 * Stable app name so that re-applying a preset updates the existing entry
 * (and reuses its Wine prefix) instead of adding a duplicate.
 */
export function getSideloadPresetAppName(preset: SideloadPreset): string {
  return `preset-${preset.id}`
}

export function getSideloadPresetPlatform(
  preset: SideloadPreset
): InstallPlatform {
  return preset.kind === 'web' ? 'Browser' : 'Windows'
}

export function getSideloadPresetLaunchArgs(
  preset: SideloadPreset,
  platform: NodeJS.Platform
): string[] {
  return preset.launcher?.launchArgs?.[platform as SideloadPresetPlatform] ?? []
}

export function getSideloadPresetLaunchEnv(
  preset: SideloadPreset
): Record<string, string> {
  return preset.launcher?.launchEnv ?? {}
}

export function getPresetInstallerArgs(
  preset: SideloadPreset,
  silent = true
): string[] {
  if (!preset.launcher) {
    return []
  }

  const { args = [], silentArgs } = preset.launcher.installer
  return silent && silentArgs?.length ? silentArgs : args
}

export function isSideloadPresetAvailableOn(
  preset: SideloadPreset,
  platform: SideloadPresetPlatform
): boolean {
  return preset.platforms.includes(platform)
}

/**
 * Static t() calls keep the preset catalog discoverable by i18next-parser.
 * Do not turn these keys into dynamically built strings.
 */
export function getSideloadPresetText(
  t: TFunction<'gamepage'>,
  preset: SideloadPreset
): { description: string; note?: string } {
  switch (preset.id) {
    case 'battlenet':
      return {
        description: t('sideload.presets.battlenet.description')
      }
    case 'ea-app':
      return {
        description: t('sideload.presets.ea-app.description'),
        note: t('sideload.presets.ea-app.note')
      }
    case 'ubisoft-connect':
      return {
        description: t('sideload.presets.ubisoft-connect.description'),
        note: t('sideload.presets.ubisoft-connect.note')
      }
    case 'xbox-cloud':
      return {
        description: t('sideload.presets.xbox-cloud.description'),
        note: t('sideload.presets.xbox-cloud.note')
      }
    default:
      return { description: preset.title }
  }
}