iw4x-client/src/Components/Modules/ConnectProtocol.cpp

233 lines
5.1 KiB
C++
Raw Normal View History

#include "STDInclude.hpp"
using namespace std::literals;
2015-12-29 17:13:23 -05:00
namespace Components
{
ConnectProtocol::Container ConnectProtocol::ConnectContainer = { false, false, "" };
2015-12-29 17:13:23 -05:00
bool ConnectProtocol::Evaluated()
{
return ConnectProtocol::ConnectContainer.Evaluated;
}
bool ConnectProtocol::Used()
{
if (!ConnectProtocol::Evaluated())
{
ConnectProtocol::EvaluateProtocol();
}
return (ConnectProtocol::ConnectContainer.ConnectString.size() > 0);
}
2015-12-29 17:13:23 -05:00
bool ConnectProtocol::InstallProtocol()
{
HKEY hKey = NULL;
std::string data;
char ownPth[MAX_PATH] = { 0 };
char workdir[MAX_PATH] = { 0 };
char regred[MAX_PATH] = { 0 };
2015-12-29 17:13:23 -05:00
DWORD dwsize = MAX_PATH;
2016-01-03 13:29:22 -05:00
HMODULE hModule = GetModuleHandle(NULL);
2015-12-29 17:13:23 -05:00
if (hModule != NULL)
{
2016-01-03 13:29:22 -05:00
if (GetModuleFileName(hModule, ownPth, MAX_PATH) == ERROR)
{
return false;
}
2016-01-03 13:29:22 -05:00
if (GetModuleFileName(hModule, workdir, MAX_PATH) == ERROR)
{
return false;
}
else
{
char* endPtr = strstr(workdir, "iw4x.exe");
if (endPtr != NULL)
{
*endPtr = 0;
}
else
{
return false;
}
}
2015-12-29 17:13:23 -05:00
}
else
{
return false;
}
SetCurrentDirectory(workdir);
LONG openRes = RegOpenKeyEx(HKEY_CURRENT_USER, "SOFTWARE\\Classes\\iw4x\\shell\\open\\command", 0, KEY_ALL_ACCESS, &hKey);
2015-12-29 17:13:23 -05:00
if (openRes == ERROR_SUCCESS)
{
2016-01-04 07:20:04 -05:00
// Check if the game has been moved.
openRes = RegQueryValueEx(hKey, 0, 0, 0, reinterpret_cast<BYTE*>(regred), &dwsize);
2015-12-29 17:13:23 -05:00
if (openRes == ERROR_SUCCESS)
{
2016-01-04 07:20:04 -05:00
char* endPtr = strstr(regred, "\" \"%1\"");
if (endPtr != NULL)
2016-01-03 13:29:22 -05:00
{
2016-01-04 07:20:04 -05:00
*endPtr = 0;
2016-01-03 13:29:22 -05:00
}
else
{
return false;
}
2015-12-29 17:13:23 -05:00
RegCloseKey(hKey);
2016-01-04 07:20:04 -05:00
if (strcmp(regred + 1, ownPth))
2015-12-29 17:13:23 -05:00
{
openRes = RegDeleteKey(HKEY_CURRENT_USER, "SOFTWARE\\Classes\\iw4x");
2015-12-29 17:13:23 -05:00
}
else
{
2015-12-29 17:13:23 -05:00
return true;
}
}
else
{
openRes = RegDeleteKey(HKEY_CURRENT_USER, "SOFTWARE\\Classes\\iw4x");
}
}
else
{
openRes = RegDeleteKey(HKEY_CURRENT_USER, "SOFTWARE\\Classes\\iw4x");
}
2016-01-03 13:29:22 -05:00
// Open SOFTWARE\\Classes
openRes = RegOpenKeyEx(HKEY_CURRENT_USER, "SOFTWARE\\Classes", 0, KEY_ALL_ACCESS, &hKey);
2015-12-29 17:13:23 -05:00
if (openRes != ERROR_SUCCESS)
{
return false;
}
2015-12-29 17:13:23 -05:00
// Create SOFTWARE\\Classes\\iw4x
openRes = RegCreateKeyEx(hKey, "iw4x", 0, 0, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0, &hKey, 0);
2015-12-29 17:13:23 -05:00
if (openRes != ERROR_SUCCESS)
{
return false;
}
2015-12-29 17:13:23 -05:00
// Write URL:iw4x Protocol
data = "URL:iw4x Protocol";
openRes = RegSetValueEx(hKey, "URL Protocol", 0, REG_SZ, reinterpret_cast<const BYTE*>(data.data()), data.size() + 1);
if (openRes != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return false;
2015-12-29 17:13:23 -05:00
}
// Create SOFTWARE\\Classes\\iw4x\\DefaultIcon
openRes = RegCreateKeyEx(hKey, "DefaultIcon", 0, 0, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0, &hKey, 0);
if (openRes != ERROR_SUCCESS)
2015-12-29 17:13:23 -05:00
{
return false;
}
2016-01-03 13:29:22 -05:00
data = Utils::VA("%s,1", ownPth);
openRes = RegSetValueEx(hKey, 0, 0, REG_SZ, reinterpret_cast<const BYTE*>(data.data()), data.size() + 1);
RegCloseKey(hKey);
2015-12-29 17:13:23 -05:00
if (openRes != ERROR_SUCCESS)
{
RegCloseKey(hKey);
return false;
2015-12-29 17:13:23 -05:00
}
openRes = RegOpenKeyEx(HKEY_CURRENT_USER, "SOFTWARE\\Classes\\iw4x", 0, KEY_ALL_ACCESS, &hKey);
2015-12-29 17:13:23 -05:00
if (openRes != ERROR_SUCCESS)
2015-12-29 17:13:23 -05:00
{
return false;
}
2015-12-29 17:13:23 -05:00
openRes = RegCreateKeyEx(hKey, "shell\\open\\command", 0, 0, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0, &hKey, 0);
2015-12-29 17:13:23 -05:00
if (openRes != ERROR_SUCCESS)
{
return false;
2015-12-29 17:13:23 -05:00
}
data = Utils::VA("\"%s\" \"%s\"", ownPth, "%1");
openRes = RegSetValueEx(hKey, 0, 0, REG_SZ, reinterpret_cast<const BYTE*>(data.data()), data.size() + 1);
RegCloseKey(hKey);
if (openRes != ERROR_SUCCESS)
2015-12-29 17:13:23 -05:00
{
return false;
}
return true;
}
void ConnectProtocol::EvaluateProtocol()
{
if (ConnectProtocol::ConnectContainer.Evaluated) return;
ConnectProtocol::ConnectContainer.Evaluated = true;
2016-01-04 07:20:04 -05:00
std::string cmdLine = GetCommandLine();
2015-12-29 17:13:23 -05:00
2016-01-04 07:20:04 -05:00
auto pos = cmdLine.find("iw4x://");
2015-12-29 17:13:23 -05:00
2016-01-04 07:20:04 -05:00
if (pos != std::string::npos)
2016-01-03 13:29:22 -05:00
{
2016-01-04 07:20:04 -05:00
cmdLine = cmdLine.substr(pos + 7);
pos = cmdLine.find_first_of("/");
if (pos != std::string::npos)
{
cmdLine = cmdLine.substr(0, pos);
}
2015-12-29 17:13:23 -05:00
2016-01-04 07:20:04 -05:00
ConnectProtocol::ConnectContainer.ConnectString = cmdLine;
}
2015-12-29 17:13:23 -05:00
}
ConnectProtocol::ConnectProtocol()
2015-12-29 17:13:23 -05:00
{
// IPC handler
IPCPipe::On("connect", [] (std::string data)
2015-12-29 17:13:23 -05:00
{
Command::Execute(Utils::VA("connect %s", data.data()), false);
});
2016-01-03 13:29:22 -05:00
// Invocation handler
// TODO: Don't call it every frame, once is enough!
Renderer::OnFrame([] ()
2015-12-29 17:13:23 -05:00
{
if (!ConnectProtocol::ConnectContainer.Invoked && ConnectProtocol::Used())
2015-12-29 17:13:23 -05:00
{
ConnectProtocol::ConnectContainer.Invoked = true;
Command::Execute(Utils::VA("connect %s", ConnectProtocol::ConnectContainer.ConnectString.data()), false);
2015-12-29 17:13:23 -05:00
}
});
2015-12-29 17:13:23 -05:00
ConnectProtocol::InstallProtocol();
2016-01-03 13:29:22 -05:00
ConnectProtocol::EvaluateProtocol();
2015-12-29 17:13:23 -05:00
// Fire protocol handlers
// Make sure this happens after the pipe-initialization!
2016-01-04 08:09:21 -05:00
if (!Singleton::IsFirstInstance() && ConnectProtocol::Used())
2015-12-29 17:13:23 -05:00
{
IPCPipe::Write("connect", ConnectProtocol::ConnectContainer.ConnectString);
ExitProcess(0);
2015-12-29 17:13:23 -05:00
}
else
2015-12-29 17:13:23 -05:00
{
// Only skip intro here, invocation will be done later.
Utils::Hook::Set<BYTE>(0x60BECF, 0xEB);
2015-12-29 17:13:23 -05:00
}
}
}