Skip to content

Commit

Permalink
[Fix] Native sideload failing because of relative path handler (#1156)
Browse files Browse the repository at this point in the history
* [Fix] Native sideload failing because of relative path handler

* chore: move fix to utils method

* tests: updated tests and fix edge case

---------

Co-authored-by: Flavio F Lima <[email protected]>
Co-authored-by: Brett <[email protected]>
  • Loading branch information
3 people authored Dec 9, 2024
1 parent 583f1e3 commit 968fce3
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 9 deletions.
49 changes: 47 additions & 2 deletions src/backend/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ describe('backend/utils.ts', () => {
expect(getExecutableAndArgs(input)).toEqual(expected)
})

it('should correctly parse executable with .exe extension and no arguments', () => {
const input = 'path/to/application.exe'
const expected = {
executable: 'path/to/application.exe',
launchArgs: ''
}
expect(getExecutableAndArgs(input)).toEqual(expected)
})

it('should correctly parse executable with .app extension and no arguments', () => {
const input = 'path/to/application.app'
const expected = {
Expand Down Expand Up @@ -226,10 +235,10 @@ describe('backend/utils.ts', () => {
})

it('should return empty strings if no executable is found', () => {
const input = '--arg1 --arg2'
const input = ''
const expected = {
executable: '',
launchArgs: '--arg1 --arg2'
launchArgs: ''
}
expect(getExecutableAndArgs(input)).toEqual(expected)
})
Expand Down Expand Up @@ -260,5 +269,41 @@ describe('backend/utils.ts', () => {
}
expect(getExecutableAndArgs(input)).toEqual(expected)
})

it('should handle simple executable name without args', () => {
const input = 'executable.exe'
const expected = {
executable: 'executable.exe',
launchArgs: ''
}
expect(getExecutableAndArgs(input)).toEqual(expected)
})

it('should handle simple executable name with args', () => {
const input = 'steam --no-browser'
const expected = {
executable: 'steam',
launchArgs: '--no-browser'
}
expect(getExecutableAndArgs(input)).toEqual(expected)
})

it('should handle absolute path from /usr/bin', () => {
const input = '/usr/bin/steam --no-browser'
const expected = {
executable: '/usr/bin/steam',
launchArgs: '--no-browser'
}
expect(getExecutableAndArgs(input)).toEqual(expected)
})

it('should handle absolute path from /usr/local/bin', () => {
const input = '/usr/local/bin/custom-launcher --fullscreen'
const expected = {
executable: '/usr/local/bin/custom-launcher',
launchArgs: '--fullscreen'
}
expect(getExecutableAndArgs(input)).toEqual(expected)
})
})
})
8 changes: 7 additions & 1 deletion src/backend/storeManagers/storeManagerCommon/games.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,13 @@ export async function launchGame(
)
// On Mac, it gives an error when changing the permissions of the file inside the app bundle. But we need it for other executables like scripts.
if (isLinux || (isMac && !exeOnly.endsWith('.app'))) {
await chmod(exeOnly, 0o775)
try {
await chmod(exeOnly, 0o775)
} catch (error) {
logWarning(
'Was not possible to change permission to this file, maybe the owner is Root?'
)
}
}
}

Expand Down
40 changes: 34 additions & 6 deletions src/backend/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import {
LogPrefix,
logWarning
} from './logger/logger'
import { basename, dirname, join, normalize } from 'path'
import { basename, dirname, isAbsolute, join, normalize } from 'path'
import { runRunnerCommand as runLegendaryCommand } from 'backend/storeManagers/legendary/library'
import {
gameInfoStore,
Expand Down Expand Up @@ -1242,20 +1242,48 @@ export function getPlatformName(platform: string): PlatformName {
return platformMap[platform] || platform || 'Unknown'
}

const splitExeAndArgs = (executableWithArgs: string) => {
const executable = executableWithArgs.split(' -')[0]
const launchArgs = executableWithArgs.replace(executable, '').trim()

return [executable, launchArgs]
}

export function getExecutableAndArgs(executableWithArgs: string): {
executable: string
launchArgs: string
} {
if (!executableWithArgs) {
return { executable: '', launchArgs: '' }
}

// Handle absolute paths first
const isAbsolutePath = isAbsolute(executableWithArgs)
if (isAbsolutePath) {
const [exe, args] = splitExeAndArgs(executableWithArgs)
return { executable: exe, launchArgs: args }
}

// Handle .app paths
if (executableWithArgs.includes('.app')) {
const executable = executableWithArgs.split(' -')[0]
const [executable, launchArgs] = splitExeAndArgs(executableWithArgs)
return { executable, launchArgs }
}

// Handle common extensions
const matchWithExt = executableWithArgs.match(/^(.*?\.(exe|bin|sh))/i)
if (matchWithExt) {
const executable = matchWithExt[0]
const launchArgs = executableWithArgs.replace(executable, '').trim()
return { executable, launchArgs }
}
const match = executableWithArgs.match(/^(.*?\.(exe|bin|sh))/i)
const executable = match ? match[0] : ''
const launchArgs = executableWithArgs.replace(executable, '').trim()

return { executable, launchArgs }
// Handle executables without extension
const [executable, ...argParts] = splitExeAndArgs(executableWithArgs)
return {
executable,
launchArgs: argParts.join(' ')
}
}

function roundToTenth(x: number) {
Expand Down

0 comments on commit 968fce3

Please sign in to comment.