gbe_fork/steamclient_loader/ColdClientLoader.cpp

188 lines
7.1 KiB
C++
Raw Normal View History

// My own modified version of ColdClientLoader originally written by Rat431
2023-12-24 06:42:36 +02:00
// https://github.com/Rat431/ColdAPI_Steam/tree/master/src/ColdClientLoader
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// Windows Header Files
#include <windows.h>
// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <stdio.h>
bool IsNotRelativePathOrRemoveFileName(WCHAR* output, bool Remove)
{
int LG = lstrlenW(output);
for (int i = LG; i > 0; i--) {
if (output[i] == '\\') {
if(Remove)
RtlFillMemory(&output[i], (LG - i) * sizeof(WCHAR), NULL);
return true;
}
}
return false;
}
int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow)
{
WCHAR CurrentDirectory[MAX_PATH] = { 0 };
WCHAR Client64Path[MAX_PATH] = { 0 };
WCHAR ClientPath[MAX_PATH] = { 0 };
WCHAR ExeFile[MAX_PATH] = { 0 };
WCHAR ExeRunDir[MAX_PATH] = { 0 };
WCHAR ExeCommandLine[4096] = { 0 };
WCHAR AppId[128] = { 0 };
STARTUPINFOW info = { 0 };
SecureZeroMemory(&info, sizeof(info));
info.cb = sizeof(info);
PROCESS_INFORMATION processInfo = { 0 };
SecureZeroMemory(&processInfo, sizeof(processInfo));
int Length = GetModuleFileNameW(GetModuleHandleW(NULL), CurrentDirectory, sizeof(CurrentDirectory)) + 1;
for (int i = Length; i > 0; i--) {
if (CurrentDirectory[i] == '\\') {
lstrcpyW(&CurrentDirectory[i + 1], L"ColdClientLoader.ini");
break;
}
}
2023-12-24 06:41:34 +02:00
if (GetFileAttributesW(CurrentDirectory) == INVALID_FILE_ATTRIBUTES) {
MessageBoxA(NULL, "Couldn't find the configuration file(ColdClientLoader.ini).", "ColdClientLoader", MB_ICONERROR);
2023-12-24 06:41:34 +02:00
return 1;
}
GetPrivateProfileStringW(L"SteamClient", L"SteamClient64Dll", L"", Client64Path, MAX_PATH, CurrentDirectory);
GetPrivateProfileStringW(L"SteamClient", L"SteamClientDll", L"", ClientPath, MAX_PATH, CurrentDirectory);
GetPrivateProfileStringW(L"SteamClient", L"Exe", NULL, ExeFile, MAX_PATH, CurrentDirectory);
GetPrivateProfileStringW(L"SteamClient", L"ExeRunDir", NULL, ExeRunDir, MAX_PATH, CurrentDirectory);
GetPrivateProfileStringW(L"SteamClient", L"ExeCommandLine", NULL, ExeCommandLine, 4096, CurrentDirectory);
GetPrivateProfileStringW(L"SteamClient", L"AppId", NULL, AppId, sizeof(AppId), CurrentDirectory);
if (AppId[0]) {
SetEnvironmentVariableW(L"SteamAppId", AppId);
SetEnvironmentVariableW(L"SteamGameId", AppId);
SetEnvironmentVariableW(L"SteamOverlayGameId", AppId);
} else {
MessageBoxA(NULL, "You forgot to set the AppId.", "ColdClientLoader", MB_ICONERROR);
2023-12-24 06:41:34 +02:00
return 1;
}
2023-12-24 06:42:02 +02:00
WCHAR TMP[MAX_PATH] = { 0 };
if (!IsNotRelativePathOrRemoveFileName(Client64Path, false)) {
lstrcpyW(TMP, Client64Path);
2023-12-24 06:40:33 +02:00
SecureZeroMemory(Client64Path, sizeof(Client64Path));
GetFullPathNameW(TMP, MAX_PATH, Client64Path, NULL);
}
if (!IsNotRelativePathOrRemoveFileName(ClientPath, false)) {
lstrcpyW(TMP, ClientPath);
2023-12-24 06:40:33 +02:00
SecureZeroMemory(ClientPath, sizeof(ClientPath));
GetFullPathNameW(TMP, MAX_PATH, ClientPath, NULL);
}
if (!IsNotRelativePathOrRemoveFileName(ExeFile, false)) {
lstrcpyW(TMP, ExeFile);
2023-12-24 06:40:33 +02:00
SecureZeroMemory(ExeFile, sizeof(ExeFile));
GetFullPathNameW(TMP, MAX_PATH, ExeFile, NULL);
}
if (!IsNotRelativePathOrRemoveFileName(ExeRunDir, false)) {
lstrcpyW(TMP, ExeRunDir);
2023-12-24 06:40:33 +02:00
SecureZeroMemory(ExeRunDir, sizeof(ExeRunDir));
GetFullPathNameW(TMP, MAX_PATH, ExeRunDir, NULL);
}
if (GetFileAttributesW(Client64Path) == INVALID_FILE_ATTRIBUTES) {
MessageBoxA(NULL, "Couldn't find the requested SteamClient64Dll.", "ColdClientLoader", MB_ICONERROR);
2023-12-24 06:41:34 +02:00
return 1;
}
if (GetFileAttributesW(ClientPath) == INVALID_FILE_ATTRIBUTES) {
MessageBoxA(NULL, "Couldn't find the requested SteamClientDll.", "ColdClientLoader", MB_ICONERROR);
2023-12-24 06:41:34 +02:00
return 1;
}
if (GetFileAttributesW(ExeFile) == INVALID_FILE_ATTRIBUTES) {
MessageBoxA(NULL, "Couldn't find the requested Exe file.", "ColdClientLoader", MB_ICONERROR);
2023-12-24 06:41:34 +02:00
return 1;
}
WCHAR CommandLine[16384] = { 0 };
_snwprintf(CommandLine, _countof(CommandLine), L"\"%ls\" %ls %ls", ExeFile, ExeCommandLine, lpCmdLine);
if (!ExeFile[0] || !CreateProcessW(ExeFile, CommandLine, NULL, NULL, TRUE, CREATE_SUSPENDED, NULL, ExeRunDir, &info, &processInfo))
{
MessageBoxA(NULL, "Unable to load the requested EXE file.", "ColdClientLoader", MB_ICONERROR);
2023-12-24 06:41:34 +02:00
return 1;
}
2023-12-24 06:42:02 +02:00
HKEY Registrykey = { 0 };
// Declare some variables to be used for Steam registry.
DWORD UserId = 0x03100004771F810D & 0xffffffff;
DWORD ProcessID = GetCurrentProcessId();
2021-02-07 19:05:22 -05:00
bool orig_steam = false;
DWORD keyType = REG_SZ;
WCHAR OrgSteamCDir[MAX_PATH] = { 0 };
WCHAR OrgSteamCDir64[MAX_PATH] = { 0 };
2021-02-07 19:05:22 -05:00
DWORD Size1 = MAX_PATH;
DWORD Size2 = MAX_PATH;
if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\Valve\\Steam\\ActiveProcess", 0, KEY_ALL_ACCESS, &Registrykey) == ERROR_SUCCESS)
{
2021-02-07 19:05:22 -05:00
orig_steam = true;
// Get original values to restore later.
RegQueryValueExW(Registrykey, L"SteamClientDll", 0, &keyType, (LPBYTE)& OrgSteamCDir, &Size1);
RegQueryValueExW(Registrykey, L"SteamClientDll64", 0, &keyType, (LPBYTE)& OrgSteamCDir64, &Size2);
2021-02-07 19:05:22 -05:00
} else {
if (RegCreateKeyExW(HKEY_CURRENT_USER, L"Software\\Valve\\Steam\\ActiveProcess", 0, 0, REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, NULL, &Registrykey, NULL) != ERROR_SUCCESS)
{
TerminateProcess(processInfo.hProcess, NULL);
MessageBoxA(NULL, "Unable to patch Steam process informations on the Windows registry.", "ColdClientLoader", MB_ICONERROR);
2023-12-24 06:41:34 +02:00
return 1;
}
}
2021-02-07 19:05:22 -05:00
// Set values to Windows registry.
RegSetValueExA(Registrykey, "ActiveUser", NULL, REG_DWORD, (LPBYTE)& UserId, sizeof(DWORD));
RegSetValueExA(Registrykey, "pid", NULL, REG_DWORD, (LPBYTE)& ProcessID, sizeof(DWORD));
2021-02-07 19:05:22 -05:00
{
// Before saving to the registry check again if the path was valid and if the file exist
if (GetFileAttributesW(ClientPath) != INVALID_FILE_ATTRIBUTES) {
RegSetValueExW(Registrykey, L"SteamClientDll", NULL, REG_SZ, (LPBYTE)ClientPath, (DWORD)(lstrlenW(ClientPath) * sizeof(WCHAR)) + 1);
}
2021-02-07 19:05:22 -05:00
else {
RegSetValueExW(Registrykey, L"SteamClientDll", NULL, REG_SZ, (LPBYTE)"", 0);
2021-02-07 19:05:22 -05:00
}
if (GetFileAttributesW(Client64Path) != INVALID_FILE_ATTRIBUTES) {
RegSetValueExW(Registrykey, L"SteamClientDll64", NULL, REG_SZ, (LPBYTE)Client64Path, (DWORD)(lstrlenW(Client64Path) * sizeof(WCHAR)) + 1);
2021-02-07 19:05:22 -05:00
}
else {
RegSetValueExW(Registrykey, L"SteamClientDll64", NULL, REG_SZ, (LPBYTE)"", 0);
2021-02-07 19:05:22 -05:00
}
}
RegSetValueExA(Registrykey, "Universe", NULL, REG_SZ, (LPBYTE)"Public", (DWORD)lstrlenA("Public") + 1);
2021-02-07 19:05:22 -05:00
// Close the HKEY Handle.
RegCloseKey(Registrykey);
2021-02-07 19:05:22 -05:00
ResumeThread(processInfo.hThread);
WaitForSingleObject(processInfo.hThread, INFINITE);
CloseHandle(processInfo.hProcess);
CloseHandle(processInfo.hThread);
2021-02-07 19:05:22 -05:00
if (orig_steam) {
if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\Valve\\Steam\\ActiveProcess", 0, KEY_ALL_ACCESS, &Registrykey) == ERROR_SUCCESS)
{
// Restore the values.
RegSetValueExW(Registrykey, L"SteamClientDll", NULL, REG_SZ, (LPBYTE)OrgSteamCDir, Size1);
RegSetValueExW(Registrykey, L"SteamClientDll64", NULL, REG_SZ, (LPBYTE)OrgSteamCDir64, Size2);
// Close the HKEY Handle.
RegCloseKey(Registrykey);
}
}
2021-02-07 19:05:22 -05:00
return 0;
}