Skip to content

Commit

Permalink
[Fix] Use runRunnerCommand to fix issue with spaces enabling egl-sync (
Browse files Browse the repository at this point in the history
…#2964)

* Use runRunnerCommand to fix issue with spaces enabling egl-sync

* Address feedback: order options, constant, safeparse
  • Loading branch information
arielj authored Aug 11, 2023
1 parent b622830 commit 1ca61fb
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 36 deletions.
35 changes: 1 addition & 34 deletions src/backend/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
appendFileSync,
constants,
existsSync,
mkdirSync,
rmSync,
unlinkSync,
watch,
Expand Down Expand Up @@ -63,7 +62,6 @@ import {
resetHeroic,
showAboutWindow,
showItemInFolder,
getLegendaryBin,
getFileSize,
detectVCRedist,
getFirstExistingParentPath,
Expand Down Expand Up @@ -1402,38 +1400,7 @@ ipcMain.handle(
)

ipcMain.handle('egsSync', async (event, args) => {
if (isWindows) {
const egl_manifestPath =
'C:\\ProgramData\\Epic\\EpicGamesLauncher\\Data\\Manifests'

if (!existsSync(egl_manifestPath)) {
mkdirSync(egl_manifestPath, { recursive: true })
}
}

const linkArgs = isWindows
? `--enable-sync`
: `--enable-sync --egl-wine-prefix ${args}`
const unlinkArgs = `--unlink`
const isLink = args !== 'unlink'
const command = isLink ? linkArgs : unlinkArgs
const { bin, dir } = getLegendaryBin()
const legendary = path.join(dir, bin)

try {
const { stderr, stdout } = await execAsync(
`${legendary} egl-sync ${command} -y`
)
logInfo(`${stdout}`, LogPrefix.Legendary)
if (stderr.includes('ERROR')) {
logError(`${stderr}`, LogPrefix.Legendary)
return 'Error'
}
return `${stdout} - ${stderr}`
} catch (error) {
logError(error, LogPrefix.Legendary)
return 'Error'
}
return LegendaryLibraryManager.toggleGamesSync(args)
})

ipcMain.handle('syncGOGSaves', async (event, gogSaves, appName, arg) =>
Expand Down
16 changes: 16 additions & 0 deletions src/backend/storeManagers/legendary/commands/egl_sync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Path } from './base'

interface EglSyncCommand {
subcommand: 'egl-sync'
'--egl-manifest-path'?: Path
'--egl-wine-prefix'?: Path
'--enable-sync'?: true
'--disable-sync'?: true
'--one-shot'?: true
'--import-only'?: true
'--export-only'?: true
'--migrate'?: true
'--unlink'?: true
}

export default EglSyncCommand
2 changes: 2 additions & 0 deletions src/backend/storeManagers/legendary/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import UninstallCommand from './uninstall'
import ImportCommand from './import'
import CleanupCommand from './cleanup'
import AuthCommand from './auth'
import EglSyncCommand from './egl_sync'

interface BaseLegendaryCommand {
'-v'?: true
Expand Down Expand Up @@ -41,4 +42,5 @@ export type LegendaryCommand = BaseLegendaryCommand &
| ImportCommand
| CleanupCommand
| AuthCommand
| EglSyncCommand
)
59 changes: 57 additions & 2 deletions src/backend/storeManagers/legendary/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
createAbortController,
deleteAbortController
} from '../../utils/aborthandler/aborthandler'
import { existsSync, readFileSync, readdirSync } from 'graceful-fs'
import { existsSync, mkdirSync, readFileSync, readdirSync } from 'graceful-fs'

import {
GameInfo,
Expand Down Expand Up @@ -33,7 +33,8 @@ import {
legendaryLogFile,
legendaryMetadata,
isLinux,
userHome
userHome,
isWindows
} from '../../constants'
import {
logDebug,
Expand Down Expand Up @@ -744,6 +745,60 @@ export async function getGameSdl(
}

/**
* Toggles the EGL synchronization on/off based on arguments
* @param path_or_action On Windows: "unlink" (turn off), "windows" (turn on). On linux/mac: "unlink" (turn off), any other string (prefix path)
* @returns string with stdout + stderr, or error message
*/
export async function toggleGamesSync(path_or_action: string) {
if (isWindows) {
const egl_manifestPath =
'C:\\ProgramData\\Epic\\EpicGamesLauncher\\Data\\Manifests'

if (!existsSync(egl_manifestPath)) {
mkdirSync(egl_manifestPath, { recursive: true })
}
}

const command: LegendaryCommand = {
subcommand: 'egl-sync',
'-y': true
}

if (path_or_action === 'unlink') {
command['--unlink'] = true
} else {
command['--enable-sync'] = true
if (!isWindows) {
const pathParse = Path.safeParse(path_or_action)
if (pathParse.success) {
command['--egl-wine-prefix'] = pathParse.data
} else {
return 'Error'
}
}
}

const { error, stderr, stdout } = await runRunnerCommand(
command,
createAbortController('toggle-sync')
)

deleteAbortController('toggle-sync')

if (error) {
logError(['Failed to toggle EGS-Sync', error], LogPrefix.Legendary)
return 'Error'
} else {
logInfo(`${stdout}`, LogPrefix.Legendary)
if (stderr.includes('ERROR') || stderr.includes('error')) {
logError(`${stderr}`, LogPrefix.Legendary)
return 'Error'
}
return `${stdout} - ${stderr}`
}
}

/*
* Converts a LegendaryCommand to a parameter list passable to Legendary
* @param command
*/
Expand Down

0 comments on commit 1ca61fb

Please sign in to comment.