Skip to content

Commit efba3ef

Browse files
committed
Fix Steam API being used incorrectly on listenservers
1 parent 90ac5e1 commit efba3ef

File tree

7 files changed

+64
-49
lines changed

7 files changed

+64
-49
lines changed

src/cs2fixes.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ class GameSessionConfiguration_t
6969

7070
SH_DECL_HOOK3_void(IServerGameDLL, GameFrame, SH_NOATTRIB, 0, bool, bool, bool);
7171
SH_DECL_HOOK0_void(IServerGameDLL, GameServerSteamAPIActivated, SH_NOATTRIB, 0);
72-
SH_DECL_HOOK0_void(IServerGameDLL, GameServerSteamAPIDeactivated, SH_NOATTRIB, 0);
7372
SH_DECL_HOOK1_void(IServerGameDLL, ApplyGameSettings, SH_NOATTRIB, 0, KeyValues*);
7473
SH_DECL_HOOK4_void(IServerGameClients, ClientActive, SH_NOATTRIB, 0, CPlayerSlot, bool, const char*, uint64);
7574
SH_DECL_HOOK5_void(IServerGameClients, ClientDisconnect, SH_NOATTRIB, 0, CPlayerSlot, ENetworkDisconnectionReason, const char*, uint64, const char*);
@@ -102,8 +101,6 @@ IGameEventSystem* g_gameEventSystem = nullptr;
102101
IGameEventManager2* g_gameEventManager = nullptr;
103102
CGameEntitySystem* g_pEntitySystem = nullptr;
104103
IVEngineServer2* g_pEngineServer2 = nullptr;
105-
ISteamHTTP* g_http = nullptr;
106-
CSteamGameServerAPIContext g_steamAPI;
107104
CCSGameRules* g_pGameRules = nullptr; // Will be null between map end & new map startup, null check if necessary!
108105
CSpawnGroupMgrGameSystem* g_pSpawnGroupMgr = nullptr; // Will be null between map end & new map startup, null check if necessary!
109106

@@ -190,7 +187,6 @@ bool CS2Fixes::Load(PluginId id, ISmmAPI* ismm, char* error, size_t maxlen, bool
190187

191188
SH_ADD_HOOK(IServerGameDLL, GameFrame, g_pSource2Server, SH_MEMBER(this, &CS2Fixes::Hook_GameFramePost), true);
192189
SH_ADD_HOOK(IServerGameDLL, GameServerSteamAPIActivated, g_pSource2Server, SH_MEMBER(this, &CS2Fixes::Hook_GameServerSteamAPIActivated), false);
193-
SH_ADD_HOOK(IServerGameDLL, GameServerSteamAPIDeactivated, g_pSource2Server, SH_MEMBER(this, &CS2Fixes::Hook_GameServerSteamAPIDeactivated), false);
194190
SH_ADD_HOOK(IServerGameDLL, ApplyGameSettings, g_pSource2Server, SH_MEMBER(this, &CS2Fixes::Hook_ApplyGameSettings), false);
195191
SH_ADD_HOOK(IServerGameClients, ClientActive, g_pSource2GameClients, SH_MEMBER(this, &CS2Fixes::Hook_ClientActive), true);
196192
SH_ADD_HOOK(IServerGameClients, ClientDisconnect, g_pSource2GameClients, SH_MEMBER(this, &CS2Fixes::Hook_ClientDisconnect), true);
@@ -400,10 +396,6 @@ bool CS2Fixes::Load(PluginId id, ISmmAPI* ismm, char* error, size_t maxlen, bool
400396
g_pVoteManager->VoteManager_Init();
401397

402398
g_pIdleSystem->Reset();
403-
404-
g_steamAPI.Init();
405-
g_http = g_steamAPI.SteamHTTP();
406-
407399
g_playerManager->OnSteamAPIActivated();
408400

409401
if (g_cvarVoteManagerEnable.Get() && !g_pMapVoteSystem->IsMapListLoaded())
@@ -421,7 +413,6 @@ bool CS2Fixes::Unload(char* error, size_t maxlen)
421413
{
422414
SH_REMOVE_HOOK(IServerGameDLL, GameFrame, g_pSource2Server, SH_MEMBER(this, &CS2Fixes::Hook_GameFramePost), true);
423415
SH_REMOVE_HOOK(IServerGameDLL, GameServerSteamAPIActivated, g_pSource2Server, SH_MEMBER(this, &CS2Fixes::Hook_GameServerSteamAPIActivated), false);
424-
SH_REMOVE_HOOK(IServerGameDLL, GameServerSteamAPIDeactivated, g_pSource2Server, SH_MEMBER(this, &CS2Fixes::Hook_GameServerSteamAPIDeactivated), false);
425416
SH_REMOVE_HOOK(IServerGameDLL, ApplyGameSettings, g_pSource2Server, SH_MEMBER(this, &CS2Fixes::Hook_ApplyGameSettings), false);
426417
SH_REMOVE_HOOK(IServerGameClients, ClientActive, g_pSource2GameClients, SH_MEMBER(this, &CS2Fixes::Hook_ClientActive), true);
427418
SH_REMOVE_HOOK(IServerGameClients, ClientDisconnect, g_pSource2GameClients, SH_MEMBER(this, &CS2Fixes::Hook_ClientDisconnect), true);
@@ -669,9 +660,6 @@ void CS2Fixes::Hook_CTriggerGravityEndTouch(CBaseEntity* pOther)
669660
}
670661
void CS2Fixes::Hook_GameServerSteamAPIActivated()
671662
{
672-
g_steamAPI.Init();
673-
g_http = g_steamAPI.SteamHTTP();
674-
675663
g_playerManager->OnSteamAPIActivated();
676664

677665
if (g_cvarVoteManagerEnable.Get() && !g_pMapVoteSystem->IsMapListLoaded())
@@ -680,13 +668,6 @@ void CS2Fixes::Hook_GameServerSteamAPIActivated()
680668
RETURN_META(MRES_IGNORED);
681669
}
682670

683-
void CS2Fixes::Hook_GameServerSteamAPIDeactivated()
684-
{
685-
g_http = nullptr;
686-
687-
RETURN_META(MRES_IGNORED);
688-
}
689-
690671
void CS2Fixes::Hook_PostEvent(CSplitScreenSlot nSlot, bool bLocalOnly, int nClientCount, const uint64* clients,
691672
INetworkMessageInternal* pEvent, const CNetMessage* pData, unsigned long nSize, NetChannelBufType_t bufType)
692673
{

src/cs2fixes.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ extern IGameEventSystem* g_gameEventSystem;
4747
extern IGameEventManager2* g_gameEventManager;
4848
extern CGameEntitySystem* g_pEntitySystem;
4949
extern IVEngineServer2* g_pEngineServer2;
50-
extern ISteamHTTP* g_http;
51-
extern CSteamGameServerAPIContext g_steamAPI;
5250
extern CCSGameRules* g_pGameRules;
5351
extern CSpawnGroupMgrGameSystem* g_pSpawnGroupMgr;
5452
extern double g_flUniversalTime;
@@ -69,7 +67,6 @@ class CS2Fixes : public ISmmPlugin, public IMetamodListener, public ICS2Fixes
6967

7068
public: // hooks
7169
void Hook_GameServerSteamAPIActivated();
72-
void Hook_GameServerSteamAPIDeactivated();
7370
void OnLevelInit(char const* pMapName,
7471
char const* pMapEntities,
7572
char const* pOldLevel,

src/httpmanager.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ void HTTPManager::TrackedRequest::OnHTTPRequestCompleted(HTTPRequestCompleted_t*
7171
else
7272
{
7373
uint32 size;
74-
g_http->GetHTTPResponseBodySize(arg->m_hRequest, &size);
74+
GetSteamHTTP()->GetHTTPResponseBodySize(arg->m_hRequest, &size);
7575

7676
uint8* response = new uint8[size + 1];
77-
g_http->GetHTTPResponseBodyData(arg->m_hRequest, response, size);
77+
GetSteamHTTP()->GetHTTPResponseBodyData(arg->m_hRequest, response, size);
7878
response[size] = 0; // Add null terminator
7979

8080
json jsonResponse;
@@ -101,8 +101,8 @@ void HTTPManager::TrackedRequest::OnHTTPRequestCompleted(HTTPRequestCompleted_t*
101101
delete[] response;
102102
}
103103

104-
if (g_http)
105-
g_http->ReleaseHTTPRequest(arg->m_hRequest);
104+
if (GetSteamHTTP())
105+
GetSteamHTTP()->ReleaseHTTPRequest(arg->m_hRequest);
106106

107107
delete this;
108108
}
@@ -141,14 +141,14 @@ void HTTPManager::GenerateRequest(EHTTPMethod method, const char* pszUrl, const
141141
CompletedCallback callbackCompleted, ErrorCallback callbackError,
142142
std::vector<HTTPHeader>* headers)
143143
{
144-
if (!g_http)
144+
if (!GetSteamHTTP())
145145
{
146-
Panic("A web request was attempted before g_http was instantiated, returning early.\n");
146+
Panic("A web request was attempted on null ISteamHTTP, returning early.\n");
147147
return;
148148
}
149149

150150
// Message("Sending HTTP:\n%s\n", pszText);
151-
auto hReq = g_http->CreateHTTPRequest(method, pszUrl);
151+
auto hReq = GetSteamHTTP()->CreateHTTPRequest(method, pszUrl);
152152
int size = strlen(pszText);
153153
// Message("HTTP request: %p\n", hReq);
154154

@@ -157,7 +157,7 @@ void HTTPManager::GenerateRequest(EHTTPMethod method, const char* pszUrl, const
157157
|| method == k_EHTTPMethodPUT
158158
|| method == k_EHTTPMethodDELETE;
159159

160-
if (shouldHaveBody && !g_http->SetHTTPRequestRawPostBody(hReq, "application/json", (uint8*)pszText, size))
160+
if (shouldHaveBody && !GetSteamHTTP()->SetHTTPRequestRawPostBody(hReq, "application/json", (uint8*)pszText, size))
161161
{
162162
// Message("Failed to SetHTTPRequestRawPostBody\n");
163163
return;
@@ -168,10 +168,10 @@ void HTTPManager::GenerateRequest(EHTTPMethod method, const char* pszUrl, const
168168

169169
if (headers != nullptr)
170170
for (HTTPHeader header : *headers)
171-
g_http->SetHTTPRequestHeaderValue(hReq, header.GetName(), header.GetValue());
171+
GetSteamHTTP()->SetHTTPRequestHeaderValue(hReq, header.GetName(), header.GetValue());
172172

173173
SteamAPICall_t hCall;
174-
g_http->SendHTTPRequest(hReq, &hCall);
174+
GetSteamHTTP()->SendHTTPRequest(hReq, &hCall);
175175

176176
new TrackedRequest(hReq, hCall, pszUrl, pszText, callbackCompleted, callbackError);
177177
}

src/map_votes.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -861,13 +861,13 @@ void CMapVoteSystem::ForceNextMap(CCSPlayerController* pController, const char*
861861

862862
void CMapVoteSystem::PrintDownloadProgress()
863863
{
864-
if (GetDownloadQueueSize() == 0)
864+
if (GetDownloadQueueSize() == 0 || !GetSteamUGC())
865865
return;
866866

867867
uint64 iBytesDownloaded = 0;
868868
uint64 iTotalBytes = 0;
869869

870-
if (!g_steamAPI.SteamUGC()->GetItemDownloadInfo(m_DownloadQueue.front(), &iBytesDownloaded, &iTotalBytes) || !iTotalBytes)
870+
if (!GetSteamUGC()->GetItemDownloadInfo(m_DownloadQueue.front(), &iBytesDownloaded, &iTotalBytes) || !iTotalBytes)
871871
return;
872872

873873
double flMBDownloaded = (double)iBytesDownloaded / 1024 / 1024;
@@ -881,7 +881,7 @@ void CMapVoteSystem::PrintDownloadProgress()
881881

882882
void CMapVoteSystem::OnMapDownloaded(DownloadItemResult_t* pResult)
883883
{
884-
if (std::find(m_DownloadQueue.begin(), m_DownloadQueue.end(), pResult->m_nPublishedFileId) == m_DownloadQueue.end())
884+
if (std::find(m_DownloadQueue.begin(), m_DownloadQueue.end(), pResult->m_nPublishedFileId) == m_DownloadQueue.end() || !GetSteamUGC())
885885
return;
886886

887887
// Some weird rate limiting that's been observed? Back off for a while then retry download
@@ -891,7 +891,7 @@ void CMapVoteSystem::OnMapDownloaded(DownloadItemResult_t* pResult)
891891
Message("Addon %llu download failed with status code 3, retrying in 2 minutes\n", workshopID);
892892

893893
m_pRateLimitedDownloadTimer = CTimer::Create(120.0f, TIMERFLAG_NONE, [workshopID]() {
894-
g_steamAPI.SteamUGC()->DownloadItem(workshopID, false);
894+
GetSteamUGC()->DownloadItem(workshopID, false);
895895

896896
return -1.0f;
897897
});
@@ -904,7 +904,7 @@ void CMapVoteSystem::OnMapDownloaded(DownloadItemResult_t* pResult)
904904
if (GetDownloadQueueSize() == 0)
905905
return;
906906

907-
g_steamAPI.SteamUGC()->DownloadItem(m_DownloadQueue.front(), false);
907+
GetSteamUGC()->DownloadItem(m_DownloadQueue.front(), false);
908908
}
909909

910910
void CMapVoteSystem::QueueMapDownload(PublishedFileId_t iWorkshopId)
@@ -915,7 +915,7 @@ void CMapVoteSystem::QueueMapDownload(PublishedFileId_t iWorkshopId)
915915
m_DownloadQueue.push_back(iWorkshopId);
916916

917917
if (m_DownloadQueue.front() == iWorkshopId)
918-
g_steamAPI.SteamUGC()->DownloadItem(iWorkshopId, false);
918+
GetSteamUGC()->DownloadItem(iWorkshopId, false);
919919
}
920920

921921
bool CMapVoteSystem::LoadMapList()
@@ -1388,17 +1388,24 @@ std::pair<int, std::shared_ptr<CMap>> CMapVoteSystem::GetMapInfoByIdentifiers(co
13881388

13891389
std::shared_ptr<CMapSystemWorkshopDetailsQuery> CMapSystemWorkshopDetailsQuery::Create(uint64 iWorkshopId, CCSPlayerController* pController, QueryCallback_t callbackSuccess)
13901390
{
1391+
if (!GetSteamUGC())
1392+
{
1393+
Panic("A workshop map query was attempted on null ISteamUGC, returning early.\n");
1394+
ClientPrint(pController, HUD_PRINTTALK, CHAT_PREFIX "Failed to query workshop map information for ID \x06%llu\x01.", iWorkshopId);
1395+
return nullptr;
1396+
}
1397+
13911398
uint64 iWorkshopIDArray[1] = {iWorkshopId};
1392-
UGCQueryHandle_t hQuery = g_steamAPI.SteamUGC()->CreateQueryUGCDetailsRequest(iWorkshopIDArray, 1);
1399+
UGCQueryHandle_t hQuery = GetSteamUGC()->CreateQueryUGCDetailsRequest(iWorkshopIDArray, 1);
13931400

13941401
if (hQuery == k_UGCQueryHandleInvalid)
13951402
{
13961403
ClientPrint(pController, HUD_PRINTTALK, CHAT_PREFIX "Failed to query workshop map information for ID \x06%llu\x01.", iWorkshopId);
13971404
return nullptr;
13981405
}
13991406

1400-
g_steamAPI.SteamUGC()->SetAllowCachedResponse(hQuery, 0);
1401-
SteamAPICall_t hCall = g_steamAPI.SteamUGC()->SendQueryUGCRequest(hQuery);
1407+
GetSteamUGC()->SetAllowCachedResponse(hQuery, 0);
1408+
SteamAPICall_t hCall = GetSteamUGC()->SendQueryUGCRequest(hQuery);
14021409

14031410
auto pQuery = std::make_shared<CMapSystemWorkshopDetailsQuery>(hQuery, iWorkshopId, pController, callbackSuccess);
14041411
g_pMapVoteSystem->AddWorkshopDetailsQuery(pQuery);
@@ -1415,7 +1422,7 @@ void CMapSystemWorkshopDetailsQuery::OnQueryCompleted(SteamUGCQueryCompleted_t*
14151422
// Only allow null controller if controller was originally null (console)
14161423
if (m_bConsole || pController)
14171424
{
1418-
if (bFailed || pCompletedQuery->m_eResult != k_EResultOK || pCompletedQuery->m_unNumResultsReturned < 1 || !g_steamAPI.SteamUGC()->GetQueryUGCResult(pCompletedQuery->m_handle, 0, &details) || details.m_eResult != k_EResultOK)
1425+
if (bFailed || pCompletedQuery->m_eResult != k_EResultOK || pCompletedQuery->m_unNumResultsReturned < 1 || !GetSteamUGC()->GetQueryUGCResult(pCompletedQuery->m_handle, 0, &details) || details.m_eResult != k_EResultOK)
14191426
{
14201427
ClientPrint(pController, HUD_PRINTTALK, CHAT_PREFIX "Failed to query workshop map information for ID \x06%llu\x01.", m_iWorkshopId);
14211428
}
@@ -1426,12 +1433,14 @@ void CMapSystemWorkshopDetailsQuery::OnQueryCompleted(SteamUGCQueryCompleted_t*
14261433
else
14271434
{
14281435
// Try to get a head start on downloading the map if needed
1429-
g_steamAPI.SteamUGC()->DownloadItem(m_iWorkshopId, false);
1436+
GetSteamUGC()->DownloadItem(m_iWorkshopId, false);
14301437
m_callbackSuccess(std::make_shared<CMap>(details.m_rgchTitle, m_iWorkshopId), pController);
14311438
}
14321439
}
14331440

1434-
g_steamAPI.SteamUGC()->ReleaseQueryUGCRequest(m_hQuery);
1441+
if (GetSteamUGC())
1442+
GetSteamUGC()->ReleaseQueryUGCRequest(m_hQuery);
1443+
14351444
g_pMapVoteSystem->RemoveWorkshopDetailsQuery(shared_from_this());
14361445
}
14371446

src/mapmigrations.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,23 @@ void CMapMigrations::UpdateMapUpdateTime(time_t timeMapUpdated)
110110

111111
std::shared_ptr<CMapMigrationWorkshopDetailsQuery> CMapMigrationWorkshopDetailsQuery::Create(uint64 iWorkshopId)
112112
{
113+
if (!GetSteamUGC())
114+
{
115+
Panic("Map migrations failed to find current map update time: null ISteamUGC\n");
116+
return nullptr;
117+
}
118+
113119
uint64 iWorkshopIDArray[1] = {iWorkshopId};
114-
UGCQueryHandle_t hQuery = g_steamAPI.SteamUGC()->CreateQueryUGCDetailsRequest(iWorkshopIDArray, 1);
120+
UGCQueryHandle_t hQuery = GetSteamUGC()->CreateQueryUGCDetailsRequest(iWorkshopIDArray, 1);
115121

116122
if (hQuery == k_UGCQueryHandleInvalid)
117123
{
118124
Panic("Map migrations failed to find current map update time: failed to query workshop map information for ID %llu\n", iWorkshopId);
119125
return nullptr;
120126
}
121127

122-
g_steamAPI.SteamUGC()->SetAllowCachedResponse(hQuery, 0);
123-
SteamAPICall_t hCall = g_steamAPI.SteamUGC()->SendQueryUGCRequest(hQuery);
128+
GetSteamUGC()->SetAllowCachedResponse(hQuery, 0);
129+
SteamAPICall_t hCall = GetSteamUGC()->SendQueryUGCRequest(hQuery);
124130

125131
auto pQuery = std::make_shared<CMapMigrationWorkshopDetailsQuery>(hQuery, iWorkshopId);
126132
g_pMapMigrations->AddWorkshopDetailsQuery(pQuery);
@@ -133,11 +139,13 @@ void CMapMigrationWorkshopDetailsQuery::OnQueryCompleted(SteamUGCQueryCompleted_
133139
{
134140
SteamUGCDetails_t details;
135141

136-
if (bFailed || pCompletedQuery->m_eResult != k_EResultOK || pCompletedQuery->m_unNumResultsReturned < 1 || !g_steamAPI.SteamUGC()->GetQueryUGCResult(pCompletedQuery->m_handle, 0, &details) || details.m_eResult != k_EResultOK)
142+
if (bFailed || pCompletedQuery->m_eResult != k_EResultOK || pCompletedQuery->m_unNumResultsReturned < 1 || !GetSteamUGC()->GetQueryUGCResult(pCompletedQuery->m_handle, 0, &details) || details.m_eResult != k_EResultOK)
137143
Panic("Map migrations failed to find current map update time: failed to query workshop map information for ID %llu\n", m_iWorkshopId);
138144
else
139145
g_pMapMigrations->UpdateMapUpdateTime(details.m_rtimeUpdated);
140146

141-
g_steamAPI.SteamUGC()->ReleaseQueryUGCRequest(m_hQuery);
147+
if (GetSteamUGC())
148+
GetSteamUGC()->ReleaseQueryUGCRequest(m_hQuery);
149+
142150
g_pMapMigrations->RemoveWorkshopDetailsQuery(shared_from_this());
143151
}

src/utils/utils.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,20 @@ std::string StringToLower(std::string strValue)
7777
strValue[i] = tolower(strValue[i]);
7878

7979
return strValue;
80+
}
81+
82+
ISteamUGC* GetSteamUGC()
83+
{
84+
if (g_pEngineServer2->IsDedicatedServer())
85+
return SteamGameServerUGC();
86+
else
87+
return SteamUGC();
88+
}
89+
90+
ISteamHTTP* GetSteamHTTP()
91+
{
92+
if (g_pEngineServer2->IsDedicatedServer())
93+
return SteamGameServerHTTP();
94+
else
95+
return SteamHTTP();
8096
}

src/utils/utils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#pragma once
2121

2222
#include "playerslot.h"
23+
#include "steam/isteamugc.h"
2324
#include "utlvector.h"
2425
#include <string>
2526

@@ -33,3 +34,6 @@ CServerSideClient* GetClientBySlot(CPlayerSlot slot);
3334

3435
uint32 GetSoundEventHash(const char* pszSoundEventName);
3536
std::string StringToLower(std::string strValue);
37+
38+
ISteamUGC* GetSteamUGC();
39+
ISteamHTTP* GetSteamHTTP();

0 commit comments

Comments
 (0)