import { initializeDynamicSettings } from '@/logic/dynamicSettings' ; import { initializeHooks } from '@/logic/hooks' ; import { initializeNavigation } from '@/logic/navigation/navigation' ; import { load } from '@/logic/storage' ; import UnloadingEvent from '@/logic/unloadingEvent' ; import { OWGameListener , OWGames , OWWindow } from '@overwolf/overwolf-api-ts' ; import { initializeHotkeyManager } from '../../logic/hotkeyManager' ; import { initializeTileCache } from '../../logic/tileCache' ; import { initializeTileMarkerCache } from '../../logic/tileMarkerCache' ; import { BackgroundWindow , ConcreteWindow , newWorldId , windowNames } from '../consts' ; import RunningGameInfo = overwolf . games . RunningGameInfo ; import WindowState = overwolf . windows . WindowStateEx ; export type BackgroundControllerWindow = typeof window & { backgroundController : BackgroundController ; } ; type GameRunningEventListener = ( gameRunning : boolean ) => void ; const bringToFrontWindowStates = [ WindowState . MAXIMIZED , WindowState . NORMAL ] ; export class BackgroundController { private static _instance : BackgroundController ; private _windows : Record < BackgroundWindow | ConcreteWindow , OWWindow > ; private _openWindows = new Set < ConcreteWindow > ( ) ; private _NewWorldGameListener : OWGameListener ; private _gameRunning = false ; private _gameRunningEvent = new UnloadingEvent < GameRunningEventListener > ( 'gameRunning' ) ; private constructor ( ) { this . _windows = { background : new OWWindow ( windowNames . background ) , desktop : new OWWindow ( windowNames . desktop ) , inGame : new OWWindow ( windowNames . inGame ) , } ; this . _NewWorldGameListener = new OWGameListener ( { onGameStarted : this . onGameStarted , onGameEnded : this . onGameEnded , } ) ; if ( ! NWMM_APP_BUILD_PRODUCTION ) { this . debug_setGameRunning = ( running ) => { this . _gameRunning = running ; this . _gameRunningEvent . fire ( running ) ; } ; } } public static get isSupported ( ) { return NWMM_APP_WINDOW === 'background' ; } public static get instance ( ) : BackgroundController { if ( ! this . isSupported ) { throw new Error ( 'Using BackgroundController directly in this window is not supported.' ) ; } if ( ! BackgroundController . _instance ) { BackgroundController . _instance = new BackgroundController ( ) ; } return BackgroundController . _instance ; } public get gameRunning ( ) { return this . _gameRunning ; } public run = async ( ) => { this . _NewWorldGameListener . start ( ) ; const running = await this . isNewWorldRunning ( ) ; const alwaysLaunchDesktop = load ( 'alwaysLaunchDesktop' ) ; if ( ! running ) { await this . openWindow ( 'desktop' ) ; } else { if ( alwaysLaunchDesktop ) { await this . openWindow ( 'desktop' ) ; } await this . openWindow ( 'inGame' ) ; } } ; public async openWindow ( window ) { if ( this . _openWindows . has ( window ) ) { const windowState = await this . _windows [ window ] . getWindowState ( ) ; if ( windowState . window_state && bringToFrontWindowStates . includes ( windowState . window_state ) ) { overwolf . windows . obtainDeclaredWindow ( window , wnd => { if ( wnd . success ) { overwolf . windows . bringToFront ( wnd . window . id , ( ) => { } ) ; } else { this . _windows [ window ] . restore ( ) ; } } ) ; } else { this . _windows [ window ] . restore ( ) ; } } else { this . _windows [ window ] . restore ( ) ; } this . _openWindows . add ( window ) ; } public closeWindow ( window ) { this . _openWindows . delete ( window ) ; this . _windows [ window ] . close ( ) ; this . exitIfNoWindowsOpen ( ) ; } public listenOnGameRunningChange = this . _gameRunningEvent . register ; public debug_setGameRunning = ( running ) => { console . log ( `Attempted to set gameRunning to ${ running }, but this was a no-op.` ) ; } ; private onGameStarted = async ( info ) => { if ( ! info || ! this . isGameNewWorld ( info ) ) { return ; } this . _gameRunning = true ; if ( load ( 'autoLaunchInGame' ) ) { this . openWindow ( 'inGame' ) ; } this . _gameRunningEvent . fire ( true ) ; } ; private onGameEnded = async ( info ) => { if ( ! info || ! this . isGameNewWorld ( info ) ) { return ; } this . _gameRunning = false ; this . closeWindow ( 'inGame' ) ; this . _gameRunningEvent . fire ( false ) ; } ; private async isNewWorldRunning ( ) { const info = await OWGames . getRunningGameInfo ( ) ; return info && info . isRunning && this . isGameNewWorld ( info ) ; } private isGameNewWorld = ( info ) => { return info . classId === newWorldId ; } ; private exitIfNoWindowsOpen = ( ) => { if ( this . _openWindows . size === 0 ) { this . _windows . background . close ( ) ; } } ; } export function getBackgroundController ( ) { return ( overwolf . windows . getMainWindow ( ) . window as BackgroundControllerWindow ) . backgroundController ; } if ( BackgroundController . isSupported ) { BackgroundController . instance . run ( ) ; ( window as BackgroundControllerWindow ) . backgroundController = BackgroundController . instance ; } initializeDynamicSettings ( ) ; initializeHooks ( ) ; initializeTileCache ( ) ; initializeTileMarkerCache ( ) ; initializeHotkeyManager ( ) ; initializeNavigation ( ) ;