iw4x-client/src/Utils/Utils.hpp

130 lines
3.0 KiB
C++
Raw Normal View History

2017-01-20 08:36:52 -05:00
#pragma once
typedef LONG NTSTATUS;
typedef NTSTATUS(NTAPI *NtCreateThreadEx_t)(PHANDLE hThread, ACCESS_MASK DesiredAccess, LPVOID ObjectAttributes, HANDLE ProcessHandle, LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter, BOOL CreateSuspended, DWORD StackZeroBits, DWORD SizeOfStackCommit, DWORD SizeOfStackReserve, LPVOID lpBytesBuffer);
typedef NTSTATUS(NTAPI* NtQueryInformationThread_t)(HANDLE ThreadHandle, LONG ThreadInformationClass, PVOID ThreadInformation, ULONG ThreadInformationLength, PULONG ReturnLength);
#define ThreadQuerySetWin32StartAddress 9
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);
std::string ParseChallenge(const std::string& data);
2017-01-19 16:23:59 -05:00
void OutputDebugLastError();
std::string GetLastWindowsError();
2017-01-19 16:23:59 -05:00
bool IsWineEnvironment();
unsigned long GetParentProcessId();
2020-12-09 14:13:34 -05:00
size_t GetModuleSize(HMODULE);
void* GetThreadStartAddress(HANDLE hThread);
HMODULE GetNTDLL();
2017-03-26 14:41:37 -04:00
2017-06-30 04:37:29 -04:00
void SetEnvironment();
2018-12-17 08:29:18 -05:00
void OpenUrl(const std::string& url);
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);
template <typename T> inline void RotLeft(T& object, size_t bits)
{
bits %= sizeof(T) * 8;
T sign = 1;
sign = sign << (sizeof(T) * 8 - 1);
bool negative = (object & sign) != 0;
object &= ~sign;
object = (object << bits) | (object >> (sizeof(T) * 8 - bits));
object |= T(negative) << ((sizeof(T) * 8 - 1 + bits) % (sizeof(T) * 8));
}
template <typename T> inline void RotRight(T& object, size_t bits)
{
bits %= (sizeof(T) * 8);
RotLeft<T>(object, ((sizeof(T) * 8) - bits));
}
2017-02-24 05:43:05 -05:00
template <typename T> inline void Merge(std::vector<T>* target, T* source, size_t length)
2017-01-19 16:23:59 -05:00
{
if (source)
{
for (size_t i = 0; i < length; ++i)
{
target->push_back(source[i]);
}
}
}
2017-02-24 05:43:05 -05:00
template <typename T> inline void Merge(std::vector<T>* target, std::vector<T> source)
2017-01-19 16:23:59 -05:00
{
for (auto &entry : source)
{
target->push_back(entry);
}
}
template <typename T> using Slot = std::function<T>;
template <typename T>
class Signal
{
public:
2017-01-22 07:44:14 -05:00
Signal()
{
std::lock_guard<std::recursive_mutex> _(this->mutex);
2017-01-22 07:44:14 -05:00
this->slots.clear();
}
Signal(Signal& obj) : Signal()
{
std::lock_guard<std::recursive_mutex> _(this->mutex);
std::lock_guard<std::recursive_mutex> __(obj.mutex);
2017-01-22 07:44:14 -05:00
Utils::Merge(&this->slots, obj.getSlots());
}
2017-01-19 16:23:59 -05:00
void connect(Slot<T> slot)
{
std::lock_guard<std::recursive_mutex> _(this->mutex);
2017-01-22 07:44:14 -05:00
if (slot)
{
this->slots.push_back(slot);
}
2017-01-19 16:23:59 -05:00
}
void clear()
{
std::lock_guard<std::recursive_mutex> _(this->mutex);
this->slots.clear();
2017-01-19 16:23:59 -05:00
}
2017-01-22 07:44:14 -05:00
std::vector<Slot<T>>& getSlots()
{
return this->slots;
}
2017-01-19 16:23:59 -05:00
template <class ...Args>
void operator()(Args&&... args) const
{
std::lock_guard<std::recursive_mutex> _(this->mutex);
std::vector<Slot<T>> copiedSlots;
Utils::Merge(&copiedSlots, this->slots);
2017-05-30 14:31:00 -04:00
for (auto& slot : copiedSlots)
2017-01-19 16:23:59 -05:00
{
2017-01-22 07:44:14 -05:00
if (slot)
{
slot(std::forward<Args>(args)...);
}
2017-01-19 16:23:59 -05:00
}
}
private:
mutable std::recursive_mutex mutex;
2017-01-19 16:23:59 -05:00
std::vector<Slot<T>> slots;
};
}