Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
151 lines (128 loc) · 5.28 KB
/
Copy pathindex.js
File metadata and controls
151 lines (128 loc) · 5.28 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
const { platform, arch } = process
/** @typedef {typeof import('./client.d')} Client */
/** @type {Client} */
let nativeBinding = undefined
if (platform === 'win32' && arch === 'x64') {
nativeBinding = require('./dist/win64/steamworksjs.win32-x64-msvc.node')
} else if (platform === 'linux' && arch === 'x64') {
nativeBinding = require('./dist/linux64/steamworksjs.linux-x64-gnu.node')
} else if (platform === 'darwin') {
if (arch === 'x64') {
nativeBinding = require('./dist/osx/steamworksjs.darwin-x64.node')
} else if (arch === 'arm64') {
nativeBinding = require('./dist/osx/steamworksjs.darwin-arm64.node')
}
} else {
throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
}
let runCallbacksInterval = undefined
let steamOverlayInitialized = false
/**
* Initialize the steam client or throw an error if it fails
* @param {number} [appId] - App ID of the game to load, if undefined, will search for a steam_appid.txt file
* @returns {Omit<Client, 'init' | 'runCallbacks'>}
*/
module.exports.init = (appId) => {
const { init: internalInit, runCallbacks, restartAppIfNecessary, ...api } = nativeBinding
internalInit(appId)
clearInterval(runCallbacksInterval)
runCallbacksInterval = setInterval(runCallbacks, 1000 / 30)
return api
}
/**
* @param {number} appId - App ID of the game to load
* {@link https://partner.steamgames.com/doc/api/steam_api#SteamAPI_RestartAppIfNecessary}
* @returns {boolean}
*/
module.exports.restartAppIfNecessary = (appId) => nativeBinding.restartAppIfNecessary(appId);
/**
* Enable the steam overlay on electron
* @param {boolean} [disableEachFrameInvalidation] - Should attach a single pixel to be rendered each frame
*/
module.exports.electronEnableSteamOverlay = (disableEachFrameInvalidation) => {
const electron = require('electron')
if (!electron) {
throw new Error('Electron module not found')
}
electron.app.commandLine.appendSwitch('in-process-gpu')
if (!disableEachFrameInvalidation) {
/** @param {electron.BrowserWindow} browserWindow */
const attachFrameInvalidator = (browserWindow) => {
browserWindow.steamworksRepaintInterval = setInterval(() => {
if (browserWindow.isDestroyed()) {
clearInterval(browserWindow.steamworksRepaintInterval)
} else if (!browserWindow.webContents.isPainting()) {
browserWindow.webContents.invalidate()
}
}, 1000 / 60)
}
electron.BrowserWindow.getAllWindows().forEach(attachFrameInvalidator)
electron.app.on('browser-window-created', (_, bw) => attachFrameInvalidator(bw))
}
}
module.exports.electronEnableSteamOverlay2 = (fpsLimit = 30) => {
if (steamOverlayInitialized) {
console.log('Steam overlay already initialized');
return;
}
const electron = require('electron')
if (!electron) {
throw new Error('Electron module not found')
}
const app = electron.app
// Check if app is ready
if (!app.isReady()) {
throw new Error('Electron app is not ready');
}
// Wrap switch appending in try-catch
try {
app.commandLine.appendSwitch('in-process-gpu');
app.commandLine.appendSwitch('disable-gpu-shader-disk-cache');
app.commandLine.appendSwitch('disable-http-cache');
app.commandLine.appendSwitch('use-angle', 'd3d11');
app.commandLine.appendSwitch('disable-renderer-backgrounding');
app.commandLine.appendSwitch('disable-background-timer-throttling');
} catch (error) {
console.error('Error appending command line switches:', error);
throw error;
}
/** @param {electron.BrowserWindow} browserWindow */
const attachOverlayInvalidator = (window) => {
// Validate window and webContents existence
if (!window || !window.webContents) {
console.error('Invalid window or webContents');
return;
}
const FPS_LIMIT = fpsLimit;
try {
const intervalId = setInterval(() => {
try {
if (!window.isDestroyed() && window.isVisible()) {
// Invalidation "douce" sans forcer si le compositeur est déjà occupé
const { webContents } = window;
if (webContents && !webContents.isPainting()) {
webContents.invalidate();
}
}
} catch (error) {
console.error('Error in overlay invalidator interval:', error);
}
}, 1000 / FPS_LIMIT);
window.once('closed', () => {
clearInterval(intervalId);
});
} catch (error) {
console.error('Error setting up overlay invalidator:', error);
}
};
try {
electron.BrowserWindow.getAllWindows().forEach(attachOverlayInvalidator)
electron.app.on('browser-window-created', (_, bw) => attachOverlayInvalidator(bw))
} catch (error) {
console.error('Error attaching overlay invalidator:', error);
throw error;
}
steamOverlayInitialized = true;
}
const SteamCallback = nativeBinding.callback.SteamCallback
module.exports.SteamCallback = SteamCallback