mirror of https://github.com/Alia5/GlosSI
Cleanup: consolidate utils and common code
parent
b33b2a0691
commit
738803fa2e
@ -1,336 +0,0 @@
|
||||
/*
|
||||
Copyright 2021-2023 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.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <fstream>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <locale>
|
||||
#include <codecvt>
|
||||
|
||||
#ifdef WIN32
|
||||
#define NOMINMAX
|
||||
#include <Windows.h>
|
||||
#include <ShlObj.h>
|
||||
#include <KnownFolders.h>
|
||||
#endif
|
||||
|
||||
namespace Settings {
|
||||
|
||||
inline struct Launch {
|
||||
bool launch = false;
|
||||
std::wstring launchPath;
|
||||
std::wstring launchAppArgs;
|
||||
bool closeOnExit = true;
|
||||
bool waitForChildProcs = true;
|
||||
bool isUWP = false;
|
||||
bool ignoreLauncher = true;
|
||||
bool killLauncher = false;
|
||||
std::vector<std::wstring> launcherProcesses{};
|
||||
} launch;
|
||||
|
||||
inline struct Devices {
|
||||
bool hideDevices = true;
|
||||
bool realDeviceIds = false;
|
||||
} devices;
|
||||
|
||||
inline struct Window {
|
||||
bool windowMode = false;
|
||||
int maxFps = 0;
|
||||
float scale = 0.f;
|
||||
bool disableOverlay = false;
|
||||
bool hideAltTab = true;
|
||||
bool disableGlosSIOverlay = false;
|
||||
} window;
|
||||
|
||||
inline struct Controller {
|
||||
int maxControllers = 1;
|
||||
bool allowDesktopConfig = false;
|
||||
bool emulateDS4 = false;
|
||||
} controller;
|
||||
|
||||
inline struct Common {
|
||||
bool no_uwp_overlay = false;
|
||||
bool disable_watchdog = false;
|
||||
bool extendedLogging = false;
|
||||
std::wstring name;
|
||||
std::wstring icon;
|
||||
int version;
|
||||
std::wstring steamPath;
|
||||
std::wstring steamUserId;
|
||||
std::wstring standaloneModeGameId; /* = L"12605636929694728192"; */
|
||||
bool standaloneUseGamepadUI = false;
|
||||
} common;
|
||||
|
||||
inline std::filesystem::path settings_path_ = "";
|
||||
|
||||
inline bool checkIsUwp(const std::wstring& launch_path)
|
||||
{
|
||||
if (launch_path.find(L"://") != std::wstring::npos) {
|
||||
return false;
|
||||
}
|
||||
std::wsmatch m;
|
||||
if (!std::regex_search(launch_path, m, std::wregex(L"^.{1,5}:"))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef WIN32
|
||||
inline bool isWin10 = false;
|
||||
|
||||
typedef LONG NTSTATUS, *PNTSTATUS;
|
||||
#define STATUS_SUCCESS (0x00000000)
|
||||
|
||||
typedef NTSTATUS(WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
|
||||
|
||||
inline RTL_OSVERSIONINFOW GetRealOSVersion()
|
||||
{
|
||||
HMODULE hMod = ::GetModuleHandleW(L"ntdll.dll");
|
||||
if (hMod) {
|
||||
RtlGetVersionPtr fxPtr = (RtlGetVersionPtr)::GetProcAddress(hMod, "RtlGetVersion");
|
||||
if (fxPtr != nullptr) {
|
||||
RTL_OSVERSIONINFOW rovi = {0};
|
||||
rovi.dwOSVersionInfoSize = sizeof(rovi);
|
||||
if (STATUS_SUCCESS == fxPtr(&rovi)) {
|
||||
return rovi;
|
||||
}
|
||||
}
|
||||
}
|
||||
RTL_OSVERSIONINFOW rovi = {0};
|
||||
return rovi;
|
||||
}
|
||||
|
||||
inline void checkWinVer()
|
||||
{
|
||||
auto VN = GetRealOSVersion();
|
||||
isWin10 = VN.dwBuildNumber < 22000;
|
||||
|
||||
if (isWin10) {
|
||||
spdlog::info("Running on Windows 10; Winver: {}.{}.{}", VN.dwMajorVersion, VN.dwMinorVersion, VN.dwBuildNumber);
|
||||
}
|
||||
else {
|
||||
spdlog::info("Running on Windows 11; Winver: {}.{}.{}", VN.dwMajorVersion, VN.dwMinorVersion, VN.dwBuildNumber);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
inline void Parse(const nlohmann::basic_json<>& json)
|
||||
{
|
||||
auto safeParseValue = [](const auto& object, const auto& key, auto& value) {
|
||||
try {
|
||||
if (object.is_null() || object.empty() || object.at(key).empty() || object.at(key).is_null()) {
|
||||
return;
|
||||
}
|
||||
value = object[key];
|
||||
}
|
||||
catch (const nlohmann::json::exception& e) {
|
||||
e.id == 403
|
||||
? spdlog::trace("Err parsing \"{}\"; {}", key, e.what())
|
||||
: spdlog::warn("Err parsing \"{}\"; {}", key, e.what());
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
spdlog::warn("Err parsing \"{}\"; {}", key, e.what());
|
||||
}
|
||||
};
|
||||
|
||||
auto safeWStringParse = [&safeParseValue](const auto& object, const auto& key, std::wstring& value) {
|
||||
std::string meh;
|
||||
safeParseValue(object, key, meh);
|
||||
if (!meh.empty()) {
|
||||
// This assumes that char is utf8 and wchar_t is utf16, which is guaranteed on Windows.
|
||||
value = std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().from_bytes(meh);
|
||||
}
|
||||
};
|
||||
|
||||
int version;
|
||||
safeParseValue(json, "version", version);
|
||||
if (version != 1) { // TODO: versioning stuff
|
||||
spdlog::warn("Config version doesn't match application version.");
|
||||
}
|
||||
|
||||
// TODO: make this as much generic as fits in about the same amount of code if one would parse every value separately.
|
||||
try {
|
||||
if (auto launchconf = json["launch"]; !launchconf.is_null() && !launchconf.empty() && launchconf.is_object()) {
|
||||
safeParseValue(launchconf, "launch", launch.launch);
|
||||
safeWStringParse(launchconf, "launchPath", launch.launchPath);
|
||||
safeWStringParse(launchconf, "launchAppArgs", launch.launchAppArgs);
|
||||
safeParseValue(launchconf, "closeOnExit", launch.closeOnExit);
|
||||
safeParseValue(launchconf, "waitForChildProcs", launch.waitForChildProcs);
|
||||
safeParseValue(launchconf, "killLauncher", launch.killLauncher);
|
||||
safeParseValue(launchconf, "ignoreLauncher", launch.ignoreLauncher);
|
||||
|
||||
if (auto launcherProcs = launchconf["launcherProcesses"];
|
||||
!launcherProcs.is_null() && !launcherProcs.empty() && launcherProcs.is_array()) {
|
||||
launch.launcherProcesses.clear();
|
||||
launch.launcherProcesses.reserve(launcherProcs.size());
|
||||
for (auto& proc : launcherProcs) {
|
||||
launch.launcherProcesses.push_back(std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().from_bytes(proc));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (auto devconf = json["devices"]; !devconf.is_null() && !devconf.empty() && devconf.is_object()) {
|
||||
safeParseValue(devconf, "hideDevices", devices.hideDevices);
|
||||
safeParseValue(devconf, "realDeviceIds", devices.realDeviceIds);
|
||||
}
|
||||
|
||||
if (auto winconf = json["window"]; !winconf.is_null() && !winconf.empty() && winconf.is_object()) {
|
||||
safeParseValue(winconf, "windowMode", window.windowMode);
|
||||
safeParseValue(winconf, "maxFps", window.maxFps);
|
||||
safeParseValue(winconf, "scale", window.scale);
|
||||
safeParseValue(winconf, "disableOverlay", window.disableOverlay);
|
||||
safeParseValue(winconf, "hideAltTab", window.hideAltTab);
|
||||
safeParseValue(winconf, "disableGlosSIOverlay", window.disableGlosSIOverlay);
|
||||
}
|
||||
|
||||
if (auto controllerConf = json["controller"]; !controllerConf.is_null() && !controllerConf.empty() && controllerConf.is_object()) {
|
||||
safeParseValue(controllerConf, "maxControllers", controller.maxControllers);
|
||||
safeParseValue(controllerConf, "allowDesktopConfig", controller.allowDesktopConfig);
|
||||
safeParseValue(controllerConf, "emulateDS4", controller.emulateDS4);
|
||||
}
|
||||
safeParseValue(json, "extendedLogging", common.extendedLogging);
|
||||
safeWStringParse(json, "name", common.name);
|
||||
safeWStringParse(json, "icon", common.icon);
|
||||
safeParseValue(json, "version", common.version);
|
||||
|
||||
safeWStringParse(json, "steamPath", common.steamPath);
|
||||
safeWStringParse(json, "steamUserId", common.steamUserId);
|
||||
|
||||
safeWStringParse(json, "standaloneModeGameId", common.standaloneModeGameId);
|
||||
safeParseValue(json, "standaloneUseGamepadUI", common.standaloneUseGamepadUI);
|
||||
}
|
||||
catch (const nlohmann::json::exception& e) {
|
||||
spdlog::warn("Err parsing config: {}", e.what());
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
spdlog::warn("Err parsing config: {}", e.what());
|
||||
}
|
||||
if (launch.launch) {
|
||||
launch.isUWP = checkIsUwp(launch.launchPath);
|
||||
}
|
||||
}
|
||||
|
||||
inline void Parse(const std::vector<std::wstring>& args)
|
||||
{
|
||||
std::wstring configName;
|
||||
for (const auto& arg : args) {
|
||||
if (arg.empty()) {
|
||||
continue;
|
||||
}
|
||||
if (arg == L"-disableuwpoverlay") {
|
||||
common.no_uwp_overlay = true;
|
||||
}
|
||||
else if (arg == L"-disablewatchdog") {
|
||||
common.disable_watchdog = true;
|
||||
}
|
||||
else if (arg == L"-ignorelauncher") {
|
||||
launch.ignoreLauncher = true;
|
||||
}
|
||||
else if (arg == L"-window") {
|
||||
window.windowMode = true;
|
||||
}
|
||||
else {
|
||||
configName += L" " + std::wstring(arg.begin(), arg.end());
|
||||
}
|
||||
}
|
||||
if (!configName.empty()) {
|
||||
if (configName[0] == L' ') {
|
||||
configName.erase(configName.begin());
|
||||
}
|
||||
if (!configName.ends_with(L".json")) {
|
||||
configName += L".json";
|
||||
}
|
||||
}
|
||||
wchar_t* localAppDataFolder;
|
||||
std::filesystem::path path;
|
||||
if (SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, NULL, &localAppDataFolder) != S_OK) {
|
||||
path = std::filesystem::temp_directory_path().parent_path().parent_path().parent_path();
|
||||
}
|
||||
else {
|
||||
path = std::filesystem::path(localAppDataFolder).parent_path();
|
||||
}
|
||||
|
||||
path /= "Roaming";
|
||||
path /= "GlosSI";
|
||||
if (!configName.empty()) {
|
||||
path /= "Targets";
|
||||
path /= configName;
|
||||
}
|
||||
else {
|
||||
spdlog::info("No config file specified, using default");
|
||||
path /= "default.json";
|
||||
}
|
||||
|
||||
std::ifstream json_file;
|
||||
json_file.open(path);
|
||||
if (!json_file.is_open()) {
|
||||
spdlog::error(L"Couldn't open settings file {}", path.wstring());
|
||||
spdlog::debug(L"Using sane defaults...");
|
||||
return;
|
||||
}
|
||||
settings_path_ = path;
|
||||
const auto& json = nlohmann::json::parse(json_file);
|
||||
Parse(json);
|
||||
|
||||
spdlog::debug("Read config file \"{}\"; config: {}", path.string(), json.dump());
|
||||
json_file.close();
|
||||
}
|
||||
|
||||
inline nlohmann::json toJson()
|
||||
{
|
||||
nlohmann::json json;
|
||||
json["version"] = 1;
|
||||
json["launch"]["launch"] = launch.launch;
|
||||
json["launch"]["launchPath"] = std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(launch.launchPath);
|
||||
json["launch"]["launchAppArgs"] = std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(launch.launchAppArgs);
|
||||
json["launch"]["closeOnExit"] = launch.closeOnExit;
|
||||
json["launch"]["waitForChildProcs"] = launch.waitForChildProcs;
|
||||
json["devices"]["hideDevices"] = devices.hideDevices;
|
||||
json["devices"]["realDeviceIds"] = devices.realDeviceIds;
|
||||
json["window"]["windowMode"] = window.windowMode;
|
||||
json["window"]["maxFps"] = window.maxFps;
|
||||
json["window"]["scale"] = window.scale;
|
||||
json["window"]["disableOverlay"] = window.disableOverlay;
|
||||
json["window"]["hideAltTab"] = window.hideAltTab;
|
||||
json["controller"]["maxControllers"] = controller.maxControllers;
|
||||
json["controller"]["allowDesktopConfig"] = controller.allowDesktopConfig;
|
||||
json["controller"]["emulateDS4"] = controller.emulateDS4;
|
||||
|
||||
json["extendedLogging"] = common.extendedLogging;
|
||||
json["name"] = std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(common.name);
|
||||
json["icon"] = std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>().to_bytes(common.icon);
|
||||
json["version"] = common.version;
|
||||
return json;
|
||||
}
|
||||
|
||||
inline void StoreSettings()
|
||||
{
|
||||
const auto& json = toJson();
|
||||
|
||||
std::ofstream json_file;
|
||||
json_file.open(settings_path_);
|
||||
if (!json_file.is_open()) {
|
||||
spdlog::error(L"Couldn't open settings file {}", settings_path_.wstring());
|
||||
return;
|
||||
}
|
||||
json_file << json.dump(4);
|
||||
json_file.close();
|
||||
}
|
||||
|
||||
} // namespace Settings
|
@ -1,66 +0,0 @@
|
||||
/*
|
||||
Copyright 2021-2023 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.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#define NOMINMAX
|
||||
#include <map>
|
||||
#include <Windows.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace UnhookUtil {
|
||||
void UnPatchHook(const std::string& name, HMODULE module);
|
||||
|
||||
std::string ReadOriginalBytes(const std::string& name, const std::wstring& moduleName);
|
||||
|
||||
static inline const std::vector<uint8_t> JUMP_INSTR_OPCODES = {
|
||||
0xE9,
|
||||
0xE8,
|
||||
0xEB,
|
||||
0xEA,
|
||||
0xFF};
|
||||
|
||||
// Valve Hooks various functions and hides Gaming devices like this.
|
||||
// To be able to query them, unpatch the hook with the original bytes...
|
||||
|
||||
// Bytes here are just fallbacks; originalbytes will get read from GlosSIConfig and stored in %APPDATA%\GlosSI\unhook_bytes
|
||||
|
||||
// 22000 ^= Windows build number
|
||||
static inline const std::map<std::string, std::string> UNHOOK_BYTES_ORIGINAL_22000 = {
|
||||
{"SetupDiEnumDeviceInfo", "\x48\x89\x5C\x24\x08"},
|
||||
{"SetupDiGetClassDevsW", "\x48\x89\x5C\x24\x08"},
|
||||
{"HidD_GetPreparsedData", "\x48\x89\x5C\x24\x18"},
|
||||
{"HidP_GetCaps", "\x4C\x8B\xD1\x48\x85\xC9"},
|
||||
{"HidD_GetAttributes", "\x40\x53\x48\x83\xEC"},
|
||||
{"HidD_GetProductString", "\x48\x83\xEC\x48\x48"},
|
||||
{"HidP_GetUsages", "\x4C\x89\x4C\x24\x20"},
|
||||
{"HidP_GetData", "\x4C\x89\x44\x24\x18"},
|
||||
{"HidP_GetValueCaps", "\x48\x83\xEC\x48\x49"},
|
||||
{"HidP_GetUsageValue", "\x40\x53\x55\x56\x48"},
|
||||
{"HidP_GetButtonCaps", "\x48\x83\xEC\x48\x49"},
|
||||
// Valve hooks "CreateProcess" to detect child-processes
|
||||
{"CreateProcessW", "\x4C\x8B\xDC\x48\x83"},
|
||||
};
|
||||
|
||||
// SetupApi.dll is different on Win10 than on Win11
|
||||
static inline const std::map<std::string, std::string> UNHOOK_BYTES_ORIGINAL_WIN10 = {
|
||||
{"SetupDiEnumDeviceInfo", "\x40\x53\x56\x57\x41\x54\x41\x55"},
|
||||
{"SetupDiGetClassDevsW", "\x48\x8B\xC4\x48\x89\x58\x08"},
|
||||
};
|
||||
|
||||
|
||||
} // namespace UnhookUtil
|
@ -1,76 +0,0 @@
|
||||
/*
|
||||
Copyright 2021-2023 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.
|
||||
*/
|
||||
#pragma once
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#define NOMINMAX
|
||||
#include <Windows.h>
|
||||
#include <tlhelp32.h>
|
||||
|
||||
namespace glossi_util {
|
||||
|
||||
inline DWORD PidByName(const std::wstring& name)
|
||||
{
|
||||
PROCESSENTRY32 entry;
|
||||
entry.dwSize = sizeof(PROCESSENTRY32);
|
||||
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
|
||||
if (Process32First(snapshot, &entry) == TRUE) {
|
||||
while (Process32Next(snapshot, &entry) == TRUE) {
|
||||
if (std::wstring(entry.szExeFile).find(name) != std::string::npos) {
|
||||
return entry.th32ProcessID;
|
||||
}
|
||||
}
|
||||
}
|
||||
CloseHandle(snapshot);
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline std::wstring GetProcName(DWORD pid)
|
||||
{
|
||||
PROCESSENTRY32 processInfo;
|
||||
processInfo.dwSize = sizeof(processInfo);
|
||||
const HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
|
||||
if (processesSnapshot == INVALID_HANDLE_VALUE) {
|
||||
spdlog::trace("util::GetProcName: can't get a process snapshot");
|
||||
return L"";
|
||||
}
|
||||
|
||||
for (BOOL bok = Process32First(processesSnapshot, &processInfo);
|
||||
bok;
|
||||
bok = Process32Next(processesSnapshot, &processInfo)) {
|
||||
if (pid == processInfo.th32ProcessID) {
|
||||
CloseHandle(processesSnapshot);
|
||||
return processInfo.szExeFile;
|
||||
}
|
||||
}
|
||||
CloseHandle(processesSnapshot);
|
||||
return L"";
|
||||
}
|
||||
|
||||
inline bool KillProcess(DWORD pid)
|
||||
{
|
||||
auto res = true;
|
||||
if (const auto proc = OpenProcess(PROCESS_TERMINATE, FALSE, pid)) {
|
||||
spdlog::debug("Terminating process: {}", pid);
|
||||
res = TerminateProcess(proc, 0);
|
||||
if (!res) {
|
||||
spdlog::error("Failed to terminate process: {}", pid);
|
||||
}
|
||||
CloseHandle(proc);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
} // namespace glossi_util
|
@ -0,0 +1,300 @@
|
||||
/*
|
||||
Copyright 2021-2023 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.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <fstream>
|
||||
#include <regex>
|
||||
#include <string>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#ifdef WIN32
|
||||
#define NOMINMAX
|
||||
#include <Windows.h>
|
||||
#endif
|
||||
|
||||
#include "../common/nlohmann_json_wstring.h"
|
||||
#include "../common/util.h"
|
||||
|
||||
|
||||
namespace Settings {
|
||||
|
||||
inline struct Launch {
|
||||
bool launch = false;
|
||||
std::wstring launchPath;
|
||||
std::wstring launchAppArgs;
|
||||
bool closeOnExit = true;
|
||||
bool waitForChildProcs = true;
|
||||
bool isUWP = false;
|
||||
bool ignoreLauncher = true;
|
||||
bool killLauncher = false;
|
||||
std::vector<std::wstring> launcherProcesses{};
|
||||
} launch;
|
||||
|
||||
inline struct Devices {
|
||||
bool hideDevices = true;
|
||||
bool realDeviceIds = false;
|
||||
} devices;
|
||||
|
||||
inline struct Window {
|
||||
bool windowMode = false;
|
||||
int maxFps = 0;
|
||||
float scale = 0.f;
|
||||
bool disableOverlay = false;
|
||||
bool hideAltTab = true;
|
||||
bool disableGlosSIOverlay = false;
|
||||
} window;
|
||||
|
||||
inline struct Controller {
|
||||
int maxControllers = 1;
|
||||
bool allowDesktopConfig = false;
|
||||
bool emulateDS4 = false;
|
||||
} controller;
|
||||
|
||||
inline struct Common {
|
||||
bool no_uwp_overlay = false;
|
||||
bool disable_watchdog = false;
|
||||
bool extendedLogging = false;
|
||||
std::wstring name;
|
||||
std::wstring icon;
|
||||
int version;
|
||||
std::wstring steamPath;
|
||||
std::wstring steamUserId;
|
||||
std::wstring standaloneModeGameId; /* = L"12605636929694728192"; */
|
||||
bool standaloneUseGamepadUI = false;
|
||||
} common;
|
||||
|
||||
inline std::filesystem::path settings_path_ = "";
|
||||
|
||||
inline bool checkIsUwp(const std::wstring& launch_path)
|
||||
{
|
||||
if (launch_path.find(L"://") != std::wstring::npos) {
|
||||
return false;
|
||||
}
|
||||
std::wsmatch m;
|
||||
if (!std::regex_search(launch_path, m, std::wregex(L"^.{1,5}:"))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef WIN32
|
||||
inline bool isWin10 = false;
|
||||
|
||||
inline void checkWinVer()
|
||||
{
|
||||
auto VN = util::win::GetRealOSVersion();
|
||||
isWin10 = VN.dwBuildNumber < 22000;
|
||||
|
||||
if (isWin10) {
|
||||
spdlog::info("Running on Windows 10; Winver: {}.{}.{}", VN.dwMajorVersion, VN.dwMinorVersion, VN.dwBuildNumber);
|
||||
}
|
||||
else {
|
||||
spdlog::info("Running on Windows 11; Winver: {}.{}.{}", VN.dwMajorVersion, VN.dwMinorVersion, VN.dwBuildNumber);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
inline void Parse(const nlohmann::basic_json<>& json)
|
||||
{
|
||||
constexpr auto safeParseValue = []<typename T>(const auto & object, const auto & key, T & value) {
|
||||
try {
|
||||
if (object.is_null() || object.empty() || object.at(key).empty() || object.at(key).is_null()) {
|
||||
return;
|
||||
}
|
||||
if constexpr (std::is_same_v<T, std::wstring>) {
|
||||
value = util::string::to_wstring(object[key].get<std::string>());
|
||||
}
|
||||
else {
|
||||
value = object[key];
|
||||
}
|
||||
}
|
||||
catch (const nlohmann::json::exception& e) {
|
||||
e.id == 403
|
||||
? spdlog::trace("Err parsing \"{}\"; {}", key, e.what())
|
||||
: spdlog::warn("Err parsing \"{}\"; {}", key, e.what());
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
spdlog::warn("Err parsing \"{}\"; {}", key, e.what());
|
||||
}
|
||||
};
|
||||
|
||||
int version;
|
||||
safeParseValue(json, "version", version);
|
||||
if (version != 1) { // TODO: versioning stuff
|
||||
spdlog::warn("Config version doesn't match application version.");
|
||||
}
|
||||
|
||||
// TODO: make this as much generic as fits in about the same amount of code if one would parse every value separately.
|
||||
try {
|
||||
if (auto launchconf = json["launch"]; !launchconf.is_null() && !launchconf.empty() && launchconf.is_object()) {
|
||||
safeParseValue(launchconf, "launch", launch.launch);
|
||||
safeParseValue(launchconf, "launchPath", launch.launchPath);
|
||||
safeParseValue(launchconf, "launchAppArgs", launch.launchAppArgs);
|
||||
safeParseValue(launchconf, "closeOnExit", launch.closeOnExit);
|
||||
safeParseValue(launchconf, "waitForChildProcs", launch.waitForChildProcs);
|
||||
safeParseValue(launchconf, "killLauncher", launch.killLauncher);
|
||||
safeParseValue(launchconf, "ignoreLauncher", launch.ignoreLauncher);
|
||||
|
||||
if (auto launcherProcs = launchconf["launcherProcesses"];
|
||||
!launcherProcs.is_null() && !launcherProcs.empty() && launcherProcs.is_array()) {
|
||||
launch.launcherProcesses.clear();
|
||||
launch.launcherProcesses.reserve(launcherProcs.size());
|
||||
for (auto& proc : launcherProcs) {
|
||||
launch.launcherProcesses.push_back(util::string::to_wstring(proc));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (auto devconf = json["devices"]; !devconf.is_null() && !devconf.empty() && devconf.is_object()) {
|
||||
safeParseValue(devconf, "hideDevices", devices.hideDevices);
|
||||
safeParseValue(devconf, "realDeviceIds", devices.realDeviceIds);
|
||||
}
|
||||
|
||||
if (auto winconf = json["window"]; !winconf.is_null() && !winconf.empty() && winconf.is_object()) {
|
||||
safeParseValue(winconf, "windowMode", window.windowMode);
|
||||
safeParseValue(winconf, "maxFps", window.maxFps);
|
||||
safeParseValue(winconf, "scale", window.scale);
|
||||
safeParseValue(winconf, "disableOverlay", window.disableOverlay);
|
||||
safeParseValue(winconf, "hideAltTab", window.hideAltTab);
|
||||
safeParseValue(winconf, "disableGlosSIOverlay", window.disableGlosSIOverlay);
|
||||
}
|
||||
|
||||
if (auto controllerConf = json["controller"]; !controllerConf.is_null() && !controllerConf.empty() && controllerConf.is_object()) {
|
||||
safeParseValue(controllerConf, "maxControllers", controller.maxControllers);
|
||||
safeParseValue(controllerConf, "allowDesktopConfig", controller.allowDesktopConfig);
|
||||
safeParseValue(controllerConf, "emulateDS4", controller.emulateDS4);
|
||||
}
|
||||
safeParseValue(json, "extendedLogging", common.extendedLogging);
|
||||
safeParseValue(json, "name", common.name);
|
||||
safeParseValue(json, "icon", common.icon);
|
||||
safeParseValue(json, "version", common.version);
|
||||
|
||||
safeParseValue(json, "steamPath", common.steamPath);
|
||||
safeParseValue(json, "steamUserId", common.steamUserId);
|
||||
|
||||
safeParseValue(json, "standaloneModeGameId", common.standaloneModeGameId);
|
||||
safeParseValue(json, "standaloneUseGamepadUI", common.standaloneUseGamepadUI);
|
||||
}
|
||||
catch (const nlohmann::json::exception& e) {
|
||||
spdlog::warn("Err parsing config: {}", e.what());
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
spdlog::warn("Err parsing config: {}", e.what());
|
||||
}
|
||||
if (launch.launch) {
|
||||
launch.isUWP = checkIsUwp(launch.launchPath);
|
||||
}
|
||||
}
|
||||
|
||||
inline void Parse(const std::vector<std::wstring>& args)
|
||||
{
|
||||
std::wstring configName;
|
||||
for (const auto& arg : args) {
|
||||
if (arg.empty()) {
|
||||
continue;
|
||||
}
|
||||
if (arg == L"-disableuwpoverlay") {
|
||||
common.no_uwp_overlay = true;
|
||||
}
|
||||
else if (arg == L"-disablewatchdog") {
|
||||
common.disable_watchdog = true;
|
||||
}
|
||||
else if (arg == L"-ignorelauncher") {
|
||||
launch.ignoreLauncher = true;
|
||||
}
|
||||
else if (arg == L"-window") {
|
||||
window.windowMode = true;
|
||||
}
|
||||
else {
|
||||
configName += L" " + std::wstring(arg.begin(), arg.end());
|
||||
}
|
||||
}
|
||||
if (!configName.empty()) {
|
||||
if (configName[0] == L' ') {
|
||||
configName.erase(configName.begin());
|
||||
}
|
||||
if (!configName.ends_with(L".json")) {
|
||||
configName += L".json";
|
||||
}
|
||||
}
|
||||
auto path = util::path::getDataDirPath();
|
||||
if (!configName.empty()) {
|
||||
path /= "Targets";
|
||||
path /= configName;
|
||||
}
|
||||
else {
|
||||
spdlog::info("No config file specified, using default");
|
||||
path /= "default.json";
|
||||
}
|
||||
|
||||
std::ifstream json_file;
|
||||
json_file.open(path);
|
||||
if (!json_file.is_open()) {
|
||||
spdlog::error(L"Couldn't open settings file {}", path.wstring());
|
||||
spdlog::debug(L"Using sane defaults...");
|
||||
return;
|
||||
}
|
||||
settings_path_ = path;
|
||||
const auto& json = nlohmann::json::parse(json_file);
|
||||
Parse(json);
|
||||
|
||||
spdlog::debug("Read config file \"{}\"; config: {}", path.string(), json.dump());
|
||||
json_file.close();
|
||||
}
|
||||
|
||||
inline nlohmann::json toJson()
|
||||
{
|
||||
nlohmann::json json;
|
||||
json["version"] = 1;
|
||||
json["launch"]["launch"] = launch.launch;
|
||||
json["launch"]["launchPath"] = launch.launchPath;
|
||||
json["launch"]["launchAppArgs"] = launch.launchAppArgs;
|
||||
json["launch"]["closeOnExit"] = launch.closeOnExit;
|
||||
json["launch"]["waitForChildProcs"] = launch.waitForChildProcs;
|
||||
json["devices"]["hideDevices"] = devices.hideDevices;
|
||||
json["devices"]["realDeviceIds"] = devices.realDeviceIds;
|
||||
json["window"]["windowMode"] = window.windowMode;
|
||||
json["window"]["maxFps"] = window.maxFps;
|
||||
json["window"]["scale"] = window.scale;
|
||||
json["window"]["disableOverlay"] = window.disableOverlay;
|
||||
json["window"]["hideAltTab"] = window.hideAltTab;
|
||||
json["controller"]["maxControllers"] = controller.maxControllers;
|
||||
json["controller"]["allowDesktopConfig"] = controller.allowDesktopConfig;
|
||||
json["controller"]["emulateDS4"] = controller.emulateDS4;
|
||||
|
||||
json["extendedLogging"] = common.extendedLogging;
|
||||
json["name"] = common.name;
|
||||
json["icon"] = common.icon;
|
||||
json["version"] = common.version;
|
||||
return json;
|
||||
}
|
||||
|
||||
inline void StoreSettings()
|
||||
{
|
||||
const auto& json = toJson();
|
||||
|
||||
std::ofstream json_file;
|
||||
json_file.open(settings_path_);
|
||||
if (!json_file.is_open()) {
|
||||
spdlog::error(L"Couldn't open settings file {}", settings_path_.wstring());
|
||||
return;
|
||||
}
|
||||
json_file << json.dump(4);
|
||||
json_file.close();
|
||||
}
|
||||
|
||||
} // namespace Settings
|
@ -0,0 +1,66 @@
|
||||
/*
|
||||
Copyright 2021-2023 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.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#define NOMINMAX
|
||||
#include <map>
|
||||
#include <Windows.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace UnhookUtil {
|
||||
void UnPatchHook(const std::string& name, HMODULE module);
|
||||
|
||||
std::string ReadOriginalBytes(const std::string& name, const std::wstring& moduleName);
|
||||
|
||||
static inline const std::vector<uint8_t> JUMP_INSTR_OPCODES = {
|
||||
0xE9,
|
||||
0xE8,
|
||||
0xEB,
|
||||
0xEA,
|
||||
0xFF };
|
||||
|
||||
// Valve Hooks various functions and hides Gaming devices like this.
|
||||
// To be able to query them, unpatch the hook with the original bytes...
|
||||
|
||||
// Bytes here are just fallbacks; originalbytes will get read from GlosSIConfig and stored in %APPDATA%\GlosSI\unhook_bytes
|
||||
|
||||
// 22000 ^= Windows build number
|
||||
static inline const std::map<std::string, std::string> UNHOOK_BYTES_ORIGINAL_22000 = {
|
||||
{"SetupDiEnumDeviceInfo", "\x48\x89\x5C\x24\x08"},
|
||||
{"SetupDiGetClassDevsW", "\x48\x89\x5C\x24\x08"},
|
||||
{"HidD_GetPreparsedData", "\x48\x89\x5C\x24\x18"},
|
||||
{"HidP_GetCaps", "\x4C\x8B\xD1\x48\x85\xC9"},
|
||||
{"HidD_GetAttributes", "\x40\x53\x48\x83\xEC"},
|
||||
{"HidD_GetProductString", "\x48\x83\xEC\x48\x48"},
|
||||
{"HidP_GetUsages", "\x4C\x89\x4C\x24\x20"},
|
||||
{"HidP_GetData", "\x4C\x89\x44\x24\x18"},
|
||||
{"HidP_GetValueCaps", "\x48\x83\xEC\x48\x49"},
|
||||
{"HidP_GetUsageValue", "\x40\x53\x55\x56\x48"},
|
||||
{"HidP_GetButtonCaps", "\x48\x83\xEC\x48\x49"},
|
||||
// Valve hooks "CreateProcess" to detect child-processes
|
||||
{"CreateProcessW", "\x4C\x8B\xDC\x48\x83"},
|
||||
};
|
||||
|
||||
// SetupApi.dll is different on Win10 than on Win11
|
||||
static inline const std::map<std::string, std::string> UNHOOK_BYTES_ORIGINAL_WIN10 = {
|
||||
{"SetupDiEnumDeviceInfo", "\x40\x53\x56\x57\x41\x54\x41\x55"},
|
||||
{"SetupDiGetClassDevsW", "\x48\x8B\xC4\x48\x89\x58\x08"},
|
||||
};
|
||||
|
||||
|
||||
} // namespace UnhookUtil
|
@ -0,0 +1,151 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>16.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{dfed4b7e-d04c-442b-bb48-5b6068a6b31b}</ProjectGuid>
|
||||
<RootNamespace>common</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Utility</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Utility</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<IncludePath>..\deps\json\include;..\deps\spdlog\include;..\deps\SFML\include;..\deps\imgui;$(IncludePath)</IncludePath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<IncludePath>..\deps\json\include;..\deps\spdlog\include;..\deps\SFML\include;..\deps\imgui;$(IncludePath)</IncludePath>
|
||||
</PropertyGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<LanguageStandard>stdcpp20</LanguageStandard>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="HidHide.h" />
|
||||
<ClInclude Include="nlohmann_json_wstring.h" />
|
||||
<ClInclude Include="Settings.h" />
|
||||
<ClInclude Include="UnhookUtil.h" />
|
||||
<ClInclude Include="util.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="HidHide.cpp" />
|
||||
<ClCompile Include="UnhookUtil.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
@ -0,0 +1,42 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="Source Files">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Header Files">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Resource Files">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="nlohmann_json_wstring.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="util.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="UnhookUtil.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Settings.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="HidHide.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="UnhookUtil.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="HidHide.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
@ -0,0 +1,36 @@
|
||||
/*
|
||||
Copyright 2021-2023 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
|
||||
namespace nlohmann {
|
||||
template <>
|
||||
struct adl_serializer<std::wstring> {
|
||||
static void to_json(json& j, const std::wstring& str) {
|
||||
j = util::string::to_string(str);
|
||||
}
|
||||
|
||||
static void from_json(const json& j, std::wstring& str) {
|
||||
str = util::string::to_wstring(j.get<std::string>());
|
||||
}
|
||||
};
|
||||
}
|
@ -0,0 +1,163 @@
|
||||
/*
|
||||
Copyright 2021-2023 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <locale>
|
||||
#include <codecvt>
|
||||
|
||||
#ifdef _WIN32
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#define NOMINMAX
|
||||
#include <Windows.h>
|
||||
#include <tlhelp32.h>
|
||||
#include <ShlObj.h>
|
||||
#include <KnownFolders.h>
|
||||
#endif
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
#ifdef SPDLOG_H
|
||||
#include <spdlog/spdlog.h>
|
||||
#endif
|
||||
|
||||
namespace util {
|
||||
namespace string
|
||||
{
|
||||
template <typename T>
|
||||
inline std::wstring to_wstring(const T& t)
|
||||
{
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
|
||||
return converter.from_bytes(t);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline std::string to_string(const T& t)
|
||||
{
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
|
||||
return converter.to_bytes(t);
|
||||
}
|
||||
}
|
||||
|
||||
namespace path
|
||||
{
|
||||
inline std::filesystem::path getDataDirPath()
|
||||
{
|
||||
wchar_t* localAppDataFolder;
|
||||
std::filesystem::path path;
|
||||
if (SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, nullptr, &localAppDataFolder) != S_OK) {
|
||||
path = std::filesystem::temp_directory_path().parent_path().parent_path().parent_path();
|
||||
}
|
||||
else {
|
||||
path = std::filesystem::path(localAppDataFolder).parent_path();
|
||||
}
|
||||
|
||||
path /= "Roaming";
|
||||
path /= "GlosSI";
|
||||
if (!std::filesystem::exists(path))
|
||||
std::filesystem::create_directories(path);
|
||||
return path;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
namespace win
|
||||
{
|
||||
|
||||
typedef LONG NTSTATUS, * PNTSTATUS;
|
||||
#define STATUS_SUCCESS (0x00000000)
|
||||
|
||||
typedef NTSTATUS(WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
|
||||
|
||||
inline RTL_OSVERSIONINFOW GetRealOSVersion()
|
||||
{
|
||||
HMODULE hMod = ::GetModuleHandleW(L"ntdll.dll");
|
||||
if (hMod) {
|
||||
RtlGetVersionPtr fxPtr = (RtlGetVersionPtr)::GetProcAddress(hMod, "RtlGetVersion");
|
||||
if (fxPtr != nullptr) {
|
||||
RTL_OSVERSIONINFOW rovi = { 0 };
|
||||
rovi.dwOSVersionInfoSize = sizeof(rovi);
|
||||
if (STATUS_SUCCESS == fxPtr(&rovi)) {
|
||||
return rovi;
|
||||
}
|
||||
}
|
||||
}
|
||||
RTL_OSVERSIONINFOW rovi = { 0 };
|
||||
return rovi;
|
||||
}
|
||||
namespace process
|
||||
{
|
||||
inline DWORD PidByName(const std::wstring& name)
|
||||
{
|
||||
PROCESSENTRY32 entry;
|
||||
entry.dwSize = sizeof(PROCESSENTRY32);
|
||||
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
|
||||
if (Process32First(snapshot, &entry) == TRUE) {
|
||||
while (Process32Next(snapshot, &entry) == TRUE) {
|
||||
if (std::wstring(entry.szExeFile).find(name) != std::string::npos) {
|
||||
return entry.th32ProcessID;
|
||||
}
|
||||
}
|
||||
}
|
||||
CloseHandle(snapshot);
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline std::wstring GetProcName(DWORD pid)
|
||||
{
|
||||
PROCESSENTRY32 processInfo;
|
||||
processInfo.dwSize = sizeof(processInfo);
|
||||
const HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
|
||||
if (processesSnapshot == INVALID_HANDLE_VALUE) {
|
||||
#ifdef SPDLOG_H
|
||||
spdlog::trace("util::GetProcName: can't get a process snapshot");
|
||||
#endif
|
||||
return L"";
|
||||
}
|
||||
|
||||
for (BOOL bok = Process32First(processesSnapshot, &processInfo);
|
||||
bok;
|
||||
bok = Process32Next(processesSnapshot, &processInfo)) {
|
||||
if (pid == processInfo.th32ProcessID) {
|
||||
CloseHandle(processesSnapshot);
|
||||
return processInfo.szExeFile;
|
||||
}
|
||||
}
|
||||
CloseHandle(processesSnapshot);
|
||||
return L"";
|
||||
}
|
||||
|
||||
inline bool KillProcess(DWORD pid)
|
||||
{
|
||||
auto res = true;
|
||||
if (const auto proc = OpenProcess(PROCESS_TERMINATE, FALSE, pid)) {
|
||||
#ifdef SPDLOG_H
|
||||
spdlog::debug("Terminating process: {}", pid);
|
||||
#endif
|
||||
res = TerminateProcess(proc, 0);
|
||||
if (!res) {
|
||||
#ifdef SPDLOG_H
|
||||
spdlog::error("Failed to terminate process: {}", pid);
|
||||
#endif
|
||||
}
|
||||
CloseHandle(proc);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
Loading…
Reference in New Issue