Greenworks

repository·master·Indexed 23 days ago

https://github.com/greenheartgames/greenworks

A 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.

Tokens
17K
Snippets
19
Records
114
Agent score
81%

What's inside greenworks

  1. What is Greenworks?

    master
    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.
  2. Supported Runtimes and SDKs

    master

    Greenworks 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.

  3. How to listen for Steam events with greenworks

    master

    The greenworks module acts as an EventEmitter, allowing you to listen for various Steam-related events. To use these APIs, you must first call greenworks.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.

  4. Work with Encrypted App Tickets

    master

    Encrypted 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 the encrypted_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 match greenworks.EncryptedAppTicketSymmetricKeyLength.
    • Returns: A Buffer representing the decrypted ticket if successful; otherwise null.

    3. Inspect Decrypted Ticket Data

    Once decrypted, use these methods to extract information:

    • greenworks.isTicketForApp(decrypted_ticket, app_id): Returns Boolean indicating if the ticket is for the specified app_id.
    • greenworks.getTicketAppId(decrypted_ticket): Returns the Integer App ID.
    • greenworks.getTicketSteamId(decrypted_ticket): Returns a SteamID object.
    • greenworks.getTicketIssueTime(decrypted_ticket): Returns an Integer representing 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; });
  5. Setup Authentication APIs

    master

    Authentication 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:

    1. Locate <steam_sdk-path>/public/steam/lib/ in your Steamworks SDK.
    2. Copy sdkencryptedappticket.dll (Windows), libsdkencryptedappticket.dylib (macOS), or libsdkencryptedappticket.so (Linux).
    3. Paste it into your <greenworks>/lib directory.
  6. Set up Greenworks with NW.js using prebuilt binaries

    master

    To use Greenworks in an NW.js application without building from source, follow these steps to arrange the required binaries and configuration files:

    1. Download Greenworks Binaries: Download the release binaries from the releases page and unzip them to your project directory (<greenworks_path>).
    2. 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.
    3. 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 (or steam_api64.dll for 64-bit)
      • macOS: libsteam_api.dylib
      • Linux: libsteam_api.so
    4. 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 (or sdkencryptedappticket64.dll for 64-bit)
      • macOS: libsdkencryptedappticket.dylib
      • Linux: libsdkencryptedappticket.so
    5. Configure AppID: Create a steam_appid.txt file containing your Steam AppID (use 480 for 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 win64 versions of the DLLs.

  7. Obtain and configure the Steamworks SDK for Greenworks

    master

    Greenworks 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

    1. Download: Log in to the official Steamworks website and download the SDK.
    2. Extract: Unzip the downloaded file.
    3. Rename: Locate the extracted sdk subdirectory and rename it to steamworks_sdk.
    4. Install: You can provide the SDK to Greenworks using one of two methods:
      • Manual Placement: Copy the steamworks_sdk directory into the <greenworks_src_dir>/deps/ folder.
      • Environment Variable: Set the STEAMWORKS_SDK_PATH environment variable to point to the absolute path of your steamworks_sdk directory.

    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.

  8. Use the Greenworks P2P API for networking

    master

    Greenworks 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.sendP2PPacket to transmit data. You must specify the recipient's steamId, the sendType (from the eP2PSendType enum), the data as a Buffer, and a nChannel number.

    Receiving Packets

    To check for incoming data, use greenworks.isP2PPacketAvailable(nChannel). If data is available, use greenworks.readP2PPacket(size, nChannel) to retrieve the Buffer and the steamIDRemote of 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 a result (boolean) and a connectionState object.
    • Closing Sessions: Use greenworks.closeP2PSessionWithUser(steamIDUser) to end a session, or greenworks.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.

  9. Handle friend chat messages

    master

    To receive chat messages from friends, you must first enable the listener using greenworks.setListenForFriendsMessage(true). Once enabled, you can listen for the game-connected-friend-chat-message event. Inside the listener, use greenworks.getFriendMessage to 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.");
      }
    });
  10. Initialize the Steam API

    master

    To use Greenworks, you must first initialize the Steam API. There are two primary methods for this:

    1. greenworks.initAPI(): Returns a Boolean indicating if initialization was successful. Note for testing: You must have the Steam Client running and logged in. You must also create a steam_appid.txt file containing your Steam APP ID (or the Steamworks example APP ID) in your application directory.
    2. greenworks.init(): Returns true if 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 returns true, your app is being restarted.

  11. Build Greenworks for Node.js

    master

    To build the Greenworks Node.js addon from source, you must have the Steamworks SDK installed and use node-gyp to compile the C++ code. After a successful build, the resulting binary (e.g., greenworks-linux.node, greenworks-win.node, or greenworks-osx.node) will be located in the build/Release directory.

    # 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