-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathmain.ts
More file actions
164 lines (145 loc) · 5.5 KB
/
Copy pathmain.ts
File metadata and controls
164 lines (145 loc) · 5.5 KB
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
import { app, ipcMain } from 'electron';
import log from './app/logger';
import {
iRacingSDKSetup,
getCurrentBridge,
getSessionLifecycle,
} from './app/bridge/iracingSdk/setup';
import { getOrCreateDefaultDashboard } from './app/storage/dashboards';
import { setupTaskbar, KeybindingManager } from './app';
import {
publishDashboardUpdates,
dashboardBridge,
} from './app/bridge/dashboard/dashboardBridge';
import { setupPitLaneBridge } from './app/bridge/pitLaneBridge';
import { setupFuelCalculatorBridge } from './app/bridge/fuelCalculatorBridge';
import { OverlayManager } from './app/overlayManager';
import {
startComponentServer,
getComponentServerPort,
} from './app/webserver/componentServer';
import { updateElectronApp } from 'update-electron-app';
// @ts-expect-error no types for squirrel
import started from 'electron-squirrel-startup';
import { Analytics } from './app/analytics';
import { setupKeybindingsBridge } from './app/bridge/keybindingsBridge';
import { setupLogBridge } from './app/bridge/logBridge';
import { setupPersonalBestLapTimesBridge } from './app/bridge/personalBestLapTimesBridge';
import {
validateReferenceLapFile,
flushReferenceLapsOnShutdown,
} from './app/storage/referenceLaps';
import { setupChromiumFlagsBridge } from './app/bridge/chromiumFlagsBridge';
import { createPerfDashboard, getPerfRunConfig } from './app/perfRunConfig';
import { ChannelBus, setupChannelBridge } from './app/bridge/channelBridge';
import { connectSessionLifecycleChannel } from './app/bridge/sessionLifecycleChannel';
import { setupRendererDataSubscriptions } from './app/bridge/rendererDataSubscriptions';
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (started) app.quit();
updateElectronApp();
const overlayManager = new OverlayManager();
const analytics = new Analytics();
const perfRun = getPerfRunConfig();
analytics.setupLogTransport();
overlayManager.setupChromiumFlags();
overlayManager.setupHardwareAcceleration();
overlayManager.setupSingleInstanceLock();
overlayManager.setupAutoStart();
// Hoisted so the quit handler can tear down the WebHID host window cleanly.
let keybindingManager: KeybindingManager | undefined;
const channelBus = new ChannelBus();
let disconnectLifecycleChannel: (() => void) | undefined;
let disposeRendererDataSubscriptions: (() => void) | undefined;
app.on('ready', async () => {
// Don't start services if we don't have the single instance lock
// (this instance should be quitting)
if (!overlayManager.hasLock()) {
return;
}
if (perfRun.enabled) {
log.info('[PerfRun] Configuration', perfRun);
if (perfRun.durationSeconds > 0) {
setTimeout(() => {
log.info(
`[PerfRun] Completed fixed ${perfRun.durationSeconds}s capture`
);
app.quit();
}, perfRun.durationSeconds * 1000);
}
}
setupChannelBridge(channelBus);
const rendererDataSubscriptions = setupRendererDataSubscriptions();
disposeRendererDataSubscriptions = rendererDataSubscriptions.dispose;
overlayManager.setRendererDataSubscriptions(
rendererDataSubscriptions.registry
);
disconnectLifecycleChannel = connectSessionLifecycleChannel(
getSessionLifecycle(),
channelBus
);
await iRacingSDKSetup(overlayManager, channelBus);
// Perform one-time cleanup of old reference laps
validateReferenceLapFile();
const dashboard = getOrCreateDefaultDashboard();
const bridge = getCurrentBridge();
// Setup IPC bridges
setupLogBridge();
setupFuelCalculatorBridge();
setupPitLaneBridge();
setupPersonalBestLapTimesBridge();
setupChromiumFlagsBridge();
// Start component server for browser components
await startComponentServer(bridge, dashboardBridge, channelBus);
ipcMain.handle('getComponentServerPort', () => getComponentServerPort());
const runDashboard = createPerfDashboard(dashboard, perfRun);
if (!perfRun.enabled || perfRun.overlayMode !== 'observer') {
// Empty mode keeps the normal overlay window count/bounds while the
// renderer receives a dashboard with every widget disabled. This isolates
// transparent-window/global-provider cost from widget rendering.
const windowDashboard =
perfRun.enabled && perfRun.overlayMode === 'empty'
? dashboard
: runDashboard;
overlayManager.createOverlays(windowDashboard, {
createSettingsWindow: !perfRun.enabled,
});
}
keybindingManager = new KeybindingManager(overlayManager);
keybindingManager.registerAll();
// Start the WebHID host window that reads game controllers for gamepad bindings.
if (!perfRun.enabled) {
keybindingManager.startGamepad();
}
setupTaskbar(overlayManager, keybindingManager);
await publishDashboardUpdates(
overlayManager,
analytics,
perfRun.enabled
? (updatedDashboard) => createPerfDashboard(updatedDashboard, perfRun)
: undefined
);
setupKeybindingsBridge(keybindingManager);
await analytics.init(overlayManager.getVersion(), dashboard);
});
app.on('window-all-closed', () => {
if (perfRun.enabled && perfRun.overlayMode === 'observer') {
return;
}
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('quit', () => {
log.info('App quit');
analytics.shutdown();
});
app.on('before-quit', () => {
overlayManager.markQuitting();
keybindingManager?.stopGamepad();
disconnectLifecycleChannel?.();
disposeRendererDataSubscriptions?.();
channelBus.dispose();
// Synchronous flush so any pending debounced reference-lap write completes
// before the process exits.
flushReferenceLapsOnShutdown();
});