Skip to content

Commit

Permalink
Merge pull request #11 from rayshader/feat/detect-exe
Browse files Browse the repository at this point in the history
feat(Launcher): autodetect game path
  • Loading branch information
maximegmd authored Nov 24, 2024
2 parents c9de190 + f69c745 commit f0636c6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 13 deletions.
1 change: 1 addition & 0 deletions code/launcher/electron/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const createWindow = () => {
height: 850,
frame: false,
webPreferences: {
webSecurity: process.env.ELECTRON_DEV !== '1',
nodeIntegration: true,
preload: (process.env.ELECTRON_DEV === '1') ? path.join(__dirname, 'preload.js') : path.join(process.cwd(), 'resources/preload.js')
}
Expand Down
80 changes: 67 additions & 13 deletions code/launcher/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ServerList from './components/servers/ServerList.tsx'
import { Box, Button, createTheme, CssBaseline, ThemeProvider } from '@mui/material'
import { Alert, Box, Button, createTheme, CssBaseline, Snackbar, ThemeProvider } from '@mui/material'
import WindowFrame from './components/WindowFrame.tsx'
import { useEffect, useState } from 'react'
import { FindInPage } from '@mui/icons-material'
Expand Down Expand Up @@ -38,7 +38,9 @@ const darkTheme = createTheme({

export default function App () {
const [_selectedFilePath, setSelectedFilePath] = useState<string | null>(null)
const [_error, setError] = useState<string>('')
const [error, setError] = useState<string | null>(null)
const [warning, setWarning] = useState<string | null>(null)
const [success, setSuccess] = useState<string | null>(null)

const handleSelectFile = async () => {
try {
Expand All @@ -49,26 +51,40 @@ export default function App () {

const filePath = result.filePaths[0]

if (filePath.endsWith('Cyberpunk2077.exe')) {
setSelectedFilePath(filePath)
setError('')
window.electronAPI.setGamePath(filePath)
} else {
setError('Please select a file named "Cyberpunk2077.exe"')
if (!filePath.endsWith('Cyberpunk2077.exe')) {
setError('You must select the file "Cyberpunk2077.exe".')
return
}
setSelectedFilePath(filePath)
setSuccess('Game file detected, you\'re ready.')
window.electronAPI.setGamePath(filePath)
} catch (err) {
console.error('Error selecting file:', err)
setError('Exception: "' + err + '"')
setError(`Error selecting file: ${JSON.stringify(err)}`)
}
}

useEffect(() => {
// Load the saved file path from electron-store
window.electronAPI.getGamePath().then((storedPath: any) => {
if (storedPath && window.electronAPI.pathExists(storedPath)) {
setSelectedFilePath(storedPath)
window.electronAPI.getGamePath().then((gamePath?: string) => {
if (gamePath && window.electronAPI.pathExists(gamePath)) {
setSelectedFilePath(gamePath)
return
}
const knownPaths: string[] = [
'C:\\Program Files (x86)\\Steam\\steamapps\\common\\Cyberpunk 2077\\bin\\x64\\Cyberpunk2077.exe',
'C:\\Program Files (x86)\\GOG Galaxy\\Games\\Cyberpunk 2077\\bin\\x64\\Cyberpunk2077.exe',
'C:\\Program Files\\Epic Games\\Cyberpunk 2077\\bin\\x64\\Cyberpunk2077.exe'
]

gamePath = knownPaths.find((path) => window.electronAPI.pathExists(path))
if (gamePath) {
window.electronAPI.setGamePath(gamePath)
setSelectedFilePath(gamePath)
setSuccess('Game file auto-detected, you\'re ready.')
} else {
handleSelectFile()
setWarning('Could not auto-detect where your game is installed.')
setTimeout(() => handleSelectFile(), 4000)
}
})
}, [])
Expand All @@ -93,6 +109,44 @@ export default function App () {

{ /* <Owadview style={{ width: '100%', height: '120px' }}/> */ }
</Box>

<Snackbar open={error != null}
onClose={() => {
setError(null)
}}
autoHideDuration={4000}>
<Alert
severity="error"
variant="outlined"
sx={{ width: '100%' }}
>
{ error }
</Alert>
</Snackbar>

<Snackbar open={warning != null}
onClose={() => setWarning(null)}
autoHideDuration={4000}>
<Alert
severity="warning"
variant="outlined"
sx={{ width: '100%' }}
>
{ warning }
</Alert>
</Snackbar>

<Snackbar open={success != null}
onClose={() => setSuccess(null)}
autoHideDuration={4000}>
<Alert
severity="success"
variant="outlined"
sx={{ width: '100%' }}
>
{ success }
</Alert>
</Snackbar>
</ThemeProvider>
)
}

0 comments on commit f0636c6

Please sign in to comment.