Skip to content

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

3.1 KB
Raw Blame
import {
  LogPrefix,
  logDebug,
  logError,
  logInfo,
  logWarning,
  getRunnerLogWriter
} from 'backend/logger'
import { libraryManagerMap } from '..'
import { GameConfig } from 'backend/game_config'
import {
  checkWineBeforeLaunch,
  sendGameStatusUpdate,
  spawnAsync
} from 'backend/utils'
import { runWineCommand, verifyWinePrefix } from 'backend/launcher'
import { isWindows } from 'backend/constants/environment'

/**
 * Handles installing dependencies for games that include PostInstall scripts
 */
export default async function setup(
  appName: string,
  installedPath?: string
): Promise<void> {
  const gameInfo = libraryManagerMap['nile'].getGameInfo(appName)
  if (!gameInfo) {
    logError([`Could not find game info for ${appName}. Skipping setup`])
    return
  }

  const basePath = installedPath ?? gameInfo?.install.install_path
  if (!basePath) {
    logError([
      `Could not find install path for ${
        gameInfo?.title ?? appName
      }. Skipping setup`
    ])
    return
  }

  const fuel = libraryManagerMap['nile'].fetchFuelJSON(appName, basePath)
  if (!fuel) {
    logError(
      [
        'Cannot install dependencies for',
        gameInfo?.title ?? appName,
        'without a fuel.json'
      ],
      LogPrefix.Nile
    )
    return
  }

  if (!fuel.PostInstall.length) {
    logInfo(
      ['No PostInstall instructions for', gameInfo?.title ?? appName],
      LogPrefix.Nile
    )
    return
  }

  logWarning(
    'Running setup instructions, if you notice issues with launching a game, please report it on our Discord server',
    LogPrefix.Nile
  )

  const gameSettings = GameConfig.get(appName).config
  if (!isWindows) {
    const logWriter = getRunnerLogWriter('nile')
    const isWineOkToLaunch = await checkWineBeforeLaunch(
      gameInfo,
      gameSettings,
      logWriter
    )

    if (!isWineOkToLaunch) {
      logError(
        ['Was not possible to run setup using', gameSettings.wineVersion.name],
        LogPrefix.Nile
      )
      return
    }
    // Make sure prefix is initialized correctly
    await verifyWinePrefix(gameSettings)
  }

  logDebug(['PostInstall:', fuel.PostInstall], LogPrefix.Nile)

  sendGameStatusUpdate({
    appName,
    runner: 'nile',
    status: 'redist',
    context: 'AMAZON'
  })

  // Actual setup logic
  for (const action of fuel.PostInstall) {
    const exeArguments = action.Args ?? []

    if (isWindows) {
      const command = [
        '-NoProfile',
        'Start-Process',
        '-FilePath',
        action.Command
      ]
      if (exeArguments.length) {
        command.push('-ArgumentList', ...exeArguments)
      }
      logInfo(['Setup: Executing', command.join(' ')], LogPrefix.Nile)
      await spawnAsync('powershell', command, {
        cwd: basePath
      })
      continue
    }

    logInfo(
      ['Setup: Executing', [action.Command, ...exeArguments].join(' ')],
      LogPrefix.Nile
    )

    await runWineCommand({
      gameSettings,
      gameInstallPath: basePath,
      commandParts: [action.Command, ...exeArguments],
      wait: true,
      protonVerb: 'run',
      startFolder: basePath
    })
  }
}