Code Monkey home page Code Monkey logo

ez-steam-api's Introduction

ez-steam-api

This is a synchronous/blocking, flat, C-style wrapper around the Steamworks SDK, originally designed for use via foreign function interfaces.

This repository also includes a Node/JavaScript wrapper that is based on Koffi. I used that wrapper for an Electron-based game on Steam.

Motivation

The Steamworks API is native C++, with an optional flat/C-style API. Unfortunately, I found both provided options cumbersome in my project, for a few reasons:

  • Callbacks are designed to be polled each frame--a hassle for event-driven environments
  • Callbacks and call results are C++ member functions, with no out-of-the-box support for blocking (synchronous results) or promises
  • The language I was using (JavaScript) doesn't have a good C++ interop story (whereas C interop is ubiquitous)

Design

Internally, ez-steam-api spins up a thread for running Steam's callbacks and takes care of marshaling results back to calling threads (who just see a trivial synchronous API).

The synchronous, flat API can be trivially used with C-based foreign function interfaces that many languages provide. See the JavaScript wrapper in this repository for an example.

Functionality and status

Currently, this library only exposes the functionality I needed for my game (listed below). Note: The code quality is "hobby ready" (as opposed to "production ready"). I have tested on Windows (VS 2019) and Linux (G++ 9) only.

  • Retrieve player's "persona name" (display name)
  • Retrieve the player's preferred language for this game
  • Get, set, and persist achievements
  • Set leaderboard scores
  • Retrieve friends' leaderboard scores

Feel free to open an issue or pull request to add additional functionality.

Usage

For Electron/Node/JavaScript usage, see the README for the npm package.

Here is an example using the C API:

#include <stdlib.h>
#include <stdio.h>
#include "ez-steam-api.h"

int main(int argc, char** argv) {
    unsigned int app_id = 12345; // Replace with your app id
    int should_exit = 0;

    // Initialize Steam
    if (!ez_steam_start(app_id, &should_exit)) {
        return -1;
    }
    else if (should_exit) {
        return 0;
    }

    // Print the user's "persona" (display) name
    char* name = 0;
    if (ez_steam_user_name_get(&name)) {
        printf("Name: %s\n", name);
        ez_steam_string_free(name);
    }

    // Print the user's language selection for this app/game
    char *language = 0;
    if (ez_steam_app_language_get(&language)) {
        printf("Language: %s\n", language);
        ez_steam_string_free(language);
    }

    // Set an achievement (this assumes an achievement named "FOOBAR" exists for the app)
    int newly_achieved = 0;
    if (ez_steam_achievement_set("FOOBAR", &newly_achieved) && newly_achieved) {
        ez_steam_achivements_store();
    }

    // Get a friend leaderboard (this assumes a leaderboard named "Score" exists for the app)
    unsigned long long leaderboard = 0;
    if (ez_steam_leaderboard_get("Score", &leaderboard)) {
        char *json = 0;
        if (ez_steam_leaderboard_get_friend_scores(leaderboard, &json)) {
            printf("Leaderboard as JSON: %s\n", json);
            ez_steam_string_free(json);
        }
    }

    ez_steam_stop();
    return 0;
}

ez-steam-api's People

Contributors

jaredkrinke avatar

Watchers

 avatar  avatar

ez-steam-api's Issues

Add license

Make sure to include dependency licenses too!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.