Textractor_test/include/types.h

115 lines
3.6 KiB
C
Raw Normal View History

2018-08-23 23:53:23 +08:00
#pragma once
#include "common.h"
#include "const.h"
template <typename T> using Array = T[];
2018-12-02 04:52:12 +08:00
template<typename E, typename M = std::mutex, template<typename...> typename P = std::unique_ptr>
class ThreadSafePtr
{
public:
2018-12-02 04:52:12 +08:00
template <typename ...Args> ThreadSafePtr(Args ...args) : ptr(new E(args...)), mtxPtr(new M) {}
2018-12-19 01:14:54 +08:00
auto operator->() const
{
2018-12-02 04:52:12 +08:00
struct
{
E* operator->() { return ptr; }
std::unique_lock<M> lock;
E* ptr;
} lockedProxy{ std::unique_lock<M>(*mtxPtr), ptr.get() };
return lockedProxy;
}
private:
2018-12-02 04:52:12 +08:00
P<E> ptr;
P<M> mtxPtr;
};
struct DefHandleCloser { void operator()(void* h) { CloseHandle(h); } };
template <typename HandleCloser = DefHandleCloser>
class AutoHandle
{
public:
AutoHandle(HANDLE h) : h(h) {}
operator HANDLE() { return h.get(); }
2018-12-04 07:29:30 +08:00
operator PHANDLE() { return &h._Myptr(); }
operator bool() { return h.get() != NULL && h.get() != INVALID_HANDLE_VALUE; }
private:
2018-12-02 04:52:12 +08:00
struct HandleCleaner : HandleCloser { void operator()(void* h) { if (h != INVALID_HANDLE_VALUE) HandleCloser::operator()(h); } };
std::unique_ptr<void, HandleCleaner> h;
};
2018-08-23 23:53:23 +08:00
// jichi 3/7/2014: Add guessed comment
struct HookParam
{
// jichi 8/24/2013: For special hooks.
typedef void(*text_fun_t)(DWORD esp, HookParam *hp, BYTE index, DWORD *data, DWORD *split, DWORD *len);
typedef bool(*filter_fun_t)(LPVOID str, DWORD *len, HookParam *hp, BYTE index); // jichi 10/24/2014: Add filter function. Return true if skip the text
typedef bool(*hook_fun_t)(DWORD esp, HookParam *hp); // jichi 10/24/2014: Add generic hook function, return false if stop execution.
uint64_t address; // absolute or relative address
2018-08-27 10:21:15 +08:00
int offset, // offset of the data in the memory
2018-08-23 23:53:23 +08:00
index, // deref_offset1
split, // offset of the split character
split_index; // deref_offset2
wchar_t module[MAX_MODULE_SIZE];
char function[MAX_MODULE_SIZE];
2018-08-23 23:53:23 +08:00
DWORD type; // flags
2018-10-31 08:50:50 +08:00
UINT codepage; // text encoding
2018-08-23 23:53:23 +08:00
WORD length_offset; // index of the string length
DWORD user_value; // 7/20/2014: jichi additional parameters for PSP games
text_fun_t text_fun;
filter_fun_t filter_fun;
hook_fun_t hook_fun;
2018-12-02 04:52:12 +08:00
char name[HOOK_NAME_SIZE];
2018-08-23 23:53:23 +08:00
};
2018-11-05 09:48:46 +08:00
struct ThreadParam
2018-08-23 23:53:23 +08:00
{
2018-11-05 09:48:46 +08: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 23:53:23 +08:00
};
2018-08-25 00:50:20 +08:00
// Artikash 5/31/2018: required for unordered_map to work with struct key
2018-12-06 14:51:27 +08:00
template <> struct std::hash<ThreadParam> { size_t operator()(ThreadParam tp) const { return std::hash<int64_t>()((tp.processId + tp.addr) ^ (tp.ctx + tp.ctx2)); } };
static bool operator==(ThreadParam one, ThreadParam two) { return one.processId == two.processId && one.addr == two.addr && one.ctx == two.ctx && one.ctx2 == two.ctx2; }
2018-08-23 23:53:23 +08:00
class WinMutex // Like CMutex but works with lock_guard
{
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 23:53:23 +08:00
struct InsertHookCmd // From host
{
2018-12-02 04:52:12 +08:00
InsertHookCmd(HookParam hp) : hp(hp) {};
2018-08-23 23:53:23 +08:00
int command = HOST_COMMAND_NEW_HOOK;
HookParam hp;
};
struct ConsoleOutputNotif // From hook
{
2018-09-23 03:45:54 +08:00
ConsoleOutputNotif(std::string message = "") { strcpy_s<MESSAGE_SIZE>(this->message, message.c_str()); };
2018-08-23 23:53:23 +08:00
int command = HOST_NOTIFICATION_TEXT;
char message[MESSAGE_SIZE] = {};
};
struct HookRemovedNotif // From hook
{
2018-09-21 09:59:07 +08:00
HookRemovedNotif(uint64_t address) : address(address) {};
2018-08-23 23:53:23 +08:00
int command = HOST_NOTIFICATION_RMVHOOK;
2018-09-21 09:59:07 +08:00
uint64_t address;
2018-08-23 23:53:23 +08:00
};
2018-08-25 00:50:20 +08:00
#define LOCK(mutex) std::lock_guard lock(mutex)