1+ const { spawn, exec } = require ( "child_process" ) ;
2+ const promisify = require ( "util" ) . promisify ;
3+ const fs = require ( 'fs' )
4+ const { homedir} = require ( 'os' )
5+ const execAsync = promisify ( exec ) ;
6+ const { fixPathForAsarUnpack } = require ( "electron-util" ) ;
7+ const path = require ( 'path' )
8+
9+ const home = homedir ( ) ;
10+ const legendaryConfigPath = `${ home } /.config/legendary` ;
11+ const heroicFolder = `${ home } /.config/heroic/` ;
12+ const heroicConfigPath = `${ heroicFolder } config.json` ;
13+ const heroicGamesConfigPath = `${ heroicFolder } GamesConfig/`
14+ const userInfo = `${ legendaryConfigPath } /user.json` ;
15+ const heroicInstallPath = `${ home } /Games/Heroic` ;
16+ const legendaryBin = fixPathForAsarUnpack ( path . join ( __dirname , "/bin/legendary" ) ) ;
17+ const icon = fixPathForAsarUnpack ( path . join ( __dirname , "/icon.png" ) ) ;
18+ const loginUrl = "https://www.epicgames.com/id/login?redirectUrl=https%3A%2F%2Fwww.epicgames.com%2Fid%2Fapi%2Fredirect" ;
19+
20+ // check other wine versions installed
21+ const getAlternativeWine = ( ) => {
22+ // TODO: Get all Proton versions
23+ const steamPath = `${ home } /.steam/` ;
24+ const steamInstallFolder = `${ steamPath } root/steamapps/common/`
25+ const steamCompatPath = `${ steamPath } root/compatibilitytools.d/` ;
26+ const lutrisPath = `${ home } /.local/share/lutris` ;
27+ const lutrisCompatPath = `${ lutrisPath } /runners/wine/` ;
28+ let steamWine = [ ] ;
29+ let lutrisWine = [ ] ;
30+
31+ const defaultWine = { name : 'Wine Default' , bin : '/usr/bin/wine' }
32+
33+ if ( fs . existsSync ( steamCompatPath ) ) {
34+ steamWine = fs . readdirSync ( steamCompatPath ) . map ( ( version ) => {
35+ return {
36+ name : `Steam - ${ version } ` ,
37+ bin : `'${ steamCompatPath } ${ version } /dist/bin/wine64'` ,
38+ } ;
39+ } ) ;
40+ }
41+
42+
43+ if ( fs . existsSync ( steamInstallFolder ) ) {
44+ fs . readdirSync ( steamInstallFolder ) . forEach ( ( version ) => {
45+ if ( version . startsWith ( 'Proton' ) ) {
46+ steamWine . push ( {
47+ name : `Steam - ${ version } ` ,
48+ bin : `'${ steamInstallFolder } ${ version } /dist/bin/wine64'` ,
49+ } ) ;
50+ }
51+ } ) ;
52+ }
53+
54+ if ( fs . existsSync ( lutrisCompatPath ) ) {
55+ lutrisWine = fs . readdirSync ( lutrisCompatPath ) . map ( ( version ) => {
56+ return {
57+ name : `Lutris - ${ version } ` ,
58+ bin : `'${ lutrisCompatPath } ${ version } /bin/wine64'` ,
59+ } ;
60+ } ) ;
61+ }
62+
63+ return [ ...steamWine , ...lutrisWine , defaultWine ] ;
64+ } ;
65+
66+ const isLoggedIn = ( ) => fs . readFileSync ( userInfo ) ;
67+
68+ const launchGame = async ( appName ) => {
69+ let envVars = ""
70+ let altWine
71+ let altWinePrefix
72+ const gameConfig = `${ heroicGamesConfigPath } ${ appName } .json`
73+ const globalConfig = heroicConfigPath
74+ let settingsPath = gameConfig
75+ let settingsName = appName
76+
77+ if ( ! fs . existsSync ( gameConfig ) ) {
78+ settingsPath = globalConfig
79+ settingsName = 'defaultSettings'
80+ }
81+
82+
83+ const settings = JSON . parse ( fs . readFileSync ( settingsPath ) )
84+ const { winePrefix, wineVersion, otherOptions } = settings [ settingsName ]
85+
86+ envVars = otherOptions
87+
88+ if ( winePrefix !== "~/.wine" ) {
89+ altWinePrefix = `${ winePrefix } `
90+ }
91+
92+ if ( wineVersion . name !== "Wine Default" ) {
93+ altWine = wineVersion . bin
94+ }
95+
96+ const wine = altWine ? `--wine ${ altWine } ` : ""
97+ const prefix = altWinePrefix ? `--wine-prefix ${ altWinePrefix } ` : ""
98+ const command = `${ envVars } ${ legendaryBin } launch ${ appName } ${ wine } ${ prefix } `
99+
100+ return await execAsync ( command )
101+ . then ( ( { stderr } ) => fs . writeFile ( `${ heroicGamesConfigPath } ${ appName } -lastPlay.log` , stderr , ( ) => 'done' ) )
102+ }
103+
104+ const writeDefaultconfig = ( ) => {
105+ const config = {
106+ defaultSettings : {
107+ defaultInstallPath : heroicInstallPath ,
108+ wineVersion : {
109+ name : "Wine Default" ,
110+ bin : "/usr/bin/wine"
111+ } ,
112+ winePrefix : "~/.wine" ,
113+ otherOptions : ""
114+ }
115+ }
116+ if ( ! fs . existsSync ( heroicConfigPath ) ) {
117+ fs . writeFile ( heroicConfigPath , JSON . stringify ( config , null , 2 ) , ( ) => {
118+ return "done" ;
119+ } ) ;
120+ }
121+
122+ if ( ! fs . existsSync ( heroicGamesConfigPath ) ) {
123+ fs . mkdir ( heroicGamesConfigPath , ( ) => {
124+ return "done" ;
125+ } )
126+ }
127+ }
128+
129+ const writeGameconfig = async ( game ) => {
130+ const { wineVersion, winePrefix, otherOptions } = JSON . parse ( fs . readFileSync ( heroicConfigPath ) ) . defaultSettings
131+
132+ const config = {
133+ [ game ] : {
134+ wineVersion,
135+ winePrefix,
136+ otherOptions
137+ }
138+ }
139+
140+ if ( ! fs . existsSync ( `${ heroicGamesConfigPath } ${ game } .json` ) ) {
141+ await Promise . resolve ( fs . writeFileSync ( `${ heroicGamesConfigPath } ${ game } .json` , JSON . stringify ( config , null , 2 ) , ( ) => {
142+ return "done" ;
143+ } ) ) ;
144+ }
145+ }
146+
147+
148+ module . exports = {
149+ getAlternativeWine,
150+ isLoggedIn,
151+ launchGame,
152+ writeDefaultconfig,
153+ writeGameconfig,
154+ userInfo,
155+ heroicConfigPath,
156+ heroicFolder,
157+ heroicGamesConfigPath,
158+ legendaryConfigPath,
159+ legendaryBin,
160+ icon,
161+ loginUrl
162+ }
0 commit comments