116 lines
3.3 KiB
C
Raw Normal View History

2018-08-23 11:53:23 -04:00
#pragma once
#include "common.h"
#include "const.h"
template <typename T> using Array = T[];
2019-01-09 22:35:01 -05:00
template <auto F>
struct Functor
{
template <typename... Args>
auto operator()(Args&&... args) const { return std::invoke(F, std::forward<Args>(args)...); }
};
2018-12-21 14:11:40 -05:00
template<typename E, typename M = std::mutex>
class ThreadSafe
{
public:
template <typename... Args> ThreadSafe(Args&&... args) : contents(std::forward<Args>(args)...) {}
2018-12-21 14:11:40 -05:00
auto operator->()
{
2018-12-01 15:52:12 -05:00
struct
{
E* operator->() { return ptr; }
std::unique_lock<M> lock;
E* ptr;
2018-12-21 14:11:40 -05:00
} lockedProxy{ std::unique_lock(mtx), &contents };
2018-12-01 15:52:12 -05:00
return lockedProxy;
}
private:
2018-12-21 14:11:40 -05:00
E contents;
M mtx;
};
2019-01-09 22:35:01 -05:00
template <typename HandleCloser = Functor<CloseHandle>>
class AutoHandle
{
public:
AutoHandle(HANDLE h) : h(h) {}
operator HANDLE() { return h.get(); }
2019-01-23 14:47:42 -05:00
PHANDLE operator&() { static_assert(sizeof(*this) == sizeof(HANDLE)); return (PHANDLE)this; }
operator bool() { return h.get() != NULL && h.get() != INVALID_HANDLE_VALUE; }
private:
2019-01-23 14:47:42 -05:00
struct HandleCleaner { void operator()(void* h) { if (h != INVALID_HANDLE_VALUE) HandleCloser()(h); } };
std::unique_ptr<void, HandleCleaner> h;
};
2018-08-23 11:53:23 -04:00
// jichi 3/7/2014: Add guessed comment
2018-12-30 22:11:23 -05:00
struct HookParam
2018-08-23 11:53:23 -04:00
{
uint64_t address; // absolute or relative address
2018-08-26 22:21:15 -04:00
int offset, // offset of the data in the memory
2018-08-23 11:53:23 -04:00
index, // deref_offset1
split, // offset of the split character
split_index; // deref_offset2
2019-01-03 17:52:16 -05:00
union
{
wchar_t module[MAX_MODULE_SIZE];
wchar_t text[MAX_MODULE_SIZE];
};
char function[MAX_MODULE_SIZE];
2018-08-23 11:53:23 -04:00
DWORD type; // flags
2018-10-30 20:50:50 -04:00
UINT codepage; // text encoding
2018-12-21 10:10:51 -05:00
short length_offset; // index of the string length
2018-08-23 11:53:23 -04:00
DWORD user_value; // 7/20/2014: jichi additional parameters for PSP games
2019-02-02 16:54:13 -05:00
void(*text_fun)(DWORD stack, HookParam* hp, BYTE obsoleteAlwaysZero, DWORD* data, DWORD* split, DWORD* len);
bool(*filter_fun)(void* data, DWORD* len, HookParam* hp, BYTE obsoleteAlwaysZero); // jichi 10/24/2014: Add filter function. Return true if skip the text
bool(*hook_fun)(DWORD stack, HookParam* hp); // jichi 10/24/2014: Add generic hook function, return false if stop execution.
2018-12-01 15:52:12 -05:00
char name[HOOK_NAME_SIZE];
2018-08-23 11:53:23 -04:00
};
2018-11-04 20:48:46 -05:00
struct ThreadParam
2018-08-23 11:53:23 -04:00
{
2019-01-09 22:35:01 -05:00
bool operator==(ThreadParam other) const { return processId == other.processId && addr == other.addr && ctx == other.ctx && ctx2 == other.ctx2; }
2018-11-04 20:48:46 -05:00
DWORD processId;
uint64_t addr;
uint64_t ctx; // The context of the hook: by default the first value on stack, usually the return address
uint64_t ctx2; // The subcontext of the hook: 0 by default, generated in a method specific to the hook
2018-08-23 11:53:23 -04:00
};
2019-01-09 22:35:01 -05:00
class WinMutex // Like CMutex but works with scoped_lock
{
public:
WinMutex(std::wstring name) : m(CreateMutexW(nullptr, FALSE, name.c_str())) {}
void lock() { if (m) WaitForSingleObject(m, 0); }
void unlock() { if (m) ReleaseMutex(m); }
private:
AutoHandle<> m;
};
2018-08-23 11:53:23 -04:00
struct InsertHookCmd // From host
{
2018-12-30 22:11:23 -05:00
InsertHookCmd(HookParam hp) : hp(hp) {}
2018-08-23 11:53:23 -04:00
int command = HOST_COMMAND_NEW_HOOK;
HookParam hp;
};
struct ConsoleOutputNotif // From hook
{
2018-12-30 22:11:23 -05:00
ConsoleOutputNotif(std::string message = "") { strcpy_s<MESSAGE_SIZE>(this->message, message.c_str()); }
2018-08-23 11:53:23 -04:00
int command = HOST_NOTIFICATION_TEXT;
char message[MESSAGE_SIZE] = {};
};
struct HookRemovedNotif // From hook
{
2018-09-20 21:59:07 -04:00
HookRemovedNotif(uint64_t address) : address(address) {};
2018-08-23 11:53:23 -04:00
int command = HOST_NOTIFICATION_RMVHOOK;
2018-09-20 21:59:07 -04:00
uint64_t address;
2018-08-23 11:53:23 -04:00
};