clang format

This commit is contained in:
Anonymous275
2022-07-26 11:43:45 +03:00
parent b26fb43746
commit 3c96bb3959
34 changed files with 1912 additions and 1832 deletions

View File

@@ -4,25 +4,27 @@
///
#pragma once
#include "Memory/Hook.h"
#include "Memory/GELua.h"
#include "Memory/IPC.h"
#include <memory>
#include <string>
#include "Memory/GELua.h"
#include "Memory/Hook.h"
#include "Memory/IPC.h"
class BeamNG {
public:
static void EntryPoint();
static void SendIPC(const std::string& Data);
private:
static inline std::unique_ptr<Hook<def::GEUpdate>> TickCountDetour;
static inline std::unique_ptr<Hook<def::lua_open_jit>> OpenJITDetour;
static inline std::unique_ptr<IPC> IPCFromLauncher;
static inline std::unique_ptr<IPC> IPCToLauncher;
static inline uint64_t GameBaseAddr;
static inline uint64_t DllBaseAddr;
static int lua_open_jit_D(lua_State* State);
static void RegisterGEFunctions();
// static int GetTickCount_D(void* GEState, void* Param2, void* Param3, void* Param4);
static void IPCListener();
public:
static void EntryPoint();
static void SendIPC(const std::string& Data);
private:
static inline std::unique_ptr<Hook<def::GEUpdate>> TickCountDetour;
static inline std::unique_ptr<Hook<def::lua_open_jit>> OpenJITDetour;
static inline std::unique_ptr<IPC> IPCFromLauncher;
static inline std::unique_ptr<IPC> IPCToLauncher;
static inline uint64_t GameBaseAddr;
static inline uint64_t DllBaseAddr;
static int lua_open_jit_D(lua_State* State);
static void RegisterGEFunctions();
// static int GetTickCount_D(void* GEState, void* Param2, void* Param3, void*
// Param4);
static void IPCListener();
};

View File

@@ -8,18 +8,19 @@
typedef struct lua_State lua_State;
typedef int (*lua_CFunction)(lua_State*);
extern int lua_gettop(lua_State *L);
extern int lua_gettop(lua_State* L);
namespace def {
typedef int (*GEUpdate)(void* Param1, void* Param2, void* Param3, void* Param4);
typedef uint32_t (*GetTickCount)();
typedef int (*lua_open_jit)(lua_State* L);
typedef void (*lua_get_field)(lua_State* L, int idx, const char* k);
typedef const char* (*lua_push_fstring)(lua_State* L, const char* fmt, ...);
typedef int (*lua_p_call)(lua_State* L, int arg, int res, int err);
typedef void (*lua_pushcclosure)(lua_State* L, lua_CFunction fn, int n);
typedef int (*lua_settop)(lua_State* L, int idx);
typedef void (*lua_settable)(lua_State* L, int idx);
typedef void (*lua_createtable)(lua_State* L, int narray, int nrec);
typedef void (*lua_setfield)(lua_State* L, int idx, const char* k);
typedef const char* (*lua_tolstring)(lua_State* L, int idx, size_t* len);
typedef int (*GEUpdate)(void* Param1, void* Param2, void* Param3,
void* Param4);
typedef uint32_t (*GetTickCount)();
typedef int (*lua_open_jit)(lua_State* L);
typedef void (*lua_get_field)(lua_State* L, int idx, const char* k);
typedef const char* (*lua_push_fstring)(lua_State* L, const char* fmt, ...);
typedef int (*lua_p_call)(lua_State* L, int arg, int res, int err);
typedef void (*lua_pushcclosure)(lua_State* L, lua_CFunction fn, int n);
typedef int (*lua_settop)(lua_State* L, int idx);
typedef void (*lua_settable)(lua_State* L, int idx);
typedef void (*lua_createtable)(lua_State* L, int narray, int nrec);
typedef void (*lua_setfield)(lua_State* L, int idx, const char* k);
typedef const char* (*lua_tolstring)(lua_State* L, int idx, size_t* len);
}

View File

@@ -7,39 +7,40 @@
#include "Definitions.h"
class GELua {
public:
static void FindAddresses();
static inline def::GEUpdate GEUpdate;
static inline def::lua_settop lua_settop;
static inline def::GetTickCount GetTickCount;
static inline def::lua_open_jit lua_open_jit;
static inline def::lua_push_fstring lua_push_fstring;
static inline def::lua_get_field lua_get_field;
static inline def::lua_p_call lua_p_call;
static inline def::lua_createtable lua_createtable;
static inline def::lua_pushcclosure lua_pushcclosure;
static inline def::lua_setfield lua_setfield;
static inline def::lua_settable lua_settable;
static inline def::lua_tolstring lua_tolstring;
static inline lua_State* State;
public:
static void FindAddresses();
static inline def::GEUpdate GEUpdate;
static inline def::lua_settop lua_settop;
static inline def::GetTickCount GetTickCount;
static inline def::lua_open_jit lua_open_jit;
static inline def::lua_push_fstring lua_push_fstring;
static inline def::lua_get_field lua_get_field;
static inline def::lua_p_call lua_p_call;
static inline def::lua_createtable lua_createtable;
static inline def::lua_pushcclosure lua_pushcclosure;
static inline def::lua_setfield lua_setfield;
static inline def::lua_settable lua_settable;
static inline def::lua_tolstring lua_tolstring;
static inline lua_State* State;
};
namespace GELuaTable {
inline void Begin(lua_State* L) {
GELua::lua_createtable(L, 0, 0);
}
inline void End(lua_State* L, const char* name) {
GELua::lua_setfield(L, -10002, name);
}
inline void BeginEntry(lua_State* L, const char* name) {
GELua::lua_push_fstring(L, "%s", name);
}
inline void EndEntry(lua_State* L) {
GELua::lua_settable(L, -3);
}
inline void InsertFunction(lua_State* L, const char* name, lua_CFunction func) {
BeginEntry(L, name);
GELua::lua_pushcclosure(L, func, 0);
EndEntry(L);
}
inline void Begin(lua_State* L) {
GELua::lua_createtable(L, 0, 0);
}
inline void End(lua_State* L, const char* name) {
GELua::lua_setfield(L, -10002, name);
}
inline void BeginEntry(lua_State* L, const char* name) {
GELua::lua_push_fstring(L, "%s", name);
}
inline void EndEntry(lua_State* L) {
GELua::lua_settable(L, -3);
}
inline void InsertFunction(lua_State* L, const char* name,
lua_CFunction func) {
BeginEntry(L, name);
GELua::lua_pushcclosure(L, func, 0);
EndEntry(L);
}
}

View File

@@ -3,46 +3,49 @@
/// Copyright (c) 2021-present Anonymous275 read the LICENSE file for more info.
///
#define WIN32_LEAN_AND_MEAN
#include "Memory/Memory.h"
#include <MinHook.h>
#include "Memory/Memory.h"
#pragma once
template <class FuncType>
template<class FuncType>
class Hook {
FuncType targetPtr;
FuncType detourFunc;
bool Attached = false;
public:
FuncType targetPtr;
FuncType detourFunc;
bool Attached = false;
Hook(FuncType src, FuncType dest) : targetPtr(src), detourFunc(dest) {
auto status = MH_CreateHook((void*)targetPtr, (void*)detourFunc, (void**)&Original);
if(status != MH_OK) {
Memory::Print(std::string("MH Error -> ") + MH_StatusToString(status));
public:
Hook(FuncType src, FuncType dest) : targetPtr(src), detourFunc(dest) {
auto status =
MH_CreateHook((void*)targetPtr, (void*)detourFunc, (void**)&Original);
if (status != MH_OK) {
Memory::Print(std::string("MH Error -> ") + MH_StatusToString(status));
return;
}
}
void Enable() {
if (!Attached) {
auto status = MH_EnableHook((void*)targetPtr);
if (status != MH_OK) {
Memory::Print(std::string("MH Error -> ") +
MH_StatusToString(status));
return;
}
}
}
Attached = true;
}
}
void Enable() {
if(!Attached){
auto status = MH_EnableHook((void*)targetPtr);
if(status != MH_OK) {
Memory::Print(std::string("MH Error -> ") + MH_StatusToString(status));
return;
}
Attached = true;
}
}
void Disable() {
if (Attached) {
auto status = MH_DisableHook((void*)targetPtr);
if (status != MH_OK) {
Memory::Print(std::string("MH Error -> ") +
MH_StatusToString(status));
return;
}
Attached = false;
}
}
void Disable() {
if(Attached){
auto status = MH_DisableHook((void*)targetPtr);
if(status != MH_OK) {
Memory::Print(std::string("MH Error -> ") + MH_StatusToString(status));
return;
}
Attached = false;
}
}
FuncType Original{};
FuncType Original{};
};

View File

@@ -7,28 +7,29 @@
#include <string>
class IPC {
public:
IPC() = default;
IPC(uint32_t ID, size_t Size) noexcept;
[[nodiscard]] size_t size() const noexcept;
[[nodiscard]] char* c_str() const noexcept;
void send(const std::string& msg) noexcept;
[[nodiscard]] void* raw() const noexcept;
[[nodiscard]] bool receive_timed_out() const noexcept;
[[nodiscard]] bool send_timed_out() const noexcept;
const std::string& msg() noexcept;
void confirm_receive() noexcept;
void try_receive() noexcept;
void receive() noexcept;
~IPC() noexcept;
static bool mem_used(uint32_t MemID) noexcept;
private:
void* SemConfHandle_;
void* MemoryHandle_;
void* SemHandle_;
std::string Msg_;
bool SendTimeout;
bool RcvTimeout;
size_t Size_;
char* Data_;
public:
IPC() = default;
IPC(uint32_t ID, size_t Size) noexcept;
[[nodiscard]] size_t size() const noexcept;
[[nodiscard]] char* c_str() const noexcept;
void send(const std::string& msg) noexcept;
[[nodiscard]] void* raw() const noexcept;
[[nodiscard]] bool receive_timed_out() const noexcept;
[[nodiscard]] bool send_timed_out() const noexcept;
const std::string& msg() noexcept;
void confirm_receive() noexcept;
void try_receive() noexcept;
void receive() noexcept;
~IPC() noexcept;
static bool mem_used(uint32_t MemID) noexcept;
private:
void* SemConfHandle_;
void* MemoryHandle_;
void* SemHandle_;
std::string Msg_;
bool SendTimeout;
bool RcvTimeout;
size_t Size_;
char* Data_;
};

View File

@@ -4,17 +4,17 @@
///
#pragma once
#include <string>
#include <set>
#include <string>
class Memory{
public:
static uint64_t FindPattern(const char* module, const char* Pattern[]);
static uint32_t GetBeamNGPID(const std::set<uint32_t>& BL);
static uint64_t GetModuleBase(const char* Name);
static void Print(const std::string& msg);
static void Inject(uint32_t PID);
static uint32_t GetTickCount();
static uint32_t EntryPoint();
static uint32_t GetPID();
class Memory {
public:
static uint64_t FindPattern(const char* module, const char* Pattern[]);
static uint32_t GetBeamNGPID(const std::set<uint32_t>& BL);
static uint64_t GetModuleBase(const char* Name);
static void Print(const std::string& msg);
static void Inject(uint32_t PID);
static uint32_t GetTickCount();
static uint32_t EntryPoint();
static uint32_t GetPID();
};

View File

@@ -5,52 +5,63 @@
#pragma once
namespace Patterns {
const char* GetTickCount[2] {
"\x48\xff\x25\x00\x00\x00\x00\xcc\xcc\xcc\xcc\xcc\xcc\xcc\xcc\xcc\x48\x83\xec\x00\x48\x8d\x4c\x24",
"xxx????xxxxxxxxxxxx?xxxx"
};
const char* open_jit[2] {
"\x48\x89\x5c\x24\x00\x48\x89\x74\x24\x00\x57\x48\x83\xec\x00\x48\x8b\x05\x00\x00\x00\x00\x48\x33\xc4\x48\x89\x44\x24\x00\x48\x8b\x71\x00\x48\x8d\x54\x24",
"xxxx?xxxx?xxxx?xxx????xxxxxxx?xxx?xxxx"
};
const char* get_field[2] {
"\x48\x89\x5c\x24\x00\x57\x48\x83\xec\x00\x4d\x8b\xd0\x48\x8b\xd9\xe8\x00\x00\x00\x00\x48\x8b\xf8\x49\xc7\xc0\x00\x00\x00\x00\x90\x49\xff\xc0\x43\x80\x3c\x02\x00\x75\x00\x49\x8b\xd2\x48\x8b\xcb\xe8\x00\x00\x00\x00\x48\xb9\x00\x00\x00\x00\x00\x00\x00\x00\x4c\x8d\x44\x24\x00\x48\x0b\xc1\x48\x8b\xd7\x48\x8b\xcb\x48\x89\x44\x24\x00\xe8\x00\x00\x00\x00\x48\x85\xc0",
"xxxx?xxxx?xxxxxxx????xxxxxx????xxxxxxxx?x?xxxxxxx????xx????????xxxx?xxxxxxxxxxxxx?x????xxx"
};
const char* push_fstring[2] {
"\x48\x89\x54\x24\x00\x4c\x89\x44\x24\x00\x4c\x89\x4c\x24\x00\x53\x48\x83\xec\x00\x4c\x8b\x41",
"xxxx?xxxx?xxxx?xxxx?xxx"
};
const char* p_call[2] {
"\x48\x89\x5c\x24\x00\x48\x89\x74\x24\x00\x57\x48\x83\xec\x00\x48\x8b\x59\x00\x41\x8b\xf0\x4c\x63\xda",
"xxxx?xxxx?xxxx?xxx?xxxxxx"
};
const char* lua_setfield[2] {
"\x48\x89\x5c\x24\x00\x57\x48\x83\xec\x00\x4d\x8b\xd0\x48\x8b\xd9\xe8\x00\x00\x00\x00\x48\x8b\xf8\x49\xc7\xc0\x00\x00\x00\x00\x90\x49\xff\xc0\x43\x80\x3c\x02\x00\x75\x00\x49\x8b\xd2\x48\x8b\xcb\xe8\x00\x00\x00\x00\x48\xb9\x00\x00\x00\x00\x00\x00\x00\x00\x4c\x8d\x44\x24\x00\x48\x0b\xc1\x48\x8b\xd7\x48\x8b\xcb\x48\x89\x44\x24\x00\xe8\x00\x00\x00\x00\x48\x8b\x53",
"xxxx?xxxx?xxxxxxx????xxxxxx????xxxxxxxx?x?xxxxxxx????xx????????xxxx?xxxxxxxxxxxxx?x????xxx"
};
const char* lua_createtable[2] {
"\x48\x89\x5c\x24\x00\x48\x89\x74\x24\x00\x57\x48\x83\xec\x00\x4c\x8b\x49\x00\x41\x8b\xf8",
"xxxx?xxxx?xxxx?xxx?xxx"
};
const char* lua_settable[2] {
"\x40\x53\x48\x83\xec\x00\x48\x8b\xd9\xe8\x00\x00\x00\x00\x4c\x8b\x43\x00\x48\x8b\xd0\x49\x83\xe8\x00\x48\x8b\xcb\xe8\x00\x00\x00\x00\x48\x8b\x53",
"xxxxx?xxxx????xxx?xxxxxx?xxxx????xxx"
};
const char* lua_pushcclosure[2] {
"\x48\x89\x5c\x24\x00\x48\x89\x74\x24\x00\x57\x48\x83\xec\x00\x48\x8b\xd9\x49\x63\xf8\x48\x8b\x49\x00\x48\x8b\xf2",
"xxxx?xxxx?xxxx?xxxxxxxxx?xxx"
};
const char* lua_tolstring[2] {
"\x48\x89\x5c\x24\x00\x48\x89\x74\x24\x00\x57\x48\x83\xec\x00\x49\x8b\xf8\x8b\xda\x48\x8b\xf1\xe8",
"xxxx?xxxx?xxxx?xxxxxxxxx"
};
const char* GEUpdate[2] {
"\x48\x89\x5c\x24\x00\x48\x89\x6c\x24\x00\x56\x57\x41\x56\x48\x83\xec\x00\x4c\x8b\x31\x49\x8b\xf0",
"xxxx?xxxx?xxxxxxx?xxxxxx"
};
const char* lua_settop[2] {
"\x4c\x8b\xc1\x85\xd2\x7e\x00\x48\x8b\x41\x00\x48\x8b\x49",
"xxxxxx?xxx?xxx"
};
const char* GetTickCount[2]{
"\x48\xff\x25\x00\x00\x00\x00\xcc\xcc\xcc\xcc\xcc\xcc\xcc\xcc\xcc\x48"
"\x83\xec\x00\x48\x8d\x4c\x24",
"xxx????xxxxxxxxxxxx?xxxx"};
const char* open_jit[2]{
"\x48\x89\x5c\x24\x00\x48\x89\x74\x24\x00\x57\x48\x83\xec\x00\x48\x8b"
"\x05\x00\x00\x00\x00\x48\x33\xc4\x48\x89\x44\x24\x00\x48\x8b\x71\x00"
"\x48\x8d\x54\x24",
"xxxx?xxxx?xxxx?xxx????xxxxxxx?xxx?xxxx"};
const char* get_field[2]{
"\x48\x89\x5c\x24\x00\x57\x48\x83\xec\x00\x4d\x8b\xd0\x48\x8b\xd9\xe8"
"\x00\x00\x00\x00\x48\x8b\xf8\x49\xc7\xc0\x00\x00\x00\x00\x90\x49\xff"
"\xc0\x43\x80\x3c\x02\x00\x75\x00\x49\x8b\xd2\x48\x8b\xcb\xe8\x00\x00"
"\x00\x00\x48\xb9\x00\x00\x00\x00\x00\x00\x00\x00\x4c\x8d\x44\x24\x00"
"\x48\x0b\xc1\x48\x8b\xd7\x48\x8b\xcb\x48\x89\x44\x24\x00\xe8\x00\x00"
"\x00\x00\x48\x85\xc0",
"xxxx?xxxx?xxxxxxx????xxxxxx????xxxxxxxx?x?xxxxxxx????xx????????xxxx?"
"xxxxxxxxxxxxx?x????xxx"};
const char* push_fstring[2]{
"\x48\x89\x54\x24\x00\x4c\x89\x44\x24\x00\x4c\x89\x4c\x24\x00\x53\x48"
"\x83\xec\x00\x4c\x8b\x41",
"xxxx?xxxx?xxxx?xxxx?xxx"};
const char* p_call[2]{
"\x48\x89\x5c\x24\x00\x48\x89\x74\x24\x00\x57\x48\x83\xec\x00\x48\x8b"
"\x59\x00\x41\x8b\xf0\x4c\x63\xda",
"xxxx?xxxx?xxxx?xxx?xxxxxx"};
const char* lua_setfield[2]{
"\x48\x89\x5c\x24\x00\x57\x48\x83\xec\x00\x4d\x8b\xd0\x48\x8b\xd9\xe8"
"\x00\x00\x00\x00\x48\x8b\xf8\x49\xc7\xc0\x00\x00\x00\x00\x90\x49\xff"
"\xc0\x43\x80\x3c\x02\x00\x75\x00\x49\x8b\xd2\x48\x8b\xcb\xe8\x00\x00"
"\x00\x00\x48\xb9\x00\x00\x00\x00\x00\x00\x00\x00\x4c\x8d\x44\x24\x00"
"\x48\x0b\xc1\x48\x8b\xd7\x48\x8b\xcb\x48\x89\x44\x24\x00\xe8\x00\x00"
"\x00\x00\x48\x8b\x53",
"xxxx?xxxx?xxxxxxx????xxxxxx????xxxxxxxx?x?xxxxxxx????xx????????xxxx?"
"xxxxxxxxxxxxx?x????xxx"};
const char* lua_createtable[2]{
"\x48\x89\x5c\x24\x00\x48\x89\x74\x24\x00\x57\x48\x83\xec\x00\x4c\x8b"
"\x49\x00\x41\x8b\xf8",
"xxxx?xxxx?xxxx?xxx?xxx"};
const char* lua_settable[2]{
"\x40\x53\x48\x83\xec\x00\x48\x8b\xd9\xe8\x00\x00\x00\x00\x4c\x8b\x43"
"\x00\x48\x8b\xd0\x49\x83\xe8\x00\x48\x8b\xcb\xe8\x00\x00\x00\x00\x48"
"\x8b\x53",
"xxxxx?xxxx????xxx?xxxxxx?xxxx????xxx"};
const char* lua_pushcclosure[2]{
"\x48\x89\x5c\x24\x00\x48\x89\x74\x24\x00\x57\x48\x83\xec\x00\x48\x8b"
"\xd9\x49\x63\xf8\x48\x8b\x49\x00\x48\x8b\xf2",
"xxxx?xxxx?xxxx?xxxxxxxxx?xxx"};
const char* lua_tolstring[2]{
"\x48\x89\x5c\x24\x00\x48\x89\x74\x24\x00\x57\x48\x83\xec\x00\x49\x8b"
"\xf8\x8b\xda\x48\x8b\xf1\xe8",
"xxxx?xxxx?xxxx?xxxxxxxxx"};
const char* GEUpdate[2]{
"\x48\x89\x5c\x24\x00\x48\x89\x6c\x24\x00\x56\x57\x41\x56\x48\x83\xec"
"\x00\x4c\x8b\x31\x49\x8b\xf0",
"xxxx?xxxx?xxxxxxx?xxxxxx"};
const char* lua_settop[2]{
"\x4c\x8b\xc1\x85\xd2\x7e\x00\x48\x8b\x41\x00\x48\x8b\x49",
"xxxxxx?xxx?xxx"};
}