Greenworks
repository·master·Indexed 23 days ago
https://github.com/greenheartgames/greenworksA Node.js addon (v0.22.0) that exposes Valve's Steamworks API (v1.62) to JavaScript. It enables HTML5 games and applications built with NW.js or Electron to integrate Steam features such as achievements, cloud saves, matchmaking, workshop, and authentication session tickets.
What's inside greenworks
- Greenworks is a Node.js addon designed to integrate HTML5 games or applications with the Steamworks API. It exposes various Steamworks functionalities to JavaScript, allowing developers to implement Steam features in web-based environments like NW.js or Electron.
Supported Runtimes and SDKs
masterGreenworks supports the following environments and versions:
- Node.js: v0.8, v0.10, v0.12, v4, v5, v6, v7, v8, v9, and v10+
- NW.js: v0.8, v0.11+
- Electron: v1.0.0+
- Steam SDK: v1.62
It is built using Native Abstractions for Node.js (NAN) to ensure compatibility across different Node.js versions.
How to listen for Steam events with greenworks
masterThe
greenworksmodule acts as anEventEmitter, allowing you to listen for various Steam-related events. To use these APIs, you must first callgreenworks.init()to initialize the module.Use
greenworks.on(eventName, callback)to register listeners for specific events. The arguments passed to the callback depend on the specific event being emitted.Work with Encrypted App Tickets
masterEncrypted App Tickets allow you to obtain authenticated Steam IDs from clients without network requests to Steam's API servers. They can be decrypted locally using your game's secret key.
1. Generate an Encrypted App Ticket
Use
greenworks.getEncryptedAppTicket(user_data, success_callback, [error_callback]).user_data(String): Arbitrary UTF-8 encoded data to be encrypted into the ticket.success_callback: Receives theencrypted_ticket(Buffer).
2. Decrypt the Ticket
Use
greenworks.decryptAppTicket(encrypted_ticket, decryption_key).encrypted_ticket(Buffer): The ticket to decrypt.decryption_key(Buffer): The secret key. Its length must matchgreenworks.EncryptedAppTicketSymmetricKeyLength.- Returns: A
Bufferrepresenting the decrypted ticket if successful; otherwisenull.
3. Inspect Decrypted Ticket Data
Once decrypted, use these methods to extract information:
greenworks.isTicketForApp(decrypted_ticket, app_id): ReturnsBooleanindicating if the ticket is for the specifiedapp_id.greenworks.getTicketAppId(decrypted_ticket): Returns theIntegerApp ID.greenworks.getTicketSteamId(decrypted_ticket): Returns aSteamIDobject.greenworks.getTicketIssueTime(decrypted_ticket): Returns anIntegerrepresenting the issue time.
var greenworks = require('./greenworks'); greenworks.init(); greenworks.getEncryptedAppTicket('test_content', function(ticket) { console.log("ticket: " + ticket.toString('hex')); // Specify the secret key (must be 32 bytes for symmetric key) var key = new Buffer(32); // TODO: initialize Buffer key with your game's secret key assert(key.length == greenworks.EncryptedAppTicketSymmetricKeyLength); var decrypted_app_ticket = greenworks.decryptAppTicket(ticket, key); if (decrypted_app_ticket) { console.log(greenworks.isTicketForApp(decrypted_app_ticket, greenworks.getAppId())); console.log(greenworks.getTicketAppId(decrypted_app_ticket)); console.log(greenworks.getTicketSteamId(decrypted_app_ticket)); console.log(greenworks.getTicketIssueTime(decrypted_app_ticket)); } }, function(err) { throw err; });Initialize Greenworks in an Electron renderer process
masterIf you are using Greenworks within an Electron renderer process, you must callprocess.activateUvLoop()before initializing Greenworks. Failure to do this will prevent the Greenworks event loop from running.Setup Authentication APIs
masterAuthentication APIs require an additional dynamic library from the Steamworks SDK. You must manually copy the appropriate library file from the Steamworks SDK to your application's Greenworks library directory:
- Locate
<steam_sdk-path>/public/steam/lib/in your Steamworks SDK. - Copy
sdkencryptedappticket.dll(Windows),libsdkencryptedappticket.dylib(macOS), orlibsdkencryptedappticket.so(Linux). - Paste it into your
<greenworks>/libdirectory.
- Locate
Set up Greenworks with NW.js using prebuilt binaries
masterTo use Greenworks in an NW.js application without building from source, follow these steps to arrange the required binaries and configuration files:
- Download Greenworks Binaries: Download the release binaries from the releases page and unzip them to your project directory (
<greenworks_path>). - Download Steamworks SDK: Download the Steamworks SDK and unzip it to a local directory (
<steam_sdk_path>). Ensure the SDK version matches the version supported by your Greenworks prebuilt binaries. - Copy Steam API Library: Copy the appropriate Steam API library from
<steam_sdk_path>/redistributable_bin/[win64|linux32|linux64|osx32]to<greenworks_path>/lib/.- Windows:
steam_api.dll(orsteam_api64.dllfor 64-bit) - macOS:
libsteam_api.dylib - Linux:
libsteam_api.so
- Windows:
- Copy Encrypted App Ticket Library: Copy the encrypted app ticket library from
<steam_sdk_path>/public/steam/lib/[win32|win64|linux32|linux64|osx32]to<greenworks_path>/lib/.- Windows:
sdkencryptedappticket.dll(orsdkencryptedappticket64.dllfor 64-bit) - macOS:
libsdkencryptedappticket.dylib - Linux:
libsdkencryptedappticket.so
- Windows:
- Configure AppID: Create a
steam_appid.txtfile containing your Steam AppID (use480for testing with the Steamworks example) in the<greenworks_path>/directory. This is only required for development; Steam handles this automatically when launching a published game.
Important Architecture Note: Ensure the architecture of the Steam dynamic libraries matches both your OS and your NW.js version. For example, if using 64-bit NW.js on 64-bit Windows, use the
win64versions of the DLLs.- Download Greenworks Binaries: Download the release binaries from the releases page and unzip them to your project directory (
Obtain and configure the Steamworks SDK for Greenworks
masterGreenworks requires the Steamworks SDK to function. Because of licensing restrictions, the SDK is not included in the Greenworks repository and must be provided by the developer.
Setup Steps
- Download: Log in to the official Steamworks website and download the SDK.
- Extract: Unzip the downloaded file.
- Rename: Locate the extracted
sdksubdirectory and rename it tosteamworks_sdk. - Install: You can provide the SDK to Greenworks using one of two methods:
- Manual Placement: Copy the
steamworks_sdkdirectory into the<greenworks_src_dir>/deps/folder. - Environment Variable: Set the
STEAMWORKS_SDK_PATHenvironment variable to point to the absolute path of yoursteamworks_sdkdirectory.
- Manual Placement: Copy the
Version Compatibility
Greenworks is designed to work with Steamworks SDK v1.41. If a newer version is released, ensure you download the version compatible with your specific Greenworks build.
Use the Greenworks P2P API for networking
masterGreenworks provides a P2P (Peer-to-Peer) networking interface based on Steamworks. You can send packets, manage sessions, and check connection states between users via their Steam IDs.
Sending Packets
Use
greenworks.sendP2PPacketto transmit data. You must specify the recipient'ssteamId, thesendType(from theeP2PSendTypeenum), thedataas aBuffer, and anChannelnumber.Receiving Packets
To check for incoming data, use
greenworks.isP2PPacketAvailable(nChannel). If data is available, usegreenworks.readP2PPacket(size, nChannel)to retrieve theBufferand thesteamIDRemoteof the sender.Session Management
- Accepting Sessions: When a user requests a connection, use
greenworks.acceptP2PSessionWithUser(steamId)to establish the link. - Checking State: Use
greenworks.getP2PSessionState(steamIDUser)to get aresult(boolean) and aconnectionStateobject. - Closing Sessions: Use
greenworks.closeP2PSessionWithUser(steamIDUser)to end a session, orgreenworks.closeP2PChannelWithUser(steamIDUser, nChannel)to close a specific channel.
Networking Environment
Use
greenworks.isBehindNAT()to determine if the user is behind a NAT, which may affect P2P connectivity.- Accepting Sessions: When a user requests a connection, use
Handle friend chat messages
masterTo receive chat messages from friends, you must first enable the listener using
greenworks.setListenForFriendsMessage(true). Once enabled, you can listen for thegame-connected-friend-chat-messageevent. Inside the listener, usegreenworks.getFriendMessageto retrieve the actual message content.// 1. Enable listening greenworks.setListenForFriendsMessage(true); // 2. Listen for the event greenworks.on('game-connected-friend-chat-message', function(steam_id, message_id) { // Retrieve message content var info = greenworks.getFriendMessage(steam_id.getRawSteamID(), message_id, 2048); if (info.chatEntryType == greenworks.ChatEntryType.ChatMsg) { var message = info.message; console.log("Receive a message from " + steam_id.getPersonaName() + ": " + message); // 3. Reply to the message greenworks.replyToFriendMessage(steam_id.getRawSteamID(), "Hello, I received your message."); } });Initialize the Steam API
masterTo use Greenworks, you must first initialize the Steam API. There are two primary methods for this:
greenworks.initAPI(): Returns aBooleanindicating if initialization was successful. Note for testing: You must have the Steam Client running and logged in. You must also create asteam_appid.txtfile containing your Steam APP ID (or the Steamworks example APP ID) in your application directory.greenworks.init(): Returnstrueif successful, otherwise it throws an error.
Use
restartAppIfNecessary(appId)to ensure the game is launched via Steam. If the app was not launched via Steam, this method signals Steam to launch the app and then causes your current process to quit. If it returnstrue, your app is being restarted.Build Greenworks for Node.js
masterTo build the Greenworks Node.js addon from source, you must have the Steamworks SDK installed and use
node-gypto compile the C++ code. After a successful build, the resulting binary (e.g.,greenworks-linux.node,greenworks-win.node, orgreenworks-osx.node) will be located in thebuild/Releasedirectory.# Change to the Greenworks source directory. cd <greenworks_src_dir> # Install the dependencies of Greenworks, "nan" module. npm install # Configure gyp project. node-gyp configure # Build Greenworks addon. node-gyp rebuild