Skip to content

Commit 302a491

Browse files
committed
feat(core): Steam Authorization
1 parent 3b7b8d3 commit 302a491

File tree

9 files changed

+116
-3
lines changed

9 files changed

+116
-3
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
Over here will be noted all the update change logs.
44

5+
## v1.6.12 - [Release](https://github.com/swiftly-solution/swiftly/releases/tag/v1.6.12)
6+
7+
### Steam Authorization
8+
9+
- Now in Scripting you can get both authorized and unauthorized SteamID64 / SteamID2, also you can get if the player is authorized.
10+
- There are also events triggered when a player is being authorized or not: [OnClientSteamAuthorize](https://swiftlycs2.net/plugin-docs/scripting/events/core/onclientsteamauthorize) and [OnClientSteamAuthorizeFail](https://swiftlycs2.net/plugin-docs/scripting/events/core/onclientsteamauthorizefail).
11+
512
## v1.6.11 - [Release](https://github.com/swiftly-solution/swiftly/releases/tag/v1.6.11)
613

714
### Automation

plugin_files/configs/core.example.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,9 @@
7474
"paddingX": 0.1,
7575
"paddingY": 0.1
7676
}
77+
},
78+
"steam_auth": {
79+
"mode": "flexible",
80+
"available_modes": ["flexible", "strict"]
7781
}
7882
}

src/core/entrypoint.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ bool SwiftlyS2::Load(PluginId id, ISmmAPI* ismm, char* error, size_t maxlen, boo
205205
{
206206
g_eventManager.RegisterGameEvents();
207207
g_SteamAPI.Init();
208+
g_playerManager.SteamAPIServerActivated();
208209
if (!BeginCrashListener()) PRINTRET("Failed to setup crash listener.\n", false);
209210
}
210211

src/scripting/player/player.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,15 @@ LoadScriptingComponent(player, [](PluginObject plugin, EContext* ctx) -> void {
245245
context->SetReturn(player->GetSteamID());
246246
});
247247

248+
ADD_CLASS_FUNCTION("Player", "GetUnauthorizedSteamID", [](FunctionContext* context, ClassData* data) -> void {
249+
int playerid = data->GetData<int>("playerid");
250+
251+
Player* player = g_playerManager.GetPlayer(playerid);
252+
if (!player) return context->SetReturn(0);
253+
254+
context->SetReturn(player->GetUnauthorizedSteamID());
255+
});
256+
248257
ADD_CLASS_FUNCTION("Player", "GetSteamID2", [](FunctionContext* context, ClassData* data) -> void {
249258
int playerid = data->GetData<int>("playerid");
250259

@@ -260,6 +269,30 @@ LoadScriptingComponent(player, [](PluginObject plugin, EContext* ctx) -> void {
260269
context->SetReturn(string_format("STEAM_0:%d:%llu", (steamid - base) & 1, (steamid - base) / 2));
261270
});
262271

272+
ADD_CLASS_FUNCTION("Player", "GetUnauthorizedSteamID2", [](FunctionContext* context, ClassData* data) -> void {
273+
int playerid = data->GetData<int>("playerid");
274+
275+
Player* player = g_playerManager.GetPlayer(playerid);
276+
if (!player) return context->SetReturn("STEAM_0:0:000000000");
277+
278+
uint64_t steamid = player->GetUnauthorizedSteamID();
279+
280+
if (steamid == 0)
281+
return context->SetReturn("STEAM_0:0:000000000");
282+
283+
static const uint64_t base = 76561197960265728;
284+
context->SetReturn(string_format("STEAM_0:%d:%llu", (steamid - base) & 1, (steamid - base) / 2));
285+
});
286+
287+
ADD_CLASS_FUNCTION("Player", "IsAuthorized", [](FunctionContext* context, ClassData* data) -> void {
288+
int playerid = data->GetData<int>("playerid");
289+
290+
Player* player = g_playerManager.GetPlayer(playerid);
291+
if (!player) return context->SetReturn(false);
292+
293+
context->SetReturn(player->IsAuthorized());
294+
});
295+
263296
ADD_CLASS_FUNCTION("Player", "HideMenu", [](FunctionContext* context, ClassData* data) -> void {
264297
int playerid = data->GetData<int>("playerid");
265298

src/server/configuration/configuration.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,9 @@ bool Configuration::LoadConfiguration()
498498

499499
RegisterConfiguration(wasEdited, coreConfigFile, "core", "core", "vgui.textBackground.textSize", 35);
500500

501+
RegisterConfiguration(wasEdited, coreConfigFile, "core", "core", "steam_auth.mode", "flexible");
502+
RegisterConfigurationVector<std::string>(wasEdited, coreConfigFile, "core", "core", "steam_auth.available_modes", { "flexible", "strict" }, true, " ");
503+
501504
if (wasEdited) {
502505
WriteJSONFile("addons/swiftly/configs/core.json", coreConfigFile);
503506
}

src/server/player/manager.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,23 @@ void PlayerManager::ClientDisconnect(CPlayerSlot slot, ENetworkDisconnectionReas
155155
g_VGUI.Unregister(player);
156156
UnregisterPlayer(slot);
157157
}
158+
}
159+
160+
void PlayerManager::SteamAPIServerActivated()
161+
{
162+
m_CallbackValidateAuthTicketResponse.Register(this, &PlayerManager::OnValidateAuthTicket);
163+
}
164+
165+
void PlayerManager::OnValidateAuthTicket(ValidateAuthTicketResponse_t* response)
166+
{
167+
uint64_t steamid = response->m_SteamID.ConvertToUint64();
168+
169+
for (int i = 0; i < GetPlayerCap(); i++) {
170+
auto player = GetPlayer(i);
171+
if (!player) continue;
172+
if (player->GetUnauthorizedSteamID() != steamid) continue;
173+
174+
player->SetAuthorized(response->m_eAuthSessionResponse == k_EAuthSessionResponseOK);
175+
break;
176+
}
158177
}

src/server/player/manager.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
#include <core/entrypoint.h>
77
#include <public/playerslot.h>
8+
#include <steam/steam_api_common.h>
9+
#include <steam/isteamuser.h>
10+
#include <steam/steamclientpublic.h>
811

912
class PlayerManager
1013
{
@@ -29,6 +32,10 @@ class PlayerManager
2932
bool ClientConnect(CPlayerSlot slot, const char* pszName, uint64 xuid, const char* pszNetworkID, bool unk1, CBufferString* pRejectReason);
3033
void OnClientConnected(CPlayerSlot slot, const char* pszName, uint64 xuid, const char* pszNetworkID, const char* pszAddress, bool bFakePlayer);
3134
void ClientDisconnect(CPlayerSlot slot, ENetworkDisconnectionReason reason, const char* pszName, uint64 xuid, const char* pszNetworkID);
35+
36+
void SteamAPIServerActivated();
37+
38+
STEAM_GAMESERVER_CALLBACK_MANUAL(PlayerManager, OnValidateAuthTicket, ValidateAuthTicketResponse_t, m_CallbackValidateAuthTicketResponse);
3239
};
3340

3441
extern PlayerManager g_playerManager;

src/server/player/player.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,29 @@ const char* Player::GetName()
210210
}
211211

212212
uint64_t Player::GetSteamID()
213+
{
214+
if (authorized) {
215+
if (IsFakeClient()) return 0;
216+
217+
auto steamid = engine->GetClientSteamID(CPlayerSlot(slot));
218+
if (!steamid) return 0;
219+
220+
return steamid->ConvertToUint64();
221+
}
222+
else if (g_Config.FetchValue<std::string>("core.steam_auth.mode") == "flexible") {
223+
return GetUnauthorizedSteamID();
224+
}
225+
else return 0;
226+
}
227+
228+
uint64_t Player::GetUnauthorizedSteamID()
213229
{
214230
if (IsFakeClient()) return 0;
215231

216-
auto controller = GetController();
217-
if (!controller) return xuid;
232+
auto steamid = engine->GetClientSteamID(CPlayerSlot(slot));
233+
if (!steamid) return xuid;
218234

219-
return schema::GetProp<uint64_t>(controller, "CBasePlayerController", "m_steamID");
235+
return steamid->ConvertToUint64();
220236
}
221237

222238
void Player::SendMsg(int dest, const char* msg, ...)
@@ -511,4 +527,21 @@ ClassData* Player::GetPlayerObject()
511527
{
512528
if (playerObject == nullptr) playerObject = new ClassData({ { "playerid", GetSlot() } }, "Player", nullptr);
513529
return playerObject;
530+
}
531+
532+
void Player::SetAuthorized(bool state)
533+
{
534+
authorized = state;
535+
536+
if (state) {
537+
g_pluginManager.ExecuteEvent("core", "OnClientSteamAuthorize", { slot }, {});
538+
}
539+
else {
540+
g_pluginManager.ExecuteEvent("core", "OnClientSteamAuthorizeFail", { slot }, {});
541+
}
542+
}
543+
544+
bool Player::IsAuthorized()
545+
{
546+
return authorized;
514547
}

src/server/player/player.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Player
3939
std::string centerMessageText;
4040

4141
uint64_t buttons;
42+
bool authorized = false;
4243

4344
public:
4445
MenuRenderer* menu_renderer = nullptr;
@@ -54,6 +55,8 @@ class Player
5455
void SendMsg(int dest, const char* msg, ...);
5556

5657
const char* GetName();
58+
59+
uint64_t GetUnauthorizedSteamID();
5760
uint64_t GetSteamID();
5861

5962
void* GetController();
@@ -89,6 +92,9 @@ class Player
8992

9093
void SetButtons(uint64_t button);
9194

95+
void SetAuthorized(bool state);
96+
bool IsAuthorized();
97+
9298
CPlayerBitVec m_selfMutes[64] = {};
9399
};
94100

0 commit comments

Comments
 (0)