Skip to content

Commit

Permalink
Fix a bug that game crash when quitting game because SteamAPI_Shutdow…
Browse files Browse the repository at this point in the history
…n was called before HUD_Shutdown by serverbrowser.dll
  • Loading branch information
hzqst committed Jan 26, 2024
1 parent dcb058e commit 4b41fb2
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ class CUtilHTTPClient : public IUtilHTTPClient
{
std::lock_guard<std::mutex> lock(m_RequestHandleLock);

for (auto itor = m_RequestPool.begin(); itor != m_RequestPool.end();)
for (auto itor = m_RequestPool.begin(); itor != m_RequestPool.end(); itor ++)
{
auto RequestInstance = (*itor).second;

Expand Down
6 changes: 4 additions & 2 deletions Plugins/SCModelDownloader/SCModelDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ class CSCModelDatabase : public ISCModelDatabase

void Shutdown() override
{
m_DatabaseQuery.reset();
m_QueryTaskList.clear();
g_Database.clear();
}
Expand Down Expand Up @@ -526,7 +527,6 @@ class CSCModelDatabase : public ISCModelDatabase

std::transform(lowerName.begin(), lowerName.end(), lowerName.begin(), ::tolower);


//Model name ends with "_v0" ~ "_v9"
if (lowerName.length() > 4 &&
lowerName[lowerName.length() - 3] == '_' &&
Expand Down Expand Up @@ -581,4 +581,6 @@ static CSCModelDatabase s_SCModelDatabase;
ISCModelDatabase* SCModelDatabase()
{
return &s_SCModelDatabase;
}
}

EXPOSE_SINGLE_INTERFACE_GLOBALVAR(CSCModelDatabase, ISCModelDatabase, SCMODEL_DATABASE_INTERFACE_VERSION, s_SCModelDatabase)
2 changes: 1 addition & 1 deletion Plugins/SCModelDownloader/SCModelDatabase.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ class ISCModelDatabase : public IBaseInterface

ISCModelDatabase* SCModelDatabase();

#define SCMODEL_DATABASE_INTERFACE_VERSION "SCModelDatabase_001"
#define SCMODEL_DATABASE_INTERFACE_VERSION "SCModelDatabase_API_001"
4 changes: 2 additions & 2 deletions Plugins/SCModelDownloader/exportfuncs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,16 @@ void HUD_Init(void)

gEngfuncs.pfnAddCommand("scmodel_reload", SCModel_Reload_f);

UtilHTTPClient_Init();
SCModelDatabase()->Init();
}

void HUD_Shutdown(void)
{
SCModelDatabase()->Shutdown();
UtilHTTPClient_Shutdown();

gExportfuncs.HUD_Shutdown();

UtilHTTPClient_Shutdown();
}

int HUD_GetStudioModelInterface(int version, struct r_studio_interface_s **ppinterface, struct engine_studio_api_s *pstudio)
Expand Down
12 changes: 12 additions & 0 deletions Plugins/SCModelDownloader/plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ int g_iEngineType = 0;
PVOID g_dwClientBase = 0;
DWORD g_dwClientSize = 0;

void DllLoadNotification(mh_load_dll_notification_context_t* ctx);

void IPluginsV4::Init(metahook_api_t *pAPI, mh_interface_t *pInterface, mh_enginesave_t *pSave)
{
Expand All @@ -40,10 +41,17 @@ void IPluginsV4::Init(metahook_api_t *pAPI, mh_interface_t *pInterface, mh_engin

void IPluginsV4::Shutdown(void)
{
g_pMetaHookAPI->UnregisterLoadDllNotificationCallback(DllLoadNotification);
}

void IPluginsV4::LoadEngine(cl_enginefunc_t *pEngfuncs)
{
if (g_pInterface->MetaHookAPIVersion < METAHOOK_API_VERSION)
{
g_pMetaHookAPI->SysError("MetaHookAPIVersion too low! expect %d, got %d !", METAHOOK_API_VERSION, g_pInterface->MetaHookAPIVersion);
return;
}

g_pFileSystem = g_pInterface->FileSystem;
g_iEngineType = g_pMetaHookAPI->GetEngineType();
g_dwEngineBuildnum = g_pMetaHookAPI->GetEngineBuildnum();
Expand All @@ -60,6 +68,8 @@ void IPluginsV4::LoadEngine(cl_enginefunc_t *pEngfuncs)

gPrivateFuncs.R_StudioChangePlayerModel = (decltype(gPrivateFuncs.R_StudioChangePlayerModel))Search_Pattern(R_STUDIOCHANGEPLAYERMODEL_SIG_SVENGINE);
Sig_FuncNotFound(R_StudioChangePlayerModel);

g_pMetaHookAPI->RegisterLoadDllNotificationCallback(DllLoadNotification);
}

void IPluginsV4::LoadClient(cl_exportfuncs_t *pExportFunc)
Expand All @@ -73,6 +83,8 @@ void IPluginsV4::LoadClient(cl_exportfuncs_t *pExportFunc)
pExportFunc->HUD_Shutdown = HUD_Shutdown;
pExportFunc->HUD_Frame = HUD_Frame;
pExportFunc->HUD_GetStudioModelInterface = HUD_GetStudioModelInterface;

UtilHTTPClient_Init();
}

void IPluginsV4::ExitGame(int iResult)
Expand Down
43 changes: 42 additions & 1 deletion Plugins/SCModelDownloader/privatehook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,45 @@

privte_funcs_t gPrivateFuncs;

player_model_t(*DM_PlayerState)[MAX_CLIENTS];
player_model_t(*DM_PlayerState)[MAX_CLIENTS];

static HMODULE g_hServerBrowser = NULL;

void NewSteamAPI_Shutdown()
{

}

void ServerBrowser_InstallHook(HMODULE hModule)
{
//idk why Sven Co-op Team added SteamAPI_Shutdown inside ServerBrowser's CVGUIModule::Shutdown which causes Steam API to be unavailable even before client's HUD_Shutdown.
if (g_pMetaHookAPI->ModuleHasImport(hModule, "steam_api.dll") && !g_pMetaHookAPI->ModuleHasImportEx(hModule, "steam_api.dll", "SteamAPI_Shutdown"))
return;

g_pMetaHookAPI->IATHook(hModule, "steam_api.dll", "SteamAPI_Shutdown", NewSteamAPI_Shutdown, NULL);
}

void ServerBrowser_UninstallHook(HMODULE hModule)
{

}

void DllLoadNotification(mh_load_dll_notification_context_t* ctx)
{
if (ctx->flags & LOAD_DLL_NOTIFICATION_IS_LOAD)
{
if (ctx->BaseDllName && ctx->hModule && !_wcsicmp(ctx->BaseDllName, L"serverbrowser.dll"))
{
g_hServerBrowser = ctx->hModule;
ServerBrowser_InstallHook(ctx->hModule);
}
}
else if (ctx->flags & LOAD_DLL_NOTIFICATION_IS_UNLOAD)
{
if (ctx->hModule == g_hServerBrowser)
{
ServerBrowser_UninstallHook(ctx->hModule);
g_hServerBrowser = NULL;
}
}
}

0 comments on commit 4b41fb2

Please sign in to comment.