Make wrapper for forcing existing instance on top

Also renamed constant 'DEFAULT_WINDOW_CLASS_NAME' to 'GAME_WINDOW_CLASS_NAME'.
This commit is contained in:
Kawe Mazidjatari 2023-08-01 00:02:13 +02:00
parent 9a446db8ba
commit d60d05115c
5 changed files with 30 additions and 16 deletions

View File

@ -25,6 +25,11 @@ add_sources( SOURCE_GROUP "Resource"
"${ENGINE_SOURCE_DIR}/resource/sdklauncher.rc"
)
add_sources( SOURCE_GROUP "Windows"
"${ENGINE_SOURCE_DIR}/windows/window.cpp"
"${ENGINE_SOURCE_DIR}/windows/window.h"
)
end_sources()
set_target_properties( ${PROJECT_NAME} PROPERTIES OUTPUT_NAME

View File

@ -7,6 +7,7 @@
#include "base_surface.h"
#include "advanced_surface.h"
#include "sdklauncher.h"
#include "sdklauncher_utils.h"
///////////////////////////////////////////////////////////////////////////////
// Purpose: initializes and runs the user interface
@ -345,7 +346,7 @@ BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
return FALSE;
}
if (strcmp(szClassName, DEFAULT_WINDOW_CLASS_NAME) == 0)
if (strcmp(szClassName, GAME_WINDOW_CLASS_NAME) == 0)
{
pHandles->push_back(hwnd);
}
@ -390,24 +391,12 @@ void LauncherLoggerSink(LogType_t logType, LogLevel_t logLevel, eDLL_T context,
///////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[]/*, char* envp[]*/)
{
HANDLE singleInstanceMutex = CreateMutexW(NULL, TRUE, L"SDKLauncher_Mutex");
if (singleInstanceMutex == NULL || GetLastError() == ERROR_ALREADY_EXISTS)
// Only 1 instance at a time.
if (SDKLauncher_ForceExistingInstanceOnTop())
{
HWND existingApp = FindWindowW(0, L"SDKLauncher001");
if (existingApp) SetForegroundWindow(existingApp);
return EXIT_SUCCESS;
}
WNDCLASSEXW wcex = { sizeof(wcex) };
wcex.lpszClassName = L"SDKLauncher001";
if (!RegisterClassExW(&wcex))
{
DWORD dwError = GetLastError();
return dwError;
}
g_pLauncher->InitLogger();
if (argc < 2)
{

View File

@ -7,7 +7,8 @@
//#define DEDI_LAUNCHER
#define GAME_CFG_PATH "platform\\cfg\\system\\"
#define DEFAULT_WINDOW_CLASS_NAME "Respawn001"
#define GAME_WINDOW_CLASS_NAME "Respawn001"
#define LAUNCHER_SETTING_FILE "launcher.vdf"
// Gigabytes.

View File

@ -1,4 +1,5 @@
#include "sdklauncher_utils.h"
#include "windows/window.h"
#include "tier1/xorstr.h"
#include "tier1/utlmap.h"
#include "tier2/curlutils.h"
@ -528,3 +529,19 @@ bool SDKLauncher_CheckForUpdate(const bool bPreRelease)
// This evaluates to '0' if the version tags are equal.
return !(localManifest["version"] == remoteManifest["tag_name"]);
}
//----------------------------------------------------------------------------
// Purpose:
//----------------------------------------------------------------------------
bool SDKLauncher_ForceExistingInstanceOnTop()
{
HWND existingApp = FindWindowA(FORM_DEFAULT_CLASS_NAME, NULL);
if (existingApp)
{
ForceForegroundWindow(existingApp);
return true;
}
return false;
}

View File

@ -19,3 +19,5 @@ bool SDKLauncher_InstallAssetList(const bool bOptionalAssets,
bool SDKLauncher_CheckDiskSpace(const int minRequiredSpace, int* const availableSize = nullptr);
bool SDKLauncher_CheckForUpdate(const bool bPreRelease);
bool SDKLauncher_ForceExistingInstanceOnTop();