@@ -16,6 +16,7 @@ import { appendFile, writeFile } from 'fs/promises'
16
16
import { gamesConfigPath , isWindows } from 'backend/constants'
17
17
import { gameManagerMap } from 'backend/storeManagers'
18
18
import { existsSync , mkdirSync , openSync } from 'graceful-fs'
19
+ import { Winetricks } from 'backend/tools'
19
20
20
21
export enum LogPrefix {
21
22
General = '' ,
@@ -361,43 +362,71 @@ const logsWriters: Record<string, LogWriter> = {}
361
362
362
363
// Base abstract class for all LogWriters
363
364
class LogWriter {
364
- queue : string [ ]
365
+ queue : ( string | Promise < string | string [ ] > ) [ ]
365
366
initialized : boolean
366
367
filePath : string
367
368
timeoutId : NodeJS . Timeout | undefined
369
+ processing : boolean
368
370
369
371
constructor ( ) {
370
372
this . initialized = false
371
373
this . queue = [ ]
372
374
this . filePath = ''
375
+ this . processing = false
373
376
374
377
if ( new . target === LogWriter ) {
375
378
throw new Error ( 'LogWriter is an abstract class' )
376
379
}
377
380
}
378
381
379
- logMessage ( message : string ) {
382
+ /**
383
+ * Append a message to the queue
384
+ * @param message string or promise that returns a string or string[]
385
+ */
386
+ logMessage ( message : string | Promise < string | string [ ] > ) {
380
387
// push messages to append to the log
381
388
this . queue . push ( message )
382
389
383
- // if the logger is initialized and we don't have a timeout,
384
- // append the message and start a timeout
390
+ // if the logger is initialized and we don't have a timeout
391
+ // and we are not proccesing the previous batch, write a new batch
385
392
//
386
393
// otherwise it means there's a timeout already running that will
387
- // write the elements in the queue in a second
388
- if ( this . initialized && ! this . timeoutId ) this . appendMessages ( )
394
+ // write the elements in the queue in a second or that we are processing
395
+ // promises
396
+ if ( this . initialized && ! this . processing && ! this . timeoutId )
397
+ this . appendMessages ( )
389
398
}
390
399
391
400
async appendMessages ( ) {
392
- const messagesToWrite = this . queue
401
+ const itemsInQueue = this . queue
393
402
394
403
// clear pending message if any
395
404
this . queue = [ ]
396
405
397
406
// clear timeout if any
398
407
delete this . timeoutId
399
408
400
- if ( ! messagesToWrite ?. length ) return
409
+ if ( ! itemsInQueue ?. length ) return
410
+
411
+ this . processing = true
412
+
413
+ // process items in queue, if they are promises we wait
414
+ // for them so we can write them in the right order
415
+ let messagesToWrite : string [ ] = [ ]
416
+ for ( const item of itemsInQueue ) {
417
+ try {
418
+ let result = await item
419
+
420
+ // support promises returning a string or an array of strings
421
+ result = Array . isArray ( result ) ? result : [ result ]
422
+
423
+ messagesToWrite = messagesToWrite . concat ( result )
424
+ } catch ( error ) {
425
+ logError ( error , LogPrefix . Backend )
426
+ }
427
+ }
428
+
429
+ this . processing = false
401
430
402
431
// if we have messages, write them and check again in 1 second
403
432
// we start the timeout before writing so we don't wait until
@@ -510,6 +539,28 @@ export async function initGamePlayLog(gameInfo: GameInfo) {
510
539
return logsWriters [ `${ gameInfo . app_name } -lastPlay` ] . initLog ( )
511
540
}
512
541
542
+ export async function appendWinetricksGamePlayLog ( gameInfo : GameInfo ) {
543
+ const logWriter = logsWriters [ `${ gameInfo . app_name } -lastPlay` ]
544
+ if ( logWriter ) {
545
+ // append a promise to the queue
546
+ logWriter . logMessage (
547
+ new Promise ( ( resolve , reject ) => {
548
+ Winetricks . listInstalled ( gameInfo . runner , gameInfo . app_name )
549
+ . then ( ( installedPackages ) => {
550
+ const packagesString = installedPackages
551
+ ? installedPackages . join ( ', ' )
552
+ : 'none'
553
+
554
+ resolve ( `Winetricks packages: ${ packagesString } \n\n` )
555
+ } )
556
+ . catch ( ( error ) => {
557
+ reject ( error )
558
+ } )
559
+ } )
560
+ )
561
+ }
562
+ }
563
+
513
564
export function stopLogger ( appName : string ) {
514
565
logsWriters [ `${ appName } -lastPlay` ] ?. logMessage (
515
566
'============= End of log ============='
0 commit comments