CS2-Kit
C++23 library for CS2 Metamod:Source plugin development
Loading...
Searching...
No Matches
SteamId.cpp
Go to the documentation of this file.
1#include <CS2Kit/Utils/SteamId.hpp>
2#include <format>
3#include <regex>
4
5namespace CS2Kit::Utils
6{
7
8std::string SteamId::ToSteamId3(int64_t steamId64)
9{
10 uint32_t accountId = GetAccountId(steamId64);
11 return std::format("[U:1:{}]", accountId);
12}
13
14std::string SteamId::ToSteamId(int64_t steamId64)
15{
16 uint32_t accountId = GetAccountId(steamId64);
17 uint32_t authServer = accountId % 2;
18 uint32_t accountNum = accountId / 2;
19 return std::format("STEAM_0:{}:{}", authServer, accountNum);
20}
21
22std::optional<int64_t> SteamId::FromSteamId3(const std::string& steamId3)
23{
24 std::regex pattern(R"(\[U:1:(\d+)\])");
25 std::smatch matches;
26 if (std::regex_match(steamId3, matches, pattern))
27 {
28 try
29 {
30 uint32_t accountId = std::stoul(matches[1].str());
31 return SteamId64Base + accountId;
32 }
33 catch (...)
34 {
35 return std::nullopt;
36 }
37 }
38 return std::nullopt;
39}
40
41std::optional<int64_t> SteamId::FromSteamId(const std::string& steamId)
42{
43 std::regex pattern(R"(STEAM_[0-5]:([01]):(\d+))");
44 std::smatch matches;
45 if (std::regex_match(steamId, matches, pattern))
46 {
47 try
48 {
49 uint32_t authServer = std::stoul(matches[1].str());
50 uint32_t accountNum = std::stoul(matches[2].str());
51 uint32_t accountId = accountNum * 2 + authServer;
52 return SteamId64Base + accountId;
53 }
54 catch (...)
55 {
56 return std::nullopt;
57 }
58 }
59 return std::nullopt;
60}
61
62bool SteamId::IsValid(int64_t steamId64)
63{
64 return steamId64 >= SteamId64Base && steamId64 < (SteamId64Base + 0x100000000LL);
65}
66
67uint32_t SteamId::GetAccountId(int64_t steamId64)
68{
69 return static_cast<uint32_t>(steamId64 - SteamId64Base);
70}
71
72} // namespace CS2Kit::Utils