-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreload.js
More file actions
407 lines (369 loc) · 20.5 KB
/
Copy pathpreload.js
File metadata and controls
407 lines (369 loc) · 20.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
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
const { contextBridge, ipcRenderer } = require('electron')
let pluginRendererAPI = null
let pluginRendererLoadError = null
try {
; ({ pluginRendererAPI } = require('./plugins/renderer'))
} catch (e) {
// 关键:不要因为插件渲染 API 的加载失败导致整个 preload 中断
pluginRendererAPI = null
pluginRendererLoadError = e
try {
console.error('[Preload] 加载 ./plugins/renderer 失败:', e?.message || e)
} catch {
// ignore
}
}
// 兜底:即使 ./plugins/renderer 加载失败,也提供最低限度的 window.plugins
// 这样插件管理页(pages/plugins.html)不会因为 window.plugins 不存在而直接不可用。
if (!pluginRendererAPI) {
pluginRendererAPI = {
openPluginManager: () => ipcRenderer.invoke('open-plugin-manager'),
getAllPlugins: () => ipcRenderer.invoke('plugins:get-all'),
getPluginDetail: (pluginId) => ipcRenderer.invoke('plugins:get-detail', pluginId),
enablePlugin: (pluginId) => ipcRenderer.invoke('plugins:enable', pluginId),
disablePlugin: (pluginId) => ipcRenderer.invoke('plugins:disable', pluginId),
reloadPlugin: (pluginId) => ipcRenderer.invoke('plugins:reload', pluginId),
getPluginSettings: (pluginId) => ipcRenderer.invoke('plugins:get-settings', pluginId),
updatePluginSettings: (pluginId, settings) => ipcRenderer.invoke('plugins:update-settings', pluginId, settings),
getCommands: () => ipcRenderer.invoke('plugins:get-commands'),
executeCommand: (commandId, ...args) => ipcRenderer.invoke('plugins:execute-command', commandId, ...args),
// 组件 API
getPluginCards: () => ipcRenderer.invoke('plugin:get-cards'),
getPluginPages: () => ipcRenderer.invoke('plugin:get-pages'),
getPluginMenuItems: () => ipcRenderer.invoke('plugin:get-plugin-menu-items'),
cardAction: (cardId, actionId, ...args) => ipcRenderer.invoke('plugin:card-action', cardId, actionId, ...args),
onNotification: (callback) => {
ipcRenderer.on('plugin-notification', (event, data) => callback(data))
},
onEvent: (callback) => {
ipcRenderer.on('eventbus:event', (event, eventName, ...args) => callback(eventName, ...args))
},
emitEvent: (eventName, ...args) => {
ipcRenderer.send('eventbus:emit', eventName, ...args)
},
subscribeEvent: (eventName) => ipcRenderer.invoke('eventbus:subscribe', eventName),
getStatusBarItems: () => ipcRenderer.invoke('plugin:get-statusbar-items'),
onStatusBarUpdate: (callback) => {
ipcRenderer.on('plugin:statusbar-update', (event, item) => callback(item))
},
// 组件变化事件监听
onComponentsChanged: (callback) => {
ipcRenderer.on('plugin:components-changed', () => callback())
},
// 前台组件 API
getFrontendWidgets: () => ipcRenderer.invoke('plugin:get-frontend-widgets'),
updateFrontendWidget: (widgetId, data) => ipcRenderer.invoke('plugin:update-frontend-widget', widgetId, data),
onFrontendWidgetsChanged: (callback) => {
ipcRenderer.on('plugin:frontend-widgets-changed', () => callback())
},
onFrontendWidgetUpdated: (callback) => {
ipcRenderer.on('plugin:frontend-widget-updated', (event, widgetId, widget) => callback(widgetId, widget))
},
removeAllListeners: (channel) => {
ipcRenderer.removeAllListeners(channel)
}
}
}
function safeExpose(name, api) {
try {
contextBridge.exposeInMainWorld(name, api)
return true
} catch (e) {
// 当 contextIsolation=false 时,contextBridge 会抛错;此时直接挂到全局即可
try {
globalThis[name] = api
return true
} catch {
return false
}
}
}
// 暴露安全的API给渲染进程
const electronAPI = {
// 房间操作
createRoom: (roomData) => ipcRenderer.invoke('create-room', roomData),
openBpWindows: (roomData) => ipcRenderer.invoke('open-bp-windows', roomData),
closeBpWindows: () => ipcRenderer.invoke('close-bp-windows'),
// 布局操作
saveLayout: (layout) => ipcRenderer.invoke('save-layout', layout),
loadLayout: () => ipcRenderer.invoke('load-layout'),
exportLayout: (layout) => ipcRenderer.invoke('export-layout', layout),
importLayout: () => ipcRenderer.invoke('import-layout'),
// 比分板布局(持久化到 layout.json,替代 localStorage)
getScoreboardLayout: (team) => ipcRenderer.invoke('get-scoreboard-layout', team),
saveScoreboardLayout: (team, scoreboardLayout) => ipcRenderer.invoke('save-scoreboard-layout', team, scoreboardLayout),
// 赛后数据布局(持久化到 layout.json)
getPostMatchLayout: () => ipcRenderer.invoke('get-postmatch-layout'),
savePostMatchLayout: (postMatchLayout) => ipcRenderer.invoke('save-postmatch-layout', postMatchLayout),
// 透明背景设置
getTransparentBackground: () => ipcRenderer.invoke('get-transparent-background'),
setTransparentBackground: (enabled) => ipcRenderer.invoke('set-transparent-background', enabled),
getBpStartupWindowAnimation: () => ipcRenderer.invoke('bp-startup-window-animation:get'),
setBpStartupWindowAnimation: (enabled) => ipcRenderer.invoke('bp-startup-window-animation:set', enabled),
getWindowsNativeNotificationSetting: () => ipcRenderer.invoke('windows-native-notification:get'),
setWindowsNativeNotificationSetting: (enabled) => ipcRenderer.invoke('windows-native-notification:set', enabled),
directorSyncGetSettings: () => ipcRenderer.invoke('director-sync:get-settings'),
directorSyncSetSettings: (patch) => ipcRenderer.invoke('director-sync:set-settings', patch),
directorSyncGetStatus: () => ipcRenderer.invoke('director-sync:get-status'),
directorSyncReconnect: () => ipcRenderer.invoke('director-sync:reconnect'),
directorSyncDisconnect: () => ipcRenderer.invoke('director-sync:disconnect'),
onDirectorSyncStatus: (callback) => ipcRenderer.on('director-sync-status', (event, status) => callback(status)),
// 背景图片
selectBackground: () => ipcRenderer.invoke('select-background'),
selectCharacterDisplayBackground: () => ipcRenderer.invoke('select-character-display-background'),
selectScoreboardBackground: (team) => ipcRenderer.invoke('select-scoreboard-background', team),
selectPostMatchBackground: () => ipcRenderer.invoke('select-postmatch-background'),
getBackgroundPath: () => ipcRenderer.invoke('get-background-path'),
selectTeamLogo: (team) => ipcRenderer.invoke('select-team-logo', team),
selectImageForSlot: () => ipcRenderer.invoke('select-image-for-slot'),
// 自定义字体
selectCustomFont: () => ipcRenderer.invoke('select-custom-font'),
getCustomFonts: () => ipcRenderer.invoke('get-custom-fonts'),
deleteCustomFont: (fileName) => ipcRenderer.invoke('delete-custom-font', fileName),
saveFontConfig: (config) => ipcRenderer.invoke('save-font-config', config),
getFontConfig: () => ipcRenderer.invoke('get-font-config'),
getFontUrl: (fileName) => ipcRenderer.invoke('get-font-url', fileName),
openFontSettings: () => ipcRenderer.invoke('open-font-settings'),
onFontConfigUpdated: (callback) => ipcRenderer.on('font-config-updated', (event, config) => callback(config)),
onCustomFontsChanged: (callback) => ipcRenderer.on('custom-fonts-changed', (event, payload) => callback(payload)),
getScoreboardOverviewLayout: () => ipcRenderer.invoke('get-scoreboard-overview-layout'),
saveScoreboardOverviewLayout: (layout) => ipcRenderer.invoke('save-scoreboard-overview-layout', layout),
selectOverviewTexture: () => ipcRenderer.invoke('select-overview-texture'),
// BP布局包
exportBpPack: () => ipcRenderer.invoke('export-bp-pack'),
importBpPack: () => ipcRenderer.invoke('import-bp-pack'),
resetLayout: () => ipcRenderer.invoke('reset-layout'),
// 本地前台(仅前台窗口)
openLocalFrontend: () => ipcRenderer.invoke('open-local-frontend'),
openCustomFrontendWindow: (windowId) => ipcRenderer.invoke('open-custom-frontend-window', windowId),
closeCustomFrontendWindow: (windowId) => ipcRenderer.invoke('close-custom-frontend-window', windowId),
// 内置地图资源列表
listMapAssets: () => ipcRenderer.invoke('list-map-assets'),
// 窗口控制
sendToFrontend: (data) => ipcRenderer.invoke('send-to-frontend', data),
toggleFrontendFrame: (show) => ipcRenderer.invoke('toggle-frontend-frame', show),
setFrontendAlwaysOnTop: (alwaysOnTop) => ipcRenderer.invoke('set-frontend-always-on-top', alwaysOnTop),
setFrontendFullscreen: (fullscreen) => ipcRenderer.invoke('set-frontend-fullscreen', fullscreen),
getFrontendRenderResolution: () => ipcRenderer.invoke('get-frontend-render-resolution'),
setFrontendRenderResolution: (resolution) => ipcRenderer.invoke('set-frontend-render-resolution', resolution),
toggleFrontendEditMode: () => ipcRenderer.invoke('toggle-frontend-edit-mode'),
showMapDisplay: (action, map, team) => ipcRenderer.invoke('show-map-display', { action, map, team }),
openScoreboard: (roomId, team) => ipcRenderer.invoke('open-scoreboard', roomId, team),
openScoreboardOverview: (roomId, boCount) => ipcRenderer.invoke('open-scoreboard-overview', roomId, boCount),
openPostMatch: (roomId) => ipcRenderer.invoke('open-postmatch', roomId),
resizeWindow: (width, height) => ipcRenderer.invoke('resize-window', width, height),
// OCR:识别对局图片
parseGameRecordImage: (imageBase64) => ipcRenderer.invoke('ai-parse-game-record-image', imageBase64),
// 布局包商店
openStore: () => ipcRenderer.invoke('open-store'),
openPluginStore: () => ipcRenderer.invoke('open-plugin-store'),
openComponentEditor: () => ipcRenderer.invoke('open-component-editor'),
openComponentDesignerTutorial: () => ipcRenderer.invoke('open-component-designer-tutorial'),
openAnimationEditor: () => ipcRenderer.invoke('open-animation-editor'),
openLogViewer: () => ipcRenderer.invoke('open-log-viewer'),
openObsAutomationEditor: () => ipcRenderer.invoke('obs-automation:open-editor'),
openObsAutomationTutorial: () => ipcRenderer.invoke('obs-automation:open-tutorial'),
logGetRecent: (limit) => ipcRenderer.invoke('log:get-recent', limit),
logGetFileInfo: () => ipcRenderer.invoke('log:get-file-info'),
logGetLevel: () => ipcRenderer.invoke('log:get-level'),
logSetLevel: (level) => ipcRenderer.invoke('log:set-level', level),
logClear: () => ipcRenderer.invoke('log:clear'),
logOpenDir: () => ipcRenderer.invoke('log:open-dir'),
openLicense: () => ipcRenderer.invoke('open-license'),
// 插件系统
openPluginManager: () => ipcRenderer.invoke('open-plugin-manager'),
getOfficialModelMap: () => ipcRenderer.invoke('get-official-model-map'),
getOfficialModelsDownloadStatus: () => ipcRenderer.invoke('get-official-model-download-status'),
prepareOfficialModels: () => ipcRenderer.invoke('prepare-official-models'),
onOfficialModelsDownloadProgress: (callback) => ipcRenderer.on('official-models-download-progress', (event, data) => callback(data)),
getEnvironment: () => ipcRenderer.invoke('get-environment'),
switchEnvironment: (env) => ipcRenderer.invoke('switch-environment', env),
localPagesGetPages: () => ipcRenderer.invoke('local-pages:get-pages'),
localPagesGetStatus: () => ipcRenderer.invoke('local-pages:get-status'),
localPagesReadFile: (fileName) => ipcRenderer.invoke('local-pages:read-file', fileName),
localPagesWriteFile: (fileName, content) => ipcRenderer.invoke('local-pages:write-file', fileName, content),
localPagesGenerateWithAi: (payload) => ipcRenderer.invoke('local-pages:generate-with-ai', payload),
localPagesSetHost: (host) => ipcRenderer.invoke('local-pages:set-host', host),
localBpAutoOpenGet: () => ipcRenderer.invoke('local-bp:auto-open:get'),
localBpAutoOpenSet: (settings) => ipcRenderer.invoke('local-bp:auto-open:set', settings),
getLocalBpConsoleBackground: () => ipcRenderer.invoke('local-bp-console-bg:get'),
setLocalBpConsoleBackground: (settings) => ipcRenderer.invoke('local-bp-console-bg:set', settings),
selectLocalBpConsoleBackground: (options) => ipcRenderer.invoke('local-bp-console-bg:select-image', options),
getFrontendResizeLock: () => ipcRenderer.invoke('frontend-resize-lock:get'),
setFrontendResizeLock: (locked) => ipcRenderer.invoke('frontend-resize-lock:set', locked),
storeGetPacks: (params) => ipcRenderer.invoke('store-get-packs', params),
storeGetPackDetail: (id) => ipcRenderer.invoke('store-get-pack-detail', id),
storeDownloadPack: (id) => ipcRenderer.invoke('store-download-pack', id),
storeGetInstalled: () => ipcRenderer.invoke('store-get-installed'),
storeUploadPack: (metadata) => ipcRenderer.invoke('store-upload-pack', metadata),
storeCheckUpdates: () => ipcRenderer.invoke('store-check-updates'),
storeGetMyPacks: () => ipcRenderer.invoke('store-get-my-packs'),
storeDeletePack: (id) => ipcRenderer.invoke('store-delete-pack', id),
storeRatePack: (id, rating) => ipcRenderer.invoke('store-rate-pack', id, rating),
storeSelectPackFile: () => ipcRenderer.invoke('store-select-pack-file'),
storeSelectPreview: () => ipcRenderer.invoke('store-select-preview'),
// 插件商店
pluginStoreGetPacks: (params) => ipcRenderer.invoke('plugin-store-get-packs', params),
pluginStoreGetPackDetail: (id) => ipcRenderer.invoke('plugin-store-get-pack-detail', id),
pluginStoreDownloadPack: (id) => ipcRenderer.invoke('plugin-store-download-pack', id),
pluginStoreGetInstalled: () => ipcRenderer.invoke('plugin-store-get-installed'),
pluginStoreGetMyPacks: () => ipcRenderer.invoke('plugin-store-get-my-packs'),
pluginStoreDeletePack: (id) => ipcRenderer.invoke('plugin-store-delete-pack', id),
pluginStoreSetVisibility: (id, isPublic) => ipcRenderer.invoke('plugin-store-set-visibility', id, isPublic),
pluginStoreExportPlugin: (pluginId) => ipcRenderer.invoke('plugin-store-export-plugin', pluginId),
pluginStoreUninstall: (pluginId) => ipcRenderer.invoke('plugin-store-uninstall', pluginId),
pluginStoreSelectFile: () => ipcRenderer.invoke('plugin-store-select-file'),
pluginStoreUploadPack: (metadata) => ipcRenderer.invoke('plugin-store-upload-pack', metadata),
// 应用控制
restartApp: () => ipcRenderer.invoke('app-restart'),
notifyMainUiBootstrapReady: () => ipcRenderer.invoke('main-ui-bootstrap-ready'),
// 应用更新
checkForUpdate: () => ipcRenderer.invoke('check-for-update'),
manualCheckUpdate: () => ipcRenderer.invoke('manual-check-update'),
getAppVersion: () => ipcRenderer.invoke('get-app-version'),
// ASG账号登录与竞猜功能
login: (email, password) => ipcRenderer.invoke('asg-login', email, password),
logout: () => ipcRenderer.invoke('asg-logout'),
getAuthStatus: () => ipcRenderer.invoke('asg-get-auth-status'),
getMyEvents: () => ipcRenderer.invoke('asg-get-my-events'),
getEventMatches: (eventId) => ipcRenderer.invoke('asg-get-event-matches', eventId),
getPublicPartners: (type) => ipcRenderer.invoke('asg-get-public-partners', type),
getMatchPredictions: (matchId) => ipcRenderer.invoke('asg-get-match-predictions', matchId),
createBilibiliPrediction: (dto) => ipcRenderer.invoke('asg-create-bilibili-prediction', dto),
openPredictionWindow: (matchId) => ipcRenderer.invoke('open-prediction-window', matchId),
// B站弹幕
startBilibiliDanmaku: (roomId, matchId) => ipcRenderer.invoke('start-bilibili-danmaku', roomId, matchId),
stopBilibiliDanmaku: () => ipcRenderer.invoke('stop-bilibili-danmaku'),
onBilibiliDanmaku: (callback) => ipcRenderer.on('bilibili-danmaku', (event, data) => callback(data)),
onBilibiliPrediction: (callback) => ipcRenderer.on('bilibili-prediction', (event, data) => callback(data)),
onLoadMatch: (callback) => ipcRenderer.on('load-match', (event, matchId) => callback(matchId)),
// 本地BP系统
invoke: (channel, ...args) => ipcRenderer.invoke(channel, ...args),
send: (channel, ...args) => ipcRenderer.send(channel, ...args),
on: (channel, callback) => ipcRenderer.on(channel, (event, ...args) => callback(...args)),
// 本地BP状态更新监听
onLocalBpStateUpdate: (callback) => {
ipcRenderer.on('update-data', (event, data) => {
if (data && data.type === 'state') {
callback(data.state)
}
})
},
// 3D模型设置
selectFolder: () => ipcRenderer.invoke('select-folder'),
selectFileWithFilter: (options) => ipcRenderer.invoke('select-file', options),
importBundledAsset: (filePath, options) => ipcRenderer.invoke('localBp:importBundledAsset', filePath, options),
readBinaryFile: (filePath) => ipcRenderer.invoke('read-binary-file', filePath),
saveModelConfig: (config) => ipcRenderer.invoke('save-model-config', config),
getModelConfig: () => ipcRenderer.invoke('get-model-config'),
scanModels: (dir) => ipcRenderer.invoke('scan-models', dir),
onModelConfigUpdated: (callback) => ipcRenderer.on('model-config-updated', (event, config) => callback(config)),
// 角色管理
openCharacterManager: () => ipcRenderer.invoke('open-character-manager'),
characterGetAll: () => ipcRenderer.invoke('character:get-all'),
characterGetIndex: () => ipcRenderer.invoke('character:get-index'),
characterAdd: (data) => ipcRenderer.invoke('character:add', data),
characterUpdate: (data) => ipcRenderer.invoke('character:update', data),
characterDelete: (data) => ipcRenderer.invoke('character:delete', data),
characterSelectImage: (imageType) => ipcRenderer.invoke('character:select-image', imageType),
characterGetImageBase64: (imagePath) => ipcRenderer.invoke('character:get-image-base64', imagePath),
characterRefresh: () => ipcRenderer.invoke('character:refresh'),
// 插件命令(给插件自带窗口/页面使用)
executeCommand: (commandId, ...args) => ipcRenderer.invoke('plugin:execute-command', commandId, ...args),
// 事件监听
onRoomData: (callback) => ipcRenderer.on('room-data', (event, data) => callback(data)),
onUpdateData: (callback) => ipcRenderer.on('update-data', (event, data) => callback(data)),
onToggleEditMode: (callback) => ipcRenderer.on('toggle-edit-mode', (event) => callback()),
onReloadLayoutFromPack: (callback) => ipcRenderer.on('reload-layout-from-pack', (event) => callback()),
// 移除监听
removeAllListeners: (channel) => ipcRenderer.removeAllListeners(channel)
}
safeExpose('electronAPI', electronAPI)
// 插件系统(渲染进程)API
safeExpose('plugins', pluginRendererAPI)
// ============================
// 全局错误拦截(渲染进程 -> 主进程 -> 终端)
// ============================
try {
if (!globalThis.__asgRendererErrorBridgeInstalled) {
globalThis.__asgRendererErrorBridgeInstalled = true
const safeToString = (v) => {
try {
if (v instanceof Error) return v.stack || v.message || String(v)
if (typeof v === 'string') return v
return JSON.stringify(v)
} catch {
try { return String(v) } catch { return '[unserializable]' }
}
}
const sendRendererLog = (payload) => {
try {
ipcRenderer.send('renderer:log', {
ts: Date.now(),
url: typeof location !== 'undefined' ? location.href : null,
...payload
})
} catch {
// ignore
}
}
globalThis.addEventListener('error', (event) => {
sendRendererLog({
level: 'error',
source: 'window.error',
message: event?.message || 'Unhandled error',
filename: event?.filename,
lineno: event?.lineno,
colno: event?.colno,
stack: event?.error?.stack || event?.error?.message
})
})
globalThis.addEventListener('unhandledrejection', (event) => {
const reason = event?.reason
sendRendererLog({
level: 'error',
source: 'unhandledrejection',
message: safeToString(reason),
stack: reason?.stack
})
})
// 统一转发 console.* 到主进程日志,便于追踪渲染流程与报错。
const consoleBridgeMap = {
debug: 'debug',
log: 'info',
info: 'info',
warn: 'warn',
error: 'error'
}
for (const [method, level] of Object.entries(consoleBridgeMap)) {
const original = typeof console[method] === 'function' ? console[method].bind(console) : null
if (!original) continue
console[method] = (...args) => {
try {
sendRendererLog({
level,
source: `console.${method}`,
message: args.map(safeToString).join(' ')
})
} catch {
// ignore
}
original(...args)
}
}
}
} catch {
// ignore
}
// 给主进程一个“preload 已加载”的信号,便于定位是否注入成功
try {
ipcRenderer.send('preload:loaded', {
url: typeof location !== 'undefined' ? location.href : null,
hasElectronAPI: true,
hasPlugins: Boolean(pluginRendererAPI),
pluginsSource: pluginRendererLoadError ? 'fallback' : 'required',
pluginsLoadError: pluginRendererLoadError ? (pluginRendererLoadError?.message || String(pluginRendererLoadError)) : null
})
} catch {
// ignore
}