Skip to content

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

3.1 KB
Raw Blame
import { ZoomDownloadFile } from './types/zoom'

export interface ZoomFileGroups {
  game: ZoomDownloadFile[]
  extras: ZoomDownloadFile[]
}

function allZoomFiles(files: ZoomFileGroups): ZoomDownloadFile[] {
  return [...files.game, ...files.extras]
}

export function zoomFilesBytes(files: ZoomDownloadFile[]): number {
  return files.reduce((total, file) => total + file.total_size, 0)
}

export function selectedZoomFileGroups(
  files: ZoomFileGroups,
  selectedIds: string[]
): ZoomFileGroups {
  return {
    game: files.game.filter((file) => selectedIds.includes(file.id)),
    extras: files.extras.filter((file) => selectedIds.includes(file.id))
  }
}

export function hasSelectedGameFile(
  files: ZoomFileGroups,
  selectedIds: string[]
): boolean {
  return files.game.some((file) => selectedIds.includes(file.id))
}

// One game file and no extras installs in one click, everything else needs
// the file selection step. A product with no files still shows the step so it
// can explain why there is nothing to install.
export function shouldShowZoomCollections(files: ZoomFileGroups): boolean {
  return !(files.game.length === 1 && files.extras.length === 0)
}

// Zoom lists bonus content and occasionally non-installable archives in the
// game group. Only offer what the platform install loop can actually apply.
export function isInstallableZoomFile(
  filename: string,
  platform: 'windows' | 'linux'
): boolean {
  const name = filename.toLowerCase()

  if (platform === 'windows') {
    return name.endsWith('.exe')
  }

  return (
    name.endsWith('.tar.xz') || name.endsWith('.sh') || name.endsWith('.run')
  )
}

// Changing platform reloads the file list: keep only the ids that still
// exist, and fall back to the defaults when no selected game file survives.
export function carryZoomSelection(
  files: ZoomFileGroups,
  previousIds: string[],
  storedIds?: string[]
): string[] {
  const knownIds = new Set(allZoomFiles(files).map((file) => file.id))
  const carried = previousIds.filter((id) => knownIds.has(id))

  if (hasSelectedGameFile(files, carried)) {
    return carried
  }

  return zoomSelectionDefaults(files, storedIds)
}

// Stored ids win when at least one of them is still a game file, so a
// reinstall keeps the previous selection. Missing ids are dropped silently;
// if none of the stored game files exist anymore, fall back to all game files.
export function zoomSelectionDefaults(
  files: ZoomFileGroups,
  storedIds?: string[]
): string[] {
  const knownIds = new Set(allZoomFiles(files).map((file) => file.id))
  const stored = (storedIds ?? []).filter((id) => knownIds.has(id))

  if (hasSelectedGameFile(files, stored)) {
    return stored
  }

  return files.game.map((file) => file.id)
}

// The install modal may keep the previous file list around while a reload
// fails. Only allow install when the current fetch succeeded and the list has
// a selected game file.
export function canInstallZoomSelection(
  files: ZoomFileGroups,
  selectedIds: string[],
  error: string | null | undefined,
  loaded: boolean
): boolean {
  return loaded && !error && hasSelectedGameFile(files, selectedIds)
}