Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Commit

Permalink
clang-format with style chromium
Browse files Browse the repository at this point in the history
  • Loading branch information
skomski committed Oct 10, 2015
1 parent 8e9790b commit 4950abd
Show file tree
Hide file tree
Showing 11 changed files with 829 additions and 896 deletions.
39 changes: 16 additions & 23 deletions hooking/HookFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,28 @@

static HookFunctionBase* g_hookFunctions;

void HookFunctionBase::Register()
{
m_next = g_hookFunctions;
g_hookFunctions = this;
void HookFunctionBase::Register() {
m_next = g_hookFunctions;
g_hookFunctions = this;
}

void HookFunctionBase::RunAll()
{
for (auto func = g_hookFunctions; func; func = func->m_next)
{
func->Run();
}
void HookFunctionBase::RunAll() {
for (auto func = g_hookFunctions; func; func = func->m_next) {
func->Run();
}
}

static RuntimeHookFunction* g_runtimeHookFunctions;

void RuntimeHookFunction::Register()
{
m_next = g_runtimeHookFunctions;
g_runtimeHookFunctions = this;
void RuntimeHookFunction::Register() {
m_next = g_runtimeHookFunctions;
g_runtimeHookFunctions = this;
}

void RuntimeHookFunction::Run(const char* key)
{
for (auto func = g_runtimeHookFunctions; func; func = func->m_next)
{
if (func->m_key == key)
{
func->m_function();
}
}
void RuntimeHookFunction::Run(const char* key) {
for (auto func = g_runtimeHookFunctions; func; func = func->m_next) {
if (func->m_key == key) {
func->m_function();
}
}
}
67 changes: 27 additions & 40 deletions hooking/HookFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,57 +13,44 @@
// Initialization function that will be called after the game is loaded.
//

class HookFunctionBase
{
private:
HookFunctionBase* m_next;
class HookFunctionBase {
private:
HookFunctionBase* m_next;

public:
HookFunctionBase()
{
Register();
}
public:
HookFunctionBase() { Register(); }

virtual void Run() = 0;
virtual void Run() = 0;

static void RunAll();
void Register();
static void RunAll();
void Register();
};

class HookFunction : public HookFunctionBase
{
private:
void(*m_function)();
class HookFunction : public HookFunctionBase {
private:
void (*m_function)();

public:
HookFunction(void(*function)())
{
m_function = function;
}
public:
HookFunction(void (*function)()) { m_function = function; }

virtual void Run()
{
m_function();
}
virtual void Run() { m_function(); }
};

class RuntimeHookFunction
{
private:
void(*m_function)();
std::string m_key;
class RuntimeHookFunction {
private:
void (*m_function)();
std::string m_key;

RuntimeHookFunction* m_next;
RuntimeHookFunction* m_next;

public:
RuntimeHookFunction(const char* key, void(*function)())
{
m_key = key;
m_function = function;
public:
RuntimeHookFunction(const char* key, void (*function)()) {
m_key = key;
m_function = function;

Register();
}
Register();
}

static void Run(const char* key);
void Register();
static void Run(const char* key);
void Register();
};
220 changes: 106 additions & 114 deletions hooking/Hooking.FuncCalls.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,153 +10,145 @@
#include <jitasm.h>
#include <stdint.h>

namespace hook
{
struct passed
{
template<typename ...T> passed(T...) {}
namespace hook {
struct passed {
template <typename... T>
passed(T...) {}
};

class CallStubEx : public jitasm::Frontend
{
private:
void* m_callback;
void* m_userData;
class CallStubEx : public jitasm::Frontend {
private:
void* m_callback;
void* m_userData;

size_t m_argumentSize;
size_t m_argumentSize;

public:
CallStubEx()
{
public:
CallStubEx() {}

}
inline void SetCallback(void* cb, void* userData) {
m_callback = cb;
m_userData = userData;
}

inline void SetCallback(void* cb, void* userData) { m_callback = cb; m_userData = userData; }
inline void SetArgumentSize(size_t size) { m_argumentSize = size; }

inline void SetArgumentSize(size_t size) { m_argumentSize = size; }
virtual void InternalMain() {
int argStart = 4;

virtual void InternalMain()
{
int argStart = 4;
pushad();
argStart += 8 * sizeof(uintptr_t);

pushad();
argStart += 8 * sizeof(uintptr_t);
push((uintptr_t)m_userData);
argStart += 4;

push((uintptr_t)m_userData);
argStart += 4;
sub(esp, m_argumentSize);
argStart += m_argumentSize;

sub(esp, m_argumentSize);
argStart += m_argumentSize;
lea(esi, dword_ptr[esp]);
add(esi, argStart);

lea(esi, dword_ptr[esp]);
add(esi, argStart);
lea(edi, dword_ptr[esp]);

lea(edi, dword_ptr[esp]);
push(ecx);
mov(ecx, m_argumentSize);

push(ecx);
mov(ecx, m_argumentSize);
rep_movsb();

rep_movsb();
pop(ecx);

pop(ecx);
mov(eax, (uintptr_t)m_callback);
call(eax);

mov(eax, (uintptr_t)m_callback);
call(eax);
popad();

popad();
ret(m_argumentSize);
}

ret(m_argumentSize);
}
void Inject(uintptr_t address) {
auto assembly = new FunctionAssembly(*this);

void Inject(uintptr_t address)
{
auto assembly = new FunctionAssembly(*this);
int origAddress = *(int*)(address + 1);
origAddress += 5;
origAddress += address;

int origAddress = *(int*)(address + 1);
origAddress += 5;
origAddress += address;

// and patch
put<int>(address + 1, (uintptr_t)assembly->GetCode() - (uintptr_t)get_adjusted(address) - 5);
}
// and patch
put<int>(address + 1, (uintptr_t)assembly->GetCode() -
(uintptr_t)get_adjusted(address) - 5);
}
};

template<typename TRet, typename TClass, typename ...Args>
struct thiscall
{
typedef std::function<TRet(Args...)> TOrigFunc;
typedef TRet(__thiscall* TOrigCB)(void*, Args...);
typedef std::function<TRet(TClass*, Args...)> THookFunc;
template <typename TRet, typename TClass, typename... Args>
struct thiscall {
typedef std::function<TRet(Args...)> TOrigFunc;
typedef TRet(__thiscall* TOrigCB)(void*, Args...);
typedef std::function<TRet(TClass*, Args...)> THookFunc;

private:
struct hookdata
{
THookFunc hookFunc;
TOrigCB origCB;
};
private:
struct hookdata {
THookFunc hookFunc;
TOrigCB origCB;
};

static TRet __thiscall InternalHook(void* class_, Args... args, hookdata* hookDataPtr)
{
hookdata* data = static_cast<hookdata*>(hookDataPtr);
static TRet __thiscall InternalHook(void* class_,
Args... args,
hookdata* hookDataPtr) {
hookdata* data = static_cast<hookdata*>(hookDataPtr);

return data->hookFunc((TClass*)class_, args...);
}
return data->hookFunc((TClass*)class_, args...);
}

public:
static void inject(uintptr_t address, THookFunc hookFunc)
{
auto hookData = new hookdata;
hookData->hookFunc = hookFunc;
public:
static void inject(uintptr_t address, THookFunc hookFunc) {
auto hookData = new hookdata;
hookData->hookFunc = hookFunc;

size_t argSize = 0;
size_t argSize = 0;

passed{ ([&]
{
int size = min(sizeof(Args), sizeof(uintptr_t));
passed{(
[&] {
int size = min(sizeof(Args), sizeof(uintptr_t));

argSize += size;
}(), 1)... };
argSize += size;
}(),
1)...};

CallStubEx callStub;
callStub.SetCallback((void*)InternalHook, hookData);
callStub.SetArgumentSize(argSize);
CallStubEx callStub;
callStub.SetCallback((void*)InternalHook, hookData);
callStub.SetArgumentSize(argSize);

callStub.Inject(address);
}
callStub.Inject(address);
}
};

template<typename TRet, typename TClass>
struct thiscall<TRet, TClass, void>
{
typedef std::function<TRet()> TOrigFunc;
typedef TRet(__thiscall* TOrigCB)(void*);
typedef std::function<TRet(TClass*)> THookFunc;

private:
struct hookdata
{
THookFunc hookFunc;
TOrigCB origCB;
};

static TRet __thiscall InternalHook(void* class_, hookdata* hookDataPtr)
{
hookdata* data = static_cast<hookdata*>(hookDataPtr);

return data->hookFunc((TClass*)class_);
}

public:
static void inject(uintptr_t address, THookFunc hookFunc)
{
auto hookData = new hookdata;
hookData->hookFunc = hookFunc;

CallStubEx callStub;
callStub.SetCallback((void*)InternalHook, hookData);
callStub.SetArgumentSize(0);

callStub.Inject(address);
}
template <typename TRet, typename TClass>
struct thiscall<TRet, TClass, void> {
typedef std::function<TRet()> TOrigFunc;
typedef TRet(__thiscall* TOrigCB)(void*);
typedef std::function<TRet(TClass*)> THookFunc;

private:
struct hookdata {
THookFunc hookFunc;
TOrigCB origCB;
};

static TRet __thiscall InternalHook(void* class_, hookdata* hookDataPtr) {
hookdata* data = static_cast<hookdata*>(hookDataPtr);

return data->hookFunc((TClass*)class_);
}

public:
static void inject(uintptr_t address, THookFunc hookFunc) {
auto hookData = new hookdata;
hookData->hookFunc = hookFunc;

CallStubEx callStub;
callStub.SetCallback((void*)InternalHook, hookData);
callStub.SetArgumentSize(0);

callStub.Inject(address);
}
};
}

0 comments on commit 4950abd

Please sign in to comment.