- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSteamID.cpp
More file actions
Latest commit
207 lines (179 loc) · 6.47 KB
/
Copy pathSteamID.cpp
File metadata and controls
207 lines (179 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include"SteamID.hpp"
SteamID::SteamID(const std::string &id) {
if (id.empty()) {
this->universe = UNIVERSE_INVALID;
this->type = TYPE_INVALID;
this->instance = INSTANCE_ALL;
this->accountId = 0;
return;
}
// check if steam2 rendered
std::regex patternSteam2("^STEAM_([0-5]):([0-1]):([0-9]+)$");
std::smatch matches;
if (std::regex_search(id, matches, patternSteam2)) {
if (matches.size() < 4) {
return;
}
// matches[0]: full steam id
// matches[1]: universe type
// matches[2]: instance type
// matches[3]: account id
try {
int _universe = std::stoi(matches[1].str());
int _instance = std::stoi(matches[2].str());
int _account = std::stoi(matches[3].str());
this->universe = _universe;
if (_universe <= UNIVERSE_PUBLIC) {
this->universe = UNIVERSE_PUBLIC;
}
this->type = TYPE_INDIVIDUAL;
this->instance = INSTANCE_DESKTOP;
this->accountId = _account * 2 + _instance;
} catch (const std::exception &e) {
// throw a custom exception?
throwstd::invalid_argument("invalid steam id");
}
return;
}
// check if steam3 rendered
std::regex patternSteam3("^\\[([a-zA-Z]):([0-5]):([0-9]+)(:[0-9]+)?\\]$");
if (std::regex_search(id, matches, patternSteam3)) {
if (matches.size() < 5) {
return;
}
// matches[0]: full steam id
// matches[1]: type char
// matches[2]: universe type
// matches[3]: account id
// matches[4]: instance type
try {
int _universe = std::stoi(matches[2].str());
std::string _typeChar = matches[1].str();
int _account = std::stoi(matches[3].str());
std::string _instance = matches[4].str();
this->universe = _universe;
this->accountId = _account;
if (!_instance.empty()) {
std::string instanceStr = _instance.substr(1);
this->instance = std::stoi(instanceStr);
} else {
this->instance = INSTANCE_DESKTOP;
if (_typeChar == "g" || _typeChar == "T" || _typeChar == "c" || _typeChar == "L") {
this->instance = INSTANCE_ALL;
}
}
if (_typeChar == "c") {
this->instance |= CHAT_INSTANCE_FLAG_CLAN;
this->type = TYPE_CHAT;
} elseif (_typeChar == "L") {
this->instance |= CHAT_INSTANCE_FLAG_LOBBY;
this->type = TYPE_CHAT;
} else {
this->type = getTypeFromChar(_typeChar[0]);
}
} catch (const std::exception &e) {
// throw a custom exception?
throwstd::invalid_argument("invalid steam id");
}
return;
}
//check if steam 64
try {
longlong _id = std::stoll(id);
this->universe = static_cast<int>(_id >> 56);
this->type = static_cast<int>((_id >> 52) & 0xF);
this->instance = static_cast<int>((_id >> 32) & 0xFFFFF);
this->accountId = _id & 0xFFFFFFFF;
} catch (const std::exception &e) {
throwstd::invalid_argument("invalid steam id");
}
}
std::string SteamID::getIdSteam2Rendered(bool newerFormat = false) const {
if (this->type != TYPE_INDIVIDUAL) {
throwstd::runtime_error("Can't get Steam2 rendered ID for non-individual ID");
}
longlong _universe = this->universe;
if (_universe == 1 && !newerFormat) {
_universe = 0;
}
std::string steam2;
steam2.append("STEAM_")
.append(std::to_string(_universe))
.append(":")
.append(std::to_string((this->accountId & 1)))
.append(":")
.append(std::to_string((int) (this->accountId / 2)));
return steam2;
}
std::string SteamID::getIdSteam3Rendered() const {
char typeChar = getCharFromType(this->type) != '\0' ? getCharFromType(this->type) : 'i';
if (this->instance & CHAT_INSTANCE_FLAG_LOBBY) {
typeChar = 'c';
} elseif (this->instance & CHAT_INSTANCE_FLAG_LOBBY) {
typeChar = 'L';
}
bool renderInstance = (this->type == TYPE_ANON_GAMESERVER || this->type == TYPE_MULTISEAT ||
(this->type == TYPE_INDIVIDUAL && this->instance != INSTANCE_DESKTOP));
std::string steam3;
steam3.append("[")
.append(std::string(1, typeChar))
.append(":")
.append(std::to_string(this->universe))
.append(":")
.append(std::to_string(this->accountId));
if (renderInstance) {
steam3.append(":");
steam3.append(std::to_string(this->instance));
}
steam3.append("]");
return steam3;
}
longlongSteamID::getIdSteam64() const {
return ((this->universe << 56) | (this->type << 52) | (this->instance << 32) | (this->accountId));
}
intSteamID::getTypeFromChar(char c) {
constchar typeChars[] = {'I', 'U', 'M', 'G', 'A', 'P', 'C', 'g', 'T', 'a'};
for (int i = 0; i < sizeof typeChars; i++) {
if (c == typeChars[i]) {
return i;
}
}
returnTYPE_INVALID;
}
charSteamID::getCharFromType(longlong t) {
constchar typeChars[] = {'I', 'U', 'M', 'G', 'A', 'P', 'C', 'g', 'T', 'a'};
if (t < 0 || t > sizeof typeChars) {
return'\0';
}
return typeChars[t];
}
boolSteamID::isValid() const {
if (this->type <= TYPE_INVALID || this->type > TYPE_ANON_USER) {
returnfalse;
}
if (this->universe <= UNIVERSE_INVALID || this->universe > UNIVERSE_DEV) {
returnfalse;
}
if (this->type == TYPE_INDIVIDUAL && (this->accountId == 0 || this->instance > INSTANCE_WEB)) {
returnfalse;
}
if (this->type == TYPE_CLAN && (this->accountId == 0 || this->instance != INSTANCE_ALL)) {
returnfalse;
}
if (this->type == TYPE_GAMESERVER && this->accountId == 0) {
returnfalse;
}
returntrue;
}
boolSteamID::isClanChat() const {
returnthis->type == TYPE_CHAT && (this->instance & CHAT_INSTANCE_FLAG_CLAN);
}
boolSteamID::isLobbyChat() const {
returnthis->type == TYPE_CHAT && (this->instance & CHAT_INSTANCE_FLAG_LOBBY);
}
boolSteamID::isMMSLobbyChat() const {
returnthis->type == TYPE_CHAT && (this->instance & CHAT_INSTANCE_FLAG_MMSLOBBY);
}
longlongSteamID::getAccountId() const {
returnthis->accountId;
}