-
-
Notifications
You must be signed in to change notification settings - Fork 406
/
config.ts
652 lines (583 loc) · 18.6 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
import {
existsSync,
mkdirSync,
readFileSync,
readdirSync,
writeFileSync
} from 'graceful-fs'
import { homedir, userInfo as user } from 'os'
import { parse as plistParse, PlistObject } from 'plist'
import {
AppSettings,
GlobalConfigVersion,
WineInstallation
} from 'common/types'
import { LegendaryUser } from './legendary/user'
import {
currentGlobalConfigVersion,
heroicConfigPath,
heroicDefaultWinePrefix,
heroicGamesConfigPath,
heroicInstallPath,
heroicToolsPath,
userHome,
isFlatpak,
isMac,
isWindows,
getSteamLibraries,
getSteamCompatFolder,
configStore
} from './constants'
import { execAsync } from './utils'
import { execSync } from 'child_process'
import { logError, logInfo, LogPrefix } from './logger/logger'
import { dirname, join } from 'path'
/**
* This class does config handling.
* This can't be constructed directly. Use the static method get().
* It automatically selects the appropriate config loader based on the config version.
*
* It also implements all the config features that won't change across versions.
*/
abstract class GlobalConfig {
protected static globalInstance: GlobalConfig
public abstract version: GlobalConfigVersion
protected config: AppSettings | undefined
public set(config: AppSettings) {
this.config = config
}
/**
* Get the global configuartion handler.
* If one doesn't exist, create one.
*
* @returns GlobalConfig instance.
*/
public static get(): GlobalConfig {
let version: GlobalConfigVersion
// Config file doesn't already exist, make one with the current version.
if (!existsSync(heroicConfigPath)) {
version = currentGlobalConfigVersion
}
// Config file exists, detect its version.
else {
// Check version field in the config.
try {
version = JSON.parse(readFileSync(heroicConfigPath, 'utf-8'))['version']
} catch (error) {
logError(
`Config file is corrupted, please check ${heroicConfigPath}`,
LogPrefix.Backend
)
version = 'v0'
}
// Legacy config file without a version field, it's a v0 config.
if (!version) {
version = 'v0'
}
}
if (!GlobalConfig.globalInstance) {
GlobalConfig.reload(version)
}
return GlobalConfig.globalInstance
}
/**
* Recreate the global configuration handler.
*
* @param version Config version to load file using.
* @returns void
*/
private static reload(version: GlobalConfigVersion): void {
// Select loader to use.
switch (version) {
case 'v0':
GlobalConfig.globalInstance = new GlobalConfigV0()
break
default:
logError(
`Invalid config version '${version}' requested.`,
LogPrefix.GlobalConfig
)
break
}
// Try to upgrade outdated config.
if (GlobalConfig.globalInstance.upgrade()) {
// Upgrade done, we need to fully reload config.
logInfo(
`Upgraded outdated ${version} config to ${currentGlobalConfigVersion}.`,
LogPrefix.GlobalConfig
)
return GlobalConfig.reload(currentGlobalConfigVersion)
} else if (version !== currentGlobalConfigVersion) {
// Upgrade failed.
logError(
`Failed to upgrade outdated ${version} config.`,
LogPrefix.GlobalConfig
)
}
}
/**
* Loads the default wine installation path and version.
*
* @returns Promise<WineInstallation>
*/
public getDefaultWine(): WineInstallation {
const defaultWine: WineInstallation = {
bin: '',
name: 'Default Wine - Not Found',
type: 'wine'
}
try {
let stdout = execSync(`which wine`).toString()
const wineBin = stdout.split('\n')[0]
defaultWine.bin = wineBin
stdout = execSync(`wine --version`).toString()
const version = stdout.split('\n')[0]
defaultWine.name = `Wine Default - ${version}`
return {
...defaultWine,
...this.getWineExecs(wineBin)
}
} catch {
return defaultWine
}
}
/**
* Detects Wine installed on home application folder on Mac
*
* @returns Promise<Set<WineInstallation>>
*/
public async getWineOnMac(): Promise<Set<WineInstallation>> {
const wineSet = new Set<WineInstallation>()
if (!isMac) {
return wineSet
}
const winePaths = new Set<string>()
// search for wine installed on $HOME/Library/Application Support/heroic/tools/wine
const wineToolsPath = `${heroicToolsPath}/wine/`
if (existsSync(wineToolsPath)) {
readdirSync(wineToolsPath).forEach((path) => {
winePaths.add(join(wineToolsPath, path))
})
}
// search for wine installed around the system
await execAsync('mdfind kMDItemCFBundleIdentifier = "*.wine"').then(
async ({ stdout }) => {
stdout.split('\n').forEach((winePath) => {
winePaths.add(winePath)
})
}
)
winePaths.forEach((winePath) => {
const infoFilePath = join(winePath, 'Contents/Info.plist')
if (winePath && existsSync(infoFilePath)) {
const info = plistParse(
readFileSync(infoFilePath, 'utf-8')
) as PlistObject
const version = info['CFBundleShortVersionString'] || ''
const name = info['CFBundleName'] || ''
const wineBin = join(winePath, '/Contents/Resources/wine/bin/wine64')
if (existsSync(wineBin)) {
wineSet.add({
...this.getWineExecs(wineBin),
lib: `${winePath}/Contents/Resources/wine/lib`,
lib32: `${winePath}/Contents/Resources/wine/lib`,
bin: wineBin,
name: `${name} - ${version}`,
type: 'wine',
...this.getWineExecs(wineBin)
})
}
}
})
return wineSet
}
public async getWineskinWine(): Promise<Set<WineInstallation>> {
const wineSet = new Set<WineInstallation>()
if (!isMac) {
return wineSet
}
const wineSkinPath = `${userHome}/Applications/Wineskin`
if (existsSync(wineSkinPath)) {
const apps = readdirSync(wineSkinPath)
for (const app of apps) {
if (app.includes('.app')) {
const wineBin = `${userHome}/Applications/Wineskin/${app}/Contents/SharedSupport/wine/bin/wine64`
if (existsSync(wineBin)) {
try {
const { stdout: out } = await execAsync(`'${wineBin}' --version`)
const version = out.split('\n')[0]
wineSet.add({
...this.getWineExecs(wineBin),
lib: `${userHome}/Applications/Wineskin/${app}/Contents/SharedSupport/wine/lib`,
lib32: `${userHome}/Applications/Wineskin/${app}/Contents/SharedSupport/wine/lib`,
name: `Wineskin - ${version}`,
type: 'wine',
bin: wineBin
})
} catch (error) {
logError(
`Error getting wine version for ${wineBin}`,
LogPrefix.GlobalConfig
)
}
}
}
}
}
return wineSet
}
/**
* Detects CrossOver installs on Mac
*
* @returns Promise<Set<WineInstallation>>
*/
public async getCrossover(): Promise<Set<WineInstallation>> {
const crossover = new Set<WineInstallation>()
if (!isMac) {
return crossover
}
await execAsync(
'mdfind kMDItemCFBundleIdentifier = "com.codeweavers.CrossOver"'
).then(async ({ stdout }) => {
stdout.split('\n').forEach((crossoverMacPath) => {
const infoFilePath = join(crossoverMacPath, 'Contents/Info.plist')
if (crossoverMacPath && existsSync(infoFilePath)) {
const info = plistParse(
readFileSync(infoFilePath, 'utf-8')
) as PlistObject
const version = info['CFBundleShortVersionString'] || ''
const crossoverWineBin = join(
crossoverMacPath,
'Contents/SharedSupport/CrossOver/bin/wine'
)
crossover.add({
bin: crossoverWineBin,
name: `CrossOver - ${version}`,
type: 'crossover',
...this.getWineExecs(crossoverWineBin)
})
}
})
})
return crossover
}
public async getMacOsWineSet(): Promise<Set<WineInstallation>> {
if (!isMac) {
return new Set<WineInstallation>()
}
const crossover = await this.getCrossover()
const wineOnMac = await this.getWineOnMac()
const wineskinWine = await this.getWineskinWine()
return new Set([...crossover, ...wineOnMac, ...wineskinWine])
}
/**
* Detects Wine/Proton on the user's system.
*
* @returns An Array of Wine/Proton installations.
*/
public async getAlternativeWine(
scanCustom = true
): Promise<WineInstallation[]> {
if (isMac) {
const macOsWineSet = await this.getMacOsWineSet()
return [...macOsWineSet]
}
if (!existsSync(`${heroicToolsPath}/wine`)) {
mkdirSync(`${heroicToolsPath}/wine`, { recursive: true })
}
if (!existsSync(`${heroicToolsPath}/proton`)) {
mkdirSync(`${heroicToolsPath}/proton`, { recursive: true })
}
const altWine = new Set<WineInstallation>()
readdirSync(`${heroicToolsPath}/wine/`).forEach((version) => {
const wineBin = join(heroicToolsPath, 'wine', version, 'bin', 'wine')
altWine.add({
bin: wineBin,
name: `Wine - ${version}`,
type: 'wine',
...this.getWineLibs(wineBin),
...this.getWineExecs(wineBin)
})
})
const lutrisPath = `${homedir()}/.local/share/lutris`
const lutrisCompatPath = `${lutrisPath}/runners/wine/`
if (existsSync(lutrisCompatPath)) {
readdirSync(lutrisCompatPath).forEach((version) => {
const wineBin = join(lutrisCompatPath, version, 'bin', 'wine')
altWine.add({
bin: wineBin,
name: `Wine - ${version}`,
type: 'wine',
...this.getWineLibs(wineBin),
...this.getWineExecs(wineBin)
})
})
}
const protonPaths = [`${heroicToolsPath}/proton/`]
await getSteamLibraries().then((libs) => {
libs.forEach((path) => {
protonPaths.push(`${path}/steam/steamapps/common`)
protonPaths.push(`${path}/steamapps/common`)
protonPaths.push(`${path}/root/compatibilitytools.d`)
protonPaths.push(`${path}/compatibilitytools.d`)
return
})
})
const proton = new Set<WineInstallation>()
protonPaths.forEach((path) => {
if (existsSync(path)) {
readdirSync(path).forEach((version) => {
const protonBin = join(path, version, 'proton')
// check if bin exists to avoid false positives
if (existsSync(protonBin)) {
proton.add({
bin: protonBin,
name: `Proton - ${version}`,
type: 'proton'
// No need to run this.getWineExecs here since Proton ships neither Wineboot nor Wineserver
})
}
})
}
})
const defaultWineSet = new Set<WineInstallation>()
const defaultWine = await this.getDefaultWine()
if (!defaultWine.name.includes('Not Found')) {
defaultWineSet.add(defaultWine)
}
let customWineSet = new Set<WineInstallation>()
if (scanCustom) {
customWineSet = this.getCustomWinePaths()
}
return [...defaultWineSet, ...altWine, ...proton, ...customWineSet]
}
/**
* Checks if a Wine version has Wineboot/Wineserver executables and returns the path to those if they're present
* @param wineBin The unquoted path to the Wine binary ('wine')
* @returns The quoted paths to wineboot and wineserver, if present
*/
public getWineExecs(wineBin: string): {
wineboot: string
wineserver: string
} {
const wineDir = dirname(wineBin)
const ret = { wineserver: '', wineboot: '' }
const potWineserverPath = join(wineDir, 'wineserver')
if (existsSync(potWineserverPath)) {
ret.wineserver = potWineserverPath
}
const potWinebootPath = join(wineDir, 'wineboot')
if (existsSync(potWinebootPath)) {
ret.wineboot = potWinebootPath
}
return ret
}
/**
* Checks if a Wine version has lib/lib32 folders and returns the path to those if they're present
* @param wineBin The unquoted path to the Wine binary ('wine')
* @returns The paths to lib and lib32, if present
*/
public getWineLibs(wineBin: string): {
lib: string
lib32: string
} {
const wineDir = dirname(wineBin)
const ret = { lib: '', lib32: '' }
const potLib32Path = join(wineDir, '../lib')
if (existsSync(potLib32Path)) {
ret.lib32 = potLib32Path
}
const potLibPath = join(wineDir, '../lib64')
if (existsSync(potLibPath)) {
ret.lib = potLibPath
}
return ret
}
/**
* Gets the actual settings from the config file.
* Does not modify its parent object.
* Always reads from file regardless of `this.config`.
*
* @returns Settings present in config file.
*/
public abstract getSettings(): AppSettings
/**
* Updates this.config, this.version to upgrade the current config file.
*
* Writes to file after that.
* DO NOT call `flush()` afterward.
*
* @returns true if upgrade successful if upgrade fails or no upgrade needed.
*/
public abstract upgrade(): boolean
/**
* Get custom Wine installations as defined in the config file.
*
* @returns Set of Wine installations.
*/
public abstract getCustomWinePaths(): Set<WineInstallation>
/**
* Get default settings as if the user's config file doesn't exist.
* Doesn't modify the parent object.
* Doesn't access config files.
*
* @returns AppSettings
*/
public abstract getFactoryDefaults(): AppSettings
/**
* Reset `this.config` to `getFactoryDefaults()` and flush.
*/
public abstract resetToDefaults(): void
protected writeToFile(config: Record<string, unknown>) {
return writeFileSync(heroicConfigPath, JSON.stringify(config, null, 2))
}
/**
* Write `this.config` to file.
* Uses the config version defined in `this.version`.
*/
public abstract flush(): void
/** change a specific setting */
public abstract setSetting(key: string, value: unknown): void
/**
* Load the config file, upgrade if needed.
*/
protected load() {
// Config file doesn't exist, make one.
if (!existsSync(heroicConfigPath)) {
this.resetToDefaults()
}
// Always upgrade before loading to avoid errors.
// `getSettings` doesn't return an `AppSettings` otherwise.
if (this.version !== currentGlobalConfigVersion) {
// Do not load the config.
// Wait for `upgrade` to be called by `reload`.
} else {
// No upgrades necessary, load config.
// `this.version` should be `currentGlobalConfigVersion` at this point.
this.config = this.getSettings()
}
}
}
class GlobalConfigV0 extends GlobalConfig {
public version: GlobalConfigVersion = 'v0'
constructor() {
super()
this.load()
}
public upgrade() {
// Here we rewrite the config object to match the latest format and write to file.
// Not necessary as this is the current version.
return false
}
public getSettings(): AppSettings {
if (this.config) {
return this.config
}
if (!existsSync(heroicGamesConfigPath)) {
mkdirSync(heroicGamesConfigPath, { recursive: true })
}
if (!existsSync(heroicConfigPath)) {
return this.getFactoryDefaults()
}
let settings = JSON.parse(readFileSync(heroicConfigPath, 'utf-8'))
const defaultSettings = settings.defaultSettings as AppSettings
// fix relative paths
const winePrefix = !isWindows
? defaultSettings?.winePrefix?.replace('~', userHome)
: ''
settings = {
...this.getFactoryDefaults(),
...settings.defaultSettings,
winePrefix
} as AppSettings
// TODO: Remove this after a couple of stable releases
// Get settings only from config-store
const currentConfigStore = configStore.get_nodefault('settings')
if (!currentConfigStore?.defaultInstallPath) {
configStore.set('settings', settings)
}
return settings
}
public getCustomWinePaths(): Set<WineInstallation> {
const customPaths = new Set<WineInstallation>()
// skips this on new installations to avoid infinite loops
if (existsSync(heroicConfigPath)) {
const { customWinePaths = [] } = this.getSettings()
customWinePaths.forEach((path: string) => {
if (path.endsWith('proton')) {
return customPaths.add({
bin: path,
name: `Custom Proton - ${path}`,
type: 'proton'
})
}
return customPaths.add({
bin: path,
name: `Custom Wine - ${path}`,
type: 'wine',
...this.getWineExecs(path)
})
})
}
return customPaths
}
public getFactoryDefaults(): AppSettings {
const account_id = LegendaryUser.getUserInfo()?.account_id
const userName = user().username
const defaultWine = isWindows ? {} : this.getDefaultWine()
// @ts-expect-error TODO: We need to settle on *one* place to define settings defaults
return {
checkUpdatesInterval: 10,
enableUpdates: false,
addDesktopShortcuts: false,
addStartMenuShortcuts: false,
autoInstallDxvk: true,
autoInstallVkd3d: true,
addSteamShortcuts: false,
preferSystemLibs: false,
checkForUpdatesOnStartup: !isFlatpak,
autoUpdateGames: false,
customWinePaths: isWindows ? null : [],
defaultInstallPath: heroicInstallPath,
libraryTopSection: 'disabled',
defaultSteamPath: getSteamCompatFolder(),
defaultWinePrefix: heroicDefaultWinePrefix,
hideChangelogsOnStartup: false,
language: 'en',
maxWorkers: 0,
minimizeOnLaunch: false,
nvidiaPrime: false,
enviromentOptions: [],
wrapperOptions: [],
showFps: false,
useGameMode: false,
userInfo: {
epicId: account_id,
name: userName
},
wineCrossoverBottle: 'Heroic',
winePrefix: isWindows ? defaultWine : `${userHome}/.wine`,
wineVersion: isWindows ? {} : defaultWine
} as AppSettings
}
public setSetting(key: string, value: unknown) {
const config = this.getSettings()
const configStoreSettings = configStore.get_nodefault('settings') || config
configStore.set('settings', { ...configStoreSettings, [key]: value })
config[key] = value
this.config = config
logInfo(`Heroic: Setting ${key} to ${JSON.stringify(value)}`)
return this.flush()
}
public resetToDefaults() {
this.config = this.getFactoryDefaults()
return this.flush()
}
public flush() {
return this.writeToFile({
defaultSettings: this.config,
version: 'v0'
})
}
}
export { GlobalConfig }