Welcome to The Forum

Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads

Steamid C++ Class Source Code


Recommended Posts

Here's a SteamID C++ class I wrote to make handling SteamIDs extremely easy.

 

Constructed with from std::string of a Steam ID in either legacy (STEAM_0:1:xxxxxx) or ID3 (U:1:xxxxxx) format.

 

Can also be constructed from an 64 bit ID struct, aka the long number in steam community profile urls.

 

The class takes up only 4 bytes ( sizeof(uint32_t) ) of memory for each SteamID object.

 

operator== for equality checking against other SteamIDs

operator<< and operator>> for serialization (saving to file or database) and is human readable (std::cout)

 

SteamIDs constructed from any format can easily be converted with the id() id3() id_i32() and if_i64() functions.

The conversion is built on each function call making the object itself have a footprint of only 4 bytes since based on my observation, the account id (32bits) is the only meaningful part of a SteamID.

 

The Code:

 

Header

#pragma once
#include <string>
#include <vector>
#include <cstdint>

// For splitting strings
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems);
std::vector<std::string> split(const std::string &s, char delim);

// https://developer.valvesoftware.com/wiki/SteamID
// When representing a SteamID in a program you only need
//   32bits to hold the actual account ID. Although valve
//   says that a 64bit data struct is required, the upper
//   32 bits of that struct are always identical for every
//   SteamID
class SteamID {
public:
 // Default constructor for empty SteamID
 SteamID();
 // Construct from either SteamID or SteamID3
 SteamID(std::string &strSteamID);
 // Construct from SteadID64
 SteamID(uint64_t &_steamID64);
 // Construct copy
 SteamID(const SteamID& from);
~SteamID();
// serialization, for human readable output use id() or id3()
friend std::ostream& operator<<(std::ostream &os, const SteamID& s);
friend std::istream& operator>>(std::istream &is, SteamID& s);
// returns legacy SteamID
std::string id() const;
// returns SteamID3
std::string id3() const;
// returns SteamID64
uint64_t id_i64() const;
// returns account id
uint32_t id_i32() const;
private:
// The internal lower 32bits of SteamID64 (account ID)
uint32_t _ID;
};

 

 

Source

#include "SteamID.hpp"
#include <sstream>
#define ID64_HIGHPART 0x0110000100000000
SteamID::SteamID()
 : _ID(0)
{}
SteamID::SteamID(std::string &strSteamID)
 : _ID(0)
{
 std::vector<std::string> IDTokens = split(strSteamID, ':');
 if (IDTokens.size() == 3)
 {
   if (strSteamID[0] == 'S')
   {
  _ID += strtoll(IDTokens[1].c_str(), nullptr, 10);
  _ID += (strtoll(IDTokens[2].c_str(), nullptr, 10) * 2);
   }
   else if (strSteamID[0] == 'U')
   {
  _ID += strtoll(IDTokens[2].c_str(), nullptr, 10);
  if (!(_ID % 2))
    _ID++;
   }
   else
   { // failed
  _ID = 0;
   }
 }
 else
 { // failed
   _ID = 0;
 }
}
SteamID::SteamID(uint64_t &_steamID64)
{
 if ((_steamID64 & ID64_HIGHPART) == ID64_HIGHPART)
 {
   // cast away upper 32 bits
   _ID = static_cast<uint32_t>(_steamID64);
 }
 else
 { // failed
   _ID = 0;
 }
}
SteamID::SteamID(const SteamID & from)
: _ID(from._ID)
{}
SteamID::~SteamID(){}
std::string SteamID::id() const
{
 std::string strID("STEAM_0:");
 if (_ID % 2)
 {
   strID.append("1:");
 }
 else
 {
   strID.append("0:");
 }
 strID.append(std::to_string(_ID / 2));
 return strID;
}
std::string SteamID::id3() const
{
 std::string strID3("U:1:");
 strID3.append(std::to_string(_ID));
 return strID3;
}
uint64_t SteamID::id_i64() const
{ return (static_cast<uint64_t>(_ID) | ID64_HIGHPART); }
uint32_t SteamID::id_i32() const
{ return uint32_t(_ID); }
std::ostream& operator<<(std::ostream& os, const SteamID& s)
{
 os << s._ID;
 return os;
}
std::istream & operator >> (std::istream & is, SteamID & s)
{
 is >> s._ID;
 return is;
}

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems)
{
 std::stringstream ss(s);
 std::string item;
 while (std::getline(ss, item, delim))
   elems.push_back(item);
 return elems;
}

std::vector<std::string> split(const std::string &s, char delim)
{
 std::vector<std::string> elems;
 split(s, delim, elems);
 return elems;
}

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share