import { Injectable } from '@angular/core' ; import { OverwolfService } from '../overwolf.service' ; const HEARTHSTONE_GAME_ID = 9898 ; declare let overwolf ; @ Injectable ( ) export class TemporaryResolutionOverrideService { private readonly RESOLUTION_ENUM = { 0 : overwolf . settings . enums . ResolutionSettings . Original , 1 : overwolf . settings . enums . ResolutionSettings . R1080p , 2 : overwolf . settings . enums . ResolutionSettings . R720p , 3 : overwolf . settings . enums . ResolutionSettings . R480p , } ; private listenerRegistered = false ; private restoring = false ; private oldSettings ; constructor ( private owService ) { overwolf . games . onGameInfoUpdated . addListener ( ( res ) => { if ( this . exitGame ( res ) ) { this . restoreSettings ( ) ; } else if ( this . gameLaunched ( res ) ) { this . overrideVideoSettings ( ) ; } } ) ; overwolf . games . getRunningGameInfo ( ( res ) => { if ( res && res . isRunning && res . id && Math . floor ( res . id / 10 ) === HEARTHSTONE_GAME_ID ) { this . overrideVideoSettings ( ) ; } } ) ; } private async overrideVideoSettings ( ) { if ( ! this . listenerRegistered && ! this . restoring ) { overwolf . settings . OnVideoCaptureSettingsChanged . addListener ( ( ) => this . overrideVideoSettings ( ) ) ; this . listenerRegistered = true ; } const settings = await this . owService . getVideoCaptureSettings ( ) ; if ( ! this . oldSettings ) { this . oldSettings = settings ; } if ( settings . resolution <= 1 ) { this . owService . setVideoCaptureSettings ( this . RESOLUTION_ENUM [ 2 ] , 60 ) ; } } private restoreSettings ( ) { if ( ! this . oldSettings ) { return ; } this . restoring = true ; this . owService . setVideoCaptureSettings ( this . RESOLUTION_ENUM [ this . oldSettings . resolution ] , this . oldSettings . fps ) ; } private exitGame ( gameInfoResult ) { return ! gameInfoResult || ! gameInfoResult . gameInfo || ! gameInfoResult . gameInfo . isRunning ; } private gameLaunched ( gameInfoResult ) { if ( ! gameInfoResult || ! gameInfoResult . gameInfo || ! gameInfoResult . gameInfo . isRunning ) { return false ; } if ( Math . floor ( gameInfoResult . gameInfo . id / 10 ) !== HEARTHSTONE_GAME_ID ) { return false ; } return gameInfoResult . runningChanged || gameInfoResult . gameChanged ; } }