|
| 1 | +import { |
| 2 | + getBinExecIfExists, |
| 3 | + getHyperPlayGameInfo |
| 4 | +} from 'backend/hyperplay/library' |
| 5 | +import { existsSync } from 'graceful-fs' |
| 6 | +import { isWindows, isMac, isLinux } from '../constants' |
| 7 | +import { killPattern } from 'backend/utils' |
| 8 | +import { InstallPlatform, Runner } from 'common/types' |
| 9 | +import { hpLibraryStore } from './electronStore' |
| 10 | +import { sendFrontendMessage } from 'backend/main_window' |
| 11 | +import * as path from 'path' |
| 12 | +import * as fs from 'fs' |
| 13 | +import { LogPrefix, logInfo } from 'backend/logger/logger' |
| 14 | + |
| 15 | +export const isHpGameAvailable = (appName: string) => { |
| 16 | + const hpGameInfo = getHyperPlayGameInfo(appName) |
| 17 | + if (hpGameInfo && hpGameInfo.install.platform === 'web') { |
| 18 | + return true |
| 19 | + } |
| 20 | + |
| 21 | + if (hpGameInfo.install && hpGameInfo.install.executable) { |
| 22 | + return existsSync(hpGameInfo.install.executable) |
| 23 | + } |
| 24 | + return false |
| 25 | +} |
| 26 | + |
| 27 | +export function isHpGameNative(appName: string): boolean { |
| 28 | + const { |
| 29 | + install: { platform } |
| 30 | + } = getHyperPlayGameInfo(appName) |
| 31 | + if (platform) { |
| 32 | + if (platform === 'web') { |
| 33 | + return true |
| 34 | + } |
| 35 | + |
| 36 | + if (isWindows) { |
| 37 | + return true |
| 38 | + } |
| 39 | + |
| 40 | + if (isMac && platform === 'Mac') { |
| 41 | + return true |
| 42 | + } |
| 43 | + |
| 44 | + // small hack, but needs to fix the typings |
| 45 | + const plat = platform.toLowerCase() |
| 46 | + if (isLinux && plat === 'linux') { |
| 47 | + return true |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return false |
| 52 | +} |
| 53 | + |
| 54 | +export async function stopHpGame(appName: string): Promise<void> { |
| 55 | + const { |
| 56 | + install: { executable = undefined } |
| 57 | + } = getHyperPlayGameInfo(appName) |
| 58 | + |
| 59 | + if (executable) { |
| 60 | + const split = executable.split('/') |
| 61 | + const exe = split[split.length - 1] |
| 62 | + killPattern(exe) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +export async function importGame( |
| 67 | + appName: string, |
| 68 | + pathName: string, |
| 69 | + runner: Runner, |
| 70 | + platform: InstallPlatform |
| 71 | +) { |
| 72 | + const currentLibrary = hpLibraryStore.get('games', []) |
| 73 | + |
| 74 | + // TODO refactor this to constant time check with a set |
| 75 | + // not important for alpha release |
| 76 | + const gameInLibrary = currentLibrary.find((val) => { |
| 77 | + return val.app_name === appName |
| 78 | + }) |
| 79 | + |
| 80 | + if (gameInLibrary === undefined) { |
| 81 | + logInfo('Cannot find game in library so cannot import', LogPrefix.HyperPlay) |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + let exec = '' |
| 86 | + const files = await fs.promises.readdir(pathName) |
| 87 | + files.forEach((val) => { |
| 88 | + const splitFile = val.split('.') |
| 89 | + const extension = splitFile[splitFile.length - 1] |
| 90 | + if (extension === 'exe') { |
| 91 | + exec = val |
| 92 | + } |
| 93 | + }) |
| 94 | + |
| 95 | + const executableFullPath = path.join(pathName, exec) |
| 96 | + const binExec = getBinExecIfExists(executableFullPath) |
| 97 | + gameInLibrary.install = { |
| 98 | + install_path: pathName, |
| 99 | + executable: binExec === '' ? executableFullPath : binExec, |
| 100 | + install_size: '0 GiB', |
| 101 | + is_dlc: false, |
| 102 | + version: '-1', |
| 103 | + platform: platform |
| 104 | + } |
| 105 | + |
| 106 | + gameInLibrary.is_installed = true |
| 107 | + hpLibraryStore.set('games', currentLibrary) |
| 108 | + |
| 109 | + sendFrontendMessage('refreshLibrary') |
| 110 | +} |
0 commit comments