@hagicode/steamworks.js 0.0.0-stage → 0.1.6-dev.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +477 -2
- package/callbacks.d.ts +170 -0
- package/client.d.ts +1182 -0
- package/dist/linux64/libsteam_api.so +0 -0
- package/dist/linux64/steamworksjs.linux-x64-gnu.node +0 -0
- package/dist/osx/libsteam_api.dylib +0 -0
- package/dist/osx/steamworksjs.darwin-arm64.node +0 -0
- package/dist/osx/steamworksjs.darwin-x64.node +0 -0
- package/dist/win64/steam_api64.dll +0 -0
- package/dist/win64/steam_api64.lib +0 -0
- package/dist/win64/steamworksjs.win32-x64-msvc.node +0 -0
- package/index.d.ts +9 -0
- package/index.js +94 -0
- package/package.json +56 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Gabriel Francisco Dos Santos
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,478 @@
|
|
|
1
|
-
|
|
1
|
+
[](https://github.com/HagiCode-org/steamworks.js/actions/workflows/publish.yml)
|
|
2
|
+
[](https://npmjs.com/package/@hagicode/steamworks.js)
|
|
3
|
+
[](https://opensource.org/licenses/MIT)
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
# Steamworks.js
|
|
6
|
+
|
|
7
|
+
A modern implementation of the Steamworks SDK for HTML/JS and NodeJS based applications.
|
|
8
|
+
|
|
9
|
+
## Why
|
|
10
|
+
|
|
11
|
+
I used [greenworks](https://github.com/greenheartgames/greenworks) for a long time and it's great, but I gave up for the following reasons.
|
|
12
|
+
|
|
13
|
+
* It's not being maintained anymore.
|
|
14
|
+
* It's not up to date.
|
|
15
|
+
* It's not context-aware.
|
|
16
|
+
* You have to build the binaries by yourself.
|
|
17
|
+
* Don't have typescript definitions.
|
|
18
|
+
* The API it's not trustful.
|
|
19
|
+
* The API implement callbacks instead of return flags or promises.
|
|
20
|
+
* I hate C++.
|
|
21
|
+
|
|
22
|
+
## API
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
const steamworks = require('steamworks.js')
|
|
26
|
+
|
|
27
|
+
// You can pass an appId, or don't pass anything and use a steam_appid.txt file
|
|
28
|
+
const client = steamworks.init(480)
|
|
29
|
+
|
|
30
|
+
// Print Steam username
|
|
31
|
+
console.log(client.localplayer.getName())
|
|
32
|
+
|
|
33
|
+
// Tries to activate an achievement
|
|
34
|
+
if (client.achievement.activate('ACHIEVEMENT')) {
|
|
35
|
+
// ...
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
You can refer to the [declarations file](https://github.com/HagiCode-org/steamworks.js/blob/main/client.d.ts) to check the API support and get more detailed documentation of each function.
|
|
40
|
+
|
|
41
|
+
## Installation
|
|
42
|
+
|
|
43
|
+
To use steamworks.js you don't have to build anything, just install it from npm:
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
npm i @hagicode/steamworks.js
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Migration from `steamworks.js`
|
|
50
|
+
|
|
51
|
+
If you were using the unscoped `steamworks.js` package, update your `package.json`:
|
|
52
|
+
|
|
53
|
+
```json
|
|
54
|
+
{
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"@hagicode/steamworks.js": "latest"
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Then run `pnpm install`. Your `require('steamworks.js')` statements will continue to work without changes.
|
|
62
|
+
|
|
63
|
+
### Electron
|
|
64
|
+
|
|
65
|
+
Steamworks.js is a native module and cannot be used by default in the renderer process. To enable the usage of native modules on the renderer process, the following configurations should be made on `main.js`:
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
const mainWindow = new BrowserWindow({
|
|
69
|
+
// ...
|
|
70
|
+
webPreferences: {
|
|
71
|
+
// ...
|
|
72
|
+
contextIsolation: false,
|
|
73
|
+
nodeIntegration: true
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
To make the steam overlay working, call the `electronEnableSteamOverlay` on the end of your `main.js` file:
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
require('steamworks.js').electronEnableSteamOverlay()
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
For the production build, copy the relevant distro files from `sdk/redistributable_bin/{YOUR_DISTRO}` into the root of your build. If you are using electron-forge, look for [#75](https://github.com/HagiCode-org/steamworks.js/issues/75).
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
## How to build
|
|
88
|
+
|
|
89
|
+
> You **only** need to build if you are going to change something on steamworks.js code, if you are looking to just consume the library or use it in your game, refer to the [installation section](#installation).
|
|
90
|
+
|
|
91
|
+
Make sure you have the latest [node.js](https://nodejs.org/en/), [Rust](https://www.rust-lang.org/tools/install) and [Clang](https://rust-lang.github.io/rust-bindgen/requirements.html). We also need [Steam](https://store.steampowered.com/about/) installed and running.
|
|
92
|
+
|
|
93
|
+
### Cloning the repository
|
|
94
|
+
|
|
95
|
+
This project uses git submodules for the steamworks-rs dependency. When cloning the repository, use:
|
|
96
|
+
|
|
97
|
+
```sh
|
|
98
|
+
git clone --recurse-submodules https://github.com/HagiCode-org/steamworks.js.git
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
If you have already cloned the repository without submodules, initialize them with:
|
|
102
|
+
|
|
103
|
+
```sh
|
|
104
|
+
git submodule update --init --recursive
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Building
|
|
108
|
+
|
|
109
|
+
Install dependencies with `pnpm install` and then run `pnpm run build:debug` to build the library.
|
|
110
|
+
|
|
111
|
+
There is no way to build for all targets easily. The good news is that you don't need to. You can develop and test on your current target, and open a PR. When the code is merged to main, a github action will build for all targets and publish a new version.
|
|
112
|
+
|
|
113
|
+
### Testing Electron
|
|
114
|
+
|
|
115
|
+
Go to the [test/electron](./test/electron) directory. There, you can run `pnpm install` and then `pnpm start` to run the Electron app.
|
|
116
|
+
|
|
117
|
+
Click "activate overlay" to test the overlay.
|
|
118
|
+
|
|
119
|
+
## Contributing
|
|
120
|
+
|
|
121
|
+
Please see [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines on commit conventions and the pull request process.
|
|
122
|
+
|
|
123
|
+
## Workshop Screenshots (Additional Previews)
|
|
124
|
+
|
|
125
|
+
Besides the main preview, a Workshop item can show extra screenshots. Read them with `includeAdditionalPreviews`, and add more with `additionalPreviewPaths` (an update that carries only the new images):
|
|
126
|
+
|
|
127
|
+
```js
|
|
128
|
+
const steamworks = require('steamworks.js')
|
|
129
|
+
const client = steamworks.init(appId)
|
|
130
|
+
|
|
131
|
+
// list: each entry has urlOrVideoId, originalFilename and previewType (ItemPreviewType)
|
|
132
|
+
const item = await client.workshop.getItem(itemId, { includeAdditionalPreviews: true })
|
|
133
|
+
console.log(item.additionalPreviews)
|
|
134
|
+
|
|
135
|
+
// add: appended to the item's screenshots; no content and no title change are sent
|
|
136
|
+
await client.workshop.updateItem(itemId, { additionalPreviewPaths: ['/abs/path/shot-2.png'] }, appId)
|
|
137
|
+
|
|
138
|
+
// remove: pass the 0-based indexes from `additionalPreviews`; the main preview is not affected
|
|
139
|
+
await client.workshop.updateItem(itemId, { removeAdditionalPreviewIndexes: [0, 2] }, appId)
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
- Removals run before additions in the same update, from the highest index down. Indexes always refer to the list as it was before this update, so read the list again before removing.
|
|
143
|
+
- Screenshots cannot be reordered through this API. The main preview is replaced with `previewPath`, not removed.
|
|
144
|
+
- Steam lists screenshots by file name. Send only the files that are not listed yet, otherwise they are added again.
|
|
145
|
+
- Adding screenshots needs a Steam client that is running and logged in, and an account that owns the app.
|
|
146
|
+
|
|
147
|
+
## Workshop Preview and Content Updates
|
|
148
|
+
|
|
149
|
+
`updateItem` accepts every field as optional, so a call that carries only `previewPath` or only `contentPath` already updates just that part. Two explicit helpers make that intent obvious and check the path before talking to Steam:
|
|
150
|
+
|
|
151
|
+
```js
|
|
152
|
+
const steamworks = require('steamworks.js')
|
|
153
|
+
const client = steamworks.init(appId)
|
|
154
|
+
|
|
155
|
+
// replace only the main preview image; the mod files are not uploaded
|
|
156
|
+
await client.workshop.updateItemPreview(itemId, '/abs/path/preview.png', appId, 'New preview')
|
|
157
|
+
|
|
158
|
+
// upload only the mod folder; the preview image is not touched
|
|
159
|
+
await client.workshop.updateItemContent(itemId, '/abs/path/mod-folder', appId, 'Fix crash on load')
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
- Signature for both: `(itemId, path, appId?, changeNote?)`. `appId` defaults to the app the client was initialized with, and `changeNote` is optional.
|
|
163
|
+
- `updateItemPreview` rejects when the path is missing or is not a file. `updateItemContent` rejects when the path is missing or is not a folder. Both check before any Steam call.
|
|
164
|
+
- Title, description, tags and visibility are not sent, so they stay as they are. To change them together with a file, keep using `updateItem`.
|
|
165
|
+
- Neither helper reports upload progress. For a large content upload use `updateItemWithCallback`.
|
|
166
|
+
|
|
167
|
+
## Workshop Item Dependencies
|
|
168
|
+
|
|
169
|
+
A Workshop item can require another item, so Steam installs the required item along with it. `addDependency` and `removeDependency` take the parent and the child as `bigint` ids and resolve with the Steam result code. `1` means OK. Any other code is a Steam failure reason, and it arrives as a resolved number, not a rejection.
|
|
170
|
+
|
|
171
|
+
```js
|
|
172
|
+
const steamworks = require('steamworks.js')
|
|
173
|
+
const client = steamworks.init(appId)
|
|
174
|
+
|
|
175
|
+
const modId = 123456789n // your mod: an item owned by the logged-in account
|
|
176
|
+
const requiredId = 3836453389n // the required child item (Outing Picker)
|
|
177
|
+
|
|
178
|
+
// mark the child as required by the mod
|
|
179
|
+
const added = await client.workshop.addDependency(modId, requiredId)
|
|
180
|
+
if (added !== 1) {
|
|
181
|
+
console.error(`Steam refused the dependency, result code ${added}`)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// remove it again; the result code is returned the same way
|
|
185
|
+
const removed = await client.workshop.removeDependency(modId, requiredId)
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
- Both calls need a running, logged-in Steam client, and the parent must be an item the logged-in account owns.
|
|
189
|
+
- An id that is negative or does not fit in 64 bits rejects before any Steam call. A failed call with no completion also rejects, and the error says the outcome is unknown, so check the item before retrying.
|
|
190
|
+
|
|
191
|
+
## Workshop Multilingual Support
|
|
192
|
+
|
|
193
|
+
Steamworks.js supports localized content for Steam Workshop items. You can update the title and description for different languages using the `language` parameter.
|
|
194
|
+
|
|
195
|
+
### Basic Usage
|
|
196
|
+
|
|
197
|
+
```js
|
|
198
|
+
// Update English content (backward compatible - no language specified)
|
|
199
|
+
await workshop.updateItem(itemId, {
|
|
200
|
+
title: "My Mod",
|
|
201
|
+
description: "English description"
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
// Update English content (explicitly)
|
|
205
|
+
await workshop.updateItem(itemId, {
|
|
206
|
+
title: "My Mod",
|
|
207
|
+
description: "English description",
|
|
208
|
+
language: "english"
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
// Update Chinese content
|
|
212
|
+
await workshop.updateItem(itemId, {
|
|
213
|
+
title: "我的模组",
|
|
214
|
+
description: "中文描述",
|
|
215
|
+
language: "schinese"
|
|
216
|
+
});
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### Multiple Languages
|
|
220
|
+
|
|
221
|
+
Since Steam API only supports updating one language at a time, you need to make multiple `updateItem()` calls for each language:
|
|
222
|
+
|
|
223
|
+
```js
|
|
224
|
+
// Sequential updates
|
|
225
|
+
await workshop.updateItem(itemId, {
|
|
226
|
+
title: "My Mod",
|
|
227
|
+
description: "English description",
|
|
228
|
+
language: "english"
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
await workshop.updateItem(itemId, {
|
|
232
|
+
title: "Mon Mod",
|
|
233
|
+
description: "Description française",
|
|
234
|
+
language: "french"
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
await workshop.updateItem(itemId, {
|
|
238
|
+
title: "Mi Mod",
|
|
239
|
+
description: "Descripción en español",
|
|
240
|
+
language: "spanish"
|
|
241
|
+
});
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
Or use `Promise.all()` for parallel updates:
|
|
245
|
+
|
|
246
|
+
```js
|
|
247
|
+
await Promise.all([
|
|
248
|
+
workshop.updateItem(itemId, {
|
|
249
|
+
title: "Mein Mod",
|
|
250
|
+
description: "Deutsche Beschreibung",
|
|
251
|
+
language: "german"
|
|
252
|
+
}),
|
|
253
|
+
workshop.updateItem(itemId, {
|
|
254
|
+
title: "私のMod",
|
|
255
|
+
description: "日本語の説明",
|
|
256
|
+
language: "japanese"
|
|
257
|
+
}),
|
|
258
|
+
workshop.updateItem(itemId, {
|
|
259
|
+
title: "내 모드",
|
|
260
|
+
description: "한국어 설명",
|
|
261
|
+
language: "korean"
|
|
262
|
+
})
|
|
263
|
+
]);
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
### Supported Language Codes
|
|
267
|
+
|
|
268
|
+
Steam supports the following language codes:
|
|
269
|
+
|
|
270
|
+
| Code | Language | Code | Language |
|
|
271
|
+
|------|----------|------|----------|
|
|
272
|
+
| `english` | English | `schinese` | Simplified Chinese |
|
|
273
|
+
| `tchinese` | Traditional Chinese | `german` | German |
|
|
274
|
+
| `french` | French | `italian` | Italian |
|
|
275
|
+
| `korean` | Korean | `spanish` | Spanish |
|
|
276
|
+
| `russian` | Russian | `thai` | Thai |
|
|
277
|
+
| `japanese` | Japanese | `portuguese` | Portuguese |
|
|
278
|
+
| `polish` | Polish | `danish` | Danish |
|
|
279
|
+
| `dutch` | Dutch | `finnish` | Finnish |
|
|
280
|
+
| `norwegian` | Norwegian | `swedish` | Swedish |
|
|
281
|
+
| `hungarian` | Hungarian | `czech` | Czech |
|
|
282
|
+
| `romanian` | Romanian | `turkish` | Turkish |
|
|
283
|
+
| `brazilian` | Brazilian Portuguese | `bulgarian` | Bulgarian |
|
|
284
|
+
| `greek` | Greek | `arabic` | Arabic |
|
|
285
|
+
| `ukrainian` | Ukrainian | `vietnamese` | Vietnamese |
|
|
286
|
+
|
|
287
|
+
**Note**: If `language` is omitted, it defaults to `"english"`.
|
|
288
|
+
|
|
289
|
+
## Friends
|
|
290
|
+
|
|
291
|
+
The `friends` namespace reads the local user's friend list, friend state and avatars, and sends game invites. SteamIDs are `bigint` values.
|
|
292
|
+
|
|
293
|
+
```js
|
|
294
|
+
const steamworks = require('steamworks.js')
|
|
295
|
+
const client = steamworks.init(appId)
|
|
296
|
+
|
|
297
|
+
// 0x4 = immediate friends (FriendFlags.IMMEDIATE)
|
|
298
|
+
for (const friend of client.friends.getFriends(0x4)) {
|
|
299
|
+
const state = client.friends.getFriendState(friend.steamId64)
|
|
300
|
+
const avatar = client.friends.getMediumAvatar(friend.steamId64) // Buffer of RGBA pixels, or null
|
|
301
|
+
console.log(friend.nickName ?? friend.name, state, avatar?.length ?? 0)
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// Invite a friend to the current game with a connect string
|
|
305
|
+
client.friends.inviteUserToGame(friendSteamId, 'lobby=abc')
|
|
306
|
+
|
|
307
|
+
// Remove the Rich Presence keys set with localplayer.setRichPresence
|
|
308
|
+
client.friends.clearRichPresence()
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
- `getFriendState` returns a `FriendState` value; `getFriendGamePlayed` returns the AppID of the game a friend is playing, or `null`.
|
|
312
|
+
- Avatars are `null` until Steam has loaded them. Call `client.friends.requestUserInformation(steamId64, true)` to ask for a user's information, and wait for the `PersonaStateChange` callback before reading it again.
|
|
313
|
+
- Name and state updates arrive through `runCallbacks()`, which `init()` already calls on a timer.
|
|
314
|
+
|
|
315
|
+
## Stats, Achievements and Leaderboards
|
|
316
|
+
|
|
317
|
+
`stats` gains float stats, the achievement count and a request for another user's stats. `achievement` gains global unlock percentages. The `leaderboard` namespace finds and creates leaderboards, uploads scores and downloads entries.
|
|
318
|
+
|
|
319
|
+
```js
|
|
320
|
+
const steamworks = require('steamworks.js')
|
|
321
|
+
const client = steamworks.init(appId)
|
|
322
|
+
|
|
323
|
+
client.stats.setFloat('distance', 12.5)
|
|
324
|
+
console.log(client.stats.getFloat('distance')) // 12.5, or null when the stat is not defined
|
|
325
|
+
console.log(client.stats.getNumAchievements())
|
|
326
|
+
|
|
327
|
+
// Global unlock percentages: call the request first, then read each achievement
|
|
328
|
+
await client.achievement.requestGlobalPercentages()
|
|
329
|
+
console.log(client.achievement.getAchievedPercent('FIRST_RUN')) // 0 to 100, or null
|
|
330
|
+
|
|
331
|
+
// Leaderboards: 0 = Ascending, 0 = Numeric; upload with 0 = KeepBest, download with 0 = Global
|
|
332
|
+
const board = await client.leaderboard.findOrCreate('Fastest', 0, 0)
|
|
333
|
+
if (board) {
|
|
334
|
+
const result = await board.uploadScore(0, 1234, []) // null when Steam did not accept the upload
|
|
335
|
+
const top = await board.downloadEntries(0, 0, 10, 0) // ranks 0 to 9
|
|
336
|
+
console.log(board.getName(), board.getEntryCount(), result, top)
|
|
337
|
+
}
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
- `requestUserStats(steamId64)` only starts the request. Its result arrives through the `UserStatsReceived` callback (see `callback.register`).
|
|
341
|
+
- The Promise-returning functions (`requestGlobalPercentages`, `find`, `findOrCreate`, `uploadScore`, `downloadEntries`) resolve when Steam answers. Steam delivers those answers inside `runCallbacks()`. `init()` runs it about 30 times a second, so you do not need to call it yourself while the client is initialized.
|
|
342
|
+
- A Promise rejects with a readable message when Steam reports a failure. When the answer is lost, the message says the outcome is unknown, so check the data before retrying an upload.
|
|
343
|
+
|
|
344
|
+
## Networking: `networking` and the New Namespaces
|
|
345
|
+
|
|
346
|
+
Two families of networking APIs coexist. The `networking` namespace is the original P2P packet API (`sendP2PPacket`, `readP2PPacket`, `acceptP2PSession`), and it is unchanged. The new `networkingSockets`, `networkingMessages` and `networkingUtils` namespaces wrap Steam's Networking Sockets, Networking Messages and Networking Utils interfaces.
|
|
347
|
+
|
|
348
|
+
- `networking` sends unreliable or reliable packets to a SteamID and reads them back in arrival order. There is no connection object, so you cannot close one peer or tell when it dropped.
|
|
349
|
+
- `networkingSockets` creates listen sockets and connections (by IP address or by SteamID), sends and receives messages with send flags, and groups connections into poll groups. Connections are objects with `close`, and state queries show whether each one is open.
|
|
350
|
+
- `networkingMessages` is connectionless, like UDP: send to a SteamID on a channel, receive by channel, and handle session requests and failures. Steam creates the underlying session implicitly.
|
|
351
|
+
- `networkingUtils` starts relay network access and reports its availability.
|
|
352
|
+
|
|
353
|
+
```js
|
|
354
|
+
const steamworks = require('steamworks.js')
|
|
355
|
+
const client = steamworks.init(appId)
|
|
356
|
+
|
|
357
|
+
// Relay access and authentication: start them early if you expect relayed or authenticated connections
|
|
358
|
+
client.networkingUtils.initRelayNetworkAccess()
|
|
359
|
+
console.log(client.networkingUtils.getRelayNetworkStatus()) // NetworkingAvailability value
|
|
360
|
+
client.networkingSockets.initAuthentication()
|
|
361
|
+
|
|
362
|
+
// Host: poll the listen socket without waiting, accept connecting peers and take the connection when it is connected
|
|
363
|
+
const listen = client.networkingSockets.createListenSocketIp('0.0.0.0:27015')
|
|
364
|
+
const event = listen.tryReceiveEvent() // null when nothing is pending
|
|
365
|
+
if (event && event.kind === 0 /* Connecting */) {
|
|
366
|
+
event.accept()
|
|
367
|
+
}
|
|
368
|
+
if (event && event.kind === 1 /* Connected */) {
|
|
369
|
+
const connection = event.takeConnection()
|
|
370
|
+
connection.send(Buffer.from('hello'), 8) // 8 = reliable
|
|
371
|
+
console.log(connection.receive(16))
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// Client: connect by IP address, then close with a reason
|
|
375
|
+
const connection = client.networkingSockets.connectByIpAddress('203.0.113.5:27015')
|
|
376
|
+
connection.setUserData(1n)
|
|
377
|
+
connection.close(1000, 'done', true)
|
|
378
|
+
|
|
379
|
+
// Messages: accept requests from Steam users and read channel 3
|
|
380
|
+
client.networkingMessages.onSessionRequest((request) => request.accept())
|
|
381
|
+
client.networkingMessages.sendMessageToUser(friendSteamId, 8, Buffer.from('hi'), 3)
|
|
382
|
+
console.log(client.networkingMessages.receiveMessagesOnChannel(3, 10))
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
- Send flags are a bitmask of Steam's `k_nSteamNetworkingSend_*` values: `0` unreliable, `8` reliable, `1` no Nagle delay. Invalid addresses and closed handles fail with a readable error.
|
|
386
|
+
- Listen socket events and session requests are only produced while `runCallbacks()` runs. `init()` already calls it about 30 times a second.
|
|
387
|
+
- A connection or request you never close or answer is closed or rejected when its JavaScript object is garbage collected. Close connections explicitly.
|
|
388
|
+
- Connections and messages need a Steam network path, so they cannot be exercised offline. The offline smoke test only checks that these functions are exported.
|
|
389
|
+
|
|
390
|
+
## Server Browser, Timeline, Screenshots and Remote Play
|
|
391
|
+
|
|
392
|
+
`matchmakingServers` lists game servers and queries single servers. `timeline` marks game state and events for Steam's timeline. `screenshots` hooks the screenshot hotkey and adds images to the Steam library. `remotePlay` lists Remote Play Together sessions and sends invites.
|
|
393
|
+
|
|
394
|
+
```js
|
|
395
|
+
const steamworks = require('steamworks.js')
|
|
396
|
+
const client = steamworks.init(appId)
|
|
397
|
+
|
|
398
|
+
// Server list: request, read, then release it
|
|
399
|
+
const list = client.matchmakingServers.lanServerList(appId)
|
|
400
|
+
try {
|
|
401
|
+
for (let i = 0; i < list.getServerCount(); i++) {
|
|
402
|
+
const server = list.getServerDetails(i) // ip, port, name, map, players, maxPlayers, ping
|
|
403
|
+
console.log(server.name, server.players, server.maxPlayers, server.ping)
|
|
404
|
+
}
|
|
405
|
+
} finally {
|
|
406
|
+
list.release()
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// Single server: these are Promises and reject when the server does not answer
|
|
410
|
+
const ping = await client.matchmakingServers.pingServer('192.168.1.10', 27015)
|
|
411
|
+
const players = await client.matchmakingServers.playerDetails('192.168.1.10', 27015)
|
|
412
|
+
const rules = await client.matchmakingServers.serverRules('192.168.1.10', 27015)
|
|
413
|
+
|
|
414
|
+
// Timeline: 0 = Playing (the game mode values are Playing, Staging, Menus, LoadingScreen)
|
|
415
|
+
client.timeline.setGameMode(0)
|
|
416
|
+
client.timeline.addEvent('icon', 'Boss defeated', '', 0, -5, 5, 1) // clip priority 1 = Standard
|
|
417
|
+
|
|
418
|
+
// Screenshots: with the hook on, the hotkey raises ScreenshotRequested instead of Steam taking the picture
|
|
419
|
+
client.screenshots.hookScreenshots(true)
|
|
420
|
+
client.screenshots.triggerScreenshot()
|
|
421
|
+
|
|
422
|
+
// Remote Play Together
|
|
423
|
+
for (const session of client.remotePlay.sessions()) {
|
|
424
|
+
console.log(session.sessionId, session.steamId64, client.remotePlay.sessionDetails(session.sessionId))
|
|
425
|
+
}
|
|
426
|
+
client.remotePlay.invite(friendSteamId)
|
|
427
|
+
```
|
|
428
|
+
|
|
429
|
+
- Steam fills a server list over time, through callbacks that `init()` pumps automatically. Read the count after a short wait, not right after the request. The single-server functions are Promises that resolve the same way.
|
|
430
|
+
- Release every server list request when you finish with it. Methods called after `release()` throw a readable error.
|
|
431
|
+
- Invalid IPv4 addresses, negative durations and strings with NUL characters are rejected with a readable error before any Steam call.
|
|
432
|
+
- `timeline` calls are silent when the Steam timeline interface is unavailable.
|
|
433
|
+
|
|
434
|
+
## Supplemental Functions
|
|
435
|
+
|
|
436
|
+
These functions extend existing namespaces. Nothing that existed before has changed.
|
|
437
|
+
|
|
438
|
+
```js
|
|
439
|
+
const client = steamworks.init(appId)
|
|
440
|
+
|
|
441
|
+
// apps: the launch command line, and a query parameter from a Steam link such as steam://run/480//mode=demo
|
|
442
|
+
client.apps.launchCommandLine()
|
|
443
|
+
client.apps.launchQueryParam('mode') // '' when the parameter is absent
|
|
444
|
+
|
|
445
|
+
// utils: overlay and UI state
|
|
446
|
+
client.utils.isOverlayEnabled()
|
|
447
|
+
client.utils.isSteamInBigPictureMode()
|
|
448
|
+
client.utils.getUiLanguage() // for example 'english'
|
|
449
|
+
|
|
450
|
+
// input: digital and analog action data, and the origins bound to an action
|
|
451
|
+
const pressed = client.input.getDigitalActionData(controllerHandle, actionHandle).state
|
|
452
|
+
const stick = client.input.getAnalogActionData(controllerHandle, analogHandle) // { x, y, active }
|
|
453
|
+
const origins = client.input.getDigitalActionOrigins(controllerHandle, actionSetHandle, actionHandle)
|
|
454
|
+
const label = client.input.getStringForActionOrigin(origins[0] ?? 0)
|
|
455
|
+
|
|
456
|
+
// networking: P2P packets on a channel, and closing a session
|
|
457
|
+
client.networking.sendP2PPacketOnChannel(friendSteamId, SendType.Reliable, Buffer.from('hi'), 2)
|
|
458
|
+
client.networking.isP2PPacketAvailableOnChannel(2)
|
|
459
|
+
client.networking.closeP2PSession(friendSteamId)
|
|
460
|
+
|
|
461
|
+
// cloud: share a file (a Promise), stop syncing it, and choose the platforms it syncs to
|
|
462
|
+
const shareHandle = await client.cloud.shareFile('save.json')
|
|
463
|
+
client.cloud.forgetFile('save.json')
|
|
464
|
+
client.cloud.setSyncPlatforms('save.json', 0 /* SyncPlatform.None */)
|
|
465
|
+
```
|
|
466
|
+
|
|
467
|
+
- `getStringForActionOrigin` and `getGlyphForActionOrigin` return an empty string for an origin number that Steam does not define.
|
|
468
|
+
- `getSyncPlatforms` rejects when a file syncs to a combination of platforms, because a single `SyncPlatform` value cannot describe it.
|
|
469
|
+
- `shareFile` resolves once Steam answers, which happens through `runCallbacks()`. `init()` runs it for you.
|
|
470
|
+
|
|
471
|
+
## Release Process
|
|
472
|
+
|
|
473
|
+
This project uses automated release management:
|
|
474
|
+
|
|
475
|
+
- **Pre-releases**: Automatically created on each push to the `main` branch for testing
|
|
476
|
+
- **Official releases**: Created when a version tag is pushed (e.g., `v1.2.3`)
|
|
477
|
+
|
|
478
|
+
Release notes are automatically generated from commit messages. See [CONTRIBUTING.md](./CONTRIBUTING.md) for the commit message format.
|
package/callbacks.d.ts
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import client = require('./client')
|
|
2
|
+
|
|
3
|
+
export const enum ChatMemberStateChange {
|
|
4
|
+
/** This user has joined or is joining the lobby. */
|
|
5
|
+
Entered,
|
|
6
|
+
/** This user has left or is leaving the lobby. */
|
|
7
|
+
Left,
|
|
8
|
+
/** User disconnected without leaving the lobby first. */
|
|
9
|
+
Disconnected,
|
|
10
|
+
/** The user has been kicked. */
|
|
11
|
+
Kicked,
|
|
12
|
+
/** The user has been kicked and banned. */
|
|
13
|
+
Banned,
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Serde form of steamworks-rs `SteamError`: a unit variant name such as `"NoConnection"`,
|
|
18
|
+
* or `{ UnknownResult: code }` for a Steam result code that has no name.
|
|
19
|
+
*/
|
|
20
|
+
export type SteamErrorName = string | { UnknownResult: number }
|
|
21
|
+
|
|
22
|
+
/** Serde form of `Result<(), SteamError>`: `{ Ok: null }` on success, `{ Err: ... }` on failure. */
|
|
23
|
+
export type SteamResult = { Ok: null } | { Err: SteamErrorName }
|
|
24
|
+
|
|
25
|
+
/** Serde form of steamworks-rs `ChatEntryType` (a unit variant name). */
|
|
26
|
+
export type ChatEntryTypeName =
|
|
27
|
+
| 'Invalid'
|
|
28
|
+
| 'ChatMsg'
|
|
29
|
+
| 'Typing'
|
|
30
|
+
| 'InviteGame'
|
|
31
|
+
| 'Emote'
|
|
32
|
+
| 'LeftConversation'
|
|
33
|
+
| 'Entered'
|
|
34
|
+
| 'WasKicked'
|
|
35
|
+
| 'WasBanned'
|
|
36
|
+
| 'Disconnected'
|
|
37
|
+
| 'HistoricalChat'
|
|
38
|
+
| 'LinkBlocked'
|
|
39
|
+
|
|
40
|
+
/** Serde form of steamworks-rs `ChatRoomEnterResponse` (a unit variant name). */
|
|
41
|
+
export type ChatRoomEnterResponseName =
|
|
42
|
+
| 'Success'
|
|
43
|
+
| 'DoesntExist'
|
|
44
|
+
| 'NotAllowed'
|
|
45
|
+
| 'Full'
|
|
46
|
+
| 'Error'
|
|
47
|
+
| 'Banned'
|
|
48
|
+
| 'Limited'
|
|
49
|
+
| 'ClanDisabled'
|
|
50
|
+
| 'CommunityBan'
|
|
51
|
+
| 'MemberBlockedYou'
|
|
52
|
+
| 'YouBlockedMember'
|
|
53
|
+
| 'RatelimitExceeded'
|
|
54
|
+
|
|
55
|
+
/** Serde form of steamworks-rs `AuthSessionValidateError` (a unit variant name). */
|
|
56
|
+
export type AuthSessionValidateErrorName =
|
|
57
|
+
| 'UserNotConnectedToSteam'
|
|
58
|
+
| 'NoLicenseOrExpired'
|
|
59
|
+
| 'VACBanned'
|
|
60
|
+
| 'LoggedInElseWhere'
|
|
61
|
+
| 'VACCheckTimedOut'
|
|
62
|
+
| 'AuthTicketCancelled'
|
|
63
|
+
| 'AuthTicketInvalidAlreadyUsed'
|
|
64
|
+
| 'AuthTicketInvalid'
|
|
65
|
+
| 'PublisherIssuedBan'
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Measured with napi-rs 2.16 (`serde-json` and `napi6` features): a JSON integer within
|
|
69
|
+
* +/-(2^53 - 1) reaches JavaScript as a `number`, and a larger one as a `bigint`.
|
|
70
|
+
* SteamIDs, lobby IDs and friend IDs are always beyond 2^53, so they are `bigint`.
|
|
71
|
+
* `GameId` and `PublishedFileId` are `u64` values that may be small, so they are `number | bigint`.
|
|
72
|
+
*/
|
|
73
|
+
export interface CallbackReturns {
|
|
74
|
+
[client.callback.SteamCallback.PersonaStateChange]: {
|
|
75
|
+
steam_id: bigint
|
|
76
|
+
flags: { bits: number }
|
|
77
|
+
}
|
|
78
|
+
[client.callback.SteamCallback.SteamServersConnected]: {}
|
|
79
|
+
[client.callback.SteamCallback.SteamServersDisconnected]: {
|
|
80
|
+
reason: number
|
|
81
|
+
}
|
|
82
|
+
[client.callback.SteamCallback.SteamServerConnectFailure]: {
|
|
83
|
+
reason: number
|
|
84
|
+
still_retrying: boolean
|
|
85
|
+
}
|
|
86
|
+
[client.callback.SteamCallback.LobbyDataUpdate]: {
|
|
87
|
+
lobby: bigint
|
|
88
|
+
member: bigint
|
|
89
|
+
success: boolean
|
|
90
|
+
}
|
|
91
|
+
[client.callback.SteamCallback.LobbyChatUpdate]: {
|
|
92
|
+
lobby: bigint
|
|
93
|
+
user_changed: bigint
|
|
94
|
+
making_change: bigint
|
|
95
|
+
member_state_change: ChatMemberStateChange
|
|
96
|
+
}
|
|
97
|
+
[client.callback.SteamCallback.P2PSessionRequest]: {
|
|
98
|
+
remote: bigint
|
|
99
|
+
}
|
|
100
|
+
[client.callback.SteamCallback.P2PSessionConnectFail]: {
|
|
101
|
+
remote: bigint
|
|
102
|
+
error: number
|
|
103
|
+
}
|
|
104
|
+
[client.callback.SteamCallback.GameLobbyJoinRequested]: {
|
|
105
|
+
lobby_steam_id: bigint
|
|
106
|
+
friend_steam_id: bigint
|
|
107
|
+
}
|
|
108
|
+
[client.callback.SteamCallback.MicroTxnAuthorizationResponse]: {
|
|
109
|
+
app_id: number
|
|
110
|
+
order_id: number | bigint
|
|
111
|
+
authorized: boolean
|
|
112
|
+
}
|
|
113
|
+
[client.callback.SteamCallback.DownloadItemResult]: {
|
|
114
|
+
app_id: number
|
|
115
|
+
published_file_id: number | bigint
|
|
116
|
+
/** `null` when the item downloaded without error. */
|
|
117
|
+
error: SteamErrorName | null
|
|
118
|
+
}
|
|
119
|
+
[client.callback.SteamCallback.UserStatsReceived]: {
|
|
120
|
+
steam_id: bigint
|
|
121
|
+
game_id: number | bigint
|
|
122
|
+
result: SteamResult
|
|
123
|
+
}
|
|
124
|
+
[client.callback.SteamCallback.UserStatsStored]: {
|
|
125
|
+
game_id: number | bigint
|
|
126
|
+
result: SteamResult
|
|
127
|
+
}
|
|
128
|
+
[client.callback.SteamCallback.UserAchievementStored]: {
|
|
129
|
+
game_id: number | bigint
|
|
130
|
+
achievement_name: string
|
|
131
|
+
current_progress: number
|
|
132
|
+
max_progress: number
|
|
133
|
+
}
|
|
134
|
+
[client.callback.SteamCallback.GameOverlayActivated]: {
|
|
135
|
+
active: boolean
|
|
136
|
+
}
|
|
137
|
+
[client.callback.SteamCallback.GameRichPresenceJoinRequested]: {
|
|
138
|
+
friend_steam_id: bigint
|
|
139
|
+
connect: string
|
|
140
|
+
}
|
|
141
|
+
[client.callback.SteamCallback.LobbyEnter]: {
|
|
142
|
+
lobby: bigint
|
|
143
|
+
chat_permissions: number
|
|
144
|
+
blocked: boolean
|
|
145
|
+
chat_room_enter_response: ChatRoomEnterResponseName
|
|
146
|
+
}
|
|
147
|
+
[client.callback.SteamCallback.LobbyChatMsg]: {
|
|
148
|
+
lobby: bigint
|
|
149
|
+
user: bigint
|
|
150
|
+
chat_entry_type: ChatEntryTypeName
|
|
151
|
+
chat_id: number
|
|
152
|
+
}
|
|
153
|
+
/** Unit struct: serialises to `null`. */
|
|
154
|
+
[client.callback.SteamCallback.ScreenshotRequested]: null
|
|
155
|
+
[client.callback.SteamCallback.ScreenshotReady]: {
|
|
156
|
+
/** `{ Ok: handle }` with the library handle, or `{ Err: ... }` when the screenshot could not be saved. */
|
|
157
|
+
local_handle: { Ok: number } | { Err: 'Fail' | 'IoFailure' }
|
|
158
|
+
}
|
|
159
|
+
/** Unit struct: serialises to `null`. */
|
|
160
|
+
[client.callback.SteamCallback.NewUrlLaunchParameters]: null
|
|
161
|
+
[client.callback.SteamCallback.AuthSessionTicketResponse]: {
|
|
162
|
+
ticket: number
|
|
163
|
+
result: SteamResult
|
|
164
|
+
}
|
|
165
|
+
[client.callback.SteamCallback.ValidateAuthTicketResponse]: {
|
|
166
|
+
steam_id: bigint
|
|
167
|
+
response: { Ok: null } | { Err: AuthSessionValidateErrorName }
|
|
168
|
+
owner_steam_id: bigint
|
|
169
|
+
}
|
|
170
|
+
}
|