2018-03-11 22:47:55 +00:00
|
|
|
|
/*
|
|
|
|
|
Copyright 2018 Peter Repukat - FlatspotSoftware
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
#include "SteamTarget.h"
|
2018-03-10 22:02:39 +00:00
|
|
|
|
|
2018-03-11 02:38:36 +00:00
|
|
|
|
#include "OverlayHookFunction.h"
|
2018-03-12 21:45:14 +00:00
|
|
|
|
|
|
|
|
|
#include "../common/common_hookfuns.h"
|
2018-03-12 19:44:00 +00:00
|
|
|
|
#include "../common/Injector.h"
|
2018-03-12 21:45:14 +00:00
|
|
|
|
#include "../common/process_alive.h"
|
|
|
|
|
|
2018-03-27 21:01:36 +00:00
|
|
|
|
#define LOGURU_IMPLEMENTATION 1
|
|
|
|
|
#include "../common/loguru.hpp"
|
|
|
|
|
|
2018-03-12 19:27:43 +00:00
|
|
|
|
#include <QProcess>
|
2018-03-12 20:05:07 +00:00
|
|
|
|
#include <QDir>
|
2018-03-12 21:45:14 +00:00
|
|
|
|
#include <QFile>
|
|
|
|
|
#include <QTextStream>
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
#include <QSettings>
|
|
|
|
|
#include <qmessagebox.h>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
#include <Windows.h>
|
|
|
|
|
#include <atlbase.h>
|
|
|
|
|
#include <Shobjidl.h>
|
|
|
|
|
|
2018-03-11 02:38:36 +00:00
|
|
|
|
|
2018-03-27 21:01:36 +00:00
|
|
|
|
|
2018-03-10 22:02:39 +00:00
|
|
|
|
SteamTarget::SteamTarget(int& argc, char** argv) : QApplication(argc, argv)
|
|
|
|
|
{
|
2018-03-27 21:01:36 +00:00
|
|
|
|
loguru::init(argc, argv);
|
2018-03-10 22:02:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SteamTarget::init()
|
|
|
|
|
{
|
2018-03-27 21:01:36 +00:00
|
|
|
|
loguru::add_file("last.log", loguru::Truncate, loguru::Verbosity_INFO);
|
2018-03-10 22:02:39 +00:00
|
|
|
|
connect(this, SIGNAL(aboutToQuit()), this, SLOT(onAboutToQuit()));
|
|
|
|
|
SetConsoleCtrlHandler(reinterpret_cast<PHANDLER_ROUTINE>(ConsoleCtrlCallback), true);
|
|
|
|
|
SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
|
2018-03-27 20:15:31 +00:00
|
|
|
|
readIni();
|
2018-03-27 21:06:27 +00:00
|
|
|
|
target_overlay_.init(!enable_overlay_, enable_overlay_only_config_);
|
2018-03-11 02:38:36 +00:00
|
|
|
|
initOverlayEvents();
|
2018-03-27 20:15:31 +00:00
|
|
|
|
controller_thread_ = std::make_unique<VirtualControllerThread>(update_rate_);
|
2018-03-10 22:02:39 +00:00
|
|
|
|
if (enable_controllers_)
|
2018-03-27 20:15:31 +00:00
|
|
|
|
controller_thread_->run();
|
2018-03-11 22:46:25 +00:00
|
|
|
|
if (hook_steam_ && !use_desktop_conf_)
|
|
|
|
|
Injector::hookSteam();
|
2018-03-12 20:05:07 +00:00
|
|
|
|
launchWatchdog();
|
2018-03-12 21:45:14 +00:00
|
|
|
|
if (launch_game_)
|
|
|
|
|
launchApplication();
|
2018-03-17 20:07:07 +00:00
|
|
|
|
|
|
|
|
|
sys_tray_icon_.setIcon(QIcon(":/SteamTarget/Resources/GloSC_Icon.png"));
|
|
|
|
|
tray_icon_menu_.addAction("Quit");
|
|
|
|
|
sys_tray_icon_.setContextMenu(&tray_icon_menu_);
|
|
|
|
|
|
|
|
|
|
connect(&sys_tray_icon_, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
|
|
|
|
|
this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
|
|
|
|
|
connect(*tray_icon_menu_.actions().begin(), SIGNAL(triggered()), this, SLOT(quit()));
|
2018-03-10 22:02:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BOOL SteamTarget::ConsoleCtrlCallback(DWORD dwCtrlType)
|
|
|
|
|
{
|
|
|
|
|
if (dwCtrlType == CTRL_CLOSE_EVENT || dwCtrlType == CTRL_BREAK_EVENT || dwCtrlType == CTRL_C_EVENT)
|
|
|
|
|
{
|
|
|
|
|
quit();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SteamTarget::onAboutToQuit()
|
|
|
|
|
{
|
2018-03-11 22:46:25 +00:00
|
|
|
|
if (hook_steam_ && !use_desktop_conf_)
|
|
|
|
|
Injector::unhookSteam();
|
|
|
|
|
|
2018-03-27 20:15:31 +00:00
|
|
|
|
controller_thread_->stop();
|
2018-03-10 22:02:39 +00:00
|
|
|
|
target_overlay_.stop();
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-27 20:15:31 +00:00
|
|
|
|
void SteamTarget::readIni()
|
2018-03-10 22:02:39 +00:00
|
|
|
|
{
|
|
|
|
|
if (arguments().size() == 1)
|
|
|
|
|
{
|
2018-03-27 21:01:36 +00:00
|
|
|
|
LOG_F(WARNING, "Target configuration file must be specified! Using default Values!");
|
2018-03-11 21:46:39 +00:00
|
|
|
|
QMessageBox::warning(nullptr, "GloSC", "Target configuration file must be specified! Using default Values!");
|
2018-03-10 22:02:39 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
QSettings settings(arguments().at(1), QSettings::IniFormat);
|
|
|
|
|
settings.beginGroup("BaseConf");
|
|
|
|
|
QStringList childKeys = settings.childKeys();
|
|
|
|
|
for (auto &childkey : childKeys)
|
|
|
|
|
{
|
|
|
|
|
if (childkey == "bEnableOverlay") {
|
|
|
|
|
enable_overlay_ = settings.value(childkey).toBool();
|
|
|
|
|
}
|
2018-03-18 15:31:36 +00:00
|
|
|
|
if (childkey == "bEnableOverlayOnlyConfig") {
|
|
|
|
|
enable_overlay_only_config_ = settings.value(childkey).toBool();
|
|
|
|
|
}
|
2018-03-10 22:02:39 +00:00
|
|
|
|
else if (childkey == "bEnableControllers") {
|
|
|
|
|
enable_controllers_ = settings.value(childkey).toBool();
|
|
|
|
|
}
|
|
|
|
|
else if (childkey == "bHookSteam") {
|
|
|
|
|
hook_steam_ = settings.value(childkey).toBool();
|
|
|
|
|
}
|
|
|
|
|
else if (childkey == "bUseDesktopConfig") {
|
|
|
|
|
use_desktop_conf_ = settings.value(childkey).toBool();
|
|
|
|
|
}
|
2018-03-27 20:15:31 +00:00
|
|
|
|
else if (childkey == "iUpdateRate") {
|
|
|
|
|
bool isInt = false;
|
|
|
|
|
update_rate_ = settings.value(childkey).toInt(&isInt);
|
|
|
|
|
if (!isInt)
|
|
|
|
|
update_rate_ = 5000;
|
|
|
|
|
if (update_rate_ < 0)
|
|
|
|
|
update_rate_ = 5000;
|
|
|
|
|
}
|
2018-03-10 22:02:39 +00:00
|
|
|
|
}
|
|
|
|
|
settings.endGroup();
|
|
|
|
|
settings.beginGroup("LaunchGame");
|
|
|
|
|
childKeys = settings.childKeys();
|
|
|
|
|
for (auto &childkey : childKeys)
|
|
|
|
|
{
|
|
|
|
|
if (childkey == "bLaunchGame") {
|
|
|
|
|
launch_game_ = settings.value(childkey).toBool();
|
|
|
|
|
}
|
|
|
|
|
else if (childkey == "Path") {
|
|
|
|
|
launch_app_path_ = settings.value(childkey).toString().toStdString();
|
|
|
|
|
}
|
|
|
|
|
else if (childkey == "Type") {
|
|
|
|
|
launch_uwp_ = settings.value(childkey).toString() == QString("UWP");
|
|
|
|
|
}
|
|
|
|
|
else if (childkey == "bCloseWhenDone") {
|
|
|
|
|
close_launched_done_ = settings.value(childkey).toBool();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
settings.endGroup();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-11 02:38:36 +00:00
|
|
|
|
void SteamTarget::initOverlayEvents()
|
|
|
|
|
{
|
|
|
|
|
//You hook into **MY** process? I'm ready to play your games, Valve! I'll hook back!!! 😅
|
2018-03-18 13:37:53 +00:00
|
|
|
|
const DWORD addressOpen = hook_commons::FindPattern(overlay_module_name,
|
|
|
|
|
overlay_open_func_sig,
|
|
|
|
|
overlay_open_func_mask);
|
2018-03-11 02:38:36 +00:00
|
|
|
|
|
|
|
|
|
if (addressOpen != 0)
|
|
|
|
|
{
|
|
|
|
|
DWORD addressClosed = 0;
|
|
|
|
|
|
|
|
|
|
for (DWORD i = 0; i < 1024; i++) //search next signature relativ to "addressOpened"
|
|
|
|
|
{
|
|
|
|
|
bool found = true;
|
2018-03-18 13:37:53 +00:00
|
|
|
|
for (DWORD j = 0; j < std::string(overlay_closed_func_mask).length(); j++)
|
2018-03-11 02:38:36 +00:00
|
|
|
|
found &=
|
2018-03-18 13:37:53 +00:00
|
|
|
|
overlay_closed_func_mask[j] == '?' ||
|
|
|
|
|
overlay_closed_func_sig[j] == *reinterpret_cast<char*>(addressOpen + j + i);
|
2018-03-11 02:38:36 +00:00
|
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
|
{
|
|
|
|
|
addressClosed = addressOpen + i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (addressClosed != 0)
|
|
|
|
|
{
|
2018-03-18 13:37:53 +00:00
|
|
|
|
overlay_hook::JMPBackOpen = addressOpen + std::string(overlay_open_func_mask).length();
|
|
|
|
|
overlay_hook::JMPBackClosed = addressClosed + std::string(overlay_closed_func_mask).length();
|
2018-03-11 02:38:36 +00:00
|
|
|
|
overlay_hook::target_overlay = &target_overlay_;
|
|
|
|
|
|
|
|
|
|
hook_commons::PlaceJMP(reinterpret_cast<BYTE*>(addressOpen),
|
2018-03-18 13:37:53 +00:00
|
|
|
|
reinterpret_cast<DWORD>(overlay_hook::overlay_opend_hookFN), std::string(overlay_open_func_mask).length());
|
2018-03-11 02:38:36 +00:00
|
|
|
|
|
|
|
|
|
hook_commons::PlaceJMP(reinterpret_cast<BYTE*>(addressClosed),
|
2018-03-18 13:37:53 +00:00
|
|
|
|
reinterpret_cast<DWORD>(overlay_hook::overlay_closed_hookFN), std::string(overlay_closed_func_mask).length());
|
2018-03-27 21:01:36 +00:00
|
|
|
|
} else {
|
|
|
|
|
LOG_F(WARNING, "Failed to find overlayClosed signature!");
|
2018-03-11 02:38:36 +00:00
|
|
|
|
}
|
2018-03-27 21:01:36 +00:00
|
|
|
|
} else {
|
|
|
|
|
LOG_F(WARNING, "Failed to find overlayOpened signature!");
|
2018-03-11 02:38:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-12 20:05:07 +00:00
|
|
|
|
void SteamTarget::launchWatchdog() const
|
2018-03-12 19:27:43 +00:00
|
|
|
|
{
|
2018-03-12 20:05:07 +00:00
|
|
|
|
const QString watchDogPath = QDir::toNativeSeparators(applicationDirPath()) + "\\GloSC_Watchdog.exe";
|
2018-03-27 21:01:36 +00:00
|
|
|
|
if(QProcess::startDetached("explorer.exe", QStringList() << watchDogPath))
|
|
|
|
|
LOG_F(INFO, "Launched Watchdog");
|
|
|
|
|
else
|
|
|
|
|
LOG_F(WARNING, "Failed to launch Watchdog!");
|
2018-03-12 19:27:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-12 21:45:14 +00:00
|
|
|
|
void SteamTarget::launchApplication()
|
|
|
|
|
{
|
|
|
|
|
if (!launch_uwp_)
|
|
|
|
|
{
|
|
|
|
|
// To get our launched application not get hooked by Steam, we have to launch through Windows explorer
|
|
|
|
|
// To use arguments, launching using explorer, we have to use a batch file...
|
|
|
|
|
const QString batchContents = '\"' + QDir::toNativeSeparators(QString::fromStdString(launch_app_path_))
|
|
|
|
|
+ '\"' + " " + QString::fromStdString(launch_app_args_);
|
|
|
|
|
|
|
|
|
|
QFile file("launchApp.bat");
|
|
|
|
|
if (file.open(QIODevice::ReadWrite | QIODevice::Text | QIODevice::Truncate)) {
|
|
|
|
|
QTextStream stream(&file);
|
|
|
|
|
stream << "@Echo off\n" << batchContents;
|
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
|
|
const QString launchPath = QDir::toNativeSeparators(applicationDirPath()) + "\\launchApp.bat";
|
|
|
|
|
QProcess::startDetached("explorer.exe", QStringList() << launchPath);
|
|
|
|
|
|
|
|
|
|
if (close_launched_done_)
|
|
|
|
|
{
|
|
|
|
|
const QString appName = QDir::toNativeSeparators(QString::fromStdString(launch_app_path_)).split('\\').last();
|
|
|
|
|
connect(&launch_check_timer_, &QTimer::timeout, [appName]()
|
|
|
|
|
{
|
2018-03-12 21:48:58 +00:00
|
|
|
|
if (!process_alive::IsProcessRunning(appName.toStdWString().c_str()))
|
2018-03-12 21:45:14 +00:00
|
|
|
|
SteamTarget::quit();
|
|
|
|
|
});
|
|
|
|
|
QTimer::singleShot(10000, this, [this]()
|
|
|
|
|
{
|
|
|
|
|
launch_check_timer_.start(1000);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// We don't need such bullshit explorer tricks when dealing with UWP, as Valve still hasn't figured out how to hook them.
|
|
|
|
|
// Or they just don't wan't to
|
|
|
|
|
// If you're interested in how to hook UWP: https://behind.flatspot.pictures/hacking-windows-universal-apps-uwp/
|
|
|
|
|
|
|
|
|
|
DWORD pid = 0;
|
|
|
|
|
const HRESULT hr = CoInitialize(nullptr);
|
|
|
|
|
std::wstring appUMId = QString::fromStdString(launch_app_path_).toStdWString();
|
|
|
|
|
if (SUCCEEDED(hr)) {
|
|
|
|
|
const HRESULT result = LaunchUWPApp(appUMId.c_str(), &pid);
|
|
|
|
|
if (SUCCEEDED(result))
|
|
|
|
|
{
|
|
|
|
|
if (close_launched_done_)
|
|
|
|
|
{
|
|
|
|
|
connect(&launch_check_timer_, &QTimer::timeout, [pid]()
|
|
|
|
|
{
|
2018-03-12 21:48:58 +00:00
|
|
|
|
if (!process_alive::IsProcessRunning(pid))
|
2018-03-12 21:45:14 +00:00
|
|
|
|
SteamTarget::quit();
|
|
|
|
|
});
|
|
|
|
|
QTimer::singleShot(10000, this, [this]()
|
|
|
|
|
{
|
|
|
|
|
launch_check_timer_.start(1000);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
CoUninitialize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HRESULT SteamTarget::LaunchUWPApp(LPCWSTR packageFullName, PDWORD pdwProcessId)
|
|
|
|
|
{
|
|
|
|
|
CComPtr<IApplicationActivationManager> spAppActivationManager;
|
|
|
|
|
HRESULT result = E_INVALIDARG;
|
|
|
|
|
// Initialize IApplicationActivationManager
|
|
|
|
|
result = CoCreateInstance(CLSID_ApplicationActivationManager, NULL, CLSCTX_LOCAL_SERVER, IID_IApplicationActivationManager, (LPVOID*)&spAppActivationManager);
|
|
|
|
|
|
|
|
|
|
if (!SUCCEEDED(result))
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
// This call ensures that the app is launched as the foreground window and sometimes may randomly fail...
|
|
|
|
|
result = CoAllowSetForegroundWindow(spAppActivationManager, NULL);
|
|
|
|
|
|
|
|
|
|
// Launch the app
|
|
|
|
|
result = spAppActivationManager->ActivateApplication(packageFullName, NULL, AO_NONE, pdwProcessId);
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-10 22:02:39 +00:00
|
|
|
|
|
2018-03-11 22:46:25 +00:00
|
|
|
|
|