forked from ceifa/steamworks.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (64 loc) · 2.8 KB
/
index.js
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
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
/**
* 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')
electron.app.commandLine.appendSwitch('disable-direct-composition')
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))
}
}
const SteamCallback = nativeBinding.callback.SteamCallback
module.exports.SteamCallback = SteamCallback