Skip to content

[FIX] Issues with Epic Login and refresh Library #1073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 45 additions & 4 deletions electron/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,61 @@ import {
import { env } from 'process'
import { app } from 'electron'
import { existsSync } from 'graceful-fs'
import os from 'os'

const configStore = new Store({
cwd: 'store'
})

// based on https://github.com/jy95/escape-path-with-spaces/blob/master/index.js
// unfortunatelly this cant go into utils since it needs to be loaded on app start
function fixPathWithSpaces(path: string) {
// to detect on with os user had used path.resolve(...)
const is_posix_os = !isWindows
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels kinda unnecessary, why not just do if (!isWindows) below?

const version = os.release()

// For some windows version (Windows 10 v1803), it is not useful to escape spaces in path
// https://docs.microsoft.com/en-us/windows/release-information/
const windows_version_regex = /(\d+\.\d+)\.(\d+)/
const should_not_escape = (major_release = '', os_build = '') =>
/1\d+\.\d+/.test(major_release) && Number(os_build) >= 17134.1184

if (is_posix_os) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be higher up, so the regex isn't applied when it's not used anyway

return path.replace(/(\s+)/g, '\\$1')
}

// for windows, it depend of the build
const fixedPath = should_not_escape(
...windows_version_regex.exec(version).splice(1)
)
? // on major version, no need to escape anymore
// https://support.microsoft.com/en-us/help/4467268/url-encoded-unc-paths-not-url-decoded-in-windows-10-version-1803-later
path
: // on older version, replace space with symbol %20
path.replace(/(\s+)/g, '%20')
return fixedPath
}

function getLegendaryBin() {
const settings = configStore.get('settings') as { altLeg: string }
const bin =
settings?.altLeg ||
`"${fixAsarPath(
`${fixAsarPath(
join(
__dirname,
'bin',
process.platform,
isWindows ? 'legendary.exe' : 'legendary'
)
)}"`
)}`

// dont fix windows issues
if (bin.includes(' ')) {
return `"${bin}"`
}

logInfo(`Location: ${bin}`, LogPrefix.Legendary)
return bin
return fixPathWithSpaces(bin)
}

function getGOGdlBin() {
Expand All @@ -43,8 +79,13 @@ function getGOGdlBin() {
isWindows ? 'gogdl.exe' : 'gogdl'
)
)

// dont fix windows issues
if (bin.includes(' ')) {
return `"${bin}"`
}
logInfo(`Location: ${bin}`, LogPrefix.Gog)
return bin
return fixPathWithSpaces(bin)
}

const isMac = platform() === 'darwin'
Expand Down
8 changes: 6 additions & 2 deletions electron/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,14 @@ async function launch(
) {
let command = ''
if (runner == 'legendary') {
command = `${legendaryBin} launch ${appName} ${exe} ${runOffline} ${
const legendaryPath = dirname(legendaryBin).replaceAll('"', '')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be defined once in constants.ts? Setting it in like 5 different places does not seem like a good idea

logInfo(['Launch Command:', command], LogPrefix.Legendary)
process.chdir(legendaryPath)
command = `${
isWindows ? './legendary.exe' : './legendary'
} launch ${appName} ${exe} ${runOffline} ${
launchArguments ?? ''
} ${launcherArgs}`
logInfo(['Launch Command:', command], LogPrefix.Legendary)
} else if (runner == 'gog') {
// MangoHud,Gamemode, nvidia prime, audio fix can be used in Linux native titles
if (isLinux) {
Expand Down
30 changes: 23 additions & 7 deletions electron/legendary/games.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import { spawn } from 'child_process'
import Store from 'electron-store'
import { launch } from '../launcher'
import { addShortcuts, removeShortcuts } from '../shortcuts'
import { join } from 'path'
import { dirname, join } from 'path'

const legendaryPath = dirname(legendaryBin).replaceAll('"', '')
process.chdir(legendaryPath)

const store = new Store({
cwd: 'lib-cache',
Expand Down Expand Up @@ -233,7 +236,10 @@ class LegendaryGame extends Game {
const workers = maxWorkers === 0 ? '' : ` --max-workers ${maxWorkers}`
const logPath = `"${join(heroicGamesConfigPath, this.appName + '.log')}"`
const writeLog = isWindows ? `2>&1 > ${logPath}` : `|& tee ${logPath}`
const command = `${legendaryBin} update ${this.appName}${workers} -y ${writeLog}`

const command = `${isWindows ? './legendary.exe' : './legendary'} update ${
this.appName
}${workers} -y ${writeLog}`
return execAsync(command, execOptions)
.then(() => {
this.window.webContents.send('setGameStatus', {
Expand Down Expand Up @@ -303,7 +309,9 @@ class LegendaryGame extends Game {

const logPath = `"${join(heroicGamesConfigPath, this.appName + '.log')}"`
const writeLog = isWindows ? `2>&1 > ${logPath}` : `|& tee ${logPath}`
const command = `${legendaryBin} install ${this.appName} --platform ${platformToInstall} --base-path '${path}' ${withDlcs} ${installSdl} ${workers} -y ${writeLog}`
const command = `${isWindows ? './legendary.exe' : './legendary'} install ${
this.appName
} --platform ${platformToInstall} --base-path '${path}' ${withDlcs} ${installSdl} ${workers} -y ${writeLog}`
logInfo([`Installing ${this.appName} with:`, command], LogPrefix.Legendary)
return execAsync(command, execOptions)
.then(async ({ stdout, stderr }) => {
Expand All @@ -321,7 +329,9 @@ class LegendaryGame extends Game {
}

public async uninstall() {
const command = `${legendaryBin} uninstall ${this.appName} -y`
const command = `${
isWindows ? './legendary.exe' : './legendary'
} uninstall ${this.appName} -y`
logInfo(
[`Uninstalling ${this.appName} with:`, command],
LogPrefix.Legendary
Expand Down Expand Up @@ -350,7 +360,9 @@ class LegendaryGame extends Game {
const logPath = `"${join(heroicGamesConfigPath, this.appName + '.log')}"`
const writeLog = isWindows ? `2>&1 > ${logPath}` : `|& tee ${logPath}`

const command = `${legendaryBin} repair ${this.appName} ${workers} -y ${writeLog}`
const command = `${isWindows ? './legendary.exe' : './legendary'} repair ${
this.appName
} ${workers} -y ${writeLog}`

logInfo([`Repairing ${this.appName} with:`, command], LogPrefix.Legendary)
return await execAsync(command, execOptions)
Expand All @@ -365,7 +377,9 @@ class LegendaryGame extends Game {
}

public async import(path: string) {
const command = `${legendaryBin} import-game ${this.appName} '${path}'`
const command = `${
isWindows ? './legendary.exe' : './legendary'
} import-game ${this.appName} '${path}'`

logInfo(
[`Importing ${this.appName} from ${path} with:`, command],
Expand All @@ -392,7 +406,9 @@ class LegendaryGame extends Game {
? path.replaceAll("'", '').slice(0, -1)
: path.replaceAll("'", '')

const command = `${legendaryBin} sync-saves ${arg} --save-path "${fixedPath}" ${this.appName} -y`
const command = `${
isWindows ? './legendary.exe' : './legendary'
} sync-saves ${arg} --save-path "${fixedPath}" ${this.appName} -y`
const legendarySavesPath = join(home, 'legendary', '.saves')

//workaround error when no .saves folder exists
Expand Down
20 changes: 17 additions & 3 deletions electron/legendary/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { execAsync, isEpicServiceOffline, isOnline } from '../utils'
import {
fallBackImage,
installed,
isWindows,
legendaryBin,
legendaryConfigPath,
libraryPath
Expand All @@ -28,6 +29,10 @@ import { logError, logInfo, LogPrefix, logWarning } from '../logger/logger'
import { spawn } from 'child_process'
import Store from 'electron-store'
import { GlobalConfig } from '../config'
import path from 'path'

const legendaryPath = path.dirname(legendaryBin).replaceAll('"', '')
process.chdir(legendaryPath)

const libraryStore = new Store({
cwd: 'lib-cache',
Expand Down Expand Up @@ -96,9 +101,14 @@ class LegendaryLibrary {

return new Promise((res, rej) => {
const getUeAssets = showUnrealMarket ? '--include-ue' : ''
const child = spawn(legendaryBin, ['list', getUeAssets])
const child = spawn(
isWindows ? 'legendary.exe' : 'legendary',
['list', getUeAssets],
{ shell: isWindows }
)
child.stderr.on('data', (data) => {
if (`${data}`.includes('ERROR')) {
console.log(`${data}`)
logError(`${data}`.trim(), LogPrefix.Legendary)
} else {
logInfo(`${data}`.trim(), LogPrefix.Legendary)
Expand All @@ -112,6 +122,8 @@ class LegendaryLibrary {
this.loadAll()
})
.catch((err) => {
console.log(`${err}`)

logError(err, LogPrefix.Legendary)
})
}
Expand Down Expand Up @@ -221,7 +233,7 @@ class LegendaryLibrary {
}
try {
const { stdout } = await execAsync(
`${legendaryBin} -J info ${appName} ${
`${isWindows ? 'legendary.exe' : 'legendary'} -J info ${appName} ${
epicOffline ? '--offline' : ''
} --json`
)
Expand Down Expand Up @@ -255,7 +267,9 @@ class LegendaryLibrary {
return []
}

const command = `${legendaryBin} list-installed --check-updates --tsv`
const command = `${
isWindows ? 'legendary.exe' : 'legendary'
} list-installed --check-updates --tsv`
try {
const { stdout } = await execAsync(command)
logInfo('Checking for game updates', LogPrefix.Legendary)
Expand Down
15 changes: 12 additions & 3 deletions electron/legendary/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,30 @@ import { existsSync, readFileSync } from 'graceful-fs'

import { UserInfo } from '../types'
import { clearCache, execAsync } from '../utils'
import { legendaryBin, userInfo } from '../constants'
import { isWindows, legendaryBin, userInfo } from '../constants'
import { logError, logInfo, LogPrefix } from '../logger/logger'
import { spawn } from 'child_process'
import { userInfo as user } from 'os'
import Store from 'electron-store'
import { session } from 'electron'
import { dirname } from 'path'

const configStore = new Store({
cwd: 'store'
})

const legendaryPath = dirname(legendaryBin).replaceAll('"', '')
process.chdir(legendaryPath)

export class LegendaryUser {
public static async login(sid: string) {
logInfo('Logging with Legendary...', LogPrefix.Legendary)

const command = `auth --sid ${sid}`.split(' ')
return new Promise((res) => {
const child = spawn(legendaryBin, command)
const child = spawn(isWindows ? 'legendary.exe' : 'legendary', command, {
shell: isWindows
})
child.stderr.on('data', (data) => {
if (`${data}`.includes('ERROR')) {
logError(`${data}`, LogPrefix.Legendary)
Expand All @@ -45,7 +52,9 @@ export class LegendaryUser {
}

public static async logout() {
await execAsync(`${legendaryBin} auth --delete`)
await execAsync(
`${isWindows ? 'legendary.exe' : 'legendary'} auth --delete`
)
const ses = session.fromPartition('persist:epicstore')
await ses.clearStorageData()
await ses.clearCache()
Expand Down
7 changes: 5 additions & 2 deletions electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
} from 'graceful-fs'
import Backend from 'i18next-fs-backend'
import i18next from 'i18next'
import { join } from 'path'
import { dirname, join } from 'path'

import { DXVK } from './dxvk'
import { Game } from './games'
Expand Down Expand Up @@ -1167,6 +1167,9 @@ ipcMain.handle('egsSync', async (event, args) => {
}
}

const legendaryPath = dirname(legendaryBin).replaceAll('"', '')
process.chdir(legendaryPath)

const linkArgs = isWindows
? `--enable-sync`
: `--enable-sync --egl-wine-prefix ${args}`
Expand All @@ -1176,7 +1179,7 @@ ipcMain.handle('egsSync', async (event, args) => {

try {
const { stderr, stdout } = await execAsync(
`${legendaryBin} egl-sync ${command} -y`
`${isWindows ? 'legendary.exe' : 'legendary'} egl-sync ${command} -y`
)
logInfo(`${stdout}`, LogPrefix.Legendary)
if (stderr.includes('ERROR')) {
Expand Down
9 changes: 7 additions & 2 deletions electron/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ import {
legendaryBin
} from './constants'
import { logError, logInfo, LogPrefix, logWarning } from './logger/logger'
import { join } from 'path'
import { dirname, join } from 'path'

const execAsync = promisify(exec)
const statAsync = promisify(stat)

const { showErrorBox, showMessageBox } = dialog

const legendaryPath = dirname(legendaryBin).replaceAll('"', '')
process.chdir(legendaryPath)

/**
* Compares 2 SemVer strings following "major.minor.patch".
* Checks if target is newer than base.
Expand Down Expand Up @@ -93,7 +96,9 @@ export const getLegendaryVersion = async () => {
if (altLegendaryBin && !altLegendaryBin.includes('legendary')) {
return 'invalid'
}
const { stdout } = await execAsync(`${legendaryBin} --version`)
const { stdout } = await execAsync(
`${isWindows ? 'legendary.exe' : 'legendary'} --version`
)
return stdout
.split('legendary version')[1]
.replaceAll('"', '')
Expand Down