2022-02-27 07:53:44 -05:00
|
|
|
#include <STDInclude.hpp>
|
2017-01-19 16:23:59 -05:00
|
|
|
|
|
|
|
namespace Utils
|
|
|
|
{
|
2018-12-17 08:29:18 -05:00
|
|
|
std::string GetMimeType(const std::string& url)
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
|
|
|
wchar_t* mimeType = nullptr;
|
2017-01-20 16:41:03 -05:00
|
|
|
FindMimeFromData(nullptr, std::wstring(url.begin(), url.end()).data(), nullptr, 0, nullptr, 0, &mimeType, 0);
|
2017-01-19 16:23:59 -05:00
|
|
|
|
|
|
|
if (mimeType)
|
|
|
|
{
|
|
|
|
std::wstring wMimeType(mimeType);
|
2022-02-27 11:49:12 -05:00
|
|
|
return String::Convert(wMimeType);
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return "application/octet-stream";
|
|
|
|
}
|
|
|
|
|
2018-12-17 08:29:18 -05:00
|
|
|
std::string ParseChallenge(const std::string& data)
|
2017-01-19 16:23:59 -05:00
|
|
|
{
|
2022-08-24 08:15:04 -04:00
|
|
|
const auto pos = data.find_first_of("\n ");
|
2017-01-19 16:23:59 -05:00
|
|
|
if (pos == std::string::npos) return data;
|
2022-08-24 08:15:04 -04:00
|
|
|
return data.substr(0, pos);
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void OutputDebugLastError()
|
|
|
|
{
|
|
|
|
DWORD errorMessageID = ::GetLastError();
|
2017-02-10 03:50:08 -05:00
|
|
|
OutputDebugStringA(Utils::String::VA("Last error code: 0x%08X (%s)\n", errorMessageID, GetLastWindowsError().data()));
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|
|
|
|
|
2017-02-09 16:16:49 -05:00
|
|
|
std::string GetLastWindowsError()
|
|
|
|
{
|
|
|
|
DWORD errorMessageID = ::GetLastError();
|
|
|
|
LPSTR messageBuffer = nullptr;
|
|
|
|
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
|
|
nullptr, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPSTR>(&messageBuffer), 0, nullptr);
|
|
|
|
std::string message(messageBuffer, size);
|
|
|
|
LocalFree(messageBuffer);
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
2017-01-19 16:23:59 -05:00
|
|
|
bool IsWineEnvironment()
|
|
|
|
{
|
|
|
|
HMODULE hntdll = GetModuleHandleA("ntdll.dll");
|
|
|
|
if (!hntdll) return false;
|
|
|
|
|
|
|
|
return (GetProcAddress(hntdll, "wine_get_version") != nullptr);
|
|
|
|
}
|
2017-03-04 07:51:41 -05:00
|
|
|
|
|
|
|
unsigned long GetParentProcessId()
|
|
|
|
{
|
|
|
|
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
|
|
|
if (hSnapshot == INVALID_HANDLE_VALUE) return 0;
|
|
|
|
|
2022-08-26 14:12:26 -04:00
|
|
|
Memory::Allocator allocator;
|
2017-03-04 07:51:41 -05:00
|
|
|
allocator.reference(hSnapshot, [](void* handle) { CloseHandle(handle); });
|
|
|
|
|
|
|
|
PROCESSENTRY32 pe32;
|
|
|
|
ZeroMemory(&pe32, sizeof(pe32));
|
|
|
|
pe32.dwSize = sizeof(pe32);
|
|
|
|
|
|
|
|
DWORD pid = GetCurrentProcessId();
|
|
|
|
while (Process32Next(hSnapshot, &pe32))
|
|
|
|
{
|
|
|
|
if (pe32.th32ProcessID == pid)
|
|
|
|
{
|
|
|
|
return pe32.th32ParentProcessID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2017-03-26 14:41:37 -04:00
|
|
|
|
|
|
|
size_t GetModuleSize(HMODULE module)
|
|
|
|
{
|
|
|
|
PIMAGE_DOS_HEADER header = PIMAGE_DOS_HEADER(module);
|
|
|
|
PIMAGE_NT_HEADERS ntHeader = PIMAGE_NT_HEADERS(DWORD(module) + header->e_lfanew);
|
|
|
|
return ntHeader->OptionalHeader.SizeOfImage;
|
|
|
|
}
|
|
|
|
|
2017-06-10 14:24:42 -04:00
|
|
|
void* GetThreadStartAddress(HANDLE hThread)
|
|
|
|
{
|
2022-08-26 14:12:26 -04:00
|
|
|
HMODULE ntdll = GetNTDLL();
|
2017-06-10 14:24:42 -04:00
|
|
|
if (!ntdll) return nullptr;
|
|
|
|
|
|
|
|
|
|
|
|
static uint8_t ntQueryInformationThread[] = { 0xB1, 0x8B, 0xAE, 0x8A, 0x9A, 0x8D, 0x86, 0xB6, 0x91, 0x99, 0x90, 0x8D, 0x92, 0x9E, 0x8B, 0x96, 0x90, 0x91, 0xAB, 0x97, 0x8D, 0x9A, 0x9E, 0x9B }; // NtQueryInformationThread
|
2022-08-26 14:12:26 -04:00
|
|
|
NtQueryInformationThread_t NtQueryInformationThread = NtQueryInformationThread_t(GetProcAddress(ntdll, String::XOR(std::string(reinterpret_cast<char*>(ntQueryInformationThread), sizeof ntQueryInformationThread), -1).data()));
|
2017-06-10 14:24:42 -04:00
|
|
|
if (!NtQueryInformationThread) return nullptr;
|
|
|
|
|
|
|
|
HANDLE dupHandle, currentProcess = GetCurrentProcess();
|
|
|
|
if (!DuplicateHandle(currentProcess, hThread, currentProcess, &dupHandle, THREAD_QUERY_INFORMATION, FALSE, 0))
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_ACCESS_DENIED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* address = nullptr;
|
|
|
|
NTSTATUS status = NtQueryInformationThread(dupHandle, ThreadQuerySetWin32StartAddress, &address, sizeof(address), nullptr);
|
|
|
|
CloseHandle(dupHandle);
|
|
|
|
|
|
|
|
if (status != 0) return nullptr;
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:06:29 -04:00
|
|
|
void SetLegacyEnvironment()
|
|
|
|
{
|
2022-11-07 06:13:13 -05:00
|
|
|
wchar_t binaryPath[512]{};
|
2022-09-06 08:06:29 -04:00
|
|
|
GetModuleFileNameW(GetModuleHandleW(nullptr), binaryPath, sizeof(binaryPath) / sizeof(wchar_t));
|
|
|
|
|
|
|
|
auto* exeBaseName = std::wcsrchr(binaryPath, L'\\');
|
|
|
|
exeBaseName[0] = L'\0';
|
|
|
|
|
|
|
|
// Make the game work without the xlabs launcher
|
|
|
|
SetCurrentDirectoryW(binaryPath);
|
|
|
|
}
|
|
|
|
|
2017-06-30 04:37:29 -04:00
|
|
|
void SetEnvironment()
|
|
|
|
{
|
2022-08-26 14:12:26 -04:00
|
|
|
wchar_t* buffer{};
|
2022-08-17 15:53:52 -04:00
|
|
|
std::size_t size{};
|
2022-08-26 14:12:26 -04:00
|
|
|
if (_wdupenv_s(&buffer, &size, L"XLABS_MW2_INSTALL") != 0 || buffer == nullptr)
|
2022-08-17 15:53:52 -04:00
|
|
|
{
|
2022-09-06 08:06:29 -04:00
|
|
|
SetLegacyEnvironment();
|
2022-08-17 15:53:52 -04:00
|
|
|
return;
|
|
|
|
}
|
2017-06-30 04:37:29 -04:00
|
|
|
|
2022-08-17 15:53:52 -04:00
|
|
|
const auto _0 = gsl::finally([&] { std::free(buffer); });
|
2017-06-30 04:37:29 -04:00
|
|
|
|
2022-11-07 06:13:13 -05:00
|
|
|
SetCurrentDirectoryW(buffer);
|
|
|
|
SetDllDirectoryW(buffer);
|
2017-06-30 04:37:29 -04:00
|
|
|
}
|
|
|
|
|
2017-06-10 14:24:42 -04:00
|
|
|
HMODULE GetNTDLL()
|
|
|
|
{
|
|
|
|
static uint8_t ntdll[] = { 0x91, 0x8B, 0x9B, 0x93, 0x93, 0xD1, 0x9B, 0x93, 0x93 }; // ntdll.dll
|
|
|
|
return GetModuleHandleA(Utils::String::XOR(std::string(reinterpret_cast<char*>(ntdll), sizeof ntdll), -1).data());
|
|
|
|
}
|
|
|
|
|
2017-07-12 07:36:27 -04:00
|
|
|
void SafeShellExecute(HWND hwnd, LPCSTR lpOperation, LPCSTR lpFile, LPCSTR lpParameters, LPCSTR lpDirectory, INT nShowCmd)
|
2017-06-11 15:25:00 -04:00
|
|
|
{
|
2022-07-21 15:30:44 -04:00
|
|
|
[=]
|
2017-06-11 15:25:00 -04:00
|
|
|
{
|
2018-05-10 11:24:03 -04:00
|
|
|
__try
|
|
|
|
{
|
|
|
|
ShellExecuteA(hwnd, lpOperation, lpFile, lpParameters, lpDirectory, nShowCmd);
|
|
|
|
}
|
|
|
|
__finally
|
|
|
|
{}
|
|
|
|
}();
|
|
|
|
|
|
|
|
std::this_thread::yield();
|
2017-07-12 07:36:27 -04:00
|
|
|
}
|
|
|
|
|
2018-12-17 08:29:18 -05:00
|
|
|
void OpenUrl(const std::string& url)
|
2017-07-12 07:36:27 -04:00
|
|
|
{
|
|
|
|
SafeShellExecute(nullptr, "open", url.data(), nullptr, nullptr, SW_SHOWNORMAL);
|
2017-06-11 15:25:00 -04:00
|
|
|
}
|
|
|
|
|
2017-03-26 14:41:37 -04:00
|
|
|
bool HasIntercection(unsigned int base1, unsigned int len1, unsigned int base2, unsigned int len2)
|
|
|
|
{
|
2017-04-18 13:28:00 -04:00
|
|
|
return !(base1 + len1 <= base2 || base2 + len2 <= base1);
|
2017-03-26 14:41:37 -04:00
|
|
|
}
|
2017-01-19 16:23:59 -05:00
|
|
|
}
|