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 { existsSync, mkdirSync, readFileSync, writeFileSync } from 'graceful-fs'
import {
AppSettings,
GlobalConfigVersion,
WineInstallation
} from 'common/types'
import { currentGlobalConfigVersion } from 'backend/constants/others'
import { logError, logInfo, LogPrefix } from './logger'
import {
getCrossover,
getDefaultWine,
getGamePortingToolkitWine,
getLinuxWineSet,
getSystemGamePortingToolkitWine,
getWhisky,
getWineOnMac,
getWineskinWine
} from './utils/compatibility_layers'
import { backendEvents } from './backend_events'
import { configStore } from './constants/key_value_stores'
import { isFlatpak, isLinux, isMac, isWindows } from './constants/environment'
import {
configPath,
sharedWinePrefix,
gamesConfigPath,
heroicInstallPath,
userHome,
defaultWinePrefixDir
} from './constants/paths'
import { join } from 'path'
import { spawnSync } from 'child_process'
import {
updateWineVersionInfos,
wineDownloaderInfoStore
} from './wine/manager/utils'
function getSteamCompatFolder() {
// Paths are from https://savelocation.net/steam-game-folder
if (isWindows) {
const defaultWinPath = join(process.env['PROGRAMFILES(X86)'] ?? '', 'Steam')
return defaultWinPath
} else if (isMac) {
return join(userHome, 'Library/Application Support/Steam')
} else {
const flatpakSteamPath = join(
userHome,
'.var/app/com.valvesoftware.Steam/.steam/steam'
)
const nativeSteamPath = join(userHome, '.steam/steam')
if (existsSync(flatpakSteamPath)) {
if (isFlatpak) {
// There is no `flatpak` command inside Heroic's own sandbox to ask
// whether Steam is installed. The manifest gives us access to Steam's
// data folder, which stays around after Steam is uninstalled, so only
// use it when there is no native Steam to prefer instead.
if (!existsSync(nativeSteamPath)) {
return flatpakSteamPath
}
} else {
// check if steam is really installed via flatpak
const { status } = spawnSync('flatpak', [
'info',
'com.valvesoftware.Steam'
])
if (status === 0) {
return flatpakSteamPath
}
}
}
return nativeSteamPath
}
}
/**
* This class does config handling.
* This can't be constructed directly. Use the static method get().
* It automatically selects the appropriate config loader based on the config version.
*
* It also implements all the config features that won't change across versions.
*/
abstract class GlobalConfig {
protected static globalInstance: GlobalConfig
public abstract version: GlobalConfigVersion
protected config: AppSettings | undefined
public set(config: AppSettings) {
this.config = config
}
public hasSetting(key: keyof AppSettings): boolean {
const settings = configStore.get_nodefault('settings') ?? {}
return Object.prototype.hasOwnProperty.call(settings, key)
}
public reloadFromFile() {
this.config = undefined
this.config = this.getSettings()
}
/**
* Get the global configuartion handler.
* If one doesn't exist, create one.
*
* @returns GlobalConfig instance.
*/
public static get(): GlobalConfig {
let version: GlobalConfigVersion
// Config file doesn't already exist, make one with the current version.
if (!existsSync(configPath)) {
version = currentGlobalConfigVersion
}
// Config file exists, detect its version.
else {
// Check version field in the config.
try {
version = JSON.parse(readFileSync(configPath, 'utf-8'))['version']
} catch (error) {
logError(
[`Config file is corrupted, please check ${configPath}:`, error],
LogPrefix.Backend
)
version = 'v0'
}
// Legacy config file without a version field, it's a v0 config.
if (!version) {
version = 'v0'
}
}
if (!GlobalConfig.globalInstance) {
GlobalConfig.reload(version)
}
return GlobalConfig.globalInstance
}
/**
* Recreate the global configuration handler.
*
* @param version Config version to load file using.
* @returns void
*/
private static reload(version: GlobalConfigVersion): void {
// Select loader to use.
switch (version) {
case 'v0':
GlobalConfig.globalInstance = new GlobalConfigV0()
break
default:
logError(
`Invalid config version '${version}' requested.`,
LogPrefix.GlobalConfig
)
break
}
// Try to upgrade outdated config.
if (GlobalConfig.globalInstance.upgrade()) {
// Upgrade done, we need to fully reload config.
logInfo(
`Upgraded outdated ${version} config to ${currentGlobalConfigVersion}.`,
LogPrefix.GlobalConfig
)
return GlobalConfig.reload(currentGlobalConfigVersion)
} else if (version !== currentGlobalConfigVersion) {
// Upgrade failed.
logError(
`Failed to upgrade outdated ${version} config.`,
LogPrefix.GlobalConfig
)
}
}
/**
* Detects Wine on Mac
* @returns Promise<Set<WineInstallation>>
* @memberof GlobalConfig
**/
public async getMacOsWineSet(): Promise<Set<WineInstallation>> {
if (!isMac) {
return new Set<WineInstallation>()
}
const getGPTKWine = await getGamePortingToolkitWine()
const getSystemGPTK = await getSystemGamePortingToolkitWine()
const crossover = await getCrossover()
const wineOnMac = await getWineOnMac()
const wineskinWine = await getWineskinWine()
const whiskyWine = await getWhisky()
return new Set([
...getGPTKWine,
...getSystemGPTK,
...crossover,
...wineOnMac,
...wineskinWine,
...whiskyWine
])
}
/**
* Detects Wine/Proton on the user's system.
*
* @returns An Array of Wine/Proton installations.
*/
public async getAlternativeWine(
scanCustom = true
): Promise<WineInstallation[]> {
if (wineDownloaderInfoStore.get('wine-releases', []).length === 0) {
await updateWineVersionInfos(true)
}
if (isMac) {
const macOsWineSet = await this.getMacOsWineSet()
return [...macOsWineSet]
}
const linuxWineSet = await getLinuxWineSet(scanCustom)
return [...linuxWineSet]
}
/**
* Gets the actual settings from the config file.
* Does not modify its parent object.
* Always reads from file regardless of `this.config`.
*
* @returns Settings present in config file.
*/
public abstract getSettings(): AppSettings
/**
* Updates this.config, this.version to upgrade the current config file.
*
* Writes to file after that.
* DO NOT call `flush()` afterward.
*
* @returns true if upgrade successful if upgrade fails or no upgrade needed.
*/
public abstract upgrade(): boolean
/**
* Get default settings as if the user's config file doesn't exist.
* Doesn't modify the parent object.
* Doesn't access config files.
*
* @returns AppSettings
*/
public abstract getFactoryDefaults(): AppSettings
/**
* Reset `this.config` to `getFactoryDefaults()` and flush.
*/
public abstract resetToDefaults(): void
protected writeToFile(config: Record<string, unknown>) {
return writeFileSync(configPath, JSON.stringify(config, null, 2))
}
/**
* Write `this.config` to file.
* Uses the config version defined in `this.version`.
*/
public abstract flush(): void
/** change a specific setting */
public abstract setSetting(key: keyof AppSettings, value: unknown): void
/**
* Load the config file, upgrade if needed.
*/
protected load() {
// Config file doesn't exist, make one.
if (!existsSync(configPath)) {
this.resetToDefaults()
}
// Always upgrade before loading to avoid errors.
// `getSettings` doesn't return an `AppSettings` otherwise.
if (this.version !== currentGlobalConfigVersion) {
// Do not load the config.
// Wait for `upgrade` to be called by `reload`.
} else {
// No upgrades necessary, load config.
// `this.version` should be `currentGlobalConfigVersion` at this point.
this.config = this.getSettings()
}
}
}
class GlobalConfigV0 extends GlobalConfig {
public version: GlobalConfigVersion = 'v0'
constructor() {
super()
this.load()
}
public upgrade() {
// Here we rewrite the config object to match the latest format and write to file.
// Not necessary as this is the current version.
return false
}
public getSettings(): AppSettings {
if (this.config) {
return this.config
}
if (!existsSync(gamesConfigPath)) {
mkdirSync(gamesConfigPath, { recursive: true })
}
if (!existsSync(configPath)) {
return this.getFactoryDefaults()
}
let settings = JSON.parse(readFileSync(configPath, 'utf-8'))
const defaultSettings = settings.defaultSettings as AppSettings
// keep old setting value for backwards compatibility, always use defaultWinePrefixDir in new code
if (!defaultSettings.defaultWinePrefixDir)
defaultSettings.defaultWinePrefixDir = defaultSettings.defaultWinePrefix
// migrate legacy booleans to their replacements
if (
defaultSettings.hdrMode === undefined &&
defaultSettings.enableHDR !== undefined
) {
defaultSettings.hdrMode = defaultSettings.enableHDR ? 'force' : 'off'
}
if (
defaultSettings.nvapiMode === undefined &&
defaultSettings.autoInstallDxvkNvapi !== undefined
) {
defaultSettings.nvapiMode = defaultSettings.autoInstallDxvkNvapi
? 'auto'
: 'off'
}
// fix relative paths
const winePrefix = !isWindows
? defaultSettings?.winePrefix?.replace('~', userHome)
: ''
const factoryDefaults = this.getFactoryDefaults()
// The first start stores every default in config.json, so an install that
// ran before Flatpak Steam was detected keeps the native path forever. If
// that stored path does not exist and Steam was found elsewhere, follow
// the detected folder. A path the user set by hand is left alone.
const nativeSteamPath = join(userHome, '.steam/steam')
const storedSteamPath = defaultSettings.defaultSteamPath
const defaultSteamPath =
storedSteamPath === nativeSteamPath && !existsSync(nativeSteamPath)
? factoryDefaults.defaultSteamPath
: (storedSteamPath ?? factoryDefaults.defaultSteamPath)
settings = {
...factoryDefaults,
...defaultSettings,
winePrefix,
defaultSteamPath
} as AppSettings
return settings
}
public getFactoryDefaults(): AppSettings {
// @ts-expect-error FIXME: Settings values don't work like this in other parts of the codebase
const defaultWine: WineInstallation = isWindows ? {} : getDefaultWine()
const settings: Partial<AppSettings> = {
analyticsOptIn: false,
checkUpdatesInterval: 10,
enableUpdates: false,
addDesktopShortcuts: false,
addStartMenuShortcuts: false,
autoInstallDxvk: isLinux || isMac,
autoInstallVkd3d: isLinux,
autoInstallDxvkNvapi: isLinux,
addSteamShortcuts: false,
preferSystemLibs: false,
checkForUpdatesOnStartup: !isFlatpak,
autoUpdateGames: false,
backupSaves: true,
maxSaveBackupSize: 1,
customWinePaths: [],
defaultInstallPath: heroicInstallPath,
libraryTopSection: 'disabled',
defaultSteamPath: getSteamCompatFolder(),
defaultWinePrefix: defaultWinePrefixDir,
defaultWinePrefixDir: defaultWinePrefixDir,
hideChangelogsOnStartup: false,
disableDiscounts: false,
enableGOGAffiliate: false,
language: 'en',
maxWorkers: 0,
downloadUnits: 'mib',
minimizeOnLaunch: false,
nvidiaPrime: false,
enviromentOptions: [],
wrapperOptions: [],
showFps: false,
useGameMode: isFlatpak,
wineCrossoverBottle: 'Heroic',
winePrefix: isWindows ? '' : sharedWinePrefix,
wineVersion: defaultWine,
enableEsync: true,
enableFsync: isLinux,
enableMsync: isMac,
enableWineWayland: false,
enableHDR: false,
hdrMode: 'auto',
nvapiMode: 'auto',
disableNTSync: false,
fsr4Upgrade: false,
dlssUpgrade: false,
useOptiScaler: false,
umuGameId: '',
disableProtonFixes: false,
enableWoW64: false,
eacRuntime: isLinux,
gamepadRepeatDelay: 50,
gamepadInitialRepeatDelay: 300,
gamepadDeadzone: 0.15,
gamepadButtonMapping: {},
disableGuideButton: false,
battlEyeRuntime: isLinux,
framelessWindow: false,
hideWindowOnProtocolLaunch: false,
beforeLaunchScriptPath: '',
afterLaunchScriptPath: '',
disableUMU: false,
verboseLogs: true,
defaultSteamRuntime: false,
downloadProtonToSteam: false,
advertiseAvxForRosetta: isMac && defaultWine.type === 'toolkit',
noTrayIcon: false,
showValveProton: false,
steamGridDbApiKey: '',
disableGOGPresence: false
}
// @ts-expect-error TODO: We need to settle on *one* place to define settings defaults
return settings
}
public setSetting(key: keyof AppSettings, value: unknown) {
const config = this.getSettings()
const configStoreSettings = configStore.get_nodefault('settings') || config
configStore.set('settings', { ...configStoreSettings, [key]: value })
const oldValue = config[key]
config[key] = value as never
this.config = config
backendEvents.emit('settingChanged', { key, oldValue, newValue: value })
return this.flush()
}
public resetToDefaults() {
this.config = this.getFactoryDefaults()
return this.flush()
}
public flush() {
return this.writeToFile({
defaultSettings: this.config,
version: 'v0'
})
}
}
export { GlobalConfig }