1- const { app, BrowserWindow, globalShortcut, Menu } = require ( 'electron' ) ;
1+ const { app, BrowserWindow, globalShortcut, Menu, ipcMain } = require ( 'electron' ) ;
22const log = require ( 'electron-log' ) ;
33const path = require ( 'path' ) ;
4+ const Steamworks = require ( 'steamworks.js' ) ;
5+
6+ let steamClient ;
7+ let steamCallbackInterval ;
8+
9+ const parseAppId = ( appId ) => {
10+ if ( typeof appId === 'number' ) {
11+ return appId ;
12+ }
13+ const numeric = Number ( appId ) ;
14+ return Number . isNaN ( numeric ) ? appId : numeric ;
15+ } ;
16+
17+ const stopSteamCallbacks = ( ) => {
18+ if ( steamCallbackInterval ) {
19+ clearInterval ( steamCallbackInterval ) ;
20+ steamCallbackInterval = undefined ;
21+ }
22+ } ;
23+
24+ ipcMain . handle ( 'steam-initialize' , async ( _event , appId ) => {
25+ if ( steamClient ) {
26+ return true ;
27+ }
28+
29+ try {
30+ const parsedAppId = parseAppId ( appId ) ;
31+ steamClient = Steamworks . init ( parsedAppId ) ;
32+
33+ if ( typeof steamClient ?. runCallbacks === 'function' && ! steamCallbackInterval ) {
34+ steamCallbackInterval = setInterval ( ( ) => {
35+ try {
36+ steamClient . runCallbacks ( ) ;
37+ } catch ( error ) {
38+ log . error ( 'Steamworks runCallbacks failed' , error ) ;
39+ }
40+ } , 1000 / 60 ) ;
41+
42+ if ( typeof steamCallbackInterval ?. unref === 'function' ) {
43+ steamCallbackInterval . unref ( ) ;
44+ }
45+ }
46+
47+ log . info ( 'Steamworks initialized' ) ;
48+ return true ;
49+ } catch ( error ) {
50+ log . error ( 'Failed to initialize Steamworks' , error ) ;
51+ steamClient = undefined ;
52+ stopSteamCallbacks ( ) ;
53+ return false ;
54+ }
55+ } ) ;
56+
57+ ipcMain . handle ( 'steam-unlock-achievement' , async ( _event , achievementId ) => {
58+ if ( ! steamClient ) {
59+ log . warn ( `Cannot unlock achievement ${ achievementId } : Steamworks not initialized` ) ;
60+ return false ;
61+ }
62+
63+ try {
64+ const result = steamClient . achievement ?. activate
65+ ? steamClient . achievement . activate ( achievementId )
66+ : false ;
67+
68+ if ( ! result ) {
69+ log . warn ( `Steamworks failed to activate achievement ${ achievementId } ` ) ;
70+ }
71+
72+ return Boolean ( result ) ;
73+ } catch ( error ) {
74+ log . error ( `Error unlocking Steam achievement ${ achievementId } ` , error ) ;
75+ return false ;
76+ }
77+ } ) ;
78+
79+ app . commandLine . appendSwitch ( "--in-process-gpu" ) ; // 修复 steam overlay
80+ app . commandLine . appendSwitch ( "--autoplay-policy" , "no-user-gesture-required" ) ; // 允许自动播放
481
582/**
683 * 关闭默认菜单栏
@@ -12,6 +89,13 @@ Menu.setApplicationMenu(null);
1289 */
1390app . whenReady ( ) . then ( ( ) => {
1491 createWindow ( )
92+
93+ try {
94+ Steamworks . electronEnableSteamOverlay ( ) ;
95+ } catch ( error ) {
96+ log . warn ( 'Steam overlay could not be enabled' , error ) ;
97+ }
98+
1599 // 适配 Mac OS
16100 app . on ( 'activate' , ( ) => {
17101 if ( BrowserWindow . getAllWindows ( ) . length === 0 ) createWindow ( )
@@ -27,6 +111,11 @@ const createWindow = () => {
27111 height : 900 ,
28112 icon : path . join ( __dirname , '../../icon.ico' ) ,
29113 useContentSize : true ,
114+ webPreferences : {
115+ preload : path . join ( __dirname , 'preload.js' ) ,
116+ contextIsolation : true ,
117+ nodeIntegration : false ,
118+ } ,
30119 } )
31120
32121 win . loadFile ( './public/index.html' ) . then ( r => {
@@ -57,18 +146,33 @@ const createWindow = () => {
57146
58147 logMessage ( message ) ;
59148 } ) ;
60-
149+
61150 /**
62- * 侦听BrowserWindow关闭事件
63- */
64- win . on ( "close" , ( ) => {
65- app . quit ( ) ;
66- } ) ;
151+ * 侦听BrowserWindow关闭事件
152+ */
153+ win . on ( "close" , ( ) => {
154+ app . quit ( ) ;
155+ } ) ;
67156}
68157
158+ app . on ( 'before-quit' , ( ) => {
159+ globalShortcut . unregisterAll ( ) ;
160+ stopSteamCallbacks ( ) ;
161+
162+ if ( steamClient && typeof steamClient . shutdown === 'function' ) {
163+ try {
164+ steamClient . shutdown ( ) ;
165+ } catch ( error ) {
166+ log . error ( 'Steamworks shutdown failed' , error ) ;
167+ }
168+ }
169+
170+ steamClient = undefined ;
171+ } ) ;
172+
69173/**
70174 * 在关闭所有窗口时退出应用
71175 */
72176app . on ( 'window-all-closed' , ( ) => {
73177 if ( process . platform !== 'darwin' ) app . quit ( )
74- } )
178+ } )
0 commit comments