diff --git a/src/cs2fixes.cpp b/src/cs2fixes.cpp index a8dc2fb4..7aac8633 100644 --- a/src/cs2fixes.cpp +++ b/src/cs2fixes.cpp @@ -69,7 +69,6 @@ class GameSessionConfiguration_t SH_DECL_HOOK3_void(IServerGameDLL, GameFrame, SH_NOATTRIB, 0, bool, bool, bool); SH_DECL_HOOK0_void(IServerGameDLL, GameServerSteamAPIActivated, SH_NOATTRIB, 0); -SH_DECL_HOOK0_void(IServerGameDLL, GameServerSteamAPIDeactivated, SH_NOATTRIB, 0); SH_DECL_HOOK1_void(IServerGameDLL, ApplyGameSettings, SH_NOATTRIB, 0, KeyValues*); SH_DECL_HOOK4_void(IServerGameClients, ClientActive, SH_NOATTRIB, 0, CPlayerSlot, bool, const char*, uint64); SH_DECL_HOOK5_void(IServerGameClients, ClientDisconnect, SH_NOATTRIB, 0, CPlayerSlot, ENetworkDisconnectionReason, const char*, uint64, const char*); @@ -102,8 +101,6 @@ IGameEventSystem* g_gameEventSystem = nullptr; IGameEventManager2* g_gameEventManager = nullptr; CGameEntitySystem* g_pEntitySystem = nullptr; IVEngineServer2* g_pEngineServer2 = nullptr; -ISteamHTTP* g_http = nullptr; -CSteamGameServerAPIContext g_steamAPI; CCSGameRules* g_pGameRules = nullptr; // Will be null between map end & new map startup, null check if necessary! CSpawnGroupMgrGameSystem* g_pSpawnGroupMgr = nullptr; // Will be null between map end & new map startup, null check if necessary! @@ -190,7 +187,6 @@ bool CS2Fixes::Load(PluginId id, ISmmAPI* ismm, char* error, size_t maxlen, bool SH_ADD_HOOK(IServerGameDLL, GameFrame, g_pSource2Server, SH_MEMBER(this, &CS2Fixes::Hook_GameFramePost), true); SH_ADD_HOOK(IServerGameDLL, GameServerSteamAPIActivated, g_pSource2Server, SH_MEMBER(this, &CS2Fixes::Hook_GameServerSteamAPIActivated), false); - SH_ADD_HOOK(IServerGameDLL, GameServerSteamAPIDeactivated, g_pSource2Server, SH_MEMBER(this, &CS2Fixes::Hook_GameServerSteamAPIDeactivated), false); SH_ADD_HOOK(IServerGameDLL, ApplyGameSettings, g_pSource2Server, SH_MEMBER(this, &CS2Fixes::Hook_ApplyGameSettings), false); SH_ADD_HOOK(IServerGameClients, ClientActive, g_pSource2GameClients, SH_MEMBER(this, &CS2Fixes::Hook_ClientActive), true); 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 g_pVoteManager->VoteManager_Init(); g_pIdleSystem->Reset(); - - g_steamAPI.Init(); - g_http = g_steamAPI.SteamHTTP(); - g_playerManager->OnSteamAPIActivated(); if (g_cvarVoteManagerEnable.Get() && !g_pMapVoteSystem->IsMapListLoaded()) @@ -421,7 +413,6 @@ bool CS2Fixes::Unload(char* error, size_t maxlen) { SH_REMOVE_HOOK(IServerGameDLL, GameFrame, g_pSource2Server, SH_MEMBER(this, &CS2Fixes::Hook_GameFramePost), true); SH_REMOVE_HOOK(IServerGameDLL, GameServerSteamAPIActivated, g_pSource2Server, SH_MEMBER(this, &CS2Fixes::Hook_GameServerSteamAPIActivated), false); - SH_REMOVE_HOOK(IServerGameDLL, GameServerSteamAPIDeactivated, g_pSource2Server, SH_MEMBER(this, &CS2Fixes::Hook_GameServerSteamAPIDeactivated), false); SH_REMOVE_HOOK(IServerGameDLL, ApplyGameSettings, g_pSource2Server, SH_MEMBER(this, &CS2Fixes::Hook_ApplyGameSettings), false); SH_REMOVE_HOOK(IServerGameClients, ClientActive, g_pSource2GameClients, SH_MEMBER(this, &CS2Fixes::Hook_ClientActive), true); SH_REMOVE_HOOK(IServerGameClients, ClientDisconnect, g_pSource2GameClients, SH_MEMBER(this, &CS2Fixes::Hook_ClientDisconnect), true); @@ -669,9 +660,6 @@ void CS2Fixes::Hook_CTriggerGravityEndTouch(CBaseEntity* pOther) } void CS2Fixes::Hook_GameServerSteamAPIActivated() { - g_steamAPI.Init(); - g_http = g_steamAPI.SteamHTTP(); - g_playerManager->OnSteamAPIActivated(); if (g_cvarVoteManagerEnable.Get() && !g_pMapVoteSystem->IsMapListLoaded()) @@ -680,13 +668,6 @@ void CS2Fixes::Hook_GameServerSteamAPIActivated() RETURN_META(MRES_IGNORED); } -void CS2Fixes::Hook_GameServerSteamAPIDeactivated() -{ - g_http = nullptr; - - RETURN_META(MRES_IGNORED); -} - void CS2Fixes::Hook_PostEvent(CSplitScreenSlot nSlot, bool bLocalOnly, int nClientCount, const uint64* clients, INetworkMessageInternal* pEvent, const CNetMessage* pData, unsigned long nSize, NetChannelBufType_t bufType) { diff --git a/src/cs2fixes.h b/src/cs2fixes.h index 38fa306a..ea5cad5f 100644 --- a/src/cs2fixes.h +++ b/src/cs2fixes.h @@ -47,8 +47,6 @@ extern IGameEventSystem* g_gameEventSystem; extern IGameEventManager2* g_gameEventManager; extern CGameEntitySystem* g_pEntitySystem; extern IVEngineServer2* g_pEngineServer2; -extern ISteamHTTP* g_http; -extern CSteamGameServerAPIContext g_steamAPI; extern CCSGameRules* g_pGameRules; extern CSpawnGroupMgrGameSystem* g_pSpawnGroupMgr; extern double g_flUniversalTime; @@ -69,7 +67,6 @@ class CS2Fixes : public ISmmPlugin, public IMetamodListener, public ICS2Fixes public: // hooks void Hook_GameServerSteamAPIActivated(); - void Hook_GameServerSteamAPIDeactivated(); void OnLevelInit(char const* pMapName, char const* pMapEntities, char const* pOldLevel, diff --git a/src/httpmanager.cpp b/src/httpmanager.cpp index f48876dd..1e61e7f4 100644 --- a/src/httpmanager.cpp +++ b/src/httpmanager.cpp @@ -71,10 +71,10 @@ void HTTPManager::TrackedRequest::OnHTTPRequestCompleted(HTTPRequestCompleted_t* else { uint32 size; - g_http->GetHTTPResponseBodySize(arg->m_hRequest, &size); + GetSteamHTTP()->GetHTTPResponseBodySize(arg->m_hRequest, &size); uint8* response = new uint8[size + 1]; - g_http->GetHTTPResponseBodyData(arg->m_hRequest, response, size); + GetSteamHTTP()->GetHTTPResponseBodyData(arg->m_hRequest, response, size); response[size] = 0; // Add null terminator json jsonResponse; @@ -101,8 +101,8 @@ void HTTPManager::TrackedRequest::OnHTTPRequestCompleted(HTTPRequestCompleted_t* delete[] response; } - if (g_http) - g_http->ReleaseHTTPRequest(arg->m_hRequest); + if (GetSteamHTTP()) + GetSteamHTTP()->ReleaseHTTPRequest(arg->m_hRequest); delete this; } @@ -141,14 +141,14 @@ void HTTPManager::GenerateRequest(EHTTPMethod method, const char* pszUrl, const CompletedCallback callbackCompleted, ErrorCallback callbackError, std::vector* headers) { - if (!g_http) + if (!GetSteamHTTP()) { - Panic("A web request was attempted before g_http was instantiated, returning early.\n"); + Panic("A web request was attempted on null ISteamHTTP, returning early.\n"); return; } // Message("Sending HTTP:\n%s\n", pszText); - auto hReq = g_http->CreateHTTPRequest(method, pszUrl); + auto hReq = GetSteamHTTP()->CreateHTTPRequest(method, pszUrl); int size = strlen(pszText); // Message("HTTP request: %p\n", hReq); @@ -157,7 +157,7 @@ void HTTPManager::GenerateRequest(EHTTPMethod method, const char* pszUrl, const || method == k_EHTTPMethodPUT || method == k_EHTTPMethodDELETE; - if (shouldHaveBody && !g_http->SetHTTPRequestRawPostBody(hReq, "application/json", (uint8*)pszText, size)) + if (shouldHaveBody && !GetSteamHTTP()->SetHTTPRequestRawPostBody(hReq, "application/json", (uint8*)pszText, size)) { // Message("Failed to SetHTTPRequestRawPostBody\n"); return; @@ -168,10 +168,10 @@ void HTTPManager::GenerateRequest(EHTTPMethod method, const char* pszUrl, const if (headers != nullptr) for (HTTPHeader header : *headers) - g_http->SetHTTPRequestHeaderValue(hReq, header.GetName(), header.GetValue()); + GetSteamHTTP()->SetHTTPRequestHeaderValue(hReq, header.GetName(), header.GetValue()); SteamAPICall_t hCall; - g_http->SendHTTPRequest(hReq, &hCall); + GetSteamHTTP()->SendHTTPRequest(hReq, &hCall); new TrackedRequest(hReq, hCall, pszUrl, pszText, callbackCompleted, callbackError); } diff --git a/src/map_votes.cpp b/src/map_votes.cpp index 078e72cd..d74971d6 100644 --- a/src/map_votes.cpp +++ b/src/map_votes.cpp @@ -861,13 +861,13 @@ void CMapVoteSystem::ForceNextMap(CCSPlayerController* pController, const char* void CMapVoteSystem::PrintDownloadProgress() { - if (GetDownloadQueueSize() == 0) + if (GetDownloadQueueSize() == 0 || !GetSteamUGC()) return; uint64 iBytesDownloaded = 0; uint64 iTotalBytes = 0; - if (!g_steamAPI.SteamUGC()->GetItemDownloadInfo(m_DownloadQueue.front(), &iBytesDownloaded, &iTotalBytes) || !iTotalBytes) + if (!GetSteamUGC()->GetItemDownloadInfo(m_DownloadQueue.front(), &iBytesDownloaded, &iTotalBytes) || !iTotalBytes) return; double flMBDownloaded = (double)iBytesDownloaded / 1024 / 1024; @@ -881,7 +881,7 @@ void CMapVoteSystem::PrintDownloadProgress() void CMapVoteSystem::OnMapDownloaded(DownloadItemResult_t* pResult) { - if (std::find(m_DownloadQueue.begin(), m_DownloadQueue.end(), pResult->m_nPublishedFileId) == m_DownloadQueue.end()) + if (std::find(m_DownloadQueue.begin(), m_DownloadQueue.end(), pResult->m_nPublishedFileId) == m_DownloadQueue.end() || !GetSteamUGC()) return; // 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) Message("Addon %llu download failed with status code 3, retrying in 2 minutes\n", workshopID); m_pRateLimitedDownloadTimer = CTimer::Create(120.0f, TIMERFLAG_NONE, [workshopID]() { - g_steamAPI.SteamUGC()->DownloadItem(workshopID, false); + GetSteamUGC()->DownloadItem(workshopID, false); return -1.0f; }); @@ -904,7 +904,7 @@ void CMapVoteSystem::OnMapDownloaded(DownloadItemResult_t* pResult) if (GetDownloadQueueSize() == 0) return; - g_steamAPI.SteamUGC()->DownloadItem(m_DownloadQueue.front(), false); + GetSteamUGC()->DownloadItem(m_DownloadQueue.front(), false); } void CMapVoteSystem::QueueMapDownload(PublishedFileId_t iWorkshopId) @@ -915,7 +915,7 @@ void CMapVoteSystem::QueueMapDownload(PublishedFileId_t iWorkshopId) m_DownloadQueue.push_back(iWorkshopId); if (m_DownloadQueue.front() == iWorkshopId) - g_steamAPI.SteamUGC()->DownloadItem(iWorkshopId, false); + GetSteamUGC()->DownloadItem(iWorkshopId, false); } bool CMapVoteSystem::LoadMapList() @@ -1388,8 +1388,15 @@ std::pair> CMapVoteSystem::GetMapInfoByIdentifiers(co std::shared_ptr CMapSystemWorkshopDetailsQuery::Create(uint64 iWorkshopId, CCSPlayerController* pController, QueryCallback_t callbackSuccess) { + if (!GetSteamUGC()) + { + Panic("A workshop map query was attempted on null ISteamUGC, returning early.\n"); + ClientPrint(pController, HUD_PRINTTALK, CHAT_PREFIX "Failed to query workshop map information for ID \x06%llu\x01.", iWorkshopId); + return nullptr; + } + uint64 iWorkshopIDArray[1] = {iWorkshopId}; - UGCQueryHandle_t hQuery = g_steamAPI.SteamUGC()->CreateQueryUGCDetailsRequest(iWorkshopIDArray, 1); + UGCQueryHandle_t hQuery = GetSteamUGC()->CreateQueryUGCDetailsRequest(iWorkshopIDArray, 1); if (hQuery == k_UGCQueryHandleInvalid) { @@ -1397,8 +1404,8 @@ std::shared_ptr CMapSystemWorkshopDetailsQuery:: return nullptr; } - g_steamAPI.SteamUGC()->SetAllowCachedResponse(hQuery, 0); - SteamAPICall_t hCall = g_steamAPI.SteamUGC()->SendQueryUGCRequest(hQuery); + GetSteamUGC()->SetAllowCachedResponse(hQuery, 0); + SteamAPICall_t hCall = GetSteamUGC()->SendQueryUGCRequest(hQuery); auto pQuery = std::make_shared(hQuery, iWorkshopId, pController, callbackSuccess); g_pMapVoteSystem->AddWorkshopDetailsQuery(pQuery); @@ -1415,7 +1422,7 @@ void CMapSystemWorkshopDetailsQuery::OnQueryCompleted(SteamUGCQueryCompleted_t* // Only allow null controller if controller was originally null (console) if (m_bConsole || pController) { - 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) + if (bFailed || pCompletedQuery->m_eResult != k_EResultOK || pCompletedQuery->m_unNumResultsReturned < 1 || !GetSteamUGC()->GetQueryUGCResult(pCompletedQuery->m_handle, 0, &details) || details.m_eResult != k_EResultOK) { ClientPrint(pController, HUD_PRINTTALK, CHAT_PREFIX "Failed to query workshop map information for ID \x06%llu\x01.", m_iWorkshopId); } @@ -1426,12 +1433,14 @@ void CMapSystemWorkshopDetailsQuery::OnQueryCompleted(SteamUGCQueryCompleted_t* else { // Try to get a head start on downloading the map if needed - g_steamAPI.SteamUGC()->DownloadItem(m_iWorkshopId, false); + GetSteamUGC()->DownloadItem(m_iWorkshopId, false); m_callbackSuccess(std::make_shared(details.m_rgchTitle, m_iWorkshopId), pController); } } - g_steamAPI.SteamUGC()->ReleaseQueryUGCRequest(m_hQuery); + if (GetSteamUGC()) + GetSteamUGC()->ReleaseQueryUGCRequest(m_hQuery); + g_pMapVoteSystem->RemoveWorkshopDetailsQuery(shared_from_this()); } diff --git a/src/mapmigrations.cpp b/src/mapmigrations.cpp index 9a964633..2ebc5717 100644 --- a/src/mapmigrations.cpp +++ b/src/mapmigrations.cpp @@ -110,8 +110,14 @@ void CMapMigrations::UpdateMapUpdateTime(time_t timeMapUpdated) std::shared_ptr CMapMigrationWorkshopDetailsQuery::Create(uint64 iWorkshopId) { + if (!GetSteamUGC()) + { + Panic("Map migrations failed to find current map update time: null ISteamUGC\n"); + return nullptr; + } + uint64 iWorkshopIDArray[1] = {iWorkshopId}; - UGCQueryHandle_t hQuery = g_steamAPI.SteamUGC()->CreateQueryUGCDetailsRequest(iWorkshopIDArray, 1); + UGCQueryHandle_t hQuery = GetSteamUGC()->CreateQueryUGCDetailsRequest(iWorkshopIDArray, 1); if (hQuery == k_UGCQueryHandleInvalid) { @@ -119,8 +125,8 @@ std::shared_ptr CMapMigrationWorkshopDetailsQ return nullptr; } - g_steamAPI.SteamUGC()->SetAllowCachedResponse(hQuery, 0); - SteamAPICall_t hCall = g_steamAPI.SteamUGC()->SendQueryUGCRequest(hQuery); + GetSteamUGC()->SetAllowCachedResponse(hQuery, 0); + SteamAPICall_t hCall = GetSteamUGC()->SendQueryUGCRequest(hQuery); auto pQuery = std::make_shared(hQuery, iWorkshopId); g_pMapMigrations->AddWorkshopDetailsQuery(pQuery); @@ -133,11 +139,13 @@ void CMapMigrationWorkshopDetailsQuery::OnQueryCompleted(SteamUGCQueryCompleted_ { SteamUGCDetails_t details; - 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) + if (bFailed || pCompletedQuery->m_eResult != k_EResultOK || pCompletedQuery->m_unNumResultsReturned < 1 || !GetSteamUGC()->GetQueryUGCResult(pCompletedQuery->m_handle, 0, &details) || details.m_eResult != k_EResultOK) Panic("Map migrations failed to find current map update time: failed to query workshop map information for ID %llu\n", m_iWorkshopId); else g_pMapMigrations->UpdateMapUpdateTime(details.m_rtimeUpdated); - g_steamAPI.SteamUGC()->ReleaseQueryUGCRequest(m_hQuery); + if (GetSteamUGC()) + GetSteamUGC()->ReleaseQueryUGCRequest(m_hQuery); + g_pMapMigrations->RemoveWorkshopDetailsQuery(shared_from_this()); } \ No newline at end of file diff --git a/src/utils/utils.cpp b/src/utils/utils.cpp index dbdd56f3..b2114219 100644 --- a/src/utils/utils.cpp +++ b/src/utils/utils.cpp @@ -77,4 +77,20 @@ std::string StringToLower(std::string strValue) strValue[i] = tolower(strValue[i]); return strValue; +} + +ISteamUGC* GetSteamUGC() +{ + if (g_pEngineServer2->IsDedicatedServer()) + return SteamGameServerUGC(); + else + return SteamUGC(); +} + +ISteamHTTP* GetSteamHTTP() +{ + if (g_pEngineServer2->IsDedicatedServer()) + return SteamGameServerHTTP(); + else + return SteamHTTP(); } \ No newline at end of file diff --git a/src/utils/utils.h b/src/utils/utils.h index 2f55c528..24d16c66 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -20,6 +20,7 @@ #pragma once #include "playerslot.h" +#include "steam/isteamugc.h" #include "utlvector.h" #include @@ -33,3 +34,6 @@ CServerSideClient* GetClientBySlot(CPlayerSlot slot); uint32 GetSoundEventHash(const char* pszSoundEventName); std::string StringToLower(std::string strValue); + +ISteamUGC* GetSteamUGC(); +ISteamHTTP* GetSteamHTTP(); \ No newline at end of file