Steam API in C#

this is a class I wrote for Mysterious Space, to handle loading and dealing with the Steam API. C# support for the Steam API is not super-great… there are some other wrappers/libraries out there, but I wanted to use the base API provided by Valve/Steam.

there’s not a lot of information out there on how to do this, so I’m posting my own implementation in the hopes that it may help other C#/Steam developers. it works in both 32 and 64-bit environments. you may notice that it only provides easy access to Steam achievements! (that’s all I’m using right now, in Mysterious Space.) with a little research, though, you could extend this to handle other features, as well.

(the following code has been dedicated to the public domain! feel free to use it in your own project!)

using System;
using System.IO;
using System.Diagnostics;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Valve.Steamworks;

// remember to add steam_api_interop.cs to your project; it's included
// with the Steamworks SDK <https://partner.steamgames.com/>

// you may want to use a different namespace :P
namespace MysteriousSpace
{
    public class SteamIntegration
    {
        // this is a singleton class; here is our instance and accessor:
        private static SteamIntegration _instance = null;

        public static SteamIntegration Instance { get { return _instance; } }

        // to load the steam_api.dll, we will use LoadLibrary from kernel32
        // (worry not, ye 64-bit app developers: kernel32 is a misnomer; this
        // DLL is used in both 32 and 64-bit environments.)
        [DllImport("kernel32.dll")]
        public static extern IntPtr LoadLibrary(string dllToLoad);

        // be sure to init the singleton with a call to SteamIntegration.Init()!
        public static void Init()
        {
            // this bit of logic determines if we're 64 or 32-bit, and loads
            // the appropriate copy of steam_api.dll
            string fileName = Environment.Is64BitProcess ? "SteamAPI\\64\\steam_api.dll" : "SteamAPI\\32\\steam_api.dll";

            fileName = Path.GetFullPath(fileName);

            LoadLibrary(fileName);

            // TheGame is a class specific to Mysterious Space, and stores
            // various info, including library versions. feel free to change
            // this var name (perhaps making it a member of this class), or
            // get rid of it entirely.
            TheGame.SteamAPIVersion = FileVersionInfo.GetVersionInfo(fileName).FileVersion;

            // you can find your steam app id on the steamworks website
            SteamAPI.Init(YOUR_STEAM_APP_ID_HERE);

            _instance = new SteamIntegration();
        }

        private ISteamUserStats _user_stats;

        // private constructor (again: I chose to implement this as a
        // singleton; you don't have to)
        private SteamIntegration()
        {
            _user_stats = SteamAPI.SteamUserStats();

            // another Mysterious Space var I keep, used to determine if
            // Steam integration is available, and not call certain methods
            // if it isn't.
            TheGame.STEAM_INTEGRATION_ENABLED = (_user_stats.GetIntPtr() != IntPtr.Zero);
        }

        // don't call this unless TheGame.STEAM_INTEGRATION_ENABLED is true
        // (you could add a check for this, and throw an exception, or
        // something... I chose to make this check elsewhere in the game.)
        public bool HasAchievement(string name)
        {
            bool achieved = false;
            _user_stats.GetAchievement(name, ref achieved);
            
            return achieved;
        }

        public void SetAchievement(string name)
        {
            _user_stats.SetAchievement(name);

            // could be useful, when debugging:
            //Console.Write("got achievement " + name);
        }

        public void CheckForNewAchievements(Sector s)
        {
            // your achievement-checking code here; call SetAchievement if the
            // achievement's conditions are met, and HasAchievement returns
            // false
        }
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s